Merge "Lost the registered cellinfo item after the aggregation" into sc-v2-dev
diff --git a/src/com/android/settings/network/telephony/NetworkSelectSettings.java b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
index 813cc74..c1c0f8f 100644
--- a/src/com/android/settings/network/telephony/NetworkSelectSettings.java
+++ b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
@@ -50,6 +50,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
@@ -326,12 +327,19 @@
                     CellInfoUtil.getCellIdentityMccMnc(cellInfo.getCellIdentity()));
             Class className = cellInfo.getClass();
 
-            if (aggregatedList.stream().anyMatch(
+            Optional<CellInfo> itemInTheList = aggregatedList.stream().filter(
                     item -> {
                         String itemPlmn = CellInfoUtil.getNetworkTitle(item.getCellIdentity(),
                                 CellInfoUtil.getCellIdentityMccMnc(item.getCellIdentity()));
                         return itemPlmn.equals(plmn) && item.getClass().equals(className);
-                    })) {
+                    })
+                    .findFirst();
+            if (itemInTheList.isPresent()) {
+                if (cellInfo.isRegistered() && !itemInTheList.get().isRegistered()) {
+                    // Adding the registered cellinfo item into list. If there are two registered
+                    // cellinfo items, then select first one from source list.
+                    aggregatedList.set(aggregatedList.indexOf(itemInTheList.get()), cellInfo);
+                }
                 continue;
             }
             aggregatedList.add(cellInfo);
diff --git a/tests/unit/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java b/tests/unit/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
index 47e3ad9..02b8706 100644
--- a/tests/unit/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
+++ b/tests/unit/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
@@ -166,6 +166,19 @@
         assertThat(mNetworkSelectSettings.doAggregation(testList)).isEqualTo(expected);
     }
 
+    @Test
+    public void doAggregation_hasDuplicateItemsDiffMccMncCase3_removeSamePlmnRatItem() {
+        List<CellInfo> testList = Arrays.asList(
+                createLteCellInfo(false, 123, "123", "232", "CarrierA"),
+                createLteCellInfo(false, 124, "123", "233", "CarrierA"),
+                createLteCellInfo(true, 125, "123", "234", "CarrierA"),
+                createGsmCellInfo(false, 126, "456", "232", "CarrierA"));
+        List<CellInfo> expected = Arrays.asList(
+                createLteCellInfo(true, 125, "123", "234", "CarrierA"),
+                createGsmCellInfo(false, 126, "456", "232", "CarrierA"));
+        assertThat(mNetworkSelectSettings.doAggregation(testList)).isEqualTo(expected);
+    }
+
     private CellInfoLte createLteCellInfo(boolean registered, int cellId, String mcc, String mnc,
             String plmnName) {
         CellIdentityLte cil = new CellIdentityLte(