Handle null phone instances

Safety center may send broadcasts out before default phone instances are
created. We do nothing in that case. Also do nothing if the features
this receiver supports are disabled.

Test: m # (new unit tests coming in a follow up)
Bug: 323624127
Change-Id: I6138820d5187a5195a855e99263a0e35f1c8462f
diff --git a/src/com/android/phone/security/SafetySourceReceiver.java b/src/com/android/phone/security/SafetySourceReceiver.java
index feb1c31..f778d41 100644
--- a/src/com/android/phone/security/SafetySourceReceiver.java
+++ b/src/com/android/phone/security/SafetySourceReceiver.java
@@ -24,12 +24,21 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 
+import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.flags.Flags;
 import com.android.phone.PhoneGlobals;
 
 public final class SafetySourceReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
+
+        // If none of the features that depend on this receiver are enabled, there's no reason
+        // to progress.
+        if (!Flags.enableIdentifierDisclosureTransparencyUnsolEvents()
+                || !Flags.enableModemCipherTransparencyUnsolEvents()) {
+            return;
+        }
+
         String action = intent.getAction();
         if (!ACTION_REFRESH_SAFETY_SOURCES.equals(action)) {
             return;
@@ -43,10 +52,20 @@
 
         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
             if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
-                PhoneGlobals.getPhone().refreshSafetySources(refreshBroadcastId);
+                refreshSafetySources(refreshBroadcastId);
             }
         } else {
-            PhoneGlobals.getPhone().refreshSafetySources(refreshBroadcastId);
+            refreshSafetySources(refreshBroadcastId);
         }
     }
+
+    private void refreshSafetySources(String refreshBroadcastId) {
+        Phone phone = PhoneGlobals.getPhone();
+        // It's possible that phones have not been created yet. Safety center may send a refresh
+        // broadcast very early on.
+        if (phone != null) {
+            phone.refreshSafetySources(refreshBroadcastId);
+        }
+
+    }
 }