Migrate GSM SignalStrength to WCDMA on HAL 1.0

For HAL versions 1.0 and 1.1, there is no option to report
WCDMA signal strength via the SignalStrength UNSOL. Thus,
when the device is on WCDMA, we migrate the GSM RSSI and BER
to the WCDMA structure to fix the SignalStrength API to
return the correct structures.

Also, remove a second initMocks from RILTest that was causing all
mocks to be reinitialized, breaking unit tests that relied on any
existing mocks set up in TelephonyTest.

Bug: 134547711
Test: RILTest#testFixupSignalStrength10
Merged-In: I2701106d030511f41f657df5b85957476db80822
Change-Id: I2701106d030511f41f657df5b85957476db80822
(cherry picked from commit 4fb2207dddaa271a48730dc233925d9dd678dad7)
diff --git a/src/java/com/android/internal/telephony/RIL.java b/src/java/com/android/internal/telephony/RIL.java
index a98bb2b..a19c690 100644
--- a/src/java/com/android/internal/telephony/RIL.java
+++ b/src/java/com/android/internal/telephony/RIL.java
@@ -73,6 +73,12 @@
 import android.telephony.AccessNetworkConstants.AccessNetworkType;
 import android.telephony.CarrierRestrictionRules;
 import android.telephony.CellInfo;
+import android.telephony.CellSignalStrengthCdma;
+import android.telephony.CellSignalStrengthGsm;
+import android.telephony.CellSignalStrengthLte;
+import android.telephony.CellSignalStrengthNr;
+import android.telephony.CellSignalStrengthTdscdma;
+import android.telephony.CellSignalStrengthWcdma;
 import android.telephony.ClientRequestStats;
 import android.telephony.ImsiEncryptionInfo;
 import android.telephony.ModemActivityInfo;
@@ -83,6 +89,7 @@
 import android.telephony.RadioAccessSpecifier;
 import android.telephony.Rlog;
 import android.telephony.ServiceState;
+import android.telephony.SignalStrength;
 import android.telephony.SmsManager;
 import android.telephony.TelephonyHistogram;
 import android.telephony.TelephonyManager;
@@ -5884,6 +5891,58 @@
     }
 
     /**
+     * Fixup for SignalStrength 1.0 to Assume GSM to WCDMA when
+     * The current RAT type is one of the UMTS RATs.
+     * @param signalStrength the initial signal strength
+     * @return a new SignalStrength if RAT is UMTS or existing SignalStrength
+     */
+    public SignalStrength fixupSignalStrength10(SignalStrength signalStrength) {
+        List<CellSignalStrengthGsm> gsmList = signalStrength.getCellSignalStrengths(
+                CellSignalStrengthGsm.class);
+        // If GSM is not the primary type, then bail out; no fixup needed.
+        if (gsmList == null || gsmList.get(0) == null || !gsmList.get(0).isValid()) {
+            return signalStrength;
+        }
+
+        CellSignalStrengthGsm gsmStrength = gsmList.get(0);
+
+        // Use the voice RAT which is a guarantee in GSM and UMTS
+        int voiceRat = ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
+        Phone phone = PhoneFactory.getPhone(mPhoneId);
+        if (phone != null) {
+            ServiceState ss = phone.getServiceState();
+            if (ss != null) {
+                voiceRat = ss.getRilVoiceRadioTechnology();
+            }
+        }
+        switch (voiceRat) {
+            case ServiceState.RIL_RADIO_TECHNOLOGY_UMTS: /* fallthrough */
+            case ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA: /* fallthrough */
+            case ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA: /* fallthrough */
+            case ServiceState.RIL_RADIO_TECHNOLOGY_HSPA: /* fallthrough */
+            case ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP: /* fallthrough */
+                break;
+            default:
+                // If we are not currently on WCDMA/HSPA, then we don't need to do a fixup.
+                return signalStrength;
+        }
+
+        // The service state reports WCDMA, and the SignalStrength is reported for GSM, so at this
+        // point we take an educated guess that the GSM SignalStrength report is actually for
+        // WCDMA. Also, if we are in WCDMA/GSM we can safely assume that there are no other valid
+        // signal strength reports (no SRLTE, which is the only supported case in HAL 1.0).
+        // Thus, we just construct a new SignalStrength and migrate RSSI and BER from the
+        // GSM report to the WCDMA report, leaving everything else empty.
+        return new SignalStrength(
+                new CellSignalStrengthCdma(), new CellSignalStrengthGsm(),
+                new CellSignalStrengthWcdma(gsmStrength.getRssi(),
+                        gsmStrength.getBitErrorRate(),
+                        CellInfo.UNAVAILABLE, CellInfo.UNAVAILABLE),
+                new CellSignalStrengthTdscdma(), new CellSignalStrengthLte(),
+                new CellSignalStrengthNr());
+    }
+
+    /**
      * Convert CellInfo defined in 1.4/types.hal to CellInfo type.
      * @param records List of CellInfo defined in 1.4/types.hal.
      * @return List of converted CellInfo object.
diff --git a/src/java/com/android/internal/telephony/RadioIndication.java b/src/java/com/android/internal/telephony/RadioIndication.java
index 70e306d..6beab09 100644
--- a/src/java/com/android/internal/telephony/RadioIndication.java
+++ b/src/java/com/android/internal/telephony/RadioIndication.java
@@ -229,7 +229,9 @@
                                       android.hardware.radio.V1_0.SignalStrength signalStrength) {
         mRil.processIndication(indicationType);
 
-        SignalStrength ss = new SignalStrength(signalStrength);
+        SignalStrength ssInitial = new SignalStrength(signalStrength);
+
+        SignalStrength ss = mRil.fixupSignalStrength10(ssInitial);
         // Note this is set to "verbose" because it happens frequently
         if (RIL.RILJ_LOGV) mRil.unsljLogvRet(RIL_UNSOL_SIGNAL_STRENGTH, ss);
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/RILTest.java b/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
index a6b455d..dd343f8 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/RILTest.java
@@ -130,8 +130,11 @@
 import android.telephony.CellSignalStrengthCdma;
 import android.telephony.CellSignalStrengthGsm;
 import android.telephony.CellSignalStrengthLte;
+import android.telephony.CellSignalStrengthNr;
 import android.telephony.CellSignalStrengthTdscdma;
 import android.telephony.CellSignalStrengthWcdma;
+import android.telephony.ServiceState;
+import android.telephony.SignalStrength;
 import android.telephony.SmsManager;
 import android.telephony.TelephonyManager;
 import android.telephony.data.ApnSetting;
@@ -148,7 +151,6 @@
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -295,7 +297,6 @@
     @Before
     public void setUp() throws Exception {
         super.setUp(RILTest.class.getSimpleName());
-        MockitoAnnotations.initMocks(this);
         mTestHandler = new RILTestHandler(getClass().getSimpleName());
         mTestHandler.start();
         waitUntilReady();
@@ -1897,6 +1898,35 @@
     }
 
     @Test
+    public void testFixupSignalStrength10() {
+        final int gsmWcdmaRssiDbm = -65;
+
+        // Test the positive case where rat=UMTS and SignalStrength=GSM
+        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_UMTS)
+                .when(mServiceState).getRilVoiceRadioTechnology();
+
+        SignalStrength gsmSignalStrength = new SignalStrength(
+                new CellSignalStrengthCdma(),
+                new CellSignalStrengthGsm(gsmWcdmaRssiDbm, 1, CellInfo.UNAVAILABLE),
+                new CellSignalStrengthWcdma(), new CellSignalStrengthTdscdma(),
+                new CellSignalStrengthLte(), new CellSignalStrengthNr());
+        SignalStrength result = mRILUnderTest.fixupSignalStrength10(gsmSignalStrength);
+
+        assertTrue(result.getCellSignalStrengths(CellSignalStrengthGsm.class).isEmpty());
+        assertFalse(result.getCellSignalStrengths(CellSignalStrengthWcdma.class).isEmpty());
+
+        // Even though the dBm values are equal, the above checks ensure that the value has
+        // been migrated to WCDMA (with no change in the top-level getDbm() result).
+        assertEquals(result.getDbm(), gsmSignalStrength.getDbm());
+
+        // Test the no-op case where rat=GSM and SignalStrength=GSM
+        doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_GSM)
+                .when(mServiceState).getRilVoiceRadioTechnology();
+        result = mRILUnderTest.fixupSignalStrength10(gsmSignalStrength);
+        assertEquals(result, gsmSignalStrength);
+    }
+
+    @Test
     public void testCreateCarrierRestrictionList() {
         ArrayList<CarrierIdentifier> carriers = new ArrayList<>();
         carriers.add(new CarrierIdentifier("110", "120", null, null, null, null));