Do not send notification with invalid carrierName

Bug: 169198295
Test: atest com.android.server.wifi
Change-Id: I0fd9d01974e3470feaba858e6b2071a446f40155
diff --git a/service/java/com/android/server/wifi/WifiCarrierInfoManager.java b/service/java/com/android/server/wifi/WifiCarrierInfoManager.java
index 047f913..0426dba 100644
--- a/service/java/com/android/server/wifi/WifiCarrierInfoManager.java
+++ b/service/java/com/android/server/wifi/WifiCarrierInfoManager.java
@@ -1429,6 +1429,10 @@
 
     private void sendImsiPrivacyNotification(int carrierId) {
         String carrierName = getCarrierNameforSubId(getMatchingSubId(carrierId));
+        if (carrierName == null) {
+            // If carrier name could not be retrieved, do not send notification.
+            return;
+        }
         Notification.Action userAllowAppNotificationAction =
                 new Notification.Action.Builder(null,
                         mResources.getText(R.string
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiCarrierInfoManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiCarrierInfoManagerTest.java
index 750e8d4..caa22a6 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiCarrierInfoManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiCarrierInfoManagerTest.java
@@ -1766,6 +1766,25 @@
         validateImsiProtectionNotification(CARRIER_NAME);
     }
 
+    @Test
+    public void testImsiProtectionExemptionNotificationNotSentWhenCarrierNameIsInvalid() {
+        when(mCarrierConfigManager.getConfigForSubId(DATA_SUBID))
+                .thenReturn(generateTestCarrierConfig(false));
+        ArgumentCaptor<BroadcastReceiver> receiver =
+                ArgumentCaptor.forClass(BroadcastReceiver.class);
+        verify(mContext).registerReceiver(receiver.capture(), any(IntentFilter.class));
+
+        receiver.getValue().onReceive(mContext,
+                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
+        assertFalse(mWifiCarrierInfoManager.requiresImsiEncryption(DATA_SUBID));
+        when(mDataTelephonyManager.getSimCarrierIdName()).thenReturn(null);
+        mWifiCarrierInfoManager.sendImsiProtectionExemptionNotificationIfRequired(DATA_CARRIER_ID);
+        verify(mNotificationManger, never()).notify(
+                eq(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE),
+                eq(mNotification));
+
+    }
+
     private void validateImsiProtectionNotification(String carrierName) {
         verify(mNotificationManger, atLeastOnce()).notify(
                 eq(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE),