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
diff --git a/java/com/android/incallui/InCallPresenter.java b/java/com/android/incallui/InCallPresenter.java
index 150499f..17af756 100644
--- a/java/com/android/incallui/InCallPresenter.java
+++ b/java/com/android/incallui/InCallPresenter.java
@@ -273,6 +273,9 @@
 
   private SpeakEasyCallManager speakEasyCallManager;
 
+  private boolean addCallClicked = false;
+  private boolean automaticallyMutedByAddCall = false;
+
   /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
   @VisibleForTesting
   InCallPresenter() {}
@@ -1226,7 +1229,9 @@
       proximitySensor.onInCallShowing(showing);
     }
 
-    if (!showing) {
+    if (showing) {
+      refreshMuteState();
+    } else {
       updateIsChangingConfigurations();
     }
 
@@ -2033,5 +2038,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<>();
 }