blob: 99056067458d0f27fdc120df04e8761d7c346aba [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2016 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
linyuhc3968e62017-11-20 17:40:50 -080019import android.app.ActivityManager;
20import android.app.ActivityManager.AppTask;
linyuh57b093b2017-11-17 14:32:32 -080021import android.app.ActivityManager.TaskDescription;
linyuh7b86f562017-11-16 11:24:09 -080022import android.app.AlertDialog;
linyuhf99f6302017-11-15 11:23:51 -080023import android.app.Dialog;
linyuh57b093b2017-11-17 14:32:32 -080024import android.app.KeyguardManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.content.Context;
26import android.content.Intent;
linyuhc3968e62017-11-20 17:40:50 -080027import android.content.res.Configuration;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.graphics.drawable.GradientDrawable;
29import android.graphics.drawable.GradientDrawable.Orientation;
30import android.os.Bundle;
Eric Erfanian2ca43182017-08-31 06:57:16 -070031import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.support.annotation.ColorInt;
33import android.support.annotation.FloatRange;
linyuhc3968e62017-11-20 17:40:50 -080034import android.support.annotation.IntDef;
Eric Erfanianc857f902017-05-15 14:05:33 -070035import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.support.annotation.Nullable;
linyuhc3968e62017-11-20 17:40:50 -080037import android.support.annotation.VisibleForTesting;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import android.support.v4.app.FragmentManager;
39import android.support.v4.app.FragmentTransaction;
linyuh57b093b2017-11-17 14:32:32 -080040import android.support.v4.content.res.ResourcesCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import android.support.v4.graphics.ColorUtils;
linyuhc3968e62017-11-20 17:40:50 -080042import android.telecom.CallAudioState;
43import android.telecom.PhoneAccountHandle;
Eric Erfanian90508232017-03-24 09:31:16 -070044import android.telephony.TelephonyManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import android.view.KeyEvent;
46import android.view.MenuItem;
47import android.view.MotionEvent;
48import android.view.View;
linyuh9c327da2017-11-14 12:33:48 -080049import android.view.WindowManager;
linyuhc3968e62017-11-20 17:40:50 -080050import android.view.animation.Animation;
51import android.view.animation.AnimationUtils;
linyuh7b86f562017-11-16 11:24:09 -080052import android.widget.CheckBox;
53import android.widget.Toast;
linyuhf99f6302017-11-15 11:23:51 -080054import com.android.contacts.common.widget.SelectPhoneAccountDialogFragment;
linyuhc3968e62017-11-20 17:40:50 -080055import com.android.dialer.animation.AnimUtils;
56import com.android.dialer.animation.AnimationListenerAdapter;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070057import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.dialer.common.LogUtil;
weijiaxu650e7cc2017-10-31 12:38:54 -070059import com.android.dialer.common.concurrent.ThreadUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.compat.ActivityCompat;
linyuhc3968e62017-11-20 17:40:50 -080061import com.android.dialer.compat.CompatUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070062import com.android.dialer.configprovider.ConfigProviderBindings;
63import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.dialer.logging.Logger;
weijiaxu94df7202017-10-25 18:21:41 -070065import com.android.dialer.logging.LoggingBindings;
Eric Erfanian8369df02017-05-03 10:27:13 -070066import com.android.dialer.logging.ScreenEvent;
linyuhc3968e62017-11-20 17:40:50 -080067import com.android.dialer.util.ViewUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.answer.bindings.AnswerBindings;
69import com.android.incallui.answer.protocol.AnswerScreen;
70import com.android.incallui.answer.protocol.AnswerScreenDelegate;
71import com.android.incallui.answer.protocol.AnswerScreenDelegateFactory;
72import com.android.incallui.answerproximitysensor.PseudoScreenState;
linyuh57b093b2017-11-17 14:32:32 -080073import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080074import com.android.incallui.call.CallList;
75import com.android.incallui.call.DialerCall;
76import com.android.incallui.call.DialerCall.State;
linyuh57b093b2017-11-17 14:32:32 -080077import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070078import com.android.incallui.callpending.CallPendingActivity;
79import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080080import com.android.incallui.incall.bindings.InCallBindings;
81import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
82import com.android.incallui.incall.protocol.InCallButtonUiDelegateFactory;
83import com.android.incallui.incall.protocol.InCallScreen;
84import com.android.incallui.incall.protocol.InCallScreenDelegate;
85import com.android.incallui.incall.protocol.InCallScreenDelegateFactory;
linyuh7b86f562017-11-16 11:24:09 -080086import com.android.incallui.incalluilock.InCallUiLock;
linyuhf99f6302017-11-15 11:23:51 -080087import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080088import com.android.incallui.video.bindings.VideoBindings;
89import com.android.incallui.video.protocol.VideoCallScreen;
90import com.android.incallui.video.protocol.VideoCallScreenDelegate;
91import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
linyuhc3968e62017-11-20 17:40:50 -080092import com.google.common.base.Optional;
93import java.lang.annotation.Retention;
94import java.lang.annotation.RetentionPolicy;
95import java.util.ArrayList;
96import java.util.List;
Eric Erfanianccca3152017-02-22 16:32:36 -080097
98/** Version of {@link InCallActivity} that shows the new UI */
99public class InCallActivity extends TransactionSafeFragmentActivity
100 implements AnswerScreenDelegateFactory,
101 InCallScreenDelegateFactory,
102 InCallButtonUiDelegateFactory,
103 VideoCallScreenDelegateFactory,
104 PseudoScreenState.StateChangedListener {
105
linyuhc3968e62017-11-20 17:40:50 -0800106 @Retention(RetentionPolicy.SOURCE)
107 @IntDef({
108 DIALPAD_REQUEST_NONE,
109 DIALPAD_REQUEST_SHOW,
110 DIALPAD_REQUEST_HIDE,
111 })
112 @interface DialpadRequestType {}
Eric Erfanian2ca43182017-08-31 06:57:16 -0700113
linyuhc3968e62017-11-20 17:40:50 -0800114 private static final int DIALPAD_REQUEST_NONE = 1;
115 private static final int DIALPAD_REQUEST_SHOW = 2;
116 private static final int DIALPAD_REQUEST_HIDE = 3;
linyuh57b093b2017-11-17 14:32:32 -0800117
linyuhc3968e62017-11-20 17:40:50 -0800118 private static Optional<Integer> audioRouteForTesting = Optional.absent();
linyuh57b093b2017-11-17 14:32:32 -0800119
linyuhc3968e62017-11-20 17:40:50 -0800120 private final InternationalCallOnWifiCallback internationalCallOnWifiCallback =
121 new InternationalCallOnWifiCallback();
122 private final SelectPhoneAccountListener selectPhoneAccountListener =
123 new SelectPhoneAccountListener();
Eric Erfanianccca3152017-02-22 16:32:36 -0800124
linyuhc3968e62017-11-20 17:40:50 -0800125 private Animation dialpadSlideInAnimation;
126 private Animation dialpadSlideOutAnimation;
127 private Dialog errorDialog;
128 private GradientDrawable backgroundDrawable;
linyuh69a25062017-11-15 16:18:51 -0800129 private InCallOrientationEventListener inCallOrientationEventListener;
linyuhc3968e62017-11-20 17:40:50 -0800130 private View pseudoBlackScreenOverlay;
131 private SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment;
132 private String dtmfTextToPrepopulate;
133 private String showPostCharWaitDialogCallId;
134 private String showPostCharWaitDialogChars;
135 private boolean allowOrientationChange;
136 private boolean animateDialpadOnShow;
Eric Erfanianccca3152017-02-22 16:32:36 -0800137 private boolean didShowAnswerScreen;
138 private boolean didShowInCallScreen;
139 private boolean didShowVideoCallScreen;
linyuh9c327da2017-11-14 12:33:48 -0800140 private boolean dismissKeyguard;
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 private boolean isInShowMainInCallFragment;
linyuhc3968e62017-11-20 17:40:50 -0800142 private boolean isRecreating; // whether the activity is going to be recreated
143 private boolean isVisible;
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 private boolean needDismissPendingDialogs;
linyuhc3968e62017-11-20 17:40:50 -0800145 private boolean showPostCharWaitDialogOnResume;
146 private boolean touchDownWhenPseudoScreenOff;
147 private int[] backgroundDrawableColors;
148 @DialpadRequestType private int showDialpadRequest = DIALPAD_REQUEST_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800149
150 public static Intent getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700151 Context context, boolean showDialpad, boolean newOutgoingCall, boolean isForFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800152 Intent intent = new Intent(Intent.ACTION_MAIN, null);
153 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
154 intent.setClass(context, InCallActivity.class);
linyuhc3968e62017-11-20 17:40:50 -0800155 if (showDialpad) {
156 intent.putExtra(IntentExtraNames.SHOW_DIALPAD, true);
157 }
158 intent.putExtra(IntentExtraNames.NEW_OUTGOING_CALL, newOutgoingCall);
159 intent.putExtra(IntentExtraNames.FOR_FULL_SCREEN, isForFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 return intent;
161 }
162
163 @Override
164 protected void onResumeFragments() {
165 super.onResumeFragments();
166 if (needDismissPendingDialogs) {
167 dismissPendingDialogs();
168 }
169 }
170
171 @Override
linyuhc3968e62017-11-20 17:40:50 -0800172 protected void onCreate(Bundle bundle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700173 Trace.beginSection("InCallActivity.onCreate");
linyuhc3968e62017-11-20 17:40:50 -0800174 super.onCreate(bundle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800175
Eric Erfanian2ca43182017-08-31 06:57:16 -0700176 if (getIntent().getBooleanExtra(ReturnToCallController.RETURN_TO_CALL_EXTRA_KEY, false)) {
177 Logger.get(this).logImpression(DialerImpression.Type.BUBBLE_PRIMARY_BUTTON_RETURN_TO_CALL);
178 getIntent().removeExtra(ReturnToCallController.RETURN_TO_CALL_EXTRA_KEY);
179 }
180
linyuhc3968e62017-11-20 17:40:50 -0800181 if (bundle != null) {
182 didShowAnswerScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN);
183 didShowInCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN);
184 didShowVideoCallScreen = bundle.getBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -0800185 }
186
linyuhc3968e62017-11-20 17:40:50 -0800187 setWindowFlags();
188 setContentView(R.layout.incall_screen);
189 internalResolveIntent(getIntent());
190
191 boolean isLandscape =
192 getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
193 boolean isRtl = ViewUtil.isRtl();
194 if (isLandscape) {
195 dialpadSlideInAnimation =
196 AnimationUtils.loadAnimation(
197 this, isRtl ? R.anim.dialpad_slide_in_left : R.anim.dialpad_slide_in_right);
198 dialpadSlideOutAnimation =
199 AnimationUtils.loadAnimation(
200 this, isRtl ? R.anim.dialpad_slide_out_left : R.anim.dialpad_slide_out_right);
201 } else {
202 dialpadSlideInAnimation = AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_in_bottom);
203 dialpadSlideOutAnimation =
204 AnimationUtils.loadAnimation(this, R.anim.dialpad_slide_out_bottom);
205 }
206 dialpadSlideInAnimation.setInterpolator(AnimUtils.EASE_IN);
207 dialpadSlideOutAnimation.setInterpolator(AnimUtils.EASE_OUT);
208 dialpadSlideOutAnimation.setAnimationListener(
209 new AnimationListenerAdapter() {
210 @Override
211 public void onAnimationEnd(Animation animation) {
212 hideDialpadFragment();
213 }
214 });
215
216 if (bundle != null && showDialpadRequest == DIALPAD_REQUEST_NONE) {
217 // If the dialpad was shown before, set related variables so that it can be shown and
218 // populated with the previous DTMF text during onResume().
219 if (bundle.containsKey(IntentExtraNames.SHOW_DIALPAD)) {
220 boolean showDialpad = bundle.getBoolean(IntentExtraNames.SHOW_DIALPAD);
221 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_HIDE;
222 animateDialpadOnShow = false;
223 }
224 dtmfTextToPrepopulate = bundle.getString(KeysForSavedInstance.DIALPAD_TEXT);
225
226 SelectPhoneAccountDialogFragment selectPhoneAccountDialogFragment =
227 (SelectPhoneAccountDialogFragment)
228 getFragmentManager().findFragmentByTag(Tags.SELECT_ACCOUNT_FRAGMENT);
229 if (selectPhoneAccountDialogFragment != null) {
230 selectPhoneAccountDialogFragment.setListener(selectPhoneAccountListener);
231 }
232 }
233
234 InternationalCallOnWifiDialogFragment existingInternationalCallOnWifiDialogFragment =
235 (InternationalCallOnWifiDialogFragment)
236 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
237 if (existingInternationalCallOnWifiDialogFragment != null) {
238 existingInternationalCallOnWifiDialogFragment.setCallback(internationalCallOnWifiCallback);
239 }
240
linyuh69a25062017-11-15 16:18:51 -0800241 inCallOrientationEventListener = new InCallOrientationEventListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800242
243 getWindow()
244 .getDecorView()
245 .setSystemUiVisibility(
246 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
247
248 pseudoBlackScreenOverlay = findViewById(R.id.psuedo_black_screen_overlay);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700249 sendBroadcast(CallPendingActivity.getFinishBroadcast());
250 Trace.endSection();
weijiaxuc950a9b2017-11-06 16:39:04 -0800251 Logger.get(this)
252 .logStopLatencyTimer(LoggingBindings.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_INCOMING);
253 Logger.get(this)
254 .logStopLatencyTimer(LoggingBindings.ON_CALL_ADDED_TO_ON_INCALL_UI_SHOWN_OUTGOING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800255 }
256
linyuhc3968e62017-11-20 17:40:50 -0800257 private void setWindowFlags() {
258 // Allow the activity to be shown when the screen is locked and filter out touch events that are
259 // "too fat".
260 int flags =
261 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
262 | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
263
linyuhf79d1cb2017-12-15 17:49:56 -0800264 // When the audio stream is not via Bluetooth, turn on the screen once the activity is shown.
265 // When the audio stream is via Bluetooth, turn on the screen only for an incoming call.
linyuhc3968e62017-11-20 17:40:50 -0800266 final int audioRoute = getAudioRoute();
linyuhf79d1cb2017-12-15 17:49:56 -0800267 if (audioRoute != CallAudioState.ROUTE_BLUETOOTH
268 || CallList.getInstance().getIncomingCall() != null) {
linyuhc3968e62017-11-20 17:40:50 -0800269 flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
270 }
271
272 getWindow().addFlags(flags);
273 }
274
275 private static int getAudioRoute() {
276 if (audioRouteForTesting.isPresent()) {
277 return audioRouteForTesting.get();
278 }
279
280 return AudioModeProvider.getInstance().getAudioState().getRoute();
281 }
282
283 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
284 public static void setAudioRouteForTesting(int audioRoute) {
285 audioRouteForTesting = Optional.of(audioRoute);
286 }
287
288 private void internalResolveIntent(Intent intent) {
289 if (!intent.getAction().equals(Intent.ACTION_MAIN)) {
290 return;
291 }
292
293 if (intent.hasExtra(IntentExtraNames.SHOW_DIALPAD)) {
294 // IntentExtraNames.SHOW_DIALPAD can be used to specify whether the DTMF dialpad should be
295 // initially visible. If the extra is absent, leave the dialpad in its previous state.
296 boolean showDialpad = intent.getBooleanExtra(IntentExtraNames.SHOW_DIALPAD, false);
297 relaunchedFromDialer(showDialpad);
298 }
299
300 DialerCall outgoingCall = CallList.getInstance().getOutgoingCall();
301 if (outgoingCall == null) {
302 outgoingCall = CallList.getInstance().getPendingOutgoingCall();
303 }
304 if (intent.getBooleanExtra(IntentExtraNames.NEW_OUTGOING_CALL, false)) {
305 intent.removeExtra(IntentExtraNames.NEW_OUTGOING_CALL);
306
307 // InCallActivity is responsible for disconnecting a new outgoing call if there is no way of
308 // making it (i.e. no valid call capable accounts).
309 // If the version is not MSIM compatible, ignore this code.
310 if (CompatUtils.isMSIMCompatible()
311 && InCallPresenter.isCallWithNoValidAccounts(outgoingCall)) {
312 LogUtil.i(
313 "InCallActivity.internalResolveIntent", "Call with no valid accounts, disconnecting");
314 outgoingCall.disconnect();
315 }
316
317 dismissKeyguard(true);
318 }
319
320 if (showPhoneAccountSelectionDialog()) {
321 hideMainInCallFragment();
322 }
323 }
324
325 /**
326 * When relaunching from the dialer app, {@code showDialpad} indicates whether the dialpad should
327 * be shown on launch.
328 *
329 * @param showDialpad {@code true} to indicate the dialpad should be shown on launch, and {@code
330 * false} to indicate no change should be made to the dialpad visibility.
331 */
332 private void relaunchedFromDialer(boolean showDialpad) {
333 showDialpadRequest = showDialpad ? DIALPAD_REQUEST_SHOW : DIALPAD_REQUEST_NONE;
334 animateDialpadOnShow = true;
335
336 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
337 // If there's only one line in use, AND it's on hold, then we're sure the user
338 // wants to use the dialpad toward the exact line, so un-hold the holding line.
339 DialerCall call = CallList.getInstance().getActiveOrBackgroundCall();
340 if (call != null && call.getState() == State.ONHOLD) {
341 call.unhold();
342 }
343 }
344 }
345
346 /**
347 * Show a phone account selection dialog if there is a call waiting for phone account selection.
348 *
349 * @return true if the dialog was shown.
350 */
351 private boolean showPhoneAccountSelectionDialog() {
352 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
353 if (waitingForAccountCall == null) {
354 return false;
355 }
356
357 Bundle extras = waitingForAccountCall.getIntentExtras();
358 List<PhoneAccountHandle> phoneAccountHandles =
359 extras == null
360 ? new ArrayList<>()
361 : extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
362
363 selectPhoneAccountDialogFragment =
364 SelectPhoneAccountDialogFragment.newInstance(
365 R.string.select_phone_account_for_calls,
366 true /* canSetDefault */,
367 0 /* setDefaultResId */,
368 phoneAccountHandles,
369 selectPhoneAccountListener,
370 waitingForAccountCall.getId(),
371 null /* hints */);
372 selectPhoneAccountDialogFragment.show(getFragmentManager(), Tags.SELECT_ACCOUNT_FRAGMENT);
373 return true;
374 }
375
Eric Erfanianccca3152017-02-22 16:32:36 -0800376 @Override
377 protected void onSaveInstanceState(Bundle out) {
linyuh57b093b2017-11-17 14:32:32 -0800378 LogUtil.enterBlock("InCallActivity.onSaveInstanceState");
379
380 // TODO: DialpadFragment should handle this as part of its own state
linyuhc3968e62017-11-20 17:40:50 -0800381 out.putBoolean(IntentExtraNames.SHOW_DIALPAD, isDialpadVisible());
linyuh57b093b2017-11-17 14:32:32 -0800382 DialpadFragment dialpadFragment = getDialpadFragment();
383 if (dialpadFragment != null) {
linyuhc3968e62017-11-20 17:40:50 -0800384 out.putString(KeysForSavedInstance.DIALPAD_TEXT, dialpadFragment.getDtmfText());
linyuh57b093b2017-11-17 14:32:32 -0800385 }
386
linyuhc3968e62017-11-20 17:40:50 -0800387 out.putBoolean(KeysForSavedInstance.DID_SHOW_ANSWER_SCREEN, didShowAnswerScreen);
388 out.putBoolean(KeysForSavedInstance.DID_SHOW_IN_CALL_SCREEN, didShowInCallScreen);
389 out.putBoolean(KeysForSavedInstance.DID_SHOW_VIDEO_CALL_SCREEN, didShowVideoCallScreen);
linyuh57b093b2017-11-17 14:32:32 -0800390
Eric Erfanianccca3152017-02-22 16:32:36 -0800391 super.onSaveInstanceState(out);
392 isVisible = false;
393 }
394
395 @Override
396 protected void onStart() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700397 Trace.beginSection("InCallActivity.onStart");
Eric Erfanianccca3152017-02-22 16:32:36 -0800398 super.onStart();
linyuh57b093b2017-11-17 14:32:32 -0800399
Eric Erfanianccca3152017-02-22 16:32:36 -0800400 isVisible = true;
401 showMainInCallFragment();
linyuh57b093b2017-11-17 14:32:32 -0800402
403 InCallPresenter.getInstance().setActivity(this);
404 enableInCallOrientationEventListener(
405 getRequestedOrientation()
406 == InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
407 InCallPresenter.getInstance().onActivityStarted();
408
yueg10f6e822018-01-17 15:32:18 -0800409 if (!isRecreating) {
410 InCallPresenter.getInstance().onUiShowing(true);
411 }
412
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 if (ActivityCompat.isInMultiWindowMode(this)
414 && !getResources().getBoolean(R.bool.incall_dialpad_allowed)) {
415 // Hide the dialpad because there may not be enough room
416 showDialpadFragment(false, false);
417 }
linyuh57b093b2017-11-17 14:32:32 -0800418
Eric Erfanian2ca43182017-08-31 06:57:16 -0700419 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 }
421
422 @Override
423 protected void onResume() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700424 Trace.beginSection("InCallActivity.onResume");
Eric Erfanianccca3152017-02-22 16:32:36 -0800425 super.onResume();
linyuhc3968e62017-11-20 17:40:50 -0800426
427 if (!InCallPresenter.getInstance().isReadyForTearDown()) {
428 updateTaskDescription();
yueg10f6e822018-01-17 15:32:18 -0800429 InCallPresenter.getInstance().updateNotification();
linyuhc3968e62017-11-20 17:40:50 -0800430 }
431
432 // If there is a pending request to show or hide the dialpad, handle that now.
433 if (showDialpadRequest != DIALPAD_REQUEST_NONE) {
434 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
435 // Exit fullscreen so that the user has access to the dialpad hide/show button.
436 // This is important when showing the dialpad from within dialer.
437 InCallPresenter.getInstance().setFullScreen(false /* isFullScreen */, true /* force */);
438
439 showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */);
440 animateDialpadOnShow = false;
441
442 DialpadFragment dialpadFragment = getDialpadFragment();
443 if (dialpadFragment != null) {
444 dialpadFragment.setDtmfText(dtmfTextToPrepopulate);
445 dtmfTextToPrepopulate = null;
446 }
447 } else {
448 LogUtil.i("InCallActivity.onResume", "Force-hide the dialpad");
449 if (getDialpadFragment() != null) {
450 showDialpadFragment(false /* show */, false /* animate */);
451 }
452 }
453 showDialpadRequest = DIALPAD_REQUEST_NONE;
454 }
455 updateNavigationBar(isDialpadVisible());
456
457 if (showPostCharWaitDialogOnResume) {
458 showDialogForPostCharWait(showPostCharWaitDialogCallId, showPostCharWaitDialogChars);
459 }
460
461 CallList.getInstance()
462 .onInCallUiShown(getIntent().getBooleanExtra(IntentExtraNames.FOR_FULL_SCREEN, false));
463
Eric Erfanianccca3152017-02-22 16:32:36 -0800464 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState();
465 pseudoScreenState.addListener(this);
466 onPseudoScreenStateChanged(pseudoScreenState.isOn());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700467 Trace.endSection();
weijiaxu650e7cc2017-10-31 12:38:54 -0700468 // add 1 sec delay to get memory snapshot so that dialer wont react slowly on resume.
469 ThreadUtil.postDelayedOnUiThread(
weijiaxuc950a9b2017-11-06 16:39:04 -0800470 () ->
471 Logger.get(this)
472 .logRecordMemory(LoggingBindings.INCALL_ACTIVITY_ON_RESUME_MEMORY_EVENT_NAME),
weijiaxu650e7cc2017-10-31 12:38:54 -0700473 1000);
Eric Erfanianccca3152017-02-22 16:32:36 -0800474 }
475
Eric Erfanianccca3152017-02-22 16:32:36 -0800476 @Override
477 protected void onPause() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700478 Trace.beginSection("InCallActivity.onPause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800479 super.onPause();
linyuh57b093b2017-11-17 14:32:32 -0800480
481 DialpadFragment dialpadFragment = getDialpadFragment();
482 if (dialpadFragment != null) {
483 dialpadFragment.onDialerKeyUp(null);
484 }
485
yueg10f6e822018-01-17 15:32:18 -0800486 InCallPresenter.getInstance().updateNotification();
linyuh57b093b2017-11-17 14:32:32 -0800487
Eric Erfanianccca3152017-02-22 16:32:36 -0800488 InCallPresenter.getInstance().getPseudoScreenState().removeListener(this);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700489 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800490 }
491
492 @Override
493 protected void onStop() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700494 Trace.beginSection("InCallActivity.onStop");
wangqi4d705e52017-09-28 12:23:35 -0700495 isVisible = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800496 super.onStop();
linyuh57b093b2017-11-17 14:32:32 -0800497
498 // Disconnects the call waiting for a phone account when the activity is hidden (e.g., after the
499 // user presses the home button).
500 // Without this the pending call will get stuck on phone account selection and new calls can't
501 // be created.
502 // Skip this when the screen is locked since the activity may complete its current life cycle
503 // and restart.
linyuhc3968e62017-11-20 17:40:50 -0800504 if (!isRecreating && !getSystemService(KeyguardManager.class).isKeyguardLocked()) {
linyuh57b093b2017-11-17 14:32:32 -0800505 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
506 if (waitingForAccountCall != null) {
507 waitingForAccountCall.disconnect();
508 }
509 }
510
511 enableInCallOrientationEventListener(false);
512 InCallPresenter.getInstance().updateIsChangingConfigurations();
513 InCallPresenter.getInstance().onActivityStopped();
linyuhc3968e62017-11-20 17:40:50 -0800514 if (!isRecreating) {
yueg10f6e822018-01-17 15:32:18 -0800515 InCallPresenter.getInstance().onUiShowing(false);
linyuh57b093b2017-11-17 14:32:32 -0800516 if (errorDialog != null) {
517 errorDialog.dismiss();
518 }
519 }
520
yueg10f6e822018-01-17 15:32:18 -0800521 if (isFinishing()) {
522 InCallPresenter.getInstance().unsetActivity(this);
523 }
524
Eric Erfanian2ca43182017-08-31 06:57:16 -0700525 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800526 }
527
528 @Override
529 protected void onDestroy() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700530 Trace.beginSection("InCallActivity.onDestroy");
Eric Erfanianccca3152017-02-22 16:32:36 -0800531 super.onDestroy();
linyuh57b093b2017-11-17 14:32:32 -0800532
533 InCallPresenter.getInstance().unsetActivity(this);
534 InCallPresenter.getInstance().updateIsChangingConfigurations();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700535 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 }
537
538 @Override
539 public void finish() {
540 if (shouldCloseActivityOnFinish()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700541 // When user select incall ui from recents after the call is disconnected, it tries to launch
542 // a new InCallActivity but InCallPresenter is already teared down at this point, which causes
543 // crash.
544 // By calling finishAndRemoveTask() instead of finish() the task associated with
545 // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in
546 // this case.
547 //
548 // Calling finish won't clear the task and normally when an activity finishes it shouldn't
549 // clear the task since there could be parent activity in the same task that's still alive.
550 // But InCallActivity is special since it's singleInstance which means it's root activity and
551 // only instance of activity in the task. So it should be safe to also remove task when
552 // finishing.
553 // It's also necessary in the sense of it's excluded from recents. So whenever the activity
554 // finishes, the task should also be removed since it doesn't make sense to go back to it in
555 // anyway anymore.
556 super.finishAndRemoveTask();
Eric Erfanianccca3152017-02-22 16:32:36 -0800557 }
558 }
559
560 private boolean shouldCloseActivityOnFinish() {
linyuhc3968e62017-11-20 17:40:50 -0800561 if (!isVisible) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 LogUtil.i(
563 "InCallActivity.shouldCloseActivityOnFinish",
564 "allowing activity to be closed because it's not visible");
565 return true;
566 }
567
twyen8efb4952017-10-06 16:35:54 -0700568 if (InCallPresenter.getInstance().isInCallUiLocked()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800569 LogUtil.i(
570 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700571 "in call ui is locked, not closing activity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 return false;
573 }
574
575 LogUtil.i(
576 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700577 "activity is visible and has no locks, allowing activity to close");
Eric Erfanianccca3152017-02-22 16:32:36 -0800578 return true;
579 }
580
581 @Override
582 protected void onNewIntent(Intent intent) {
linyuhc3968e62017-11-20 17:40:50 -0800583 LogUtil.enterBlock("InCallActivity.onNewIntent");
Eric Erfanianccca3152017-02-22 16:32:36 -0800584
585 // If the screen is off, we need to make sure it gets turned on for incoming calls.
586 // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
587 // when the activity is first created. Therefore, to ensure the screen is turned on
588 // for the call waiting case, we recreate() the current activity. There should be no jank from
589 // this since the screen is already off and will remain so until our new activity is up.
linyuhc3968e62017-11-20 17:40:50 -0800590 if (!isVisible) {
591 onNewIntent(intent, true /* isRecreating */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800592 LogUtil.i("InCallActivity.onNewIntent", "Restarting InCallActivity to force screen on.");
593 recreate();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700594 } else {
linyuhc3968e62017-11-20 17:40:50 -0800595 onNewIntent(intent, false /* isRecreating */);
596 }
597 }
598
yuega3305352018-01-09 11:02:47 -0800599 @VisibleForTesting
600 void onNewIntent(Intent intent, boolean isRecreating) {
linyuhc3968e62017-11-20 17:40:50 -0800601 this.isRecreating = isRecreating;
602
603 // We're being re-launched with a new Intent. Since it's possible for a single InCallActivity
604 // instance to persist indefinitely (even if we finish() ourselves), this sequence can
605 // happen any time the InCallActivity needs to be displayed.
606
607 // Stash away the new intent so that we can get it in the future by calling getIntent().
608 // Otherwise getIntent() will return the original Intent from when we first got created.
609 setIntent(intent);
610
611 // Activities are always paused before receiving a new intent, so we can count on our onResume()
612 // method being called next.
613
614 // Just like in onCreate(), handle the intent.
615 // Skip if InCallActivity is going to be recreated since this will be called in onCreate().
616 if (!isRecreating) {
617 internalResolveIntent(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800618 }
619 }
620
621 @Override
622 public void onBackPressed() {
linyuh57b093b2017-11-17 14:32:32 -0800623 LogUtil.enterBlock("InCallActivity.onBackPressed");
624
linyuhc3968e62017-11-20 17:40:50 -0800625 if (!isVisible) {
linyuh57b093b2017-11-17 14:32:32 -0800626 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800627 }
linyuh57b093b2017-11-17 14:32:32 -0800628
629 if (!getCallCardFragmentVisible()) {
630 return;
631 }
632
633 DialpadFragment dialpadFragment = getDialpadFragment();
634 if (dialpadFragment != null && dialpadFragment.isVisible()) {
635 showDialpadFragment(false /* show */, true /* animate */);
636 return;
637 }
638
639 if (CallList.getInstance().getIncomingCall() != null) {
640 LogUtil.i(
641 "InCallActivity.onBackPressed",
642 "Ignore the press of the back key when an incoming call is ringing");
643 return;
644 }
645
646 // Nothing special to do. Fall back to the default behavior.
647 super.onBackPressed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800648 }
649
650 @Override
651 public boolean onOptionsItemSelected(MenuItem item) {
652 LogUtil.i("InCallActivity.onOptionsItemSelected", "item: " + item);
653 if (item.getItemId() == android.R.id.home) {
654 onBackPressed();
655 return true;
656 }
657 return super.onOptionsItemSelected(item);
658 }
659
660 @Override
661 public boolean onKeyUp(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800662 DialpadFragment dialpadFragment = getDialpadFragment();
663 if (dialpadFragment != null
664 && dialpadFragment.isVisible()
665 && dialpadFragment.onDialerKeyUp(event)) {
666 return true;
667 }
668
669 if (keyCode == KeyEvent.KEYCODE_CALL) {
670 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
671 return true;
672 }
673
674 return super.onKeyUp(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800675 }
676
677 @Override
678 public boolean onKeyDown(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800679 switch (keyCode) {
680 case KeyEvent.KEYCODE_CALL:
681 if (!InCallPresenter.getInstance().handleCallKey()) {
682 LogUtil.e(
683 "InCallActivity.onKeyDown",
684 "InCallPresenter should always handle KEYCODE_CALL in onKeyDown");
685 }
686 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
687 return true;
688
689 // Note that KEYCODE_ENDCALL isn't handled here as the standard system-wide handling of it
690 // is exactly what's needed, namely
691 // (1) "hang up" if there's an active call, or
692 // (2) "don't answer" if there's an incoming call.
693 // (See PhoneWindowManager for implementation details.)
694
695 case KeyEvent.KEYCODE_CAMERA:
696 // Consume KEYCODE_CAMERA since it's easy to accidentally press the camera button.
697 return true;
698
699 case KeyEvent.KEYCODE_VOLUME_UP:
700 case KeyEvent.KEYCODE_VOLUME_DOWN:
701 case KeyEvent.KEYCODE_VOLUME_MUTE:
702 // Ringer silencing handled by PhoneWindowManager.
703 break;
704
705 case KeyEvent.KEYCODE_MUTE:
706 TelecomAdapter.getInstance()
707 .mute(!AudioModeProvider.getInstance().getAudioState().isMuted());
708 return true;
709
710 case KeyEvent.KEYCODE_SLASH:
711 // When verbose logging is enabled, dump the view for debugging/testing purposes.
712 if (LogUtil.isVerboseEnabled()) {
713 View decorView = getWindow().getDecorView();
714 LogUtil.v("InCallActivity.onKeyDown", "View dump:\n%s", decorView);
715 return true;
716 }
717 break;
718
719 case KeyEvent.KEYCODE_EQUALS:
720 break;
721
722 default: // fall out
723 }
724
725 // Pass other key events to DialpadFragment's "onDialerKeyDown" method in case the user types
726 // in DTMF (Dual-tone multi-frequency signaling) code.
727 DialpadFragment dialpadFragment = getDialpadFragment();
728 if (dialpadFragment != null
729 && dialpadFragment.isVisible()
730 && dialpadFragment.onDialerKeyDown(event)) {
731 return true;
732 }
733
734 return super.onKeyDown(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800735 }
736
737 public boolean isInCallScreenAnimating() {
738 return false;
739 }
740
741 public void showConferenceFragment(boolean show) {
742 if (show) {
743 startActivity(new Intent(this, ManageConferenceActivity.class));
744 }
745 }
746
linyuhc3968e62017-11-20 17:40:50 -0800747 public void showDialpadFragment(boolean show, boolean animate) {
748 if (show == isDialpadVisible()) {
749 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 }
linyuhc3968e62017-11-20 17:40:50 -0800751
752 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
753 if (dialpadFragmentManager == null) {
754 LogUtil.i("InCallActivity.showDialpadFragment", "Unable to obtain a FragmentManager");
755 return;
756 }
757
758 if (!animate) {
759 if (show) {
760 showDialpadFragment();
761 } else {
762 hideDialpadFragment();
763 }
764 } else {
765 if (show) {
766 showDialpadFragment();
767 getDialpadFragment().animateShowDialpad();
768 }
769 getDialpadFragment()
770 .getView()
771 .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation);
772 }
773
774 ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor();
775 if (sensor != null) {
776 sensor.onDialpadVisible(show);
777 }
778 showDialpadRequest = DIALPAD_REQUEST_NONE;
779
780 // Note: onInCallScreenDialpadVisibilityChange is called here to ensure that the dialpad FAB
781 // repositions itself.
782 getInCallScreen().onInCallScreenDialpadVisibilityChange(show);
783 }
784
785 private void showDialpadFragment() {
786 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
787 if (dialpadFragmentManager == null) {
788 return;
789 }
790
791 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
792 DialpadFragment dialpadFragment = getDialpadFragment();
793 if (dialpadFragment == null) {
794 transaction.add(getDialpadContainerId(), new DialpadFragment(), Tags.DIALPAD_FRAGMENT);
795 } else {
796 transaction.show(dialpadFragment);
calderwoodrad5883872017-12-12 15:29:12 -0800797 dialpadFragment.setUserVisibleHint(true);
linyuhc3968e62017-11-20 17:40:50 -0800798 }
799 transaction.commitAllowingStateLoss();
800 dialpadFragmentManager.executePendingTransactions();
801
802 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, this);
803 updateNavigationBar(true /* isDialpadVisible */);
804 }
805
806 private void hideDialpadFragment() {
807 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
808 if (dialpadFragmentManager == null) {
809 return;
810 }
811
calderwoodrad5883872017-12-12 15:29:12 -0800812 DialpadFragment dialpadFragment = getDialpadFragment();
linyuhc3968e62017-11-20 17:40:50 -0800813 if (dialpadFragment != null) {
814 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
815 transaction.hide(dialpadFragment);
816 transaction.commitAllowingStateLoss();
817 dialpadFragmentManager.executePendingTransactions();
calderwoodrad5883872017-12-12 15:29:12 -0800818 dialpadFragment.setUserVisibleHint(false);
linyuhc3968e62017-11-20 17:40:50 -0800819 }
820 updateNavigationBar(false /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800821 }
822
823 public boolean isDialpadVisible() {
linyuh69a25062017-11-15 16:18:51 -0800824 DialpadFragment dialpadFragment = getDialpadFragment();
calderwoodrad5883872017-12-12 15:29:12 -0800825 return dialpadFragment != null && dialpadFragment.getUserVisibleHint();
linyuh69a25062017-11-15 16:18:51 -0800826 }
827
linyuhc3968e62017-11-20 17:40:50 -0800828 /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */
linyuh69a25062017-11-15 16:18:51 -0800829 @Nullable
linyuhc3968e62017-11-20 17:40:50 -0800830 private DialpadFragment getDialpadFragment() {
linyuh69a25062017-11-15 16:18:51 -0800831 FragmentManager fragmentManager = getDialpadFragmentManager();
832 if (fragmentManager == null) {
833 return null;
834 }
linyuhc3968e62017-11-20 17:40:50 -0800835 return (DialpadFragment) fragmentManager.findFragmentByTag(Tags.DIALPAD_FRAGMENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800836 }
837
838 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh57b093b2017-11-17 14:32:32 -0800839 updateTaskDescription();
840
841 if (newForegroundCall == null || !didShowAnswerScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800842 LogUtil.v("InCallActivity.onForegroundCallChanged", "resetting background color");
linyuh57b093b2017-11-17 14:32:32 -0800843 updateWindowBackgroundColor(0 /* progress */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 }
845 }
846
linyuhc3968e62017-11-20 17:40:50 -0800847 private void updateTaskDescription() {
linyuh57b093b2017-11-17 14:32:32 -0800848 int color =
849 getResources().getBoolean(R.bool.is_layout_landscape)
850 ? ResourcesCompat.getColor(
851 getResources(), R.color.statusbar_background_color, getTheme())
852 : InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor();
853 setTaskDescription(
854 new TaskDescription(
855 getResources().getString(R.string.notification_ongoing_call), null /* icon */, color));
856 }
857
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) {
859 ThemeColorManager themeColorManager = InCallPresenter.getInstance().getThemeColorManager();
860 @ColorInt int top;
861 @ColorInt int middle;
862 @ColorInt int bottom;
863 @ColorInt int gray = 0x66000000;
864
865 if (ActivityCompat.isInMultiWindowMode(this)) {
866 top = themeColorManager.getBackgroundColorSolid();
867 middle = themeColorManager.getBackgroundColorSolid();
868 bottom = themeColorManager.getBackgroundColorSolid();
869 } else {
870 top = themeColorManager.getBackgroundColorTop();
871 middle = themeColorManager.getBackgroundColorMiddle();
872 bottom = themeColorManager.getBackgroundColorBottom();
873 }
874
875 if (progress < 0) {
876 float correctedProgress = Math.abs(progress);
877 top = ColorUtils.blendARGB(top, gray, correctedProgress);
878 middle = ColorUtils.blendARGB(middle, gray, correctedProgress);
879 bottom = ColorUtils.blendARGB(bottom, gray, correctedProgress);
880 }
881
882 boolean backgroundDirty = false;
883 if (backgroundDrawable == null) {
884 backgroundDrawableColors = new int[] {top, middle, bottom};
885 backgroundDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, backgroundDrawableColors);
886 backgroundDirty = true;
887 } else {
888 if (backgroundDrawableColors[0] != top) {
889 backgroundDrawableColors[0] = top;
890 backgroundDirty = true;
891 }
892 if (backgroundDrawableColors[1] != middle) {
893 backgroundDrawableColors[1] = middle;
894 backgroundDirty = true;
895 }
896 if (backgroundDrawableColors[2] != bottom) {
897 backgroundDrawableColors[2] = bottom;
898 backgroundDirty = true;
899 }
900 if (backgroundDirty) {
901 backgroundDrawable.setColors(backgroundDrawableColors);
902 }
903 }
904
905 if (backgroundDirty) {
906 getWindow().setBackgroundDrawable(backgroundDrawable);
907 }
908 }
909
910 public boolean isVisible() {
911 return isVisible;
912 }
913
914 public boolean getCallCardFragmentVisible() {
915 return didShowInCallScreen || didShowVideoCallScreen;
916 }
917
918 public void dismissKeyguard(boolean dismiss) {
linyuh9c327da2017-11-14 12:33:48 -0800919 if (dismissKeyguard == dismiss) {
920 return;
921 }
922
923 dismissKeyguard = dismiss;
924 if (dismiss) {
925 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
926 } else {
927 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
928 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 }
930
linyuhc3968e62017-11-20 17:40:50 -0800931 public void showDialogForPostCharWait(String callId, String chars) {
932 if (isVisible) {
933 PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars);
934 fragment.show(getSupportFragmentManager(), Tags.POST_CHAR_DIALOG_FRAGMENT);
935
936 showPostCharWaitDialogOnResume = false;
937 showPostCharWaitDialogCallId = null;
938 showPostCharWaitDialogChars = null;
939 } else {
940 showPostCharWaitDialogOnResume = true;
941 showPostCharWaitDialogCallId = callId;
942 showPostCharWaitDialogChars = chars;
943 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800944 }
945
linyuh7b86f562017-11-16 11:24:09 -0800946 public void showDialogOrToastForDisconnectedCall(DisconnectMessage disconnectMessage) {
947 LogUtil.i(
948 "InCallActivity.showDialogOrToastForDisconnectedCall",
949 "disconnect cause: %s",
950 disconnectMessage);
951
952 if (disconnectMessage.dialog == null || isFinishing()) {
953 return;
954 }
955
956 dismissPendingDialogs();
957
958 // Show a toast if the app is in background when a dialog can't be visible.
959 if (!isVisible()) {
960 Toast.makeText(getApplicationContext(), disconnectMessage.toastMessage, Toast.LENGTH_LONG)
961 .show();
962 return;
963 }
964
965 // Show the dialog.
linyuhc3968e62017-11-20 17:40:50 -0800966 errorDialog = disconnectMessage.dialog;
linyuh7b86f562017-11-16 11:24:09 -0800967 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("showErrorDialog");
968 disconnectMessage.dialog.setOnDismissListener(
969 dialogInterface -> {
970 lock.release();
971 onDialogDismissed();
972 });
973 disconnectMessage.dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
974 disconnectMessage.dialog.show();
975 }
976
977 private void onDialogDismissed() {
linyuhc3968e62017-11-20 17:40:50 -0800978 errorDialog = null;
linyuh7b86f562017-11-16 11:24:09 -0800979 CallList.getInstance().onErrorDialogDismissed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 }
981
982 public void dismissPendingDialogs() {
linyuhc3968e62017-11-20 17:40:50 -0800983 LogUtil.enterBlock("InCallActivity.dismissPendingDialogs");
linyuhf99f6302017-11-15 11:23:51 -0800984
985 if (!isVisible) {
986 // Defer the dismissing action as the activity is not visible and onSaveInstanceState may have
987 // been called.
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 LogUtil.i(
989 "InCallActivity.dismissPendingDialogs", "defer actions since activity is not visible");
990 needDismissPendingDialogs = true;
linyuhf99f6302017-11-15 11:23:51 -0800991 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800992 }
linyuhf99f6302017-11-15 11:23:51 -0800993
994 // Dismiss the error dialog
linyuhf99f6302017-11-15 11:23:51 -0800995 if (errorDialog != null) {
996 errorDialog.dismiss();
linyuhc3968e62017-11-20 17:40:50 -0800997 errorDialog = null;
linyuhf99f6302017-11-15 11:23:51 -0800998 }
999
1000 // Dismiss the phone account selection dialog
linyuhf99f6302017-11-15 11:23:51 -08001001 if (selectPhoneAccountDialogFragment != null) {
1002 selectPhoneAccountDialogFragment.dismiss();
linyuhc3968e62017-11-20 17:40:50 -08001003 selectPhoneAccountDialogFragment = null;
linyuhf99f6302017-11-15 11:23:51 -08001004 }
1005
1006 // Dismiss the dialog for international call on WiFi
1007 InternationalCallOnWifiDialogFragment internationalCallOnWifiFragment =
1008 (InternationalCallOnWifiDialogFragment)
linyuhc3968e62017-11-20 17:40:50 -08001009 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
linyuhf99f6302017-11-15 11:23:51 -08001010 if (internationalCallOnWifiFragment != null) {
1011 internationalCallOnWifiFragment.dismiss();
1012 }
1013
1014 // Dismiss the answer screen
1015 AnswerScreen answerScreen = getAnswerScreen();
1016 if (answerScreen != null) {
1017 answerScreen.dismissPendingDialogs();
1018 }
1019
1020 needDismissPendingDialogs = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 }
1022
linyuhc3968e62017-11-20 17:40:50 -08001023 private void enableInCallOrientationEventListener(boolean enable) {
linyuh69a25062017-11-15 16:18:51 -08001024 if (enable) {
1025 inCallOrientationEventListener.enable(true /* notifyDeviceOrientationChange */);
1026 } else {
1027 inCallOrientationEventListener.disable();
1028 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 }
1030
1031 public void setExcludeFromRecents(boolean exclude) {
linyuhc3968e62017-11-20 17:40:50 -08001032 int taskId = getTaskId();
1033
1034 List<AppTask> tasks = getSystemService(ActivityManager.class).getAppTasks();
1035 for (AppTask task : tasks) {
1036 try {
1037 if (task.getTaskInfo().id == taskId) {
1038 task.setExcludeFromRecents(exclude);
1039 }
1040 } catch (RuntimeException e) {
1041 LogUtil.e("InCallActivity.setExcludeFromRecents", "RuntimeException:\n%s", e);
1042 }
1043 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001044 }
1045
Eric Erfanianccca3152017-02-22 16:32:36 -08001046 @Nullable
1047 public FragmentManager getDialpadFragmentManager() {
1048 InCallScreen inCallScreen = getInCallScreen();
1049 if (inCallScreen != null) {
1050 return inCallScreen.getInCallScreenFragment().getChildFragmentManager();
1051 }
1052 return null;
1053 }
1054
1055 public int getDialpadContainerId() {
1056 return getInCallScreen().getAnswerAndDialpadContainerResourceId();
1057 }
1058
1059 @Override
1060 public AnswerScreenDelegate newAnswerScreenDelegate(AnswerScreen answerScreen) {
1061 DialerCall call = CallList.getInstance().getCallById(answerScreen.getCallId());
1062 if (call == null) {
1063 // This is a work around for a bug where we attempt to create a new delegate after the call
1064 // has already been removed. An example of when this can happen is:
1065 // 1. incoming video call in landscape mode
1066 // 2. remote party hangs up
1067 // 3. activity switches from landscape to portrait
1068 // At step #3 the answer fragment will try to create a new answer delegate but the call won't
1069 // exist. In this case we'll simply return a stub delegate that does nothing. This is ok
1070 // because this new state is transient and the activity will be destroyed soon.
1071 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "call doesn't exist, using stub");
1072 return new AnswerScreenPresenterStub();
1073 } else {
1074 return new AnswerScreenPresenter(
1075 this, answerScreen, CallList.getInstance().getCallById(answerScreen.getCallId()));
1076 }
1077 }
1078
1079 @Override
1080 public InCallScreenDelegate newInCallScreenDelegate() {
1081 return new CallCardPresenter(this);
1082 }
1083
1084 @Override
1085 public InCallButtonUiDelegate newInCallButtonUiDelegate() {
1086 return new CallButtonPresenter(this);
1087 }
1088
1089 @Override
Eric Erfanian90508232017-03-24 09:31:16 -07001090 public VideoCallScreenDelegate newVideoCallScreenDelegate(VideoCallScreen videoCallScreen) {
1091 DialerCall dialerCall = CallList.getInstance().getCallById(videoCallScreen.getCallId());
1092 if (dialerCall != null && dialerCall.getVideoTech().shouldUseSurfaceView()) {
1093 return dialerCall.getVideoTech().createVideoCallScreenDelegate(this, videoCallScreen);
1094 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 return new VideoCallPresenter();
1096 }
1097
1098 public void onPrimaryCallStateChanged() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001099 Trace.beginSection("InCallActivity.onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 showMainInCallFragment();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001101 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001102 }
1103
linyuh7b86f562017-11-16 11:24:09 -08001104 public void showToastForWiFiToLteHandover(DialerCall call) {
1105 if (call.hasShownWiFiToLteHandoverToast()) {
1106 return;
1107 }
1108
1109 Toast.makeText(this, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG).show();
1110 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 }
1112
linyuh7b86f562017-11-16 11:24:09 -08001113 public void showDialogOrToastForWifiHandoverFailure(DialerCall call) {
1114 if (call.showWifiHandoverAlertAsToast()) {
1115 Toast.makeText(this, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
1116 .show();
1117 return;
1118 }
1119
1120 dismissPendingDialogs();
1121
1122 AlertDialog.Builder builder =
1123 new AlertDialog.Builder(this).setTitle(R.string.video_call_lte_to_wifi_failed_title);
1124
1125 // This allows us to use the theme of the dialog instead of the activity
1126 View dialogCheckBoxView =
1127 View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null /* root */);
1128 CheckBox wifiHandoverFailureCheckbox =
1129 (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox);
1130 wifiHandoverFailureCheckbox.setChecked(false);
1131
1132 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("WifiFailedDialog");
linyuhc3968e62017-11-20 17:40:50 -08001133 errorDialog =
linyuh7b86f562017-11-16 11:24:09 -08001134 builder
1135 .setView(dialogCheckBoxView)
1136 .setMessage(R.string.video_call_lte_to_wifi_failed_message)
1137 .setOnCancelListener(dialogInterface -> onDialogDismissed())
1138 .setPositiveButton(
1139 android.R.string.ok,
1140 (dialogInterface, id) -> {
1141 call.setDoNotShowDialogForHandoffToWifiFailure(
1142 wifiHandoverFailureCheckbox.isChecked());
1143 dialogInterface.cancel();
1144 onDialogDismissed();
1145 })
1146 .setOnDismissListener(dialogInterface -> lock.release())
1147 .create();
linyuh7b86f562017-11-16 11:24:09 -08001148 errorDialog.show();
Eric Erfanianccca3152017-02-22 16:32:36 -08001149 }
1150
linyuh7b86f562017-11-16 11:24:09 -08001151 public void showDialogForInternationalCallOnWifi(@NonNull DialerCall call) {
1152 if (!InternationalCallOnWifiDialogFragment.shouldShow(this)) {
1153 LogUtil.i(
1154 "InCallActivity.showDialogForInternationalCallOnWifi",
1155 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
1156 return;
1157 }
1158
1159 InternationalCallOnWifiDialogFragment fragment =
1160 InternationalCallOnWifiDialogFragment.newInstance(
linyuhc3968e62017-11-20 17:40:50 -08001161 call.getId(), internationalCallOnWifiCallback);
1162 fragment.show(getSupportFragmentManager(), Tags.INTERNATIONAL_CALL_ON_WIFI);
Eric Erfanianc857f902017-05-15 14:05:33 -07001163 }
1164
Eric Erfanian938468d2017-10-24 14:05:52 -07001165 @Override
1166 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
1167 super.onMultiWindowModeChanged(isInMultiWindowMode);
linyuh57b093b2017-11-17 14:32:32 -08001168 updateNavigationBar(isDialpadVisible());
1169 }
1170
linyuhc3968e62017-11-20 17:40:50 -08001171 private void updateNavigationBar(boolean isDialpadVisible) {
linyuh57b093b2017-11-17 14:32:32 -08001172 if (ActivityCompat.isInMultiWindowMode(this)) {
1173 return;
1174 }
1175
1176 View navigationBarBackground = getWindow().findViewById(R.id.navigation_bar_background);
1177 if (navigationBarBackground != null) {
1178 navigationBarBackground.setVisibility(isDialpadVisible ? View.VISIBLE : View.GONE);
Eric Erfanian938468d2017-10-24 14:05:52 -07001179 }
1180 }
1181
Eric Erfanianccca3152017-02-22 16:32:36 -08001182 public void setAllowOrientationChange(boolean allowOrientationChange) {
wangqi9982f0d2017-10-11 17:46:07 -07001183 if (this.allowOrientationChange == allowOrientationChange) {
1184 return;
1185 }
1186 this.allowOrientationChange = allowOrientationChange;
Eric Erfanianccca3152017-02-22 16:32:36 -08001187 if (!allowOrientationChange) {
1188 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_DISALLOW_ROTATION);
1189 } else {
1190 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
1191 }
1192 enableInCallOrientationEventListener(allowOrientationChange);
1193 }
1194
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001195 public void hideMainInCallFragment() {
linyuhc3968e62017-11-20 17:40:50 -08001196 LogUtil.enterBlock("InCallActivity.hideMainInCallFragment");
1197 if (getCallCardFragmentVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001198 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1199 hideInCallScreenFragment(transaction);
1200 hideVideoCallScreenFragment(transaction);
1201 transaction.commitAllowingStateLoss();
1202 getSupportFragmentManager().executePendingTransactions();
1203 }
1204 }
1205
1206 private void showMainInCallFragment() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001207 Trace.beginSection("InCallActivity.showMainInCallFragment");
Eric Erfanianccca3152017-02-22 16:32:36 -08001208 // If the activity's onStart method hasn't been called yet then defer doing any work.
1209 if (!isVisible) {
1210 LogUtil.i("InCallActivity.showMainInCallFragment", "not visible yet/anymore");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001211 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001212 return;
1213 }
1214
1215 // Don't let this be reentrant.
1216 if (isInShowMainInCallFragment) {
1217 LogUtil.i("InCallActivity.showMainInCallFragment", "already in method, bailing");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001218 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001219 return;
1220 }
1221
1222 isInShowMainInCallFragment = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001223 ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi();
1224 ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi();
Eric Erfanianccca3152017-02-22 16:32:36 -08001225 LogUtil.i(
1226 "InCallActivity.showMainInCallFragment",
1227 "shouldShowAnswerUi: %b, shouldShowVideoUi: %b, "
1228 + "didShowAnswerScreen: %b, didShowInCallScreen: %b, didShowVideoCallScreen: %b",
1229 shouldShowAnswerUi.shouldShow,
Eric Erfanian10b34a52017-05-04 08:23:17 -07001230 shouldShowVideoUi.shouldShow,
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 didShowAnswerScreen,
1232 didShowInCallScreen,
1233 didShowVideoCallScreen);
1234 // Only video call ui allows orientation change.
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001235 setAllowOrientationChange(shouldShowVideoUi.shouldShow);
Eric Erfanianccca3152017-02-22 16:32:36 -08001236
1237 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1238 boolean didChangeInCall;
1239 boolean didChangeVideo;
1240 boolean didChangeAnswer;
1241 if (shouldShowAnswerUi.shouldShow) {
1242 didChangeInCall = hideInCallScreenFragment(transaction);
1243 didChangeVideo = hideVideoCallScreenFragment(transaction);
1244 didChangeAnswer = showAnswerScreenFragment(transaction, shouldShowAnswerUi.call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001245 } else if (shouldShowVideoUi.shouldShow) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001246 didChangeInCall = hideInCallScreenFragment(transaction);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001247 didChangeVideo = showVideoCallScreenFragment(transaction, shouldShowVideoUi.call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001248 didChangeAnswer = hideAnswerScreenFragment(transaction);
1249 } else {
1250 didChangeInCall = showInCallScreenFragment(transaction);
1251 didChangeVideo = hideVideoCallScreenFragment(transaction);
1252 didChangeAnswer = hideAnswerScreenFragment(transaction);
1253 }
1254
1255 if (didChangeInCall || didChangeVideo || didChangeAnswer) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001256 Trace.beginSection("InCallActivity.commitTransaction");
Eric Erfanianccca3152017-02-22 16:32:36 -08001257 transaction.commitNow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001258 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001259 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1260 }
1261 isInShowMainInCallFragment = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001262 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 }
1264
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001265 private ShouldShowUiResult getShouldShowAnswerUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001266 DialerCall call = CallList.getInstance().getIncomingCall();
1267 if (call != null) {
1268 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001269 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001270 }
1271
1272 call = CallList.getInstance().getVideoUpgradeRequestCall();
1273 if (call != null) {
1274 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001275 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001276 }
1277
1278 // Check if we're showing the answer screen and the call is disconnected. If this condition is
1279 // true then we won't switch from the answer UI to the in call UI. This prevents flicker when
1280 // the user rejects an incoming call.
1281 call = CallList.getInstance().getFirstCall();
1282 if (call == null) {
1283 call = CallList.getInstance().getBackgroundCall();
1284 }
1285 if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) {
1286 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001287 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001288 }
1289
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001290 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001291 }
1292
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001293 private static ShouldShowUiResult getShouldShowVideoUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001294 DialerCall call = CallList.getInstance().getFirstCall();
1295 if (call == null) {
1296 LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001297 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001298 }
1299
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001300 if (call.isVideoCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001301 LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001302 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001303 }
1304
linyuh8fbecce2017-12-18 13:53:09 -08001305 if (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001306 LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001307 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001308 }
1309
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001310 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001311 }
1312
1313 private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) {
1314 // When rejecting a call the active call can become null in which case we should continue
1315 // showing the answer screen.
1316 if (didShowAnswerScreen && call == null) {
1317 return false;
1318 }
1319
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001320 Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null");
1321
1322 boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -08001323
1324 // Check if we're already showing an answer screen for this call.
1325 if (didShowAnswerScreen) {
1326 AnswerScreen answerScreen = getAnswerScreen();
1327 if (answerScreen.getCallId().equals(call.getId())
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001328 && answerScreen.isVideoCall() == call.isVideoCall()
Eric Erfanian2ca43182017-08-31 06:57:16 -07001329 && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest
1330 && !answerScreen.isActionTimeout()) {
1331 LogUtil.d(
1332 "InCallActivity.showAnswerScreenFragment",
1333 "answer fragment exists for same call and has NOT been accepted/rejected/timed out");
Eric Erfanianccca3152017-02-22 16:32:36 -08001334 return false;
1335 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001336 if (answerScreen.isActionTimeout()) {
1337 LogUtil.i(
1338 "InCallActivity.showAnswerScreenFragment",
1339 "answer fragment exists but has been accepted/rejected and timed out");
1340 } else {
1341 LogUtil.i(
1342 "InCallActivity.showAnswerScreenFragment",
1343 "answer fragment exists but arguments do not match");
1344 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001345 hideAnswerScreenFragment(transaction);
1346 }
1347
1348 // Show a new answer screen.
1349 AnswerScreen answerScreen =
Eric Erfanianfc37b022017-03-21 10:11:17 -07001350 AnswerBindings.createAnswerScreen(
1351 call.getId(),
1352 call.isVideoCall(),
1353 isVideoUpgradeRequest,
Eric Erfanian90508232017-03-24 09:31:16 -07001354 call.getVideoTech().isSelfManagedCamera(),
1355 shouldAllowAnswerAndRelease(call),
1356 CallList.getInstance().getBackgroundCall() != null);
linyuhc3968e62017-11-20 17:40:50 -08001357 transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001358
1359 Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this);
1360 didShowAnswerScreen = true;
1361 return true;
1362 }
1363
Eric Erfanian90508232017-03-24 09:31:16 -07001364 private boolean shouldAllowAnswerAndRelease(DialerCall call) {
1365 if (CallList.getInstance().getActiveCall() == null) {
1366 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "no active call");
1367 return false;
1368 }
1369 if (getSystemService(TelephonyManager.class).getPhoneType()
1370 == TelephonyManager.PHONE_TYPE_CDMA) {
1371 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "PHONE_TYPE_CDMA not supported");
1372 return false;
1373 }
1374 if (call.isVideoCall() || call.hasReceivedVideoUpgradeRequest()) {
1375 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "video call");
1376 return false;
1377 }
linyuhc3968e62017-11-20 17:40:50 -08001378 if (!ConfigProviderBindings.get(this)
1379 .getBoolean(ConfigNames.ANSWER_AND_RELEASE_ENABLED, true)) {
Eric Erfanian90508232017-03-24 09:31:16 -07001380 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "disabled by config");
1381 return false;
1382 }
1383
1384 return true;
1385 }
1386
Eric Erfanianccca3152017-02-22 16:32:36 -08001387 private boolean hideAnswerScreenFragment(FragmentTransaction transaction) {
1388 if (!didShowAnswerScreen) {
1389 return false;
1390 }
1391 AnswerScreen answerScreen = getAnswerScreen();
1392 if (answerScreen != null) {
1393 transaction.remove(answerScreen.getAnswerScreenFragment());
1394 }
1395
1396 didShowAnswerScreen = false;
1397 return true;
1398 }
1399
1400 private boolean showInCallScreenFragment(FragmentTransaction transaction) {
1401 if (didShowInCallScreen) {
1402 return false;
1403 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001404 InCallScreen inCallScreen = InCallBindings.createInCallScreen();
linyuhc3968e62017-11-20 17:40:50 -08001405 transaction.add(R.id.main, inCallScreen.getInCallScreenFragment(), Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001406 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1407 didShowInCallScreen = true;
1408 return true;
1409 }
1410
1411 private boolean hideInCallScreenFragment(FragmentTransaction transaction) {
1412 if (!didShowInCallScreen) {
1413 return false;
1414 }
1415 InCallScreen inCallScreen = getInCallScreen();
1416 if (inCallScreen != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001417 transaction.remove(inCallScreen.getInCallScreenFragment());
Eric Erfanianccca3152017-02-22 16:32:36 -08001418 }
1419 didShowInCallScreen = false;
1420 return true;
1421 }
1422
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001423 private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001424 if (didShowVideoCallScreen) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001425 VideoCallScreen videoCallScreen = getVideoCallScreen();
1426 if (videoCallScreen.getCallId().equals(call.getId())) {
1427 return false;
1428 }
1429 LogUtil.i(
1430 "InCallActivity.showVideoCallScreenFragment",
1431 "video call fragment exists but arguments do not match");
1432 hideVideoCallScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -08001433 }
1434
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001435 LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call);
1436
Eric Erfanian90508232017-03-24 09:31:16 -07001437 VideoCallScreen videoCallScreen =
1438 VideoBindings.createVideoCallScreen(
1439 call.getId(), call.getVideoTech().shouldUseSurfaceView());
linyuhc3968e62017-11-20 17:40:50 -08001440 transaction.add(
1441 R.id.main, videoCallScreen.getVideoCallScreenFragment(), Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001442
1443 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1444 didShowVideoCallScreen = true;
1445 return true;
1446 }
1447
1448 private boolean hideVideoCallScreenFragment(FragmentTransaction transaction) {
1449 if (!didShowVideoCallScreen) {
1450 return false;
1451 }
1452 VideoCallScreen videoCallScreen = getVideoCallScreen();
1453 if (videoCallScreen != null) {
1454 transaction.remove(videoCallScreen.getVideoCallScreenFragment());
1455 }
1456 didShowVideoCallScreen = false;
1457 return true;
1458 }
1459
linyuhc3968e62017-11-20 17:40:50 -08001460 private AnswerScreen getAnswerScreen() {
1461 return (AnswerScreen) getSupportFragmentManager().findFragmentByTag(Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001462 }
1463
linyuhc3968e62017-11-20 17:40:50 -08001464 private InCallScreen getInCallScreen() {
1465 return (InCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 }
1467
linyuhc3968e62017-11-20 17:40:50 -08001468 private VideoCallScreen getVideoCallScreen() {
1469 return (VideoCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001470 }
1471
1472 @Override
1473 public void onPseudoScreenStateChanged(boolean isOn) {
1474 LogUtil.i("InCallActivity.onPseudoScreenStateChanged", "isOn: " + isOn);
1475 pseudoBlackScreenOverlay.setVisibility(isOn ? View.GONE : View.VISIBLE);
1476 }
1477
1478 /**
1479 * For some touch related issue, turning off the screen can be faked by drawing a black view over
1480 * the activity. All touch events started when the screen is "off" is rejected.
1481 *
1482 * @see PseudoScreenState
1483 */
1484 @Override
1485 public boolean dispatchTouchEvent(MotionEvent event) {
1486 // Reject any gesture that started when the screen is in the fake off state.
1487 if (touchDownWhenPseudoScreenOff) {
1488 if (event.getAction() == MotionEvent.ACTION_UP) {
1489 touchDownWhenPseudoScreenOff = false;
1490 }
1491 return true;
1492 }
1493 // Reject all touch event when the screen is in the fake off state.
1494 if (!InCallPresenter.getInstance().getPseudoScreenState().isOn()) {
1495 if (event.getAction() == MotionEvent.ACTION_DOWN) {
1496 touchDownWhenPseudoScreenOff = true;
1497 LogUtil.i("InCallActivity.dispatchTouchEvent", "touchDownWhenPseudoScreenOff");
1498 }
1499 return true;
1500 }
1501 return super.dispatchTouchEvent(event);
1502 }
1503
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001504 private static class ShouldShowUiResult {
Eric Erfanianccca3152017-02-22 16:32:36 -08001505 public final boolean shouldShow;
1506 public final DialerCall call;
1507
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001508 ShouldShowUiResult(boolean shouldShow, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001509 this.shouldShow = shouldShow;
1510 this.call = call;
1511 }
1512 }
linyuhc3968e62017-11-20 17:40:50 -08001513
1514 private static final class IntentExtraNames {
1515 static final String FOR_FULL_SCREEN = "InCallActivity.for_full_screen_intent";
1516 static final String NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call";
1517 static final String SHOW_DIALPAD = "InCallActivity.show_dialpad";
1518 }
1519
1520 private static final class KeysForSavedInstance {
1521 static final String DIALPAD_TEXT = "InCallActivity.dialpad_text";
1522 static final String DID_SHOW_ANSWER_SCREEN = "did_show_answer_screen";
1523 static final String DID_SHOW_IN_CALL_SCREEN = "did_show_in_call_screen";
1524 static final String DID_SHOW_VIDEO_CALL_SCREEN = "did_show_video_call_screen";
1525 }
1526
1527 /** Request codes for pending intents. */
1528 public static final class PendingIntentRequestCodes {
1529 static final int NON_FULL_SCREEN = 0;
1530 static final int FULL_SCREEN = 1;
1531 static final int BUBBLE = 2;
1532 }
1533
1534 private static final class Tags {
1535 static final String ANSWER_SCREEN = "tag_answer_screen";
1536 static final String DIALPAD_FRAGMENT = "tag_dialpad_fragment";
1537 static final String IN_CALL_SCREEN = "tag_in_call_screen";
1538 static final String INTERNATIONAL_CALL_ON_WIFI = "tag_international_call_on_wifi";
1539 static final String SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment";
1540 static final String VIDEO_CALL_SCREEN = "tag_video_call_screen";
1541 static final String POST_CHAR_DIALOG_FRAGMENT = "tag_post_char_dialog_fragment";
1542 }
1543
1544 private static final class ConfigNames {
1545 static final String ANSWER_AND_RELEASE_ENABLED = "answer_and_release_enabled";
1546 }
1547
1548 private static final class InternationalCallOnWifiCallback
1549 implements InternationalCallOnWifiDialogFragment.Callback {
1550 private static final String TAG = InternationalCallOnWifiCallback.class.getCanonicalName();
1551
1552 @Override
1553 public void continueCall(@NonNull String callId) {
1554 LogUtil.i(TAG, "Continuing call with ID: %s", callId);
1555 }
1556
1557 @Override
1558 public void cancelCall(@NonNull String callId) {
1559 DialerCall call = CallList.getInstance().getCallById(callId);
1560 if (call == null) {
1561 LogUtil.i(TAG, "Call destroyed before the dialog is closed");
1562 return;
1563 }
1564
1565 LogUtil.i(TAG, "Disconnecting international call on WiFi");
1566 call.disconnect();
1567 }
1568 }
1569
1570 private static final class SelectPhoneAccountListener
1571 extends SelectPhoneAccountDialogFragment.SelectPhoneAccountListener {
1572 private static final String TAG = SelectPhoneAccountListener.class.getCanonicalName();
1573
1574 @Override
1575 public void onPhoneAccountSelected(
1576 PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) {
1577 DialerCall call = CallList.getInstance().getCallById(callId);
1578 LogUtil.i(TAG, "Phone account select with call:\n%s", call);
1579
1580 if (call != null) {
1581 call.phoneAccountSelected(selectedAccountHandle, setDefault);
1582 }
1583 }
1584
1585 @Override
1586 public void onDialogDismissed(String callId) {
1587 DialerCall call = CallList.getInstance().getCallById(callId);
1588 LogUtil.i(TAG, "Disconnecting call:\n%s" + call);
1589
1590 if (call != null) {
1591 call.disconnect();
1592 }
1593 }
1594 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001595}