Prevent video calls that down graded to audio calls from merging to
conference call.

Test: TelephonyUnitTest
Bug: 143585631
Change-Id: I7414753fcbf78ad0d0cecf15067fe1d02db04214
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 5a1e6a6..6130da9 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -922,7 +922,7 @@
      * @param handle The {@link PhoneAccountHandle}.
      * @return {@code True} if merging calls is supported.
      */
-    boolean isMergeCallSupported(PhoneAccountHandle handle) {
+    public boolean isMergeCallSupported(PhoneAccountHandle handle) {
         synchronized (mAccountsLock) {
             for (AccountEntry entry : mAccounts) {
                 if (entry.getPhoneAccountHandle().equals(handle)) {
@@ -940,7 +940,7 @@
      * @param handle The {@link PhoneAccountHandle}.
      * @return {@code True} if video conferencing is supported.
      */
-    boolean isVideoConferencingSupported(PhoneAccountHandle handle) {
+    public boolean isVideoConferencingSupported(PhoneAccountHandle handle) {
         synchronized (mAccountsLock) {
             for (AccountEntry entry : mAccounts) {
                 if (entry.getPhoneAccountHandle().equals(handle)) {
@@ -958,7 +958,7 @@
      * @param handle The {@link PhoneAccountHandle}.
      * @return {@code True} if merging of wifi calls is allowed when VoWIFI is disabled.
      */
-    boolean isMergeOfWifiCallsAllowedWhenVoWifiOff(final PhoneAccountHandle handle) {
+    public boolean isMergeOfWifiCallsAllowedWhenVoWifiOff(final PhoneAccountHandle handle) {
         synchronized (mAccountsLock) {
             Optional<AccountEntry> result = mAccounts.stream().filter(
                     entry -> entry.getPhoneAccountHandle().equals(handle)).findFirst();
@@ -978,7 +978,7 @@
      * @param handle The {@link PhoneAccountHandle}.
      * @return {@code True} if merging IMS calls is supported.
      */
-    boolean isMergeImsCallSupported(PhoneAccountHandle handle) {
+    public boolean isMergeImsCallSupported(PhoneAccountHandle handle) {
         synchronized (mAccountsLock) {
             for (AccountEntry entry : mAccounts) {
                 if (entry.getPhoneAccountHandle().equals(handle)) {
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 14f0cb8..6397a6f 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -2415,8 +2415,8 @@
         PhoneAccountHandle phoneAccountHandle = isIms ? PhoneUtils
                 .makePstnPhoneAccountHandle(phone.getDefaultPhone())
                 : PhoneUtils.makePstnPhoneAccountHandle(phone);
-        TelecomAccountRegistry telecomAccountRegistry = TelecomAccountRegistry
-                .getInstance(getPhone().getContext());
+        TelecomAccountRegistry telecomAccountRegistry = getTelecomAccountRegistry(
+                getPhone().getContext());
         boolean isConferencingSupported = telecomAccountRegistry
                 .isMergeCallSupported(phoneAccountHandle);
         boolean isImsConferencingSupported = telecomAccountRegistry
@@ -2425,6 +2425,17 @@
                 .isVideoConferencingSupported(phoneAccountHandle);
         boolean isMergeOfWifiCallsAllowedWhenVoWifiOff = telecomAccountRegistry
                 .isMergeOfWifiCallsAllowedWhenVoWifiOff(phoneAccountHandle);
+        ImsCall imsCall = ((ImsPhoneConnection) getOriginalConnection()).getImsCall();
+        CarrierConfigManager configManager = (CarrierConfigManager) phone.getContext()
+                .getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        boolean downGradedVideoCall = false;
+        if (configManager != null) {
+            PersistableBundle config = configManager.getConfigForSubId(phone.getSubId());
+            if (config != null) {
+                downGradedVideoCall = config.getBoolean(
+                        CarrierConfigManager.KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL);
+            }
+        }
 
         Log.v(this, "refreshConferenceSupported : isConfSupp=%b, isImsConfSupp=%b, " +
                 "isVidConfSupp=%b, isMergeOfWifiAllowed=%b, " +
@@ -2445,6 +2456,12 @@
         } else if (isVideoCall && !mIsCarrierVideoConferencingSupported) {
             isConferenceSupported = false;
             Log.d(this, "refreshConferenceSupported = false; video conf not supported.");
+        } else if ((imsCall.wasVideoCall() && downGradedVideoCall)
+                && !mIsCarrierVideoConferencingSupported) {
+            isConferenceSupported = false;
+            Log.d(this,
+                    "refreshConferenceSupported = false;"
+                            + " video conf not supported for downgraded audio call.");
         } else if (!isMergeOfWifiCallsAllowedWhenVoWifiOff && isWifi() && !isVoWifiEnabled) {
             isConferenceSupported = false;
             Log.d(this,
@@ -2875,4 +2892,8 @@
             listener.onStatusHintsChanged(this, statusHints);
         }
     }
+
+    public TelecomAccountRegistry getTelecomAccountRegistry(Context context) {
+        return TelecomAccountRegistry.getInstance(context);
+    }
 }
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionTest.java
index 7d15680..6f5e5c9 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionTest.java
@@ -1,15 +1,17 @@
 package com.android.services.telephony;
 
 import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.TestCase.assertFalse;
 
 import android.os.Bundle;
 import android.telecom.Connection;
 
+import androidx.test.runner.AndroidJUnit4;
+
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import androidx.test.runner.AndroidJUnit4;
-
 @RunWith(AndroidJUnit4.class)
 public class TelephonyConnectionTest {
 
@@ -22,4 +24,17 @@
         assertEquals(codec, Connection.AUDIO_CODEC_AMR);
     }
 
+    @Test
+    public void testConferenceNotSupportedForDownGradedVideoCall() {
+        TestTelephonyConnection c = new TestTelephonyConnection();
+        c.setIsImsConnection(true);
+        c.setIsVideoCall(false);
+        c.setWasVideoCall(true);
+        c.setDownGradeVideoCall(true);
+        c.refreshConferenceSupported();
+        assertFalse(c.isConferenceSupported());
+        c.setDownGradeVideoCall(false);
+        c.refreshConferenceSupported();
+        assertTrue(c.isConferenceSupported());
+    }
 }
diff --git a/tests/src/com/android/services/telephony/TestTelephonyConnection.java b/tests/src/com/android/services/telephony/TestTelephonyConnection.java
index 5b31c0f..d42ef5e 100644
--- a/tests/src/com/android/services/telephony/TestTelephonyConnection.java
+++ b/tests/src/com/android/services/telephony/TestTelephonyConnection.java
@@ -16,24 +16,29 @@
 
 package com.android.services.telephony;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.notNull;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import android.content.Context;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.os.PersistableBundle;
 import android.telecom.PhoneAccountHandle;
+import android.telecom.VideoProfile;
+import android.telephony.CarrierConfigManager;
 
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
+import com.android.ims.ImsCall;
 import com.android.internal.telephony.Call;
 import com.android.internal.telephony.Connection;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.emergency.EmergencyNumberTracker;
+import com.android.internal.telephony.imsphone.ImsPhoneConnection;
 
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
@@ -62,23 +67,45 @@
     @Mock
     EmergencyNumberTracker mEmergencyNumberTracker;
 
+    @Mock
+    ImsPhoneConnection mImsPhoneConnection;
+
+    @Mock
+    ImsCall mImsCall;
+
+    @Mock
+    TelecomAccountRegistry mTelecomAccountRegistry;
+
+    @Mock
+    CarrierConfigManager mCarrierConfigManager;
+
+    private boolean mIsImsConnection;
+    private boolean mIsConferenceSupported = true;
     private Phone mMockPhone;
     private int mNotifyPhoneAccountChangedCount = 0;
     private List<String> mLastConnectionEvents = new ArrayList<>();
     private List<Bundle> mLastConnectionEventExtras = new ArrayList<>();
+    private Object mLock = new Object();
 
     @Override
     public com.android.internal.telephony.Connection getOriginalConnection() {
-        return mMockRadioConnection;
+        if (mIsImsConnection) {
+            return mImsPhoneConnection;
+        } else {
+            return mMockRadioConnection;
+        }
     }
 
     public TestTelephonyConnection() {
         super(null, null, false);
         MockitoAnnotations.initMocks(this);
 
+        mIsImsConnection = false;
         mMockPhone = mock(Phone.class);
         mMockContext = mock(Context.class);
         mOriginalConnection = mock(Connection.class);
+        mTelecomAccountRegistry = mock(TelecomAccountRegistry.class);
+
         // Set up mMockRadioConnection and mMockPhone to contain an active call
         when(mMockRadioConnection.getState()).thenReturn(Call.State.ACTIVE);
         when(mOriginalConnection.getState()).thenReturn(Call.State.ACTIVE);
@@ -100,11 +127,17 @@
         when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_IMS);
         when(mMockCall.getState()).thenReturn(Call.State.ACTIVE);
         when(mMockCall.getPhone()).thenReturn(mMockPhone);
-    }
-
-    @Override
-    public boolean isConferenceSupported() {
-        return true;
+        when(mMockPhone.getDefaultPhone()).thenReturn(mMockPhone);
+        when(mImsPhoneConnection.getImsCall()).thenReturn(mImsCall);
+        when(mTelecomAccountRegistry.isMergeCallSupported(notNull(PhoneAccountHandle.class)))
+                .thenReturn(mIsConferenceSupported);
+        when(mTelecomAccountRegistry.isMergeImsCallSupported(notNull(PhoneAccountHandle.class)))
+                .thenReturn(mIsImsConnection);
+        when(mTelecomAccountRegistry
+                .isVideoConferencingSupported(notNull(PhoneAccountHandle.class))).thenReturn(false);
+        when(mTelecomAccountRegistry
+                .isMergeOfWifiCallsAllowedWhenVoWifiOff(notNull(PhoneAccountHandle.class)))
+                .thenReturn(false);
     }
 
     public void setMockPhone(Phone newPhone) {
@@ -144,6 +177,13 @@
     }
 
     @Override
+    public void refreshConferenceSupported() {
+        if (mIsImsConnection) {
+            super.refreshConferenceSupported();
+        }
+    }
+
+    @Override
     public CharSequence getResourceText(int messageId) {
         return "TEST";
     }
@@ -154,8 +194,30 @@
     }
 
     @Override
-    void refreshConferenceSupported() {
-        // Requires ImsManager dependencies, do not implement during testing.
+    public void setConferenceSupported(boolean conferenceSupported) {
+        mIsConferenceSupported = conferenceSupported;
+    }
+
+    @Override
+    public boolean isConferenceSupported() {
+        return mIsConferenceSupported;
+    }
+
+    @Override
+    public TelecomAccountRegistry getTelecomAccountRegistry(Context context) {
+        return mTelecomAccountRegistry;
+    }
+
+    public void setIsVideoCall(boolean isVideoCall) {
+        if (isVideoCall) {
+            setVideoState(VideoProfile.STATE_TX_ENABLED);
+        } else {
+            setVideoState(VideoProfile.STATE_AUDIO_ONLY);
+        }
+    }
+
+    public void setWasVideoCall(boolean wasVideoCall) {
+        when(mImsCall.wasVideoCall()).thenReturn(wasVideoCall);
     }
 
     public int getNotifyPhoneAccountChangedCount() {
@@ -169,4 +231,19 @@
     public List<Bundle> getLastConnectionEventExtras() {
         return mLastConnectionEventExtras;
     }
+
+    public void setIsImsConnection(boolean isImsConnection) {
+        mIsImsConnection = isImsConnection;
+        when(mTelecomAccountRegistry.isMergeImsCallSupported(notNull(PhoneAccountHandle.class)))
+                .thenReturn(isImsConnection && mIsConferenceSupported);
+    }
+
+    public void setDownGradeVideoCall(boolean downgrade) {
+        PersistableBundle bundle = new PersistableBundle();
+        bundle.putBoolean(CarrierConfigManager.KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL,
+                downgrade);
+        when(mMockContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
+                .thenReturn(mCarrierConfigManager);
+        when(mCarrierConfigManager.getConfigForSubId(anyInt())).thenReturn(bundle);
+    }
 }