Fix the issue to log the failed call audio route

The metrics log is not triggered properly on the failed audio route.
The change is to trigger the log on PENDING_ROUTE_FAILED.

Flag: com.android.server.telecom.flags.telecom_metrics_support
Bug: 368665511
Test: manual
Test: atest TelecomUnitTests:CallAudioRouteControllerTest
Change-Id: If8dd1ad3ee5bc7318587f4c0b01aec5d7d60ffff
diff --git a/src/com/android/server/telecom/CallAudioRouteController.java b/src/com/android/server/telecom/CallAudioRouteController.java
index ca9fe5b..e27535a 100644
--- a/src/com/android/server/telecom/CallAudioRouteController.java
+++ b/src/com/android/server/telecom/CallAudioRouteController.java
@@ -350,7 +350,8 @@
         mActiveBluetoothDevice = null;
         mTypeRoutes = new ArrayMap<>();
         mStreamingRoutes = new HashSet<>();
-        mPendingAudioRoute = new PendingAudioRoute(this, mAudioManager, mBluetoothRouteManager);
+        mPendingAudioRoute = new PendingAudioRoute(this, mAudioManager, mBluetoothRouteManager,
+                mFeatureFlags);
         mStreamingRoute = new AudioRoute(AudioRoute.TYPE_STREAMING, null, null);
         mStreamingRoutes.add(mStreamingRoute);
 
@@ -981,7 +982,7 @@
             mPendingAudioRoute.clearPendingMessages();
             onCurrentRouteChanged();
             if (mFeatureFlags.telecomMetricsSupport()) {
-                mMetricsController.getAudioRouteStats().onRouteExit(mPendingAudioRoute);
+                mMetricsController.getAudioRouteStats().onRouteExit(mPendingAudioRoute, true);
             }
         }
     }
@@ -1467,4 +1468,10 @@
         }
         mIsActive = active;
     }
+
+    void fallBack(String btAddressToExclude) {
+        mMetricsController.getAudioRouteStats().onRouteExit(mPendingAudioRoute, false);
+        sendMessageWithSessionInfo(SWITCH_BASELINE_ROUTE, INCLUDE_BLUETOOTH_IN_BASELINE,
+                btAddressToExclude);
+    }
 }
diff --git a/src/com/android/server/telecom/PendingAudioRoute.java b/src/com/android/server/telecom/PendingAudioRoute.java
index a544258..ffde964 100644
--- a/src/com/android/server/telecom/PendingAudioRoute.java
+++ b/src/com/android/server/telecom/PendingAudioRoute.java
@@ -27,6 +27,7 @@
 import android.util.Pair;
 
 import com.android.server.telecom.bluetooth.BluetoothRouteManager;
+import com.android.server.telecom.flags.FeatureFlags;
 
 import java.util.Set;
 
@@ -41,6 +42,7 @@
     private CallAudioRouteController mCallAudioRouteController;
     private AudioManager mAudioManager;
     private BluetoothRouteManager mBluetoothRouteManager;
+    private FeatureFlags mFeatureFlags;
     /**
      * The {@link AudioRoute} that this pending audio switching started with
      */
@@ -52,17 +54,17 @@
     private AudioRoute mDestRoute;
     private Set<Pair<Integer, String>> mPendingMessages;
     private boolean mActive;
-    private boolean mIsFailed;
     /**
      * The device that has been set for communication by Telecom
      */
     private @AudioRoute.AudioRouteType int mCommunicationDeviceType = AudioRoute.TYPE_INVALID;
 
     PendingAudioRoute(CallAudioRouteController controller, AudioManager audioManager,
-            BluetoothRouteManager bluetoothRouteManager) {
+            BluetoothRouteManager bluetoothRouteManager, FeatureFlags featureFlags) {
         mCallAudioRouteController = controller;
         mAudioManager = audioManager;
         mBluetoothRouteManager = bluetoothRouteManager;
+        mFeatureFlags = featureFlags;
         mPendingMessages = new ArraySet<>();
         mActive = false;
         mCommunicationDeviceType = AudioRoute.TYPE_INVALID;
@@ -96,10 +98,13 @@
     public void onMessageReceived(Pair<Integer, String> message, String btAddressToExclude) {
         Log.i(this, "onMessageReceived: message - %s", message);
         if (message.first == PENDING_ROUTE_FAILED) {
-            mIsFailed = true;
             // Fallback to base route
-            mCallAudioRouteController.sendMessageWithSessionInfo(
-                    SWITCH_BASELINE_ROUTE, INCLUDE_BLUETOOTH_IN_BASELINE, btAddressToExclude);
+            if (mFeatureFlags.telecomMetricsSupport()) {
+                mCallAudioRouteController.fallBack(btAddressToExclude);
+            } else {
+                mCallAudioRouteController.sendMessageWithSessionInfo(
+                        SWITCH_BASELINE_ROUTE, INCLUDE_BLUETOOTH_IN_BASELINE, btAddressToExclude);
+            }
             return;
         }
 
@@ -141,8 +146,4 @@
     public void overrideDestRoute(AudioRoute route) {
         mDestRoute = route;
     }
-
-    public boolean isFailed() {
-        return mIsFailed;
-    }
 }
diff --git a/src/com/android/server/telecom/metrics/AudioRouteStats.java b/src/com/android/server/telecom/metrics/AudioRouteStats.java
index 8755402..21624f1 100644
--- a/src/com/android/server/telecom/metrics/AudioRouteStats.java
+++ b/src/com/android/server/telecom/metrics/AudioRouteStats.java
@@ -180,10 +180,9 @@
         });
     }
 
-    public void onRouteExit(PendingAudioRoute pendingRoute) {
+    public void onRouteExit(PendingAudioRoute pendingRoute, boolean isSuccess) {
         // Check the dest type on the route exiting as it may be different as the enter
         int destType = convertAudioType(pendingRoute.getDestRoute(), false);
-        boolean isSuccess = !pendingRoute.isFailed();
         long curTime = SystemClock.elapsedRealtime();
         post(() -> {
             if (mIsOngoing) {
diff --git a/tests/src/com/android/server/telecom/tests/TelecomPulledAtomTest.java b/tests/src/com/android/server/telecom/tests/TelecomPulledAtomTest.java
index d188054..bc8aeac 100644
--- a/tests/src/com/android/server/telecom/tests/TelecomPulledAtomTest.java
+++ b/tests/src/com/android/server/telecom/tests/TelecomPulledAtomTest.java
@@ -122,7 +122,6 @@
         mTempFile = mTempFolder.newFile(FILE_NAME_TEST_ATOM);
         doReturn(mTempFile).when(mSpyContext).getFileStreamPath(anyString());
         doReturn(mFileOutputStream).when(mSpyContext).openFileOutput(anyString(), anyInt());
-        doReturn(false).when(mMockPendingAudioRoute).isFailed();
         doReturn(mMockSourceRoute).when(mMockPendingAudioRoute).getOrigRoute();
         doReturn(mMockDestRoute).when(mMockPendingAudioRoute).getDestRoute();
         doReturn(TYPE_EARPIECE).when(mMockSourceRoute).getType();
@@ -345,7 +344,7 @@
 
         audioRouteStats.onRouteEnter(mMockPendingAudioRoute);
         waitForHandlerActionDelayed(audioRouteStats, TEST_TIMEOUT, latency);
-        audioRouteStats.onRouteExit(mMockPendingAudioRoute);
+        audioRouteStats.onRouteExit(mMockPendingAudioRoute, true);
         waitForHandlerAction(audioRouteStats, 100);
 
         // Verify that the stats should not be saved before the revert threshold is expired
@@ -375,7 +374,7 @@
 
         audioRouteStats.onRouteEnter(mMockPendingAudioRoute);
         waitForHandlerActionDelayed(audioRouteStats, TEST_TIMEOUT, latency);
-        audioRouteStats.onRouteExit(mMockPendingAudioRoute);
+        audioRouteStats.onRouteExit(mMockPendingAudioRoute, true);
         waitForHandlerAction(audioRouteStats, delay);
 
         // Verify that the stats should not be saved before the revert threshold is expired
@@ -411,7 +410,7 @@
 
         audioRouteStats.onRouteEnter(mMockPendingAudioRoute);
         waitForHandlerActionDelayed(audioRouteStats, TEST_TIMEOUT, latency);
-        audioRouteStats.onRouteExit(mMockPendingAudioRoute);
+        audioRouteStats.onRouteExit(mMockPendingAudioRoute, true);
         waitForHandlerAction(audioRouteStats, delay);
 
         // Verify that the stats should not be saved before the revert threshold is expired
@@ -449,7 +448,7 @@
 
         audioRouteStats.onRouteEnter(mMockPendingAudioRoute);
         waitForHandlerActionDelayed(audioRouteStats, TEST_TIMEOUT, latency);
-        audioRouteStats.onRouteExit(mMockPendingAudioRoute);
+        audioRouteStats.onRouteExit(mMockPendingAudioRoute, true);
         waitForHandlerAction(audioRouteStats, delay);
 
         // Verify that the stats should not be saved before the revert threshold is expired
@@ -505,7 +504,7 @@
 
         audioRouteStats.onRouteEnter(mMockPendingAudioRoute);
         waitForHandlerActionDelayed(audioRouteStats, TEST_TIMEOUT, latency);
-        audioRouteStats.onRouteExit(mMockPendingAudioRoute);
+        audioRouteStats.onRouteExit(mMockPendingAudioRoute, true);
         waitForHandlerAction(audioRouteStats, 100);
 
         doReturn(mMockDestRoute).when(mMockPendingAudioRoute).getOrigRoute();