blob: f09f0d944872596739f9689e9a05f99c473ac01a [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
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 if (ActivityCompat.isInMultiWindowMode(this)
410 && !getResources().getBoolean(R.bool.incall_dialpad_allowed)) {
411 // Hide the dialpad because there may not be enough room
412 showDialpadFragment(false, false);
413 }
linyuh57b093b2017-11-17 14:32:32 -0800414
Eric Erfanian2ca43182017-08-31 06:57:16 -0700415 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 }
417
418 @Override
419 protected void onResume() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700420 Trace.beginSection("InCallActivity.onResume");
Eric Erfanianccca3152017-02-22 16:32:36 -0800421 super.onResume();
linyuhc3968e62017-11-20 17:40:50 -0800422
423 if (!InCallPresenter.getInstance().isReadyForTearDown()) {
424 updateTaskDescription();
425 InCallPresenter.getInstance().onUiShowing(true);
426 }
427
428 // If there is a pending request to show or hide the dialpad, handle that now.
429 if (showDialpadRequest != DIALPAD_REQUEST_NONE) {
430 if (showDialpadRequest == DIALPAD_REQUEST_SHOW) {
431 // Exit fullscreen so that the user has access to the dialpad hide/show button.
432 // This is important when showing the dialpad from within dialer.
433 InCallPresenter.getInstance().setFullScreen(false /* isFullScreen */, true /* force */);
434
435 showDialpadFragment(true /* show */, animateDialpadOnShow /* animate */);
436 animateDialpadOnShow = false;
437
438 DialpadFragment dialpadFragment = getDialpadFragment();
439 if (dialpadFragment != null) {
440 dialpadFragment.setDtmfText(dtmfTextToPrepopulate);
441 dtmfTextToPrepopulate = null;
442 }
443 } else {
444 LogUtil.i("InCallActivity.onResume", "Force-hide the dialpad");
445 if (getDialpadFragment() != null) {
446 showDialpadFragment(false /* show */, false /* animate */);
447 }
448 }
449 showDialpadRequest = DIALPAD_REQUEST_NONE;
450 }
451 updateNavigationBar(isDialpadVisible());
452
453 if (showPostCharWaitDialogOnResume) {
454 showDialogForPostCharWait(showPostCharWaitDialogCallId, showPostCharWaitDialogChars);
455 }
456
457 CallList.getInstance()
458 .onInCallUiShown(getIntent().getBooleanExtra(IntentExtraNames.FOR_FULL_SCREEN, false));
459
Eric Erfanianccca3152017-02-22 16:32:36 -0800460 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState();
461 pseudoScreenState.addListener(this);
462 onPseudoScreenStateChanged(pseudoScreenState.isOn());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700463 Trace.endSection();
weijiaxu650e7cc2017-10-31 12:38:54 -0700464 // add 1 sec delay to get memory snapshot so that dialer wont react slowly on resume.
465 ThreadUtil.postDelayedOnUiThread(
weijiaxuc950a9b2017-11-06 16:39:04 -0800466 () ->
467 Logger.get(this)
468 .logRecordMemory(LoggingBindings.INCALL_ACTIVITY_ON_RESUME_MEMORY_EVENT_NAME),
weijiaxu650e7cc2017-10-31 12:38:54 -0700469 1000);
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 }
471
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 @Override
473 protected void onPause() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700474 Trace.beginSection("InCallActivity.onPause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 super.onPause();
linyuh57b093b2017-11-17 14:32:32 -0800476
477 DialpadFragment dialpadFragment = getDialpadFragment();
478 if (dialpadFragment != null) {
479 dialpadFragment.onDialerKeyUp(null);
480 }
481
482 InCallPresenter.getInstance().onUiShowing(false);
483 if (isFinishing()) {
484 InCallPresenter.getInstance().unsetActivity(this);
485 }
486
Eric Erfanianccca3152017-02-22 16:32:36 -0800487 InCallPresenter.getInstance().getPseudoScreenState().removeListener(this);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700488 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800489 }
490
491 @Override
492 protected void onStop() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700493 Trace.beginSection("InCallActivity.onStop");
wangqi4d705e52017-09-28 12:23:35 -0700494 isVisible = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800495 super.onStop();
linyuh57b093b2017-11-17 14:32:32 -0800496
497 // Disconnects the call waiting for a phone account when the activity is hidden (e.g., after the
498 // user presses the home button).
499 // Without this the pending call will get stuck on phone account selection and new calls can't
500 // be created.
501 // Skip this when the screen is locked since the activity may complete its current life cycle
502 // and restart.
linyuhc3968e62017-11-20 17:40:50 -0800503 if (!isRecreating && !getSystemService(KeyguardManager.class).isKeyguardLocked()) {
linyuh57b093b2017-11-17 14:32:32 -0800504 DialerCall waitingForAccountCall = CallList.getInstance().getWaitingForAccountCall();
505 if (waitingForAccountCall != null) {
506 waitingForAccountCall.disconnect();
507 }
508 }
509
510 enableInCallOrientationEventListener(false);
511 InCallPresenter.getInstance().updateIsChangingConfigurations();
512 InCallPresenter.getInstance().onActivityStopped();
linyuhc3968e62017-11-20 17:40:50 -0800513 if (!isRecreating) {
linyuh57b093b2017-11-17 14:32:32 -0800514 if (errorDialog != null) {
515 errorDialog.dismiss();
516 }
517 }
518
Eric Erfanian2ca43182017-08-31 06:57:16 -0700519 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 }
521
522 @Override
523 protected void onDestroy() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700524 Trace.beginSection("InCallActivity.onDestroy");
Eric Erfanianccca3152017-02-22 16:32:36 -0800525 super.onDestroy();
linyuh57b093b2017-11-17 14:32:32 -0800526
527 InCallPresenter.getInstance().unsetActivity(this);
528 InCallPresenter.getInstance().updateIsChangingConfigurations();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700529 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 }
531
532 @Override
533 public void finish() {
534 if (shouldCloseActivityOnFinish()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700535 // When user select incall ui from recents after the call is disconnected, it tries to launch
536 // a new InCallActivity but InCallPresenter is already teared down at this point, which causes
537 // crash.
538 // By calling finishAndRemoveTask() instead of finish() the task associated with
539 // InCallActivity is cleared completely. So system won't try to create a new InCallActivity in
540 // this case.
541 //
542 // Calling finish won't clear the task and normally when an activity finishes it shouldn't
543 // clear the task since there could be parent activity in the same task that's still alive.
544 // But InCallActivity is special since it's singleInstance which means it's root activity and
545 // only instance of activity in the task. So it should be safe to also remove task when
546 // finishing.
547 // It's also necessary in the sense of it's excluded from recents. So whenever the activity
548 // finishes, the task should also be removed since it doesn't make sense to go back to it in
549 // anyway anymore.
550 super.finishAndRemoveTask();
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 }
552 }
553
554 private boolean shouldCloseActivityOnFinish() {
linyuhc3968e62017-11-20 17:40:50 -0800555 if (!isVisible) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800556 LogUtil.i(
557 "InCallActivity.shouldCloseActivityOnFinish",
558 "allowing activity to be closed because it's not visible");
559 return true;
560 }
561
twyen8efb4952017-10-06 16:35:54 -0700562 if (InCallPresenter.getInstance().isInCallUiLocked()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800563 LogUtil.i(
564 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700565 "in call ui is locked, not closing activity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 return false;
567 }
568
569 LogUtil.i(
570 "InCallActivity.shouldCloseActivityOnFinish",
twyen8efb4952017-10-06 16:35:54 -0700571 "activity is visible and has no locks, allowing activity to close");
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 return true;
573 }
574
575 @Override
576 protected void onNewIntent(Intent intent) {
linyuhc3968e62017-11-20 17:40:50 -0800577 LogUtil.enterBlock("InCallActivity.onNewIntent");
Eric Erfanianccca3152017-02-22 16:32:36 -0800578
579 // If the screen is off, we need to make sure it gets turned on for incoming calls.
580 // This normally works just fine thanks to FLAG_TURN_SCREEN_ON but that only works
581 // when the activity is first created. Therefore, to ensure the screen is turned on
582 // for the call waiting case, we recreate() the current activity. There should be no jank from
583 // this since the screen is already off and will remain so until our new activity is up.
linyuhc3968e62017-11-20 17:40:50 -0800584 if (!isVisible) {
585 onNewIntent(intent, true /* isRecreating */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800586 LogUtil.i("InCallActivity.onNewIntent", "Restarting InCallActivity to force screen on.");
587 recreate();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700588 } else {
linyuhc3968e62017-11-20 17:40:50 -0800589 onNewIntent(intent, false /* isRecreating */);
590 }
591 }
592
593 private void onNewIntent(Intent intent, boolean isRecreating) {
594 this.isRecreating = isRecreating;
595
596 // We're being re-launched with a new Intent. Since it's possible for a single InCallActivity
597 // instance to persist indefinitely (even if we finish() ourselves), this sequence can
598 // happen any time the InCallActivity needs to be displayed.
599
600 // Stash away the new intent so that we can get it in the future by calling getIntent().
601 // Otherwise getIntent() will return the original Intent from when we first got created.
602 setIntent(intent);
603
604 // Activities are always paused before receiving a new intent, so we can count on our onResume()
605 // method being called next.
606
607 // Just like in onCreate(), handle the intent.
608 // Skip if InCallActivity is going to be recreated since this will be called in onCreate().
609 if (!isRecreating) {
610 internalResolveIntent(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 }
612 }
613
614 @Override
615 public void onBackPressed() {
linyuh57b093b2017-11-17 14:32:32 -0800616 LogUtil.enterBlock("InCallActivity.onBackPressed");
617
linyuhc3968e62017-11-20 17:40:50 -0800618 if (!isVisible) {
linyuh57b093b2017-11-17 14:32:32 -0800619 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800620 }
linyuh57b093b2017-11-17 14:32:32 -0800621
622 if (!getCallCardFragmentVisible()) {
623 return;
624 }
625
626 DialpadFragment dialpadFragment = getDialpadFragment();
627 if (dialpadFragment != null && dialpadFragment.isVisible()) {
628 showDialpadFragment(false /* show */, true /* animate */);
629 return;
630 }
631
632 if (CallList.getInstance().getIncomingCall() != null) {
633 LogUtil.i(
634 "InCallActivity.onBackPressed",
635 "Ignore the press of the back key when an incoming call is ringing");
636 return;
637 }
638
639 // Nothing special to do. Fall back to the default behavior.
640 super.onBackPressed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800641 }
642
643 @Override
644 public boolean onOptionsItemSelected(MenuItem item) {
645 LogUtil.i("InCallActivity.onOptionsItemSelected", "item: " + item);
646 if (item.getItemId() == android.R.id.home) {
647 onBackPressed();
648 return true;
649 }
650 return super.onOptionsItemSelected(item);
651 }
652
653 @Override
654 public boolean onKeyUp(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800655 DialpadFragment dialpadFragment = getDialpadFragment();
656 if (dialpadFragment != null
657 && dialpadFragment.isVisible()
658 && dialpadFragment.onDialerKeyUp(event)) {
659 return true;
660 }
661
662 if (keyCode == KeyEvent.KEYCODE_CALL) {
663 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
664 return true;
665 }
666
667 return super.onKeyUp(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800668 }
669
670 @Override
671 public boolean onKeyDown(int keyCode, KeyEvent event) {
linyuh57b093b2017-11-17 14:32:32 -0800672 switch (keyCode) {
673 case KeyEvent.KEYCODE_CALL:
674 if (!InCallPresenter.getInstance().handleCallKey()) {
675 LogUtil.e(
676 "InCallActivity.onKeyDown",
677 "InCallPresenter should always handle KEYCODE_CALL in onKeyDown");
678 }
679 // Always consume KEYCODE_CALL to ensure the PhoneWindow won't do anything with it.
680 return true;
681
682 // Note that KEYCODE_ENDCALL isn't handled here as the standard system-wide handling of it
683 // is exactly what's needed, namely
684 // (1) "hang up" if there's an active call, or
685 // (2) "don't answer" if there's an incoming call.
686 // (See PhoneWindowManager for implementation details.)
687
688 case KeyEvent.KEYCODE_CAMERA:
689 // Consume KEYCODE_CAMERA since it's easy to accidentally press the camera button.
690 return true;
691
692 case KeyEvent.KEYCODE_VOLUME_UP:
693 case KeyEvent.KEYCODE_VOLUME_DOWN:
694 case KeyEvent.KEYCODE_VOLUME_MUTE:
695 // Ringer silencing handled by PhoneWindowManager.
696 break;
697
698 case KeyEvent.KEYCODE_MUTE:
699 TelecomAdapter.getInstance()
700 .mute(!AudioModeProvider.getInstance().getAudioState().isMuted());
701 return true;
702
703 case KeyEvent.KEYCODE_SLASH:
704 // When verbose logging is enabled, dump the view for debugging/testing purposes.
705 if (LogUtil.isVerboseEnabled()) {
706 View decorView = getWindow().getDecorView();
707 LogUtil.v("InCallActivity.onKeyDown", "View dump:\n%s", decorView);
708 return true;
709 }
710 break;
711
712 case KeyEvent.KEYCODE_EQUALS:
713 break;
714
715 default: // fall out
716 }
717
718 // Pass other key events to DialpadFragment's "onDialerKeyDown" method in case the user types
719 // in DTMF (Dual-tone multi-frequency signaling) code.
720 DialpadFragment dialpadFragment = getDialpadFragment();
721 if (dialpadFragment != null
722 && dialpadFragment.isVisible()
723 && dialpadFragment.onDialerKeyDown(event)) {
724 return true;
725 }
726
727 return super.onKeyDown(keyCode, event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 }
729
730 public boolean isInCallScreenAnimating() {
731 return false;
732 }
733
734 public void showConferenceFragment(boolean show) {
735 if (show) {
736 startActivity(new Intent(this, ManageConferenceActivity.class));
737 }
738 }
739
linyuhc3968e62017-11-20 17:40:50 -0800740 public void showDialpadFragment(boolean show, boolean animate) {
741 if (show == isDialpadVisible()) {
742 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800743 }
linyuhc3968e62017-11-20 17:40:50 -0800744
745 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
746 if (dialpadFragmentManager == null) {
747 LogUtil.i("InCallActivity.showDialpadFragment", "Unable to obtain a FragmentManager");
748 return;
749 }
750
751 if (!animate) {
752 if (show) {
753 showDialpadFragment();
754 } else {
755 hideDialpadFragment();
756 }
757 } else {
758 if (show) {
759 showDialpadFragment();
760 getDialpadFragment().animateShowDialpad();
761 }
762 getDialpadFragment()
763 .getView()
764 .startAnimation(show ? dialpadSlideInAnimation : dialpadSlideOutAnimation);
765 }
766
767 ProximitySensor sensor = InCallPresenter.getInstance().getProximitySensor();
768 if (sensor != null) {
769 sensor.onDialpadVisible(show);
770 }
771 showDialpadRequest = DIALPAD_REQUEST_NONE;
772
773 // Note: onInCallScreenDialpadVisibilityChange is called here to ensure that the dialpad FAB
774 // repositions itself.
775 getInCallScreen().onInCallScreenDialpadVisibilityChange(show);
776 }
777
778 private void showDialpadFragment() {
779 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
780 if (dialpadFragmentManager == null) {
781 return;
782 }
783
784 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
785 DialpadFragment dialpadFragment = getDialpadFragment();
786 if (dialpadFragment == null) {
787 transaction.add(getDialpadContainerId(), new DialpadFragment(), Tags.DIALPAD_FRAGMENT);
788 } else {
789 transaction.show(dialpadFragment);
calderwoodrad5883872017-12-12 15:29:12 -0800790 dialpadFragment.setUserVisibleHint(true);
linyuhc3968e62017-11-20 17:40:50 -0800791 }
792 transaction.commitAllowingStateLoss();
793 dialpadFragmentManager.executePendingTransactions();
794
795 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL_DIALPAD, this);
796 updateNavigationBar(true /* isDialpadVisible */);
797 }
798
799 private void hideDialpadFragment() {
800 FragmentManager dialpadFragmentManager = getDialpadFragmentManager();
801 if (dialpadFragmentManager == null) {
802 return;
803 }
804
calderwoodrad5883872017-12-12 15:29:12 -0800805 DialpadFragment dialpadFragment = getDialpadFragment();
linyuhc3968e62017-11-20 17:40:50 -0800806 if (dialpadFragment != null) {
807 FragmentTransaction transaction = dialpadFragmentManager.beginTransaction();
808 transaction.hide(dialpadFragment);
809 transaction.commitAllowingStateLoss();
810 dialpadFragmentManager.executePendingTransactions();
calderwoodrad5883872017-12-12 15:29:12 -0800811 dialpadFragment.setUserVisibleHint(false);
linyuhc3968e62017-11-20 17:40:50 -0800812 }
813 updateNavigationBar(false /* isDialpadVisible */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800814 }
815
816 public boolean isDialpadVisible() {
linyuh69a25062017-11-15 16:18:51 -0800817 DialpadFragment dialpadFragment = getDialpadFragment();
calderwoodrad5883872017-12-12 15:29:12 -0800818 return dialpadFragment != null && dialpadFragment.getUserVisibleHint();
linyuh69a25062017-11-15 16:18:51 -0800819 }
820
linyuhc3968e62017-11-20 17:40:50 -0800821 /** Returns the {@link DialpadFragment} that's shown by this activity, or {@code null} */
linyuh69a25062017-11-15 16:18:51 -0800822 @Nullable
linyuhc3968e62017-11-20 17:40:50 -0800823 private DialpadFragment getDialpadFragment() {
linyuh69a25062017-11-15 16:18:51 -0800824 FragmentManager fragmentManager = getDialpadFragmentManager();
825 if (fragmentManager == null) {
826 return null;
827 }
linyuhc3968e62017-11-20 17:40:50 -0800828 return (DialpadFragment) fragmentManager.findFragmentByTag(Tags.DIALPAD_FRAGMENT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800829 }
830
831 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh57b093b2017-11-17 14:32:32 -0800832 updateTaskDescription();
833
834 if (newForegroundCall == null || !didShowAnswerScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800835 LogUtil.v("InCallActivity.onForegroundCallChanged", "resetting background color");
linyuh57b093b2017-11-17 14:32:32 -0800836 updateWindowBackgroundColor(0 /* progress */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800837 }
838 }
839
linyuhc3968e62017-11-20 17:40:50 -0800840 private void updateTaskDescription() {
linyuh57b093b2017-11-17 14:32:32 -0800841 int color =
842 getResources().getBoolean(R.bool.is_layout_landscape)
843 ? ResourcesCompat.getColor(
844 getResources(), R.color.statusbar_background_color, getTheme())
845 : InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor();
846 setTaskDescription(
847 new TaskDescription(
848 getResources().getString(R.string.notification_ongoing_call), null /* icon */, color));
849 }
850
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) {
852 ThemeColorManager themeColorManager = InCallPresenter.getInstance().getThemeColorManager();
853 @ColorInt int top;
854 @ColorInt int middle;
855 @ColorInt int bottom;
856 @ColorInt int gray = 0x66000000;
857
858 if (ActivityCompat.isInMultiWindowMode(this)) {
859 top = themeColorManager.getBackgroundColorSolid();
860 middle = themeColorManager.getBackgroundColorSolid();
861 bottom = themeColorManager.getBackgroundColorSolid();
862 } else {
863 top = themeColorManager.getBackgroundColorTop();
864 middle = themeColorManager.getBackgroundColorMiddle();
865 bottom = themeColorManager.getBackgroundColorBottom();
866 }
867
868 if (progress < 0) {
869 float correctedProgress = Math.abs(progress);
870 top = ColorUtils.blendARGB(top, gray, correctedProgress);
871 middle = ColorUtils.blendARGB(middle, gray, correctedProgress);
872 bottom = ColorUtils.blendARGB(bottom, gray, correctedProgress);
873 }
874
875 boolean backgroundDirty = false;
876 if (backgroundDrawable == null) {
877 backgroundDrawableColors = new int[] {top, middle, bottom};
878 backgroundDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, backgroundDrawableColors);
879 backgroundDirty = true;
880 } else {
881 if (backgroundDrawableColors[0] != top) {
882 backgroundDrawableColors[0] = top;
883 backgroundDirty = true;
884 }
885 if (backgroundDrawableColors[1] != middle) {
886 backgroundDrawableColors[1] = middle;
887 backgroundDirty = true;
888 }
889 if (backgroundDrawableColors[2] != bottom) {
890 backgroundDrawableColors[2] = bottom;
891 backgroundDirty = true;
892 }
893 if (backgroundDirty) {
894 backgroundDrawable.setColors(backgroundDrawableColors);
895 }
896 }
897
898 if (backgroundDirty) {
899 getWindow().setBackgroundDrawable(backgroundDrawable);
900 }
901 }
902
903 public boolean isVisible() {
904 return isVisible;
905 }
906
907 public boolean getCallCardFragmentVisible() {
908 return didShowInCallScreen || didShowVideoCallScreen;
909 }
910
911 public void dismissKeyguard(boolean dismiss) {
linyuh9c327da2017-11-14 12:33:48 -0800912 if (dismissKeyguard == dismiss) {
913 return;
914 }
915
916 dismissKeyguard = dismiss;
917 if (dismiss) {
918 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
919 } else {
920 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
921 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800922 }
923
linyuhc3968e62017-11-20 17:40:50 -0800924 public void showDialogForPostCharWait(String callId, String chars) {
925 if (isVisible) {
926 PostCharDialogFragment fragment = new PostCharDialogFragment(callId, chars);
927 fragment.show(getSupportFragmentManager(), Tags.POST_CHAR_DIALOG_FRAGMENT);
928
929 showPostCharWaitDialogOnResume = false;
930 showPostCharWaitDialogCallId = null;
931 showPostCharWaitDialogChars = null;
932 } else {
933 showPostCharWaitDialogOnResume = true;
934 showPostCharWaitDialogCallId = callId;
935 showPostCharWaitDialogChars = chars;
936 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 }
938
linyuh7b86f562017-11-16 11:24:09 -0800939 public void showDialogOrToastForDisconnectedCall(DisconnectMessage disconnectMessage) {
940 LogUtil.i(
941 "InCallActivity.showDialogOrToastForDisconnectedCall",
942 "disconnect cause: %s",
943 disconnectMessage);
944
945 if (disconnectMessage.dialog == null || isFinishing()) {
946 return;
947 }
948
949 dismissPendingDialogs();
950
951 // Show a toast if the app is in background when a dialog can't be visible.
952 if (!isVisible()) {
953 Toast.makeText(getApplicationContext(), disconnectMessage.toastMessage, Toast.LENGTH_LONG)
954 .show();
955 return;
956 }
957
958 // Show the dialog.
linyuhc3968e62017-11-20 17:40:50 -0800959 errorDialog = disconnectMessage.dialog;
linyuh7b86f562017-11-16 11:24:09 -0800960 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("showErrorDialog");
961 disconnectMessage.dialog.setOnDismissListener(
962 dialogInterface -> {
963 lock.release();
964 onDialogDismissed();
965 });
966 disconnectMessage.dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
967 disconnectMessage.dialog.show();
968 }
969
970 private void onDialogDismissed() {
linyuhc3968e62017-11-20 17:40:50 -0800971 errorDialog = null;
linyuh7b86f562017-11-16 11:24:09 -0800972 CallList.getInstance().onErrorDialogDismissed();
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 }
974
975 public void dismissPendingDialogs() {
linyuhc3968e62017-11-20 17:40:50 -0800976 LogUtil.enterBlock("InCallActivity.dismissPendingDialogs");
linyuhf99f6302017-11-15 11:23:51 -0800977
978 if (!isVisible) {
979 // Defer the dismissing action as the activity is not visible and onSaveInstanceState may have
980 // been called.
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 LogUtil.i(
982 "InCallActivity.dismissPendingDialogs", "defer actions since activity is not visible");
983 needDismissPendingDialogs = true;
linyuhf99f6302017-11-15 11:23:51 -0800984 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800985 }
linyuhf99f6302017-11-15 11:23:51 -0800986
987 // Dismiss the error dialog
linyuhf99f6302017-11-15 11:23:51 -0800988 if (errorDialog != null) {
989 errorDialog.dismiss();
linyuhc3968e62017-11-20 17:40:50 -0800990 errorDialog = null;
linyuhf99f6302017-11-15 11:23:51 -0800991 }
992
993 // Dismiss the phone account selection dialog
linyuhf99f6302017-11-15 11:23:51 -0800994 if (selectPhoneAccountDialogFragment != null) {
995 selectPhoneAccountDialogFragment.dismiss();
linyuhc3968e62017-11-20 17:40:50 -0800996 selectPhoneAccountDialogFragment = null;
linyuhf99f6302017-11-15 11:23:51 -0800997 }
998
999 // Dismiss the dialog for international call on WiFi
1000 InternationalCallOnWifiDialogFragment internationalCallOnWifiFragment =
1001 (InternationalCallOnWifiDialogFragment)
linyuhc3968e62017-11-20 17:40:50 -08001002 getSupportFragmentManager().findFragmentByTag(Tags.INTERNATIONAL_CALL_ON_WIFI);
linyuhf99f6302017-11-15 11:23:51 -08001003 if (internationalCallOnWifiFragment != null) {
1004 internationalCallOnWifiFragment.dismiss();
1005 }
1006
1007 // Dismiss the answer screen
1008 AnswerScreen answerScreen = getAnswerScreen();
1009 if (answerScreen != null) {
1010 answerScreen.dismissPendingDialogs();
1011 }
1012
1013 needDismissPendingDialogs = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001014 }
1015
linyuhc3968e62017-11-20 17:40:50 -08001016 private void enableInCallOrientationEventListener(boolean enable) {
linyuh69a25062017-11-15 16:18:51 -08001017 if (enable) {
1018 inCallOrientationEventListener.enable(true /* notifyDeviceOrientationChange */);
1019 } else {
1020 inCallOrientationEventListener.disable();
1021 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001022 }
1023
1024 public void setExcludeFromRecents(boolean exclude) {
linyuhc3968e62017-11-20 17:40:50 -08001025 int taskId = getTaskId();
1026
1027 List<AppTask> tasks = getSystemService(ActivityManager.class).getAppTasks();
1028 for (AppTask task : tasks) {
1029 try {
1030 if (task.getTaskInfo().id == taskId) {
1031 task.setExcludeFromRecents(exclude);
1032 }
1033 } catch (RuntimeException e) {
1034 LogUtil.e("InCallActivity.setExcludeFromRecents", "RuntimeException:\n%s", e);
1035 }
1036 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001037 }
1038
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 @Nullable
1040 public FragmentManager getDialpadFragmentManager() {
1041 InCallScreen inCallScreen = getInCallScreen();
1042 if (inCallScreen != null) {
1043 return inCallScreen.getInCallScreenFragment().getChildFragmentManager();
1044 }
1045 return null;
1046 }
1047
1048 public int getDialpadContainerId() {
1049 return getInCallScreen().getAnswerAndDialpadContainerResourceId();
1050 }
1051
1052 @Override
1053 public AnswerScreenDelegate newAnswerScreenDelegate(AnswerScreen answerScreen) {
1054 DialerCall call = CallList.getInstance().getCallById(answerScreen.getCallId());
1055 if (call == null) {
1056 // This is a work around for a bug where we attempt to create a new delegate after the call
1057 // has already been removed. An example of when this can happen is:
1058 // 1. incoming video call in landscape mode
1059 // 2. remote party hangs up
1060 // 3. activity switches from landscape to portrait
1061 // At step #3 the answer fragment will try to create a new answer delegate but the call won't
1062 // exist. In this case we'll simply return a stub delegate that does nothing. This is ok
1063 // because this new state is transient and the activity will be destroyed soon.
1064 LogUtil.i("InCallActivity.onPrimaryCallStateChanged", "call doesn't exist, using stub");
1065 return new AnswerScreenPresenterStub();
1066 } else {
1067 return new AnswerScreenPresenter(
1068 this, answerScreen, CallList.getInstance().getCallById(answerScreen.getCallId()));
1069 }
1070 }
1071
1072 @Override
1073 public InCallScreenDelegate newInCallScreenDelegate() {
1074 return new CallCardPresenter(this);
1075 }
1076
1077 @Override
1078 public InCallButtonUiDelegate newInCallButtonUiDelegate() {
1079 return new CallButtonPresenter(this);
1080 }
1081
1082 @Override
Eric Erfanian90508232017-03-24 09:31:16 -07001083 public VideoCallScreenDelegate newVideoCallScreenDelegate(VideoCallScreen videoCallScreen) {
1084 DialerCall dialerCall = CallList.getInstance().getCallById(videoCallScreen.getCallId());
1085 if (dialerCall != null && dialerCall.getVideoTech().shouldUseSurfaceView()) {
1086 return dialerCall.getVideoTech().createVideoCallScreenDelegate(this, videoCallScreen);
1087 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001088 return new VideoCallPresenter();
1089 }
1090
1091 public void onPrimaryCallStateChanged() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 Trace.beginSection("InCallActivity.onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -08001093 showMainInCallFragment();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001094 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 }
1096
linyuh7b86f562017-11-16 11:24:09 -08001097 public void showToastForWiFiToLteHandover(DialerCall call) {
1098 if (call.hasShownWiFiToLteHandoverToast()) {
1099 return;
1100 }
1101
1102 Toast.makeText(this, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG).show();
1103 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -08001104 }
1105
linyuh7b86f562017-11-16 11:24:09 -08001106 public void showDialogOrToastForWifiHandoverFailure(DialerCall call) {
1107 if (call.showWifiHandoverAlertAsToast()) {
1108 Toast.makeText(this, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
1109 .show();
1110 return;
1111 }
1112
1113 dismissPendingDialogs();
1114
1115 AlertDialog.Builder builder =
1116 new AlertDialog.Builder(this).setTitle(R.string.video_call_lte_to_wifi_failed_title);
1117
1118 // This allows us to use the theme of the dialog instead of the activity
1119 View dialogCheckBoxView =
1120 View.inflate(builder.getContext(), R.layout.video_call_lte_to_wifi_failed, null /* root */);
1121 CheckBox wifiHandoverFailureCheckbox =
1122 (CheckBox) dialogCheckBoxView.findViewById(R.id.video_call_lte_to_wifi_failed_checkbox);
1123 wifiHandoverFailureCheckbox.setChecked(false);
1124
1125 InCallUiLock lock = InCallPresenter.getInstance().acquireInCallUiLock("WifiFailedDialog");
linyuhc3968e62017-11-20 17:40:50 -08001126 errorDialog =
linyuh7b86f562017-11-16 11:24:09 -08001127 builder
1128 .setView(dialogCheckBoxView)
1129 .setMessage(R.string.video_call_lte_to_wifi_failed_message)
1130 .setOnCancelListener(dialogInterface -> onDialogDismissed())
1131 .setPositiveButton(
1132 android.R.string.ok,
1133 (dialogInterface, id) -> {
1134 call.setDoNotShowDialogForHandoffToWifiFailure(
1135 wifiHandoverFailureCheckbox.isChecked());
1136 dialogInterface.cancel();
1137 onDialogDismissed();
1138 })
1139 .setOnDismissListener(dialogInterface -> lock.release())
1140 .create();
linyuh7b86f562017-11-16 11:24:09 -08001141 errorDialog.show();
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 }
1143
linyuh7b86f562017-11-16 11:24:09 -08001144 public void showDialogForInternationalCallOnWifi(@NonNull DialerCall call) {
1145 if (!InternationalCallOnWifiDialogFragment.shouldShow(this)) {
1146 LogUtil.i(
1147 "InCallActivity.showDialogForInternationalCallOnWifi",
1148 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
1149 return;
1150 }
1151
1152 InternationalCallOnWifiDialogFragment fragment =
1153 InternationalCallOnWifiDialogFragment.newInstance(
linyuhc3968e62017-11-20 17:40:50 -08001154 call.getId(), internationalCallOnWifiCallback);
1155 fragment.show(getSupportFragmentManager(), Tags.INTERNATIONAL_CALL_ON_WIFI);
Eric Erfanianc857f902017-05-15 14:05:33 -07001156 }
1157
Eric Erfanian938468d2017-10-24 14:05:52 -07001158 @Override
1159 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
1160 super.onMultiWindowModeChanged(isInMultiWindowMode);
linyuh57b093b2017-11-17 14:32:32 -08001161 updateNavigationBar(isDialpadVisible());
1162 }
1163
linyuhc3968e62017-11-20 17:40:50 -08001164 private void updateNavigationBar(boolean isDialpadVisible) {
linyuh57b093b2017-11-17 14:32:32 -08001165 if (ActivityCompat.isInMultiWindowMode(this)) {
1166 return;
1167 }
1168
1169 View navigationBarBackground = getWindow().findViewById(R.id.navigation_bar_background);
1170 if (navigationBarBackground != null) {
1171 navigationBarBackground.setVisibility(isDialpadVisible ? View.VISIBLE : View.GONE);
Eric Erfanian938468d2017-10-24 14:05:52 -07001172 }
1173 }
1174
Eric Erfanianccca3152017-02-22 16:32:36 -08001175 public void setAllowOrientationChange(boolean allowOrientationChange) {
wangqi9982f0d2017-10-11 17:46:07 -07001176 if (this.allowOrientationChange == allowOrientationChange) {
1177 return;
1178 }
1179 this.allowOrientationChange = allowOrientationChange;
Eric Erfanianccca3152017-02-22 16:32:36 -08001180 if (!allowOrientationChange) {
1181 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_DISALLOW_ROTATION);
1182 } else {
1183 setRequestedOrientation(InCallOrientationEventListener.ACTIVITY_PREFERENCE_ALLOW_ROTATION);
1184 }
1185 enableInCallOrientationEventListener(allowOrientationChange);
1186 }
1187
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001188 public void hideMainInCallFragment() {
linyuhc3968e62017-11-20 17:40:50 -08001189 LogUtil.enterBlock("InCallActivity.hideMainInCallFragment");
1190 if (getCallCardFragmentVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001191 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1192 hideInCallScreenFragment(transaction);
1193 hideVideoCallScreenFragment(transaction);
1194 transaction.commitAllowingStateLoss();
1195 getSupportFragmentManager().executePendingTransactions();
1196 }
1197 }
1198
1199 private void showMainInCallFragment() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001200 Trace.beginSection("InCallActivity.showMainInCallFragment");
Eric Erfanianccca3152017-02-22 16:32:36 -08001201 // If the activity's onStart method hasn't been called yet then defer doing any work.
1202 if (!isVisible) {
1203 LogUtil.i("InCallActivity.showMainInCallFragment", "not visible yet/anymore");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001204 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001205 return;
1206 }
1207
1208 // Don't let this be reentrant.
1209 if (isInShowMainInCallFragment) {
1210 LogUtil.i("InCallActivity.showMainInCallFragment", "already in method, bailing");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001211 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001212 return;
1213 }
1214
1215 isInShowMainInCallFragment = true;
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001216 ShouldShowUiResult shouldShowAnswerUi = getShouldShowAnswerUi();
1217 ShouldShowUiResult shouldShowVideoUi = getShouldShowVideoUi();
Eric Erfanianccca3152017-02-22 16:32:36 -08001218 LogUtil.i(
1219 "InCallActivity.showMainInCallFragment",
1220 "shouldShowAnswerUi: %b, shouldShowVideoUi: %b, "
1221 + "didShowAnswerScreen: %b, didShowInCallScreen: %b, didShowVideoCallScreen: %b",
1222 shouldShowAnswerUi.shouldShow,
Eric Erfanian10b34a52017-05-04 08:23:17 -07001223 shouldShowVideoUi.shouldShow,
Eric Erfanianccca3152017-02-22 16:32:36 -08001224 didShowAnswerScreen,
1225 didShowInCallScreen,
1226 didShowVideoCallScreen);
1227 // Only video call ui allows orientation change.
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001228 setAllowOrientationChange(shouldShowVideoUi.shouldShow);
Eric Erfanianccca3152017-02-22 16:32:36 -08001229
1230 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
1231 boolean didChangeInCall;
1232 boolean didChangeVideo;
1233 boolean didChangeAnswer;
1234 if (shouldShowAnswerUi.shouldShow) {
1235 didChangeInCall = hideInCallScreenFragment(transaction);
1236 didChangeVideo = hideVideoCallScreenFragment(transaction);
1237 didChangeAnswer = showAnswerScreenFragment(transaction, shouldShowAnswerUi.call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001238 } else if (shouldShowVideoUi.shouldShow) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001239 didChangeInCall = hideInCallScreenFragment(transaction);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001240 didChangeVideo = showVideoCallScreenFragment(transaction, shouldShowVideoUi.call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001241 didChangeAnswer = hideAnswerScreenFragment(transaction);
1242 } else {
1243 didChangeInCall = showInCallScreenFragment(transaction);
1244 didChangeVideo = hideVideoCallScreenFragment(transaction);
1245 didChangeAnswer = hideAnswerScreenFragment(transaction);
1246 }
1247
1248 if (didChangeInCall || didChangeVideo || didChangeAnswer) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001249 Trace.beginSection("InCallActivity.commitTransaction");
Eric Erfanianccca3152017-02-22 16:32:36 -08001250 transaction.commitNow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001251 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001252 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1253 }
1254 isInShowMainInCallFragment = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001255 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001256 }
1257
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001258 private ShouldShowUiResult getShouldShowAnswerUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001259 DialerCall call = CallList.getInstance().getIncomingCall();
1260 if (call != null) {
1261 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001262 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 }
1264
1265 call = CallList.getInstance().getVideoUpgradeRequestCall();
1266 if (call != null) {
1267 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found video upgrade request");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001268 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001269 }
1270
1271 // Check if we're showing the answer screen and the call is disconnected. If this condition is
1272 // true then we won't switch from the answer UI to the in call UI. This prevents flicker when
1273 // the user rejects an incoming call.
1274 call = CallList.getInstance().getFirstCall();
1275 if (call == null) {
1276 call = CallList.getInstance().getBackgroundCall();
1277 }
1278 if (didShowAnswerScreen && (call == null || call.getState() == State.DISCONNECTED)) {
1279 LogUtil.i("InCallActivity.getShouldShowAnswerUi", "found disconnecting incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001280 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001281 }
1282
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001283 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001284 }
1285
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001286 private static ShouldShowUiResult getShouldShowVideoUi() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001287 DialerCall call = CallList.getInstance().getFirstCall();
1288 if (call == null) {
1289 LogUtil.i("InCallActivity.getShouldShowVideoUi", "null call");
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 if (call.isVideoCall()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001294 LogUtil.i("InCallActivity.getShouldShowVideoUi", "found video call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001295 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001296 }
1297
linyuh8fbecce2017-12-18 13:53:09 -08001298 if (call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001299 LogUtil.i("InCallActivity.getShouldShowVideoUi", "upgrading to video");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001300 return new ShouldShowUiResult(true, call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001301 }
1302
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001303 return new ShouldShowUiResult(false, null);
Eric Erfanianccca3152017-02-22 16:32:36 -08001304 }
1305
1306 private boolean showAnswerScreenFragment(FragmentTransaction transaction, DialerCall call) {
1307 // When rejecting a call the active call can become null in which case we should continue
1308 // showing the answer screen.
1309 if (didShowAnswerScreen && call == null) {
1310 return false;
1311 }
1312
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001313 Assert.checkArgument(call != null, "didShowAnswerScreen was false but call was still null");
1314
1315 boolean isVideoUpgradeRequest = call.hasReceivedVideoUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -08001316
1317 // Check if we're already showing an answer screen for this call.
1318 if (didShowAnswerScreen) {
1319 AnswerScreen answerScreen = getAnswerScreen();
1320 if (answerScreen.getCallId().equals(call.getId())
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001321 && answerScreen.isVideoCall() == call.isVideoCall()
Eric Erfanian2ca43182017-08-31 06:57:16 -07001322 && answerScreen.isVideoUpgradeRequest() == isVideoUpgradeRequest
1323 && !answerScreen.isActionTimeout()) {
1324 LogUtil.d(
1325 "InCallActivity.showAnswerScreenFragment",
1326 "answer fragment exists for same call and has NOT been accepted/rejected/timed out");
Eric Erfanianccca3152017-02-22 16:32:36 -08001327 return false;
1328 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001329 if (answerScreen.isActionTimeout()) {
1330 LogUtil.i(
1331 "InCallActivity.showAnswerScreenFragment",
1332 "answer fragment exists but has been accepted/rejected and timed out");
1333 } else {
1334 LogUtil.i(
1335 "InCallActivity.showAnswerScreenFragment",
1336 "answer fragment exists but arguments do not match");
1337 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001338 hideAnswerScreenFragment(transaction);
1339 }
1340
1341 // Show a new answer screen.
1342 AnswerScreen answerScreen =
Eric Erfanianfc37b022017-03-21 10:11:17 -07001343 AnswerBindings.createAnswerScreen(
1344 call.getId(),
1345 call.isVideoCall(),
1346 isVideoUpgradeRequest,
Eric Erfanian90508232017-03-24 09:31:16 -07001347 call.getVideoTech().isSelfManagedCamera(),
1348 shouldAllowAnswerAndRelease(call),
1349 CallList.getInstance().getBackgroundCall() != null);
linyuhc3968e62017-11-20 17:40:50 -08001350 transaction.add(R.id.main, answerScreen.getAnswerScreenFragment(), Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001351
1352 Logger.get(this).logScreenView(ScreenEvent.Type.INCOMING_CALL, this);
1353 didShowAnswerScreen = true;
1354 return true;
1355 }
1356
Eric Erfanian90508232017-03-24 09:31:16 -07001357 private boolean shouldAllowAnswerAndRelease(DialerCall call) {
1358 if (CallList.getInstance().getActiveCall() == null) {
1359 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "no active call");
1360 return false;
1361 }
1362 if (getSystemService(TelephonyManager.class).getPhoneType()
1363 == TelephonyManager.PHONE_TYPE_CDMA) {
1364 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "PHONE_TYPE_CDMA not supported");
1365 return false;
1366 }
1367 if (call.isVideoCall() || call.hasReceivedVideoUpgradeRequest()) {
1368 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "video call");
1369 return false;
1370 }
linyuhc3968e62017-11-20 17:40:50 -08001371 if (!ConfigProviderBindings.get(this)
1372 .getBoolean(ConfigNames.ANSWER_AND_RELEASE_ENABLED, true)) {
Eric Erfanian90508232017-03-24 09:31:16 -07001373 LogUtil.i("InCallActivity.shouldAllowAnswerAndRelease", "disabled by config");
1374 return false;
1375 }
1376
1377 return true;
1378 }
1379
Eric Erfanianccca3152017-02-22 16:32:36 -08001380 private boolean hideAnswerScreenFragment(FragmentTransaction transaction) {
1381 if (!didShowAnswerScreen) {
1382 return false;
1383 }
1384 AnswerScreen answerScreen = getAnswerScreen();
1385 if (answerScreen != null) {
1386 transaction.remove(answerScreen.getAnswerScreenFragment());
1387 }
1388
1389 didShowAnswerScreen = false;
1390 return true;
1391 }
1392
1393 private boolean showInCallScreenFragment(FragmentTransaction transaction) {
1394 if (didShowInCallScreen) {
1395 return false;
1396 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001397 InCallScreen inCallScreen = InCallBindings.createInCallScreen();
linyuhc3968e62017-11-20 17:40:50 -08001398 transaction.add(R.id.main, inCallScreen.getInCallScreenFragment(), Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001399 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1400 didShowInCallScreen = true;
1401 return true;
1402 }
1403
1404 private boolean hideInCallScreenFragment(FragmentTransaction transaction) {
1405 if (!didShowInCallScreen) {
1406 return false;
1407 }
1408 InCallScreen inCallScreen = getInCallScreen();
1409 if (inCallScreen != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001410 transaction.remove(inCallScreen.getInCallScreenFragment());
Eric Erfanianccca3152017-02-22 16:32:36 -08001411 }
1412 didShowInCallScreen = false;
1413 return true;
1414 }
1415
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001416 private boolean showVideoCallScreenFragment(FragmentTransaction transaction, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001417 if (didShowVideoCallScreen) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001418 VideoCallScreen videoCallScreen = getVideoCallScreen();
1419 if (videoCallScreen.getCallId().equals(call.getId())) {
1420 return false;
1421 }
1422 LogUtil.i(
1423 "InCallActivity.showVideoCallScreenFragment",
1424 "video call fragment exists but arguments do not match");
1425 hideVideoCallScreenFragment(transaction);
Eric Erfanianccca3152017-02-22 16:32:36 -08001426 }
1427
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001428 LogUtil.i("InCallActivity.showVideoCallScreenFragment", "call: %s", call);
1429
Eric Erfanian90508232017-03-24 09:31:16 -07001430 VideoCallScreen videoCallScreen =
1431 VideoBindings.createVideoCallScreen(
1432 call.getId(), call.getVideoTech().shouldUseSurfaceView());
linyuhc3968e62017-11-20 17:40:50 -08001433 transaction.add(
1434 R.id.main, videoCallScreen.getVideoCallScreenFragment(), Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001435
1436 Logger.get(this).logScreenView(ScreenEvent.Type.INCALL, this);
1437 didShowVideoCallScreen = true;
1438 return true;
1439 }
1440
1441 private boolean hideVideoCallScreenFragment(FragmentTransaction transaction) {
1442 if (!didShowVideoCallScreen) {
1443 return false;
1444 }
1445 VideoCallScreen videoCallScreen = getVideoCallScreen();
1446 if (videoCallScreen != null) {
1447 transaction.remove(videoCallScreen.getVideoCallScreenFragment());
1448 }
1449 didShowVideoCallScreen = false;
1450 return true;
1451 }
1452
linyuhc3968e62017-11-20 17:40:50 -08001453 private AnswerScreen getAnswerScreen() {
1454 return (AnswerScreen) getSupportFragmentManager().findFragmentByTag(Tags.ANSWER_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001455 }
1456
linyuhc3968e62017-11-20 17:40:50 -08001457 private InCallScreen getInCallScreen() {
1458 return (InCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.IN_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001459 }
1460
linyuhc3968e62017-11-20 17:40:50 -08001461 private VideoCallScreen getVideoCallScreen() {
1462 return (VideoCallScreen) getSupportFragmentManager().findFragmentByTag(Tags.VIDEO_CALL_SCREEN);
Eric Erfanianccca3152017-02-22 16:32:36 -08001463 }
1464
1465 @Override
1466 public void onPseudoScreenStateChanged(boolean isOn) {
1467 LogUtil.i("InCallActivity.onPseudoScreenStateChanged", "isOn: " + isOn);
1468 pseudoBlackScreenOverlay.setVisibility(isOn ? View.GONE : View.VISIBLE);
1469 }
1470
1471 /**
1472 * For some touch related issue, turning off the screen can be faked by drawing a black view over
1473 * the activity. All touch events started when the screen is "off" is rejected.
1474 *
1475 * @see PseudoScreenState
1476 */
1477 @Override
1478 public boolean dispatchTouchEvent(MotionEvent event) {
1479 // Reject any gesture that started when the screen is in the fake off state.
1480 if (touchDownWhenPseudoScreenOff) {
1481 if (event.getAction() == MotionEvent.ACTION_UP) {
1482 touchDownWhenPseudoScreenOff = false;
1483 }
1484 return true;
1485 }
1486 // Reject all touch event when the screen is in the fake off state.
1487 if (!InCallPresenter.getInstance().getPseudoScreenState().isOn()) {
1488 if (event.getAction() == MotionEvent.ACTION_DOWN) {
1489 touchDownWhenPseudoScreenOff = true;
1490 LogUtil.i("InCallActivity.dispatchTouchEvent", "touchDownWhenPseudoScreenOff");
1491 }
1492 return true;
1493 }
1494 return super.dispatchTouchEvent(event);
1495 }
1496
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001497 private static class ShouldShowUiResult {
Eric Erfanianccca3152017-02-22 16:32:36 -08001498 public final boolean shouldShow;
1499 public final DialerCall call;
1500
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001501 ShouldShowUiResult(boolean shouldShow, DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 this.shouldShow = shouldShow;
1503 this.call = call;
1504 }
1505 }
linyuhc3968e62017-11-20 17:40:50 -08001506
1507 private static final class IntentExtraNames {
1508 static final String FOR_FULL_SCREEN = "InCallActivity.for_full_screen_intent";
1509 static final String NEW_OUTGOING_CALL = "InCallActivity.new_outgoing_call";
1510 static final String SHOW_DIALPAD = "InCallActivity.show_dialpad";
1511 }
1512
1513 private static final class KeysForSavedInstance {
1514 static final String DIALPAD_TEXT = "InCallActivity.dialpad_text";
1515 static final String DID_SHOW_ANSWER_SCREEN = "did_show_answer_screen";
1516 static final String DID_SHOW_IN_CALL_SCREEN = "did_show_in_call_screen";
1517 static final String DID_SHOW_VIDEO_CALL_SCREEN = "did_show_video_call_screen";
1518 }
1519
1520 /** Request codes for pending intents. */
1521 public static final class PendingIntentRequestCodes {
1522 static final int NON_FULL_SCREEN = 0;
1523 static final int FULL_SCREEN = 1;
1524 static final int BUBBLE = 2;
1525 }
1526
1527 private static final class Tags {
1528 static final String ANSWER_SCREEN = "tag_answer_screen";
1529 static final String DIALPAD_FRAGMENT = "tag_dialpad_fragment";
1530 static final String IN_CALL_SCREEN = "tag_in_call_screen";
1531 static final String INTERNATIONAL_CALL_ON_WIFI = "tag_international_call_on_wifi";
1532 static final String SELECT_ACCOUNT_FRAGMENT = "tag_select_account_fragment";
1533 static final String VIDEO_CALL_SCREEN = "tag_video_call_screen";
1534 static final String POST_CHAR_DIALOG_FRAGMENT = "tag_post_char_dialog_fragment";
1535 }
1536
1537 private static final class ConfigNames {
1538 static final String ANSWER_AND_RELEASE_ENABLED = "answer_and_release_enabled";
1539 }
1540
1541 private static final class InternationalCallOnWifiCallback
1542 implements InternationalCallOnWifiDialogFragment.Callback {
1543 private static final String TAG = InternationalCallOnWifiCallback.class.getCanonicalName();
1544
1545 @Override
1546 public void continueCall(@NonNull String callId) {
1547 LogUtil.i(TAG, "Continuing call with ID: %s", callId);
1548 }
1549
1550 @Override
1551 public void cancelCall(@NonNull String callId) {
1552 DialerCall call = CallList.getInstance().getCallById(callId);
1553 if (call == null) {
1554 LogUtil.i(TAG, "Call destroyed before the dialog is closed");
1555 return;
1556 }
1557
1558 LogUtil.i(TAG, "Disconnecting international call on WiFi");
1559 call.disconnect();
1560 }
1561 }
1562
1563 private static final class SelectPhoneAccountListener
1564 extends SelectPhoneAccountDialogFragment.SelectPhoneAccountListener {
1565 private static final String TAG = SelectPhoneAccountListener.class.getCanonicalName();
1566
1567 @Override
1568 public void onPhoneAccountSelected(
1569 PhoneAccountHandle selectedAccountHandle, boolean setDefault, String callId) {
1570 DialerCall call = CallList.getInstance().getCallById(callId);
1571 LogUtil.i(TAG, "Phone account select with call:\n%s", call);
1572
1573 if (call != null) {
1574 call.phoneAccountSelected(selectedAccountHandle, setDefault);
1575 }
1576 }
1577
1578 @Override
1579 public void onDialogDismissed(String callId) {
1580 DialerCall call = CallList.getInstance().getCallById(callId);
1581 LogUtil.i(TAG, "Disconnecting call:\n%s" + call);
1582
1583 if (call != null) {
1584 call.disconnect();
1585 }
1586 }
1587 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001588}