Merge "Use STREAM_VOICE_CALL for ToneGenerator" into main
diff --git a/Android.bp b/Android.bp
index 94654a6..0d1c81d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -84,9 +84,9 @@
         "tests/res",
     ],
     libs: [
-        "android.test.mock",
-        "android.test.base",
-        "android.test.runner",
+        "android.test.mock.stubs.system",
+        "android.test.base.stubs.system",
+        "android.test.runner.stubs.system",
     ],
 
     jni_libs: [
diff --git a/TEST_MAPPING b/TEST_MAPPING
index acab8ef..09ebfe2 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -58,5 +58,15 @@
         }
       ]
     }
+  ],
+  "postsubmit": [
+    {
+      "name": "CtsTelecomCujTestCases",
+      "options": [
+        {
+          "exclude-annotation": "androidx.test.filters.FlakyTest"
+        }
+      ]
+    }
   ]
 }
diff --git a/flags/telecom_anomaly_report_flags.aconfig b/flags/telecom_anomaly_report_flags.aconfig
index 296b300..b060ed0 100644
--- a/flags/telecom_anomaly_report_flags.aconfig
+++ b/flags/telecom_anomaly_report_flags.aconfig
@@ -8,3 +8,11 @@
   description: "When getCurrentFocusCall times out, generate an anom. report"
   bug: "309541253"
 }
+
+# OWNER=tjstuart TARGET=25Q2
+flag {
+  name: "disconnect_self_managed_stuck_startup_calls"
+  namespace: "telecom"
+  description: "If a self-managed call is stuck in certain states, disconnect it"
+  bug: "360298368"
+}
diff --git a/res/values-mk/strings.xml b/res/values-mk/strings.xml
index c607df5..0f6e41f 100644
--- a/res/values-mk/strings.xml
+++ b/res/values-mk/strings.xml
@@ -90,7 +90,7 @@
     <string name="answering_ends_other_managed_video_call" msgid="1988508241432031327">"Ако одговорите, ќе се прекине вашиот тековен видеоповик"</string>
     <string name="answer_incoming_call" msgid="2045888814782215326">"Одговорете"</string>
     <string name="decline_incoming_call" msgid="922147089348451310">"Одбијте"</string>
-    <string name="cant_call_due_to_no_supported_service" msgid="6720817368116820027">"Не може да се оствари повик. Проверете ја мрежата на уредот."</string>
+    <string name="cant_call_due_to_no_supported_service" msgid="6720817368116820027">"Не може да се оствари повик. Проверете ја врската на уредот."</string>
     <string name="cant_call_due_to_ongoing_call" msgid="8004235328451385493">"Не може да се воспостави повик поради вашиот повик на <xliff:g id="OTHER_CALL">%1$s</xliff:g>."</string>
     <string name="cant_call_due_to_ongoing_calls" msgid="6379163795277824868">"Не може да се воспостави повик поради вашите повици на <xliff:g id="OTHER_CALL">%1$s</xliff:g>."</string>
     <string name="cant_call_due_to_ongoing_unknown_call" msgid="8243532328969433172">"Не може да се воспостави повик поради вашиот повик на друга апликација."</string>
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index 59cbdae..ccf2ae3 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -37,7 +37,6 @@
 import android.os.ParcelFileDescriptor;
 import android.os.RemoteException;
 import android.os.SystemClock;
-import android.os.Trace;
 import android.os.UserHandle;
 import android.provider.CallLog;
 import android.provider.ContactsContract.Contacts;
@@ -2151,10 +2150,15 @@
                 isWorkCall = UserUtil.isManagedProfile(mContext, userHandle, mFlags);
             }
 
-            isCallRecordingToneSupported = (phoneAccount.hasCapabilities(
-                    PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) && phoneAccount.getExtras() != null
-                    && phoneAccount.getExtras().getBoolean(
-                    PhoneAccount.EXTRA_PLAY_CALL_RECORDING_TONE, false));
+            if (!mFlags.telecomResolveHiddenDependencies()) {
+                isCallRecordingToneSupported = (phoneAccount.hasCapabilities(
+                        PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
+                        && phoneAccount.getExtras() != null
+                        && phoneAccount.getExtras().getBoolean(
+                        PhoneAccount.EXTRA_PLAY_CALL_RECORDING_TONE, false));
+            } else {
+                isCallRecordingToneSupported = false;
+            }
             isSimCall = phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
         }
         mIsWorkCall = isWorkCall;
@@ -3858,7 +3862,6 @@
      * @param callerInfo The new caller information to set.
      */
     private void setCallerInfo(Uri handle, CallerInfo callerInfo) {
-        Trace.beginSection("setCallerInfo");
         if (callerInfo == null) {
             Log.i(this, "CallerInfo lookup returned null, skipping update");
             return;
@@ -3882,8 +3885,6 @@
                 l.onCallerInfoChanged(this);
             }
         }
-
-        Trace.endSection();
     }
 
     public CallerInfo getCallerInfo() {
diff --git a/src/com/android/server/telecom/CallAnomalyWatchdog.java b/src/com/android/server/telecom/CallAnomalyWatchdog.java
index 045671e..497d7e6 100644
--- a/src/com/android/server/telecom/CallAnomalyWatchdog.java
+++ b/src/com/android/server/telecom/CallAnomalyWatchdog.java
@@ -18,15 +18,22 @@
 
 import static com.android.server.telecom.LogUtils.Events.STATE_TIMEOUT;
 
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.os.Build;
+import android.os.UserHandle;
 import android.provider.DeviceConfig;
 import android.telecom.ConnectionService;
 import android.telecom.DisconnectCause;
 import android.telecom.Log;
+import android.telecom.PhoneAccountHandle;
 import android.util.LocalLog;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.telecom.stats.CallStateChangedAtomWriter;
+import com.android.server.telecom.flags.FeatureFlags;
 
 import java.util.Collections;
 import java.util.Map;
@@ -113,6 +120,7 @@
     private final TelecomSystem.SyncRoot mLock;
     private final Timeouts.Adapter mTimeoutAdapter;
     private final ClockProxy mClockProxy;
+    private final FeatureFlags mFeatureFlags;
     private AnomalyReporterAdapter mAnomalyReporter = new AnomalyReporterAdapterImpl();
     // Pre-allocate space for 2 calls; realistically thats all we should ever need (tm)
     private final Map<Call, ScheduledFuture<?>> mScheduledFutureMap = new ConcurrentHashMap<>(2);
@@ -140,6 +148,11 @@
             UUID.fromString("d57d8aab-d723-485e-a0dd-d1abb0f346c8");
     public static final String WATCHDOG_DISCONNECTED_STUCK_EMERGENCY_CALL_MSG =
             "Telecom CallAnomalyWatchdog caught and disconnected a stuck/zombie emergency call.";
+    public static final UUID WATCHDOG_DISCONNECTED_STUCK_VOIP_CALL_UUID =
+            UUID.fromString("3fbecd12-059d-4fd3-87b7-6c3079891c23");
+    public static final String WATCHDOG_DISCONNECTED_STUCK_VOIP_CALL_MSG =
+            "Telecom CallAnomalyWatchdog caught stuck VoIP call in a starting state";
+
 
     @VisibleForTesting
     public void setAnomalyReporterAdapter(AnomalyReporterAdapter mAnomalyReporterAdapter){
@@ -148,10 +161,12 @@
 
     public CallAnomalyWatchdog(ScheduledExecutorService executorService,
             TelecomSystem.SyncRoot lock,
+            FeatureFlags featureFlags,
             Timeouts.Adapter timeoutAdapter, ClockProxy clockProxy,
             EmergencyCallDiagnosticLogger emergencyCallDiagnosticLogger) {
         mScheduledExecutorService = executorService;
         mLock = lock;
+        mFeatureFlags = featureFlags;
         mTimeoutAdapter = timeoutAdapter;
         mClockProxy = clockProxy;
         mEmergencyCallDiagnosticLogger = emergencyCallDiagnosticLogger;
@@ -272,8 +287,13 @@
      */
     private void maybeTrackCall(Call call) {
         final WatchdogCallState currentState = mWatchdogCallStateMap.get(call);
+        boolean isCreateConnectionComplete = call.isCreateConnectionComplete();
+        if (mFeatureFlags.disconnectSelfManagedStuckStartupCalls()) {
+            isCreateConnectionComplete =
+                    isCreateConnectionComplete || call.isTransactionalCall();
+        }
         final WatchdogCallState newState = new WatchdogCallState(call.getState(),
-                call.isCreateConnectionComplete(), mClockProxy.elapsedRealtime());
+                isCreateConnectionComplete, mClockProxy.elapsedRealtime());
         if (Objects.equals(currentState, newState)) {
             // No state change; skip.
             return;
@@ -348,8 +368,13 @@
                 }
                 // Ensure that at timeout we are still in the original state when we posted the
                 // timeout.
+                boolean isCreateConnectionComplete = call.isCreateConnectionComplete();
+                if (mFeatureFlags.disconnectSelfManagedStuckStartupCalls()) {
+                    isCreateConnectionComplete =
+                            isCreateConnectionComplete || call.isTransactionalCall();
+                }
                 final WatchdogCallState expiredState = new WatchdogCallState(call.getState(),
-                        call.isCreateConnectionComplete(), mClockProxy.elapsedRealtime());
+                        isCreateConnectionComplete, mClockProxy.elapsedRealtime());
                 if (expiredState.equals(newState)
                         && getDurationInCurrentStateMillis(newState) > timeoutMillis) {
                     // The call has been in this transitory or intermediate state too long,
@@ -368,7 +393,7 @@
                                 WATCHDOG_DISCONNECTED_STUCK_CALL_MSG);
                     }
 
-                    if (isEnabledDisconnect) {
+                    if (isEnabledDisconnect || isInSelfManagedStuckStartingState(call)) {
                         call.setOverrideDisconnectCauseCode(
                                 new DisconnectCause(DisconnectCause.ERROR, "state_timeout"));
                         call.disconnect("State timeout");
@@ -387,6 +412,50 @@
         return cleanupRunnable;
     }
 
+    private boolean isInSelfManagedStuckStartingState(Call call) {
+        Context context = call.getContext();
+        if (!mFeatureFlags.disconnectSelfManagedStuckStartupCalls() || context == null) {
+            return false;
+        }
+        int currentStuckState = call.getState();
+        return call.isSelfManaged() &&
+                (currentStuckState == CallState.NEW ||
+                        currentStuckState == CallState.RINGING ||
+                        currentStuckState == CallState.DIALING ||
+                        currentStuckState == CallState.CONNECTING) &&
+                isVanillaIceCreamBuildOrHigher(context, call);
+    }
+
+    private boolean isVanillaIceCreamBuildOrHigher(Context context, Call call) {
+        // report the anomaly for metrics purposes
+        mAnomalyReporter.reportAnomaly(
+                WATCHDOG_DISCONNECTED_STUCK_VOIP_CALL_UUID,
+                WATCHDOG_DISCONNECTED_STUCK_VOIP_CALL_MSG);
+        // only disconnect calls running on V and when the flag is enabled!
+        PhoneAccountHandle phoneAccountHandle = call.getTargetPhoneAccount();
+        PackageManager pm = context.getPackageManager();
+        if (pm == null ||
+                phoneAccountHandle == null ||
+                phoneAccountHandle.getComponentName() == null) {
+            return false;
+        }
+        String packageName = phoneAccountHandle.getComponentName().getPackageName();
+        Log.d(this, "pah=[%s], user=[%s]", phoneAccountHandle, call.getAssociatedUser());
+        ApplicationInfo applicationInfo;
+        try {
+            applicationInfo = pm.getApplicationInfoAsUser(
+                    packageName,
+                    0,
+                    call.getAssociatedUser());
+        } catch (Exception e) {
+            Log.e(this, e, "iVICBOH: pm.getApplicationInfoAsUser(...) exception");
+            return false;
+        }
+        int targetSdk = (applicationInfo == null) ? 0 : applicationInfo.targetSdkVersion;
+        Log.i(this, "iVICBOH: packageName=[%s], sdk=[%d]", packageName, targetSdk);
+        return targetSdk >= Build.VERSION_CODES.VANILLA_ICE_CREAM;
+    }
+
     /**
      * Returns whether the action to disconnect the call when the Transitory state and
      * Intermediate state time expires is enabled or disabled.
diff --git a/src/com/android/server/telecom/CallAudioModeStateMachine.java b/src/com/android/server/telecom/CallAudioModeStateMachine.java
index cd1d6a3..fb196f2 100644
--- a/src/com/android/server/telecom/CallAudioModeStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioModeStateMachine.java
@@ -21,7 +21,6 @@
 import android.media.AudioManager;
 import android.os.Looper;
 import android.os.Message;
-import android.os.Trace;
 import android.telecom.Log;
 import android.telecom.Logging.Runnable;
 import android.telecom.Logging.Session;
@@ -461,40 +460,35 @@
         private boolean mHasFocus = false;
 
         private void tryStartRinging() {
-            Trace.traceBegin(Trace.TRACE_TAG_AUDIO, "CallAudioMode.tryStartRinging");
-            try {
-                if (mHasFocus && mCallAudioManager.isRingtonePlaying()) {
-                    Log.i(LOG_TAG,
-                        "RingingFocusState#tryStartRinging -- audio focus previously"
-                            + " acquired and ringtone already playing -- skipping.");
-                    return;
-                }
+            if (mHasFocus && mCallAudioManager.isRingtonePlaying()) {
+                Log.i(LOG_TAG,
+                    "RingingFocusState#tryStartRinging -- audio focus previously"
+                        + " acquired and ringtone already playing -- skipping.");
+                return;
+            }
 
-                if (mCallAudioManager.startRinging()) {
-                    if (mFeatureFlags.telecomResolveHiddenDependencies()) {
-                        mCurrentAudioFocusRequest = RING_AUDIO_FOCUS_REQUEST;
-                        Log.i(this, "tryStartRinging: AudioManager#requestAudioFocus(RING)");
-                        mAudioManager.requestAudioFocus(RING_AUDIO_FOCUS_REQUEST);
-                    } else {
-                        mAudioManager.requestAudioFocusForCall(
-                                AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
-                    }
-                    // Do not set MODE_RINGTONE if we were previously in the CALL_SCREENING mode --
-                    // this trips up the audio system.
-                    if (mAudioManager.getMode() != AudioManager.MODE_CALL_SCREENING) {
-                        Log.i(this, "enter: AudioManager#setMode(MODE_RINGTONE)");
-                        mAudioManager.setMode(AudioManager.MODE_RINGTONE);
-                        mLocalLog.log("Mode MODE_RINGTONE");
-                    }
-                    mCallAudioManager.setCallAudioRouteFocusState(
-                        CallAudioRouteStateMachine.RINGING_FOCUS);
-                    mHasFocus = true;
+            if (mCallAudioManager.startRinging()) {
+                if (mFeatureFlags.telecomResolveHiddenDependencies()) {
+                    mCurrentAudioFocusRequest = RING_AUDIO_FOCUS_REQUEST;
+                    Log.i(this, "tryStartRinging: AudioManager#requestAudioFocus(RING)");
+                    mAudioManager.requestAudioFocus(RING_AUDIO_FOCUS_REQUEST);
                 } else {
-                    Log.i(
-                        LOG_TAG, "RINGING state, try start ringing but not acquiring audio focus");
+                    mAudioManager.requestAudioFocusForCall(
+                            AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                 }
-            } finally {
-                Trace.traceEnd(Trace.TRACE_TAG_AUDIO);
+                // Do not set MODE_RINGTONE if we were previously in the CALL_SCREENING mode --
+                // this trips up the audio system.
+                if (mAudioManager.getMode() != AudioManager.MODE_CALL_SCREENING) {
+                    Log.i(this, "enter: AudioManager#setMode(MODE_RINGTONE)");
+                    mAudioManager.setMode(AudioManager.MODE_RINGTONE);
+                    mLocalLog.log("Mode MODE_RINGTONE");
+                }
+                mCallAudioManager.setCallAudioRouteFocusState(
+                    CallAudioRouteStateMachine.RINGING_FOCUS);
+                mHasFocus = true;
+            } else {
+                Log.i(
+                    LOG_TAG, "RINGING state, try start ringing but not acquiring audio focus");
             }
         }
 
diff --git a/src/com/android/server/telecom/CallAudioRouteController.java b/src/com/android/server/telecom/CallAudioRouteController.java
index 9c593f4..9446746 100644
--- a/src/com/android/server/telecom/CallAudioRouteController.java
+++ b/src/com/android/server/telecom/CallAudioRouteController.java
@@ -533,14 +533,6 @@
                 mIsScoAudioConnected);
         mIsActive = active;
         mPendingAudioRoute.evaluatePendingState();
-        postTimeoutMessage();
-    }
-
-    private void postTimeoutMessage() {
-        // reset timeout handler
-        mHandler.removeMessages(PENDING_ROUTE_TIMEOUT);
-        mHandler.postDelayed(() -> mHandler.sendMessage(
-                Message.obtain(mHandler, PENDING_ROUTE_TIMEOUT)), TIMEOUT_LIMIT);
     }
 
     private void handleWiredHeadsetConnected() {
@@ -657,9 +649,6 @@
                 mPendingAudioRoute.onMessageReceived(new Pair<>(BT_AUDIO_CONNECTED,
                         bluetoothDevice.getAddress()), null);
             }
-        } else {
-            // ignore, not triggered by telecom
-            Log.i(this, "handleBtAudioActive: ignoring handling bt audio active.");
         }
     }
 
@@ -679,9 +668,6 @@
                 mPendingAudioRoute.onMessageReceived(new Pair<>(BT_AUDIO_DISCONNECTED,
                         bluetoothDevice.getAddress()), null);
             }
-        } else {
-            // ignore, not triggered by telecom
-            Log.i(this, "handleBtAudioInactive: ignoring handling bt audio inactive.");
         }
     }
 
@@ -1227,7 +1213,8 @@
 
     public AudioRoute getBaseRoute(boolean includeBluetooth, String btAddressToExclude) {
         AudioRoute destRoute = getPreferredAudioRouteFromStrategy();
-        if (destRoute == null || (destRoute.getBluetoothAddress() != null && !includeBluetooth)) {
+        if (destRoute == null || (destRoute.getBluetoothAddress() != null && (!includeBluetooth
+                || destRoute.getBluetoothAddress().equals(btAddressToExclude)))) {
             destRoute = getPreferredAudioRouteFromDefault(includeBluetooth, btAddressToExclude);
         }
         if (destRoute != null && !getCallSupportedRoutes().contains(destRoute)) {
diff --git a/src/com/android/server/telecom/CallIntentProcessor.java b/src/com/android/server/telecom/CallIntentProcessor.java
index 8e1f754..c77b9ff 100644
--- a/src/com/android/server/telecom/CallIntentProcessor.java
+++ b/src/com/android/server/telecom/CallIntentProcessor.java
@@ -14,7 +14,6 @@
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Looper;
-import android.os.Trace;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.telecom.DefaultDialerManager;
@@ -95,14 +94,12 @@
         final boolean isUnknownCall = intent.getBooleanExtra(KEY_IS_UNKNOWN_CALL, false);
         Log.i(this, "onReceive - isUnknownCall: %s", isUnknownCall);
 
-        Trace.beginSection("processNewCallCallIntent");
         if (isUnknownCall) {
             processUnknownCallIntent(mCallsManager, intent);
         } else {
             processOutgoingCallIntent(mContext, mCallsManager, intent, callingPackage,
                     mDefaultDialerCache, mFeatureFlags);
         }
-        Trace.endSection();
     }
 
 
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index ae62fb1..a7ad3b8 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -75,7 +75,6 @@
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.SystemVibrator;
-import android.os.Trace;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.BlockedNumberContract;
@@ -444,6 +443,7 @@
     private final InCallController mInCallController;
     private final CallDiagnosticServiceController mCallDiagnosticServiceController;
     private final CallAudioManager mCallAudioManager;
+    /** @deprecated not used any more */
     private final CallRecordingTonePlayer mCallRecordingTonePlayer;
     private RespondViaSmsManager mRespondViaSmsManager;
     private final Ringer mRinger;
@@ -696,8 +696,13 @@
                 new Ringer.VibrationEffectProxy(), mInCallController,
                 mContext.getSystemService(NotificationManager.class),
                 accessibilityManagerAdapter, featureFlags);
-        mCallRecordingTonePlayer = new CallRecordingTonePlayer(mContext, audioManager,
-                mTimeoutsAdapter, mLock);
+        if (featureFlags.telecomResolveHiddenDependencies()) {
+            // This is now deprecated
+            mCallRecordingTonePlayer = null;
+        } else {
+            mCallRecordingTonePlayer = new CallRecordingTonePlayer(mContext, audioManager,
+                    mTimeoutsAdapter, mLock);
+        }
         mCallAudioManager = new CallAudioManager(callAudioRouteAdapter,
                 this, callAudioModeStateMachineFactory.create(systemStateHelper,
                 (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE),
@@ -742,7 +747,9 @@
         mListeners.add(mCallEndpointController);
         mListeners.add(mCallDiagnosticServiceController);
         mListeners.add(mCallAudioManager);
-        mListeners.add(mCallRecordingTonePlayer);
+        if (!featureFlags.telecomResolveHiddenDependencies()) {
+            mListeners.add(mCallRecordingTonePlayer);
+        }
         mListeners.add(missedCallNotifier);
         mListeners.add(mDisconnectedCallNotifier);
         mListeners.add(mHeadsetMediaButton);
@@ -4621,7 +4628,6 @@
             Log.i(this, "addCall(%s) is already added");
             return;
         }
-        Trace.beginSection("addCall");
         Log.i(this, "addCall(%s)", call);
         call.addListener(this);
         mCalls.add(call);
@@ -4638,20 +4644,12 @@
         updateExternalCallCanPullSupport();
         // onCallAdded for calls which immediately take the foreground (like the first call).
         for (CallsManagerListener listener : mListeners) {
-            if (LogUtils.SYSTRACE_DEBUG) {
-                Trace.beginSection(listener.getClass().toString() + " addCall");
-            }
             listener.onCallAdded(call);
-            if (LogUtils.SYSTRACE_DEBUG) {
-                Trace.endSection();
-            }
         }
-        Trace.endSection();
     }
 
     @VisibleForTesting
     public void removeCall(Call call) {
-        Trace.beginSection("removeCall");
         Log.v(this, "removeCall(%s)", call);
 
         if (call.isTransactionalCall() && call.getTransactionServiceWrapper() != null) {
@@ -4678,16 +4676,9 @@
             updateCanAddCall();
             updateHasActiveRttCall();
             for (CallsManagerListener listener : mListeners) {
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.beginSection(listener.getClass().toString() + " onCallRemoved");
-                }
                 listener.onCallRemoved(call);
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.endSection();
-                }
             }
         }
-        Trace.endSection();
     }
 
     private void updateHasActiveRttCall() {
@@ -4750,13 +4741,8 @@
                 call.getAnalytics().setMissedReason(call.getMissedReason());
 
                 maybeShowErrorDialogOnDisconnect(call);
-
-                Trace.beginSection("onCallStateChanged");
-
                 maybeHandleHandover(call, newState);
                 notifyCallStateChanged(call, oldState, newState);
-
-                Trace.endSection();
             } else {
                 Log.i(this, "failed in setting the state to new state");
             }
@@ -4769,14 +4755,7 @@
             updateCanAddCall();
             updateHasActiveRttCall();
             for (CallsManagerListener listener : mListeners) {
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.beginSection(listener.getClass().toString() +
-                            " onCallStateChanged");
-                }
                 listener.onCallStateChanged(call, oldState, newState);
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.endSection();
-                }
             }
         }
     }
@@ -4901,13 +4880,7 @@
         if (newCanAddCall != mCanAddCall) {
             mCanAddCall = newCanAddCall;
             for (CallsManagerListener listener : mListeners) {
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.beginSection(listener.getClass().toString() + " updateCanAddCall");
-                }
                 listener.onCanAddCallChanged(mCanAddCall);
-                if (LogUtils.SYSTRACE_DEBUG) {
-                    Trace.endSection();
-                }
             }
         }
     }
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 6164638..0f62f87 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -45,7 +45,6 @@
 import android.os.Looper;
 import android.os.PackageTagsList;
 import android.os.RemoteException;
-import android.os.Trace;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.telecom.CallAudioState;
@@ -2573,7 +2572,6 @@
             Log.e(this, e, "Failed to set the in-call adapter.");
             mAnomalyReporter.reportAnomaly(SET_IN_CALL_ADAPTER_ERROR_UUID,
                     SET_IN_CALL_ADAPTER_ERROR_MSG);
-            Trace.endSection();
             return false;
         }
 
diff --git a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
index c24ac97..fce3f1a 100644
--- a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
+++ b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
@@ -25,7 +25,6 @@
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.Trace;
 import android.os.UserHandle;
 import android.telecom.GatewayInfo;
 import android.telecom.Log;
@@ -126,7 +125,6 @@
         public void onReceive(Context context, Intent intent) {
             try {
                 Log.startSession("NOCBIR.oR");
-                Trace.beginSection("onReceiveNewOutgoingCallBroadcast");
                 synchronized (mLock) {
                     Log.v(this, "onReceive: %s", intent);
 
@@ -194,7 +192,6 @@
                                     VideoProfile.STATE_AUDIO_ONLY));
                 }
             } finally {
-                Trace.endSection();
                 Log.endSession();
             }
         }
diff --git a/src/com/android/server/telecom/TelecomSystem.java b/src/com/android/server/telecom/TelecomSystem.java
index d7dcf38..7ff083f 100644
--- a/src/com/android/server/telecom/TelecomSystem.java
+++ b/src/com/android/server/telecom/TelecomSystem.java
@@ -375,7 +375,8 @@
 
             CallAnomalyWatchdog callAnomalyWatchdog = new CallAnomalyWatchdog(
                     Executors.newSingleThreadScheduledExecutor(),
-                    mLock, timeoutsAdapter, clockProxy, emergencyCallDiagnosticLogger);
+                    mLock, mFeatureFlags, timeoutsAdapter, clockProxy,
+                    emergencyCallDiagnosticLogger);
 
             TransactionManager transactionManager = TransactionManager.getInstance();
 
diff --git a/src/com/android/server/telecom/bluetooth/BluetoothStateReceiver.java b/src/com/android/server/telecom/bluetooth/BluetoothStateReceiver.java
index b4c3d8d..cd52889 100644
--- a/src/com/android/server/telecom/bluetooth/BluetoothStateReceiver.java
+++ b/src/com/android/server/telecom/bluetooth/BluetoothStateReceiver.java
@@ -23,6 +23,8 @@
 import static com.android.server.telecom.CallAudioRouteAdapter.BT_DEVICE_ADDED;
 import static com.android.server.telecom.CallAudioRouteAdapter.BT_DEVICE_REMOVED;
 import static com.android.server.telecom.CallAudioRouteAdapter.PENDING_ROUTE_FAILED;
+import static com.android.server.telecom.CallAudioRouteAdapter.SWITCH_BASELINE_ROUTE;
+import static com.android.server.telecom.CallAudioRouteController.INCLUDE_BLUETOOTH_IN_BASELINE;
 import static com.android.server.telecom.bluetooth.BluetoothRouteManager.BT_AUDIO_IS_ON;
 import static com.android.server.telecom.bluetooth.BluetoothRouteManager.BT_AUDIO_LOST;
 
@@ -153,8 +155,19 @@
                     CallAudioRouteController audioRouteController =
                             (CallAudioRouteController) mCallAudioRouteAdapter;
                     audioRouteController.setIsScoAudioConnected(false);
-                    mCallAudioRouteAdapter.sendMessageWithSessionInfo(BT_AUDIO_DISCONNECTED, 0,
-                            device);
+                    if (audioRouteController.isPending()) {
+                        mCallAudioRouteAdapter.sendMessageWithSessionInfo(BT_AUDIO_DISCONNECTED, 0,
+                                device);
+                    } else {
+                        // Handle case where BT stack signals SCO disconnected but Telecom isn't
+                        // processing any pending routes. This explicitly addresses cf instances
+                        // where a remote device disconnects SCO. Telecom should ensure that audio
+                        // is properly routed in the UI.
+                        audioRouteController.getPendingAudioRoute()
+                                .setCommunicationDeviceType(AudioRoute.TYPE_INVALID);
+                        mCallAudioRouteAdapter.sendMessageWithSessionInfo(SWITCH_BASELINE_ROUTE,
+                                INCLUDE_BLUETOOTH_IN_BASELINE, device.getAddress());
+                    }
                 }  else {
                     mBluetoothRouteManager.sendMessage(BT_AUDIO_LOST, args);
                 }
diff --git a/src/com/android/server/telecom/settings/BlockedNumbersUtil.java b/src/com/android/server/telecom/settings/BlockedNumbersUtil.java
index 3e1da17..99c5746 100644
--- a/src/com/android/server/telecom/settings/BlockedNumbersUtil.java
+++ b/src/com/android/server/telecom/settings/BlockedNumbersUtil.java
@@ -133,9 +133,12 @@
     public static boolean isEnhancedCallBlockingEnabledByPlatform(Context context) {
         CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService(
                 Context.CARRIER_CONFIG_SERVICE);
-        PersistableBundle carrierConfig = configManager.getConfig();
+        PersistableBundle carrierConfig = null;
+        if (configManager != null) {
+            carrierConfig = configManager.getConfig();
+        }
         if (carrierConfig == null) {
-            carrierConfig = configManager.getDefaultConfig();
+            carrierConfig = CarrierConfigManager.getDefaultConfig();
         }
         return carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_SUPPORT_ENHANCED_CALL_BLOCKING_BOOL)
diff --git a/src/com/android/server/telecom/voip/OutgoingCallTransaction.java b/src/com/android/server/telecom/voip/OutgoingCallTransaction.java
index 8c970db..68ffecf 100644
--- a/src/com/android/server/telecom/voip/OutgoingCallTransaction.java
+++ b/src/com/android/server/telecom/voip/OutgoingCallTransaction.java
@@ -114,6 +114,12 @@
                             Log.d(TAG, "processTransaction: call done. id=" + call.getId());
                         }
 
+                        if (mFeatureFlags.disconnectSelfManagedStuckStartupCalls()) {
+                            // set to dialing so the CallAnomalyWatchdog gives the VoIP calls 1
+                            // minute to timeout rather than 5 seconds.
+                            mCallsManager.markCallAsDialing(call);
+                        }
+
                         return CompletableFuture.completedFuture(
                                 new VoipCallTransactionResult(
                                         VoipCallTransactionResult.RESULT_SUCCEED,
diff --git a/tests/src/com/android/server/telecom/tests/CallAnomalyWatchdogTest.java b/tests/src/com/android/server/telecom/tests/CallAnomalyWatchdogTest.java
index 86d24f9..a6f63bc 100644
--- a/tests/src/com/android/server/telecom/tests/CallAnomalyWatchdogTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallAnomalyWatchdogTest.java
@@ -122,7 +122,7 @@
         doReturn(new ComponentName(mContext, CallTest.class))
                 .when(mMockConnectionService).getComponentName();
         mCallAnomalyWatchdog = new CallAnomalyWatchdog(mTestScheduledExecutorService, mLock,
-                mTimeouts, mMockClockProxy, mMockEmergencyCallDiagnosticLogger);
+                mFeatureFlags, mTimeouts, mMockClockProxy, mMockEmergencyCallDiagnosticLogger);
         mCallAnomalyWatchdog.setAnomalyReporterAdapter(mAnomalyReporterAdapter);
         when(mMockCallsManager.getCurrentUserHandle()).thenReturn(UserHandle.CURRENT);
     }
diff --git a/tests/src/com/android/server/telecom/tests/CallRecordingTonePlayerTest.java b/tests/src/com/android/server/telecom/tests/CallRecordingTonePlayerTest.java
index 60952d3..5ccb2fe 100644
--- a/tests/src/com/android/server/telecom/tests/CallRecordingTonePlayerTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallRecordingTonePlayerTest.java
@@ -42,6 +42,7 @@
 import android.media.MediaRecorder;
 import android.os.Handler;
 import android.os.Looper;
+import android.platform.test.annotations.RequiresFlagsDisabled;
 import android.telecom.PhoneAccountHandle;
 
 import androidx.test.filters.MediumTest;
@@ -52,6 +53,7 @@
 import com.android.server.telecom.CallState;
 import com.android.server.telecom.TelecomSystem;
 import com.android.server.telecom.Timeouts;
+import com.android.server.telecom.flags.Flags;
 
 import org.junit.After;
 import org.junit.Before;
@@ -71,6 +73,7 @@
  * Unit tests for the {@link com.android.server.telecom.CallRecordingTonePlayer} class.
  */
 @RunWith(JUnit4.class)
+@RequiresFlagsDisabled(Flags.FLAG_TELECOM_RESOLVE_HIDDEN_DEPENDENCIES)
 public class CallRecordingTonePlayerTest extends TelecomTestCase {
 
     private static final String PHONE_ACCOUNT_PACKAGE = "com.android.telecom.test";