Handle the cases when RCS config is null

1. The RCS config should be reset when notifyRcsAutoConfigurationRemoved
is called.
2. When RCS config is null, notifyRcsAutoConfigurationReceived should
not been called.

Bug: 176443559
Test: atest TeleServiceTests:com.android.phone.RcsProvisioningMonitorTest
Change-Id: Iefda454f5a87338b66cd3acef6b8b6184c4001df
diff --git a/src/com/android/phone/RcsProvisioningMonitor.java b/src/com/android/phone/RcsProvisioningMonitor.java
index 8e4d35e..98003bd 100644
--- a/src/com/android/phone/RcsProvisioningMonitor.java
+++ b/src/com/android/phone/RcsProvisioningMonitor.java
@@ -333,6 +333,7 @@
             mConfigs.forEach((k, v) -> {
                 if (isAcsUsed(k)) {
                     logv("acs used, trigger to re-configure.");
+                    mConfigs.put(k, null);
                     notifyRcsAutoConfigurationRemoved(k);
                     triggerRcsReconfiguration(k);
                 } else {
@@ -345,6 +346,11 @@
 
     private void notifyRcsAutoConfigurationReceived(int subId, byte[] config,
             boolean isCompressed) {
+        if (config == null) {
+            logd("Rcs config is null for sub : " + subId);
+            return;
+        }
+
         IImsConfig imsConfig = getIImsConfig(subId, ImsFeature.FEATURE_RCS);
         if (imsConfig != null) {
             try {
@@ -358,6 +364,7 @@
     }
 
     private void notifyRcsAutoConfigurationRemoved(int subId) {
+        RcsConfig.updateConfigForSub(mPhone, subId, null, true);
         IImsConfig imsConfig = getIImsConfig(subId, ImsFeature.FEATURE_RCS);
         if (imsConfig != null) {
             try {
@@ -461,8 +468,7 @@
 
     private void onReconfigRequest(int subId) {
         logv("onReconfigRequest, subId:" + subId);
-        mConfigs.remove(subId);
-        RcsConfig.updateConfigForSub(mPhone, subId, null, true);
+        mConfigs.put(subId, null);
         notifyRcsAutoConfigurationRemoved(subId);
         triggerRcsReconfiguration(subId);
     }
diff --git a/tests/src/com/android/phone/RcsProvisioningMonitorTest.java b/tests/src/com/android/phone/RcsProvisioningMonitorTest.java
index 7ef9768..7c06507 100644
--- a/tests/src/com/android/phone/RcsProvisioningMonitorTest.java
+++ b/tests/src/com/android/phone/RcsProvisioningMonitorTest.java
@@ -29,6 +29,7 @@
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -156,6 +157,7 @@
 
     private class SimInfoContentProvider extends MockContentProvider {
         private Cursor mCursor;
+        private ContentValues mValues;
 
         SimInfoContentProvider(Context context) {
             super(context);
@@ -174,8 +176,13 @@
         @Override
         public int update(Uri uri, ContentValues values,
                 String selection, String[] selectionArgs) {
+            mValues = values;
             return 1;
         }
+
+        ContentValues getContentValues() {
+            return mValues;
+        }
     }
 
     @Before
@@ -272,7 +279,7 @@
 
     @Test
     @SmallTest
-    public void testInit() throws Exception {
+    public void testInitWithSavedConfig() throws Exception {
         createMonitor(3);
         ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
         for (int i = 0; i < 3; i++) {
@@ -291,6 +298,21 @@
 
     @Test
     @SmallTest
+    public void testInitWithoutSavedConfig() throws Exception {
+        when(mCursor.getBlob(anyInt())).thenReturn(null);
+        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
+        createMonitor(3);
+
+        verify(mPhone, times(3)).sendBroadcast(captorIntent.capture());
+        Intent capturedIntent = captorIntent.getAllValues().get(1);
+        assertEquals(ProvisioningManager.ACTION_RCS_SINGLE_REGISTRATION_CAPABILITY_UPDATE,
+                capturedIntent.getAction());
+        //Should not notify null config
+        verify(mIImsConfig, never()).notifyRcsAutoConfigurationReceived(any(), anyBoolean());
+    }
+
+    @Test
+    @SmallTest
     public void testSubInfoChanged() throws Exception {
         createMonitor(3);
         ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
@@ -318,17 +340,37 @@
 
     @Test
     @SmallTest
-    public void testDefaultMessagingApplicationChanged() throws Exception {
+    public void testDefaultMessagingApplicationChangedWithAcs() throws Exception {
+        createMonitor(1);
+        updateDefaultMessageApplication(DEFAULT_MESSAGING_APP2);
+        mBundle.putBoolean(CarrierConfigManager.KEY_USE_ACS_FOR_RCS_BOOL, true);
+        processAllMessages();
+        byte[] configCached = mRcsProvisioningMonitor.getConfig(FAKE_SUB_ID_BASE);
+
+        assertNull(configCached);
+        assertNull(mProvider.getContentValues().get(SimInfo.COLUMN_RCS_CONFIG));
+        verify(mIImsConfig, atLeastOnce()).notifyRcsAutoConfigurationRemoved();
+        verify(mIImsConfig, atLeastOnce()).triggerRcsReconfiguration();
+        // The api should only be called when monitor is initilized.
+        verify(mIImsConfig, times(1))
+                .notifyRcsAutoConfigurationReceived(any(), anyBoolean());
+    }
+
+    @Test
+    @SmallTest
+    public void testDefaultMessagingApplicationChangedWithoutAcs() throws Exception {
         createMonitor(1);
         updateDefaultMessageApplication(DEFAULT_MESSAGING_APP2);
         mBundle.putBoolean(CarrierConfigManager.KEY_USE_ACS_FOR_RCS_BOOL, false);
         processAllMessages();
-        verify(mIImsConfig, atLeastOnce())
+        byte[] configCached = mRcsProvisioningMonitor.getConfig(FAKE_SUB_ID_BASE);
+
+        assertTrue(Arrays.equals(SAMPLE_CONFIG.getBytes(), configCached));
+        verify(mIImsConfig, never()).notifyRcsAutoConfigurationRemoved();
+        // The api should be called 2 times, one happens when monitor is initilized,
+        // Another happens when DMS is changed.
+        verify(mIImsConfig, times(2))
                 .notifyRcsAutoConfigurationReceived(any(), anyBoolean());
-        mBundle.putBoolean(CarrierConfigManager.KEY_USE_ACS_FOR_RCS_BOOL, true);
-        updateDefaultMessageApplication(DEFAULT_MESSAGING_APP1);
-        processAllMessages();
-        verify(mIImsConfig, atLeastOnce()).triggerRcsReconfiguration();
     }
 
     @Test
@@ -494,7 +536,6 @@
         ((Map<String, IBinder>) field.get(null)).put(serviceName, binder);
     }
 
-
     private void processAllMessages() {
         while (!mLooper.getLooper().getQueue().isIdle()) {
             mLooper.processAllMessages();