Remove key from database if download URL is not set

Test: atest FrameworksTelephonyTests
Fix: 172912885
Change-Id: Ieb7078af4be07edd1ca287c01cec6e2652fb3bf8
diff --git a/src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java b/src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java
index 9fa9509..4d57b4f 100644
--- a/src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java
+++ b/src/java/com/android/internal/telephony/CarrierKeyDownloadManager.java
@@ -197,6 +197,7 @@
         } else {
             // delete any existing alarms.
             cleanupRenewalAlarms();
+            mPhone.deleteCarrierInfoForImsiEncryption();
         }
     }
 
diff --git a/src/java/com/android/internal/telephony/GsmCdmaPhone.java b/src/java/com/android/internal/telephony/GsmCdmaPhone.java
index f64211b..2fe904e 100644
--- a/src/java/com/android/internal/telephony/GsmCdmaPhone.java
+++ b/src/java/com/android/internal/telephony/GsmCdmaPhone.java
@@ -1850,6 +1850,11 @@
     }
 
     @Override
+    public void deleteCarrierInfoForImsiEncryption() {
+        CarrierInfoManager.deleteCarrierInfoForImsiEncryption(mContext);
+    }
+
+    @Override
     public int getCarrierId() {
         return mCarrierResolver.getCarrierId();
     }
diff --git a/src/java/com/android/internal/telephony/Phone.java b/src/java/com/android/internal/telephony/Phone.java
index cc2e8a5..8bb388f 100644
--- a/src/java/com/android/internal/telephony/Phone.java
+++ b/src/java/com/android/internal/telephony/Phone.java
@@ -3677,6 +3677,13 @@
         return;
     }
 
+    /**
+     * Deletes all the keys for a given Carrier from the device keystore.
+     */
+    public void deleteCarrierInfoForImsiEncryption() {
+        return;
+    }
+
     public int getCarrierId() {
         return TelephonyManager.UNKNOWN_CARRIER_ID;
     }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java b/tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java
index 5a7f688..465b841 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/CarrierKeyDownloadMgrTest.java
@@ -284,6 +284,30 @@
     }
 
     /**
+     * Tests sending the ACTION_CARRIER_CONFIG_CHANGED intent with an empty key.
+     * Verify that the carrier keys are removed if IMSI_KEY_DOWNLOAD_URL_STRING is null.
+     */
+    @Test
+    @SmallTest
+    public void testCarrierConfigChangedEmptyKey() {
+        CarrierConfigManager carrierConfigManager = (CarrierConfigManager)
+                mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        int slotId = mPhone.getPhoneId();
+        PersistableBundle bundle = carrierConfigManager.getConfigForSubId(slotId);
+        bundle.putInt(CarrierConfigManager.IMSI_KEY_AVAILABILITY_INT, 3);
+        bundle.putString(CarrierConfigManager.IMSI_KEY_DOWNLOAD_URL_STRING, null);
+
+        Intent mIntent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
+        mIntent.putExtra(PhoneConstants.PHONE_KEY, 0);
+        mContext.sendBroadcast(mIntent);
+        processAllMessages();
+        SharedPreferences preferences = getDefaultSharedPreferences(mContext);
+        String mccMnc = preferences.getString("CARRIER_KEY_DM_MCC_MNC" + slotId, null);
+        assertEquals(null, mccMnc);
+        verify(mPhone).deleteCarrierInfoForImsiEncryption();
+    }
+
+    /**
      * Tests sending the INTENT_KEY_RENEWAL_ALARM_PREFIX intent.
      * Verify that the right mnc/mcc gets stored in the preferences.
      **/