Simplifying implementation of the coalescing logic in the new call log.

Bug: 70388714
Test: Existing tests
PiperOrigin-RevId: 181231987
Change-Id: I0c7386f60e92f7087f9f5ad1b1f454b43b7227e7
diff --git a/java/com/android/dialer/calllog/database/Coalescer.java b/java/com/android/dialer/calllog/database/Coalescer.java
index f4ef02a..9b78814 100644
--- a/java/com/android/dialer/calllog/database/Coalescer.java
+++ b/java/com/android/dialer/calllog/database/Coalescer.java
@@ -80,40 +80,45 @@
             CoalescedAnnotatedCallLog.ALL_COLUMNS,
             Assert.isNotNull(allAnnotatedCallLogRowsSortedByTimestampDesc).getCount());
 
-    if (allAnnotatedCallLogRowsSortedByTimestampDesc.moveToFirst()) {
-      int coalescedRowId = 0;
+    if (!allAnnotatedCallLogRowsSortedByTimestampDesc.moveToFirst()) {
+      return allCoalescedRowsMatrixCursor;
+    }
 
-      List<ContentValues> currentRowGroup = new ArrayList<>();
+    int coalescedRowId = 0;
+    List<ContentValues> currentRowGroup = new ArrayList<>();
 
-      do {
-        ContentValues currentRow =
-            cursorRowToContentValues(allAnnotatedCallLogRowsSortedByTimestampDesc);
+    ContentValues firstRow = cursorRowToContentValues(allAnnotatedCallLogRowsSortedByTimestampDesc);
+    currentRowGroup.add(firstRow);
 
-        if (currentRowGroup.isEmpty()) {
-          currentRowGroup.add(currentRow);
-          continue;
+    while (!currentRowGroup.isEmpty()) {
+      // Group consecutive rows
+      ContentValues firstRowInGroup = currentRowGroup.get(0);
+      ContentValues currentRow = null;
+      while (allAnnotatedCallLogRowsSortedByTimestampDesc.moveToNext()) {
+        currentRow = cursorRowToContentValues(allAnnotatedCallLogRowsSortedByTimestampDesc);
+
+        if (!rowsShouldBeCombined(dialerPhoneNumberUtil, firstRowInGroup, currentRow)) {
+          break;
         }
 
-        ContentValues previousRow = currentRowGroup.get(currentRowGroup.size() - 1);
-
-        if (!rowsShouldBeCombined(dialerPhoneNumberUtil, previousRow, currentRow)) {
-          ContentValues coalescedRow = coalesceRowsForAllDataSources(currentRowGroup);
-          coalescedRow.put(
-              CoalescedAnnotatedCallLog.COALESCED_IDS,
-              getCoalescedIds(currentRowGroup).toByteArray());
-          addContentValuesToMatrixCursor(
-              coalescedRow, allCoalescedRowsMatrixCursor, coalescedRowId++);
-          currentRowGroup.clear();
-        }
         currentRowGroup.add(currentRow);
-      } while (allAnnotatedCallLogRowsSortedByTimestampDesc.moveToNext());
+      }
 
-      // Deal with leftover rows.
+      // Coalesce the group into a single row
       ContentValues coalescedRow = coalesceRowsForAllDataSources(currentRowGroup);
       coalescedRow.put(
           CoalescedAnnotatedCallLog.COALESCED_IDS, getCoalescedIds(currentRowGroup).toByteArray());
-      addContentValuesToMatrixCursor(coalescedRow, allCoalescedRowsMatrixCursor, coalescedRowId);
+      addContentValuesToMatrixCursor(coalescedRow, allCoalescedRowsMatrixCursor, coalescedRowId++);
+
+      // Clear the current group after the rows are coalesced.
+      currentRowGroup.clear();
+
+      // Add the first of the remaining rows to the current group.
+      if (!allAnnotatedCallLogRowsSortedByTimestampDesc.isAfterLast()) {
+        currentRowGroup.add(currentRow);
+      }
     }
+
     return allCoalescedRowsMatrixCursor;
   }