Merge "Fix where post-disconnect CEP causes single party call mode to exit." into qt-dev
diff --git a/src/com/android/services/telephony/ImsConference.java b/src/com/android/services/telephony/ImsConference.java
index 8ef0464..8cd8051 100644
--- a/src/com/android/services/telephony/ImsConference.java
+++ b/src/com/android/services/telephony/ImsConference.java
@@ -731,12 +731,16 @@
             // Determine if the conference event package represents a single party conference.
             // A single party conference is one where there is no other participant other than the
             // conference host and one other participant.
+            // Note: We consider 0 to still be a single party conference since some carriers will
+            // send a conference event package with JUST the host in it when the conference is
+            // disconnected.  We don't want to change back to conference mode prior to disconnection
+            // or we will not log the call.
             boolean isSinglePartyConference = participants.stream()
                     .filter(p -> {
                         Pair<Uri, Uri> pIdent = new Pair<>(p.getHandle(), p.getEndpoint());
                         return !Objects.equals(mHostParticipantIdentity, pIdent);
                     })
-                    .count() == 1;
+                    .count() <= 1;
 
             // We will only process the CEP data if:
             // 1. We're not emulating a single party call.
diff --git a/tests/src/com/android/services/telephony/ImsConferenceTest.java b/tests/src/com/android/services/telephony/ImsConferenceTest.java
index 909ab65..8d67973 100644
--- a/tests/src/com/android/services/telephony/ImsConferenceTest.java
+++ b/tests/src/com/android/services/telephony/ImsConferenceTest.java
@@ -16,9 +16,11 @@
 
 package com.android.services.telephony;
 
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.mockito.Mockito.any;
@@ -30,6 +32,7 @@
 
 import android.net.Uri;
 import android.os.Looper;
+import android.telecom.Conference;
 import android.telecom.ConferenceParticipant;
 import android.telecom.Connection;
 import android.telecom.PhoneAccountHandle;
@@ -91,16 +94,83 @@
         imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
                 Arrays.asList(participant1, participant2));
         assertEquals(2, imsConference.getNumberOfParticipants());
+        verify(mMockTelephonyConnectionServiceProxy, times(2)).addExistingConnection(
+                any(PhoneAccountHandle.class), any(Connection.class),
+                eq(imsConference));
 
         // Because we're pretending its a single party, there should be no participants any more.
         imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
                 Arrays.asList(participant1));
         assertEquals(0, imsConference.getNumberOfParticipants());
+        verify(mMockTelephonyConnectionServiceProxy, times(2)).removeConnection(
+                any(Connection.class));
+        reset(mMockTelephonyConnectionServiceProxy);
 
         // Back to 2!
         imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
                 Arrays.asList(participant1, participant2));
         assertEquals(2, imsConference.getNumberOfParticipants());
+        verify(mMockTelephonyConnectionServiceProxy, times(2)).addExistingConnection(
+                any(PhoneAccountHandle.class), any(Connection.class),
+                eq(imsConference));
+    }
+
+    /**
+     * We have seen a scenario on a carrier where a conference event package comes in just prior to
+     * the call disconnecting with only the conference host in it.  This caused a problem because
+     * it triggered exiting single party conference mode (due to a bug) and caused the call to not
+     * be logged.
+     */
+    @Test
+    @SmallTest
+    public void testSinglePartyEmulationWithPreDisconnectParticipantUpdate() {
+        when(mMockTelecomAccountRegistry.isUsingSimCallManager(any(PhoneAccountHandle.class)))
+                .thenReturn(false);
+
+        ImsConference imsConference = new ImsConference(mMockTelecomAccountRegistry,
+                mMockTelephonyConnectionServiceProxy, mConferenceHost,
+                null /* phoneAccountHandle */, () -> true /* featureFlagProxy */);
+
+        final boolean[] isConferenceState = new boolean[1];
+        Conference.Listener conferenceListener = new Conference.Listener() {
+            @Override
+            public void onConferenceStateChanged(Conference c, boolean isConference) {
+                super.onConferenceStateChanged(c, isConference);
+                isConferenceState[0] = isConference;
+            }
+        };
+        imsConference.addListener(conferenceListener);
+
+        ConferenceParticipant participant1 = new ConferenceParticipant(
+                Uri.parse("tel:6505551212"),
+                "A",
+                Uri.parse("sip:6505551212@testims.com"),
+                Connection.STATE_ACTIVE);
+        ConferenceParticipant participant2 = new ConferenceParticipant(
+                Uri.parse("tel:6505551213"),
+                "A",
+                Uri.parse("sip:6505551213@testims.com"),
+                Connection.STATE_ACTIVE);
+        imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
+                Arrays.asList(participant1, participant2));
+        assertEquals(2, imsConference.getNumberOfParticipants());
+        verify(mMockTelephonyConnectionServiceProxy, times(2)).addExistingConnection(
+                any(PhoneAccountHandle.class), any(Connection.class),
+                eq(imsConference));
+
+        // Because we're pretending its a single party, there should be only a single participant.
+        imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
+                Arrays.asList(participant1));
+        assertEquals(0, imsConference.getNumberOfParticipants());
+        verify(mMockTelephonyConnectionServiceProxy, times(2)).removeConnection(
+                any(Connection.class));
+
+        // Emulate a pre-disconnect conference event package; there will be zero participants.
+        imsConference.handleConferenceParticipantsUpdate(mConferenceHost,
+                Arrays.asList());
+
+        // We should still not be considered a conference (hence we should be logging this call).
+        assertFalse(isConferenceState[0]);
     }
 
     /**