Fix the issue that automatic mute state remains ON after adding VT call

An issue occurs because InCallFragment#onResume() that release the
automatic mute state is not called when switching fragments by adding
call.

To resolve this issue, handle automatic mute state in InCallActivity.

Bug: 110815828
Test: Checked that the mute state is turned OFF when add a video call
during a voice call.
Change-Id: Ided7c58e1148f6ee12bdfeaa813d596a4716c1d6

Change-Id: I7a956205fe8fbca56ec96a3bb672d792482c624a
diff --git a/java/com/android/incallui/CallButtonPresenter.java b/java/com/android/incallui/CallButtonPresenter.java
index 269d93e..cff283c 100644
--- a/java/com/android/incallui/CallButtonPresenter.java
+++ b/java/com/android/incallui/CallButtonPresenter.java
@@ -67,17 +67,11 @@
         InCallButtonUiDelegate,
         DialerCallListener {
 
-  private static final String KEY_AUTOMATICALLY_MUTED_BY_ADD_CALL =
-      "incall_key_automatically_muted_by_add_call";
-  private static final String KEY_PREVIOUS_MUTE_STATE = "incall_key_previous_mute_state";
-
   private static final String KEY_RECORDING_WARNING_PRESENTED = "recording_warning_presented";
 
   private final Context context;
   private InCallButtonUi inCallButtonUi;
   private DialerCall call;
-  private boolean automaticallyMutedByAddCall = false;
-  private boolean previousMuteState = false;
   private boolean isInCallButtonUiReady;
   private PhoneAccountHandle otherAccount;
 
@@ -321,18 +315,7 @@
             DialerImpression.Type.IN_CALL_ADD_CALL_BUTTON_PRESSED,
             call.getUniqueCallId(),
             call.getTimeAddedMs());
-    if (automaticallyMutedByAddCall) {
-      // Since clicking add call button brings user to MainActivity and coming back refreshes mute
-      // state, add call button should only be clicked once during InCallActivity shows. Otherwise,
-      // we set previousMuteState wrong.
-      return;
-    }
-    // Automatically mute the current call
-    automaticallyMutedByAddCall = true;
-    previousMuteState = AudioModeProvider.getInstance().getAudioState().isMuted();
-    // Simulate a click on the mute button
-    muteClicked(true /* checked */, false /* clickedByUser */);
-    TelecomAdapter.getInstance().addCall();
+    InCallPresenter.getInstance().addCallClicked();
   }
 
   @Override
@@ -638,31 +621,10 @@
   }
 
   @Override
-  public void refreshMuteState() {
-    // Restore the previous mute state
-    if (automaticallyMutedByAddCall
-        && AudioModeProvider.getInstance().getAudioState().isMuted() != previousMuteState) {
-      if (inCallButtonUi == null) {
-        return;
-      }
-      muteClicked(previousMuteState, false /* clickedByUser */);
-    }
-    automaticallyMutedByAddCall = false;
-  }
+  public void onSaveInstanceState(Bundle outState) {}
 
   @Override
-  public void onSaveInstanceState(Bundle outState) {
-    outState.putBoolean(KEY_AUTOMATICALLY_MUTED_BY_ADD_CALL, automaticallyMutedByAddCall);
-    outState.putBoolean(KEY_PREVIOUS_MUTE_STATE, previousMuteState);
-  }
-
-  @Override
-  public void onRestoreInstanceState(Bundle savedInstanceState) {
-    automaticallyMutedByAddCall =
-        savedInstanceState.getBoolean(
-            KEY_AUTOMATICALLY_MUTED_BY_ADD_CALL, automaticallyMutedByAddCall);
-    previousMuteState = savedInstanceState.getBoolean(KEY_PREVIOUS_MUTE_STATE, previousMuteState);
-  }
+  public void onRestoreInstanceState(Bundle savedInstanceState) {}
 
   @Override
   public void onCameraPermissionGranted() {
diff --git a/java/com/android/incallui/InCallPresenter.java b/java/com/android/incallui/InCallPresenter.java
index 83ec9ab..8881029 100644
--- a/java/com/android/incallui/InCallPresenter.java
+++ b/java/com/android/incallui/InCallPresenter.java
@@ -275,6 +275,9 @@
 
   private SpeakEasyCallManager speakEasyCallManager;
 
+  private boolean addCallClicked = false;
+  private boolean automaticallyMutedByAddCall = false;
+
   /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
   @VisibleForTesting
   InCallPresenter() {}
@@ -1234,7 +1237,9 @@
       proximitySensor.onInCallShowing(showing);
     }
 
-    if (!showing) {
+    if (showing) {
+      refreshMuteState();
+    } else {
       updateIsChangingConfigurations();
     }
 
@@ -2051,5 +2056,38 @@
     return true;
   }
 
+  public void addCallClicked() {
+    if (addCallClicked) {
+      // Since clicking add call button brings user to MainActivity and coming back refreshes mute
+      // state, add call button should only be clicked once during InCallActivity shows.
+      return;
+    }
+    addCallClicked = true;
+    if (!AudioModeProvider.getInstance().getAudioState().isMuted()) {
+      // Automatically mute the current call
+      TelecomAdapter.getInstance().mute(true);
+      automaticallyMutedByAddCall = true;
+    }
+    TelecomAdapter.getInstance().addCall();
+  }
+
+  /** Refresh mute state after call UI resuming from add call screen. */
+  public void refreshMuteState() {
+    LogUtil.i(
+        "InCallPresenter.refreshMuteState",
+        "refreshMuteStateAfterAddCall: %b addCallClicked: %b",
+        automaticallyMutedByAddCall,
+        addCallClicked);
+    if (!addCallClicked) {
+      return;
+    }
+    if (automaticallyMutedByAddCall) {
+      // Restore the previous mute state
+      TelecomAdapter.getInstance().mute(false);
+      automaticallyMutedByAddCall = false;
+    }
+    addCallClicked = false;
+  }
+
   private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
 }
diff --git a/java/com/android/incallui/callpending/CallPendingActivity.java b/java/com/android/incallui/callpending/CallPendingActivity.java
index e98b2d4..5177783 100644
--- a/java/com/android/incallui/callpending/CallPendingActivity.java
+++ b/java/com/android/incallui/callpending/CallPendingActivity.java
@@ -232,9 +232,6 @@
           public void onRestoreInstanceState(Bundle savedInstanceState) {}
 
           @Override
-          public void refreshMuteState() {}
-
-          @Override
           public void addCallClicked() {}
 
           @Override
diff --git a/java/com/android/incallui/incall/impl/InCallFragment.java b/java/com/android/incallui/incall/impl/InCallFragment.java
index f5cd38d..336550d 100644
--- a/java/com/android/incallui/incall/impl/InCallFragment.java
+++ b/java/com/android/incallui/incall/impl/InCallFragment.java
@@ -210,7 +210,6 @@
   @Override
   public void onResume() {
     super.onResume();
-    inCallButtonUiDelegate.refreshMuteState();
     inCallScreenDelegate.onInCallScreenResumed();
   }
 
diff --git a/java/com/android/incallui/incall/protocol/InCallButtonUiDelegate.java b/java/com/android/incallui/incall/protocol/InCallButtonUiDelegate.java
index 8b3c59b..4e25ff0 100644
--- a/java/com/android/incallui/incall/protocol/InCallButtonUiDelegate.java
+++ b/java/com/android/incallui/incall/protocol/InCallButtonUiDelegate.java
@@ -31,8 +31,6 @@
 
   void onRestoreInstanceState(Bundle savedInstanceState);
 
-  void refreshMuteState();
-
   void addCallClicked();
 
   void muteClicked(boolean checked, boolean clickedByUser);
diff --git a/java/com/android/incallui/rtt/impl/RttChatFragment.java b/java/com/android/incallui/rtt/impl/RttChatFragment.java
index 6567dff..3e76f1f 100644
--- a/java/com/android/incallui/rtt/impl/RttChatFragment.java
+++ b/java/com/android/incallui/rtt/impl/RttChatFragment.java
@@ -361,7 +361,6 @@
 
   @Override
   public void onRttScreenStart() {
-    inCallButtonUiDelegate.refreshMuteState();
     rttCallScreenDelegate.onRttCallScreenUiReady();
     Activity activity = getActivity();
     Window window = getActivity().getWindow();
diff --git a/java/com/android/incallui/video/impl/SurfaceViewVideoCallFragment.java b/java/com/android/incallui/video/impl/SurfaceViewVideoCallFragment.java
index e8ec221..07965b9 100644
--- a/java/com/android/incallui/video/impl/SurfaceViewVideoCallFragment.java
+++ b/java/com/android/incallui/video/impl/SurfaceViewVideoCallFragment.java
@@ -308,7 +308,6 @@
 
   @Override
   public void onVideoScreenStart() {
-    inCallButtonUiDelegate.refreshMuteState();
     videoCallScreenDelegate.onVideoCallScreenUiReady();
     getView().postDelayed(cameraPermissionDialogRunnable, CAMERA_PERMISSION_DIALOG_DELAY_IN_MILLIS);
   }
diff --git a/java/com/android/incallui/video/impl/VideoCallFragment.java b/java/com/android/incallui/video/impl/VideoCallFragment.java
index b659792..3fbce5c 100644
--- a/java/com/android/incallui/video/impl/VideoCallFragment.java
+++ b/java/com/android/incallui/video/impl/VideoCallFragment.java
@@ -397,7 +397,6 @@
 
   @Override
   public void onVideoScreenStart() {
-    inCallButtonUiDelegate.refreshMuteState();
     videoCallScreenDelegate.onVideoCallScreenUiReady();
     getView().postDelayed(cameraPermissionDialogRunnable, CAMERA_PERMISSION_DIALOG_DELAY_IN_MILLIS);
     getView()