Fix crash that occurs on devices that does not have telephony features.

If the device does not have telephony features, internal modules should not create or initialize.

Bug: 323431082
Test: verify cf_x86_64_auto-trunk_staging-userdebug,
      tangorpro-trunk_staging-userdebug on boot time.
Test: manual test with tangor
      wifi on/off, bluetooth on/off, capture image, video

Change-Id: Ie079f1f7005efc23132bba95337d65d7f07b8452
diff --git a/src/com/android/phone/PhoneApp.java b/src/com/android/phone/PhoneApp.java
index df151bf..bb663dc 100644
--- a/src/com/android/phone/PhoneApp.java
+++ b/src/com/android/phone/PhoneApp.java
@@ -38,7 +38,11 @@
             mPhoneGlobals = new PhoneGlobals(this);
             mPhoneGlobals.onCreate();
 
-            TelecomAccountRegistry.getInstance(this).setupOnBoot();
+            TelecomAccountRegistry telecomAccountRegistry =
+                    TelecomAccountRegistry.getInstance(this);
+            if (telecomAccountRegistry != null) {
+                telecomAccountRegistry.setupOnBoot();
+            }
         }
     }
 }
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 7fba651..cbbc889 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -241,14 +241,14 @@
     private final CarrierVvmPackageInstalledReceiver mCarrierVvmPackageInstalledReceiver =
             new CarrierVvmPackageInstalledReceiver();
 
-    private final SettingsObserver mSettingsObserver;
+    private SettingsObserver mSettingsObserver;
     private BinderCallsStats.SettingsObserver mBinderCallsSettingsObserver;
 
     // Mapping of phone ID to the associated TelephonyCallback. These should be registered without
     // fine or coarse location since we only use ServiceState for
     private PhoneAppCallback[] mTelephonyCallbacks;
 
-    private FeatureFlags mFeatureFlags;
+    private FeatureFlags mFeatureFlags = new FeatureFlagsImpl();
 
     private class PhoneAppCallback extends TelephonyCallback implements
             TelephonyCallback.ServiceStateListener {
@@ -462,7 +462,13 @@
     public PhoneGlobals(Context context) {
         super(context);
         sMe = this;
-        mSettingsObserver = new SettingsObserver(context, mHandler);
+        if (mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()) {
+            if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
+                mSettingsObserver = new SettingsObserver(context, mHandler);
+            }
+        } else {
+            mSettingsObserver = new SettingsObserver(context, mHandler);
+        }
     }
 
     public void onCreate() {
@@ -470,6 +476,13 @@
 
         ContentResolver resolver = getContentResolver();
 
+        if (mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()) {
+            if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
+                Log.v(LOG_TAG, "onCreate()... but not defined FEATURE_TELEPHONY");
+                return;
+            }
+        }
+
         // Initialize the shim from frameworks/opt/telephony into packages/services/Telephony.
         TelephonyLocalConnection.setInstance(new LocalConnectionImpl(this));
 
@@ -499,7 +512,6 @@
             DomainSelectionResolver.make(this, dssComponentName);
 
             // Initialize the telephony framework
-            mFeatureFlags = new FeatureFlagsImpl();
             PhoneFactory.makeDefaultPhones(this, mFeatureFlags);
 
             // Initialize the DomainSelectionResolver after creating the Phone instance
diff --git a/src/com/android/phone/security/SafetySourceReceiver.java b/src/com/android/phone/security/SafetySourceReceiver.java
index c846c43..feb1c31 100644
--- a/src/com/android/phone/security/SafetySourceReceiver.java
+++ b/src/com/android/phone/security/SafetySourceReceiver.java
@@ -22,7 +22,9 @@
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 
+import com.android.internal.telephony.flags.Flags;
 import com.android.phone.PhoneGlobals;
 
 public final class SafetySourceReceiver extends BroadcastReceiver {
@@ -39,6 +41,12 @@
             return;
         }
 
-        PhoneGlobals.getPhone().refreshSafetySources(refreshBroadcastId);
+        if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
+            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
+                PhoneGlobals.getPhone().refreshSafetySources(refreshBroadcastId);
+            }
+        } else {
+            PhoneGlobals.getPhone().refreshSafetySources(refreshBroadcastId);
+        }
     }
 }
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index efa5278..a8c6c10 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -1258,7 +1258,17 @@
      */
     public static synchronized TelecomAccountRegistry getInstance(Context context) {
         if (sInstance == null && context != null) {
-            sInstance = new TelecomAccountRegistry(context);
+            if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
+                PackageManager pm = context.getPackageManager();
+                if (pm != null && pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
+                        && pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CALLING)) {
+                    sInstance = new TelecomAccountRegistry(context);
+                } else {
+                    Log.i(LOG_TAG, "getInstance: Telephony features required");
+                }
+            } else {
+                sInstance = new TelecomAccountRegistry(context);
+            }
         }
         return sInstance;
     }