Merge "Always switch if default is OOS" into main
diff --git a/src/java/com/android/internal/telephony/data/AutoDataSwitchController.java b/src/java/com/android/internal/telephony/data/AutoDataSwitchController.java
index 49f5c90..0ea0598 100644
--- a/src/java/com/android/internal/telephony/data/AutoDataSwitchController.java
+++ b/src/java/com/android/internal/telephony/data/AutoDataSwitchController.java
@@ -638,18 +638,21 @@
                 Phone secondaryDataPhone = null;
                 debugMessage.append(", found phone ").append(phoneId).append(" in HOME reg");
 
-                if (isRatSignalStrengthBasedSwitchEnabled()) {
-                    // Use score if RAT/signal strength based switch is enabled.
-                    int defaultScore = mPhonesSignalStatus[defaultPhoneId].getRatSignalScore();
-                    int candidateScore = mPhonesSignalStatus[phoneId].getRatSignalScore();
-                    if ((candidateScore - defaultScore) > mScoreTolerance) {
-                        debugMessage.append(" with higher score ").append(candidateScore)
-                                .append(" versus current ").append(defaultScore);
-                        secondaryDataPhone = PhoneFactory.getPhone(phoneId);
-                    } else {
-                        debugMessage.append(", but its score ").append(candidateScore)
-                                .append(" doesn't meet the bar to switch given the current ")
-                                .append(defaultScore);
+                if (isInService(mPhonesSignalStatus[defaultPhoneId].mDataRegState)) {
+                    // Use score if RAT/signal strength based switch is enabled and both phone are
+                    // in service.
+                    if (isRatSignalStrengthBasedSwitchEnabled()) {
+                        int defaultScore = mPhonesSignalStatus[defaultPhoneId].getRatSignalScore();
+                        int candidateScore = mPhonesSignalStatus[phoneId].getRatSignalScore();
+                        if ((candidateScore - defaultScore) > mScoreTolerance) {
+                            debugMessage.append(" with higher score ").append(candidateScore)
+                                    .append(" versus current ").append(defaultScore);
+                            secondaryDataPhone = PhoneFactory.getPhone(phoneId);
+                        } else {
+                            debugMessage.append(", but its score ").append(candidateScore)
+                                    .append(" doesn't meet the bar to switch given the current ")
+                                    .append(defaultScore);
+                        }
                     }
                 } else {
                     // Only OOS/in service switch is enabled.
diff --git a/tests/telephonytests/src/com/android/internal/telephony/data/AutoDataSwitchControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/data/AutoDataSwitchControllerTest.java
index 22e4bc2..4b88f0a 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/data/AutoDataSwitchControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/data/AutoDataSwitchControllerTest.java
@@ -170,12 +170,24 @@
         // Verify attempting to switch
         verify(mMockedPhoneSwitcherCallback).onRequireValidation(PHONE_2, true/*needValidation*/);
 
-        // 1. Service state becomes not ideal - secondary lost its advantage score
+        // 1.1 Service state becomes not ideal - secondary lost its advantage score,
+        // but primary is OOS, so continue to switch.
         clearInvocations(mMockedPhoneSwitcherCallback);
         displayInfoChanged(PHONE_2, mBadTelephonyDisplayInfo);
         signalStrengthChanged(PHONE_2, SignalStrength.SIGNAL_STRENGTH_POOR);
         processAllFutureMessages();
 
+        verify(mMockedPhoneSwitcherCallback, never())
+                .onRequireCancelAnyPendingAutoSwitchValidation();
+
+        // 1.2 Service state becomes not ideal - secondary lost its advantage score,
+        // since primary is in service, no need to switch.
+        clearInvocations(mMockedPhoneSwitcherCallback);
+        serviceStateChanged(PHONE_1, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        displayInfoChanged(PHONE_2, mBadTelephonyDisplayInfo);
+        signalStrengthChanged(PHONE_2, SignalStrength.SIGNAL_STRENGTH_POOR);
+        processAllFutureMessages();
+
         verify(mMockedPhoneSwitcherCallback).onRequireCancelAnyPendingAutoSwitchValidation();
 
         // 2.1 User data disabled on primary SIM
@@ -211,13 +223,26 @@
 
     @Test
     public void testCancelSwitch_onPrimary_rat_signalStrength() {
-        // 4.1 Display info and signal strength on secondary phone became bad
+        // 4.1.1 Display info and signal strength on secondary phone became bad,
+        // but primary is still OOS, so still switch to the secondary.
         prepareIdealUsesNonDdsCondition();
         processAllFutureMessages();
         clearInvocations(mMockedPhoneSwitcherCallback);
         displayInfoChanged(PHONE_2, mBadTelephonyDisplayInfo);
         signalStrengthChanged(PHONE_2, SignalStrength.SIGNAL_STRENGTH_MODERATE);
         processAllFutureMessages();
+        verify(mMockedPhoneSwitcherCallback, never())
+                .onRequireCancelAnyPendingAutoSwitchValidation();
+
+        // 4.1.2 Display info and signal strength on secondary phone became bad,
+        // but primary become service, then don't switch.
+        prepareIdealUsesNonDdsCondition();
+        processAllFutureMessages();
+        clearInvocations(mMockedPhoneSwitcherCallback);
+        serviceStateChanged(PHONE_1, NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
+        displayInfoChanged(PHONE_2, mBadTelephonyDisplayInfo);
+        signalStrengthChanged(PHONE_2, SignalStrength.SIGNAL_STRENGTH_MODERATE);
+        processAllFutureMessages();
         verify(mMockedPhoneSwitcherCallback).onRequireCancelAnyPendingAutoSwitchValidation();
 
         // 4.2 Display info on default phone became good just as the secondary