Support carrier config for NI SUPL message injection

Test: on-device

1. Insert SIM
2. Enable flag android.location.flags.enable_ni_supl_message_injection_by_carrier_config
3. adb shell cmd phone cc set-value gps.enable_ni_supl_message_injection_bool true
4. Enable NFW lock
5. atest SmsNiSuplTest
6. Confirm the SUPL message is injected to HAL
7. adb shell cmd phone cc set-value gps.enable_ni_supl_message_injection_bool false
8. atest SmsNiSuplTest
9. Confirm no SUPL message is injected to HAL

Bug: 242105192
Flag: android.location.flags.enable_ni_supl_message_injection_by_carrier_config
Change-Id: I86a86517db139137a16e76bff2b820aad447a734
diff --git a/core/api/current.txt b/core/api/current.txt
index c7df662..2816f59 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -44068,6 +44068,7 @@
   }
 
   public static final class CarrierConfigManager.Gps {
+    field @FlaggedApi("android.location.flags.enable_ni_supl_message_injection_by_carrier_config") public static final String KEY_ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL = "gps.enable_ni_supl_message_injection_bool";
     field public static final String KEY_PERSIST_LPP_MODE_BOOL = "gps.persist_lpp_mode_bool";
     field public static final String KEY_PREFIX = "gps.";
   }
diff --git a/location/java/android/location/flags/location.aconfig b/location/java/android/location/flags/location.aconfig
index 0edaaef..ec33eb5 100644
--- a/location/java/android/location/flags/location.aconfig
+++ b/location/java/android/location/flags/location.aconfig
@@ -102,3 +102,11 @@
     description: "Flag for GNSS configuration from resource"
     bug: "317734846"
 }
+
+flag {
+    name: "enable_ni_supl_message_injection_by_carrier_config"
+    namespace: "location"
+    description: "Flag for enabling NI SUPL message injection by carrier config"
+    bug: "242105192"
+    is_fixed_read_only: true
+}
diff --git a/services/core/java/com/android/server/location/gnss/GnssConfiguration.java b/services/core/java/com/android/server/location/gnss/GnssConfiguration.java
index a439f16..1740010 100644
--- a/services/core/java/com/android/server/location/gnss/GnssConfiguration.java
+++ b/services/core/java/com/android/server/location/gnss/GnssConfiguration.java
@@ -80,8 +80,8 @@
             "ENABLE_PSDS_PERIODIC_DOWNLOAD";
     private static final String CONFIG_ENABLE_ACTIVE_SIM_EMERGENCY_SUPL =
             "ENABLE_ACTIVE_SIM_EMERGENCY_SUPL";
-    private static final String CONFIG_ENABLE_NI_SUPL_MESSAGE_INJECTION =
-            "ENABLE_NI_SUPL_MESSAGE_INJECTION";
+    private static final String CONFIG_ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL =
+            "ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL";
     static final String CONFIG_LONGTERM_PSDS_SERVER_1 = "LONGTERM_PSDS_SERVER_1";
     static final String CONFIG_LONGTERM_PSDS_SERVER_2 = "LONGTERM_PSDS_SERVER_2";
     static final String CONFIG_LONGTERM_PSDS_SERVER_3 = "LONGTERM_PSDS_SERVER_3";
@@ -230,7 +230,8 @@
      * Default false if not set.
      */
     boolean isNiSuplMessageInjectionEnabled() {
-        return getBooleanConfig(CONFIG_ENABLE_NI_SUPL_MESSAGE_INJECTION, false);
+        return getBooleanConfig(CONFIG_ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL,
+                false);
     }
 
     /**
diff --git a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
index 1938150..4b2c12a 100644
--- a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
+++ b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
@@ -68,6 +68,7 @@
 import android.location.LocationRequest;
 import android.location.LocationResult;
 import android.location.LocationResult.BadLocationException;
+import android.location.flags.Flags;
 import android.location.provider.ProviderProperties;
 import android.location.provider.ProviderRequest;
 import android.location.util.identity.CallerIdentity;
@@ -310,6 +311,7 @@
     private String mC2KServerHost;
     private int mC2KServerPort;
     private boolean mSuplEsEnabled = false;
+    private boolean mNiSuplMessageListenerRegistered = false;
 
     private final LocationExtras mLocationExtras = new LocationExtras();
     private final NetworkTimeHelper mNetworkTimeHelper;
@@ -387,6 +389,10 @@
             // Reload gnss config for no SIM case
             mGnssConfiguration.reloadGpsProperties();
         }
+        if (Flags.enableNiSuplMessageInjectionByCarrierConfig()) {
+            updateNiSuplMessageListenerRegistration(
+                    mGnssConfiguration.isNiSuplMessageInjectionEnabled());
+        }
     }
 
     private void reloadGpsProperties() {
@@ -532,28 +538,9 @@
         intentFilter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
         mContext.registerReceiver(mIntentReceiver, intentFilter, null, mHandler);
 
-        if (mNetworkConnectivityHandler.isNativeAgpsRilSupported()
-                && mGnssConfiguration.isNiSuplMessageInjectionEnabled()) {
-            // Listen to WAP PUSH NI SUPL message.
-            // See User Plane Location Protocol Candidate Version 3.0,
-            // OMA-TS-ULP-V3_0-20110920-C, Section 8.3 OMA Push.
-            intentFilter = new IntentFilter();
-            intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION);
-            try {
-                intentFilter.addDataType("application/vnd.omaloc-supl-init");
-            } catch (IntentFilter.MalformedMimeTypeException e) {
-                Log.w(TAG, "Malformed SUPL init mime type");
-            }
-            mContext.registerReceiver(mIntentReceiver, intentFilter, null, mHandler);
-
-            // Listen to MT SMS NI SUPL message.
-            // See User Plane Location Protocol Candidate Version 3.0,
-            // OMA-TS-ULP-V3_0-20110920-C, Section 8.4 MT SMS.
-            intentFilter = new IntentFilter();
-            intentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION);
-            intentFilter.addDataScheme("sms");
-            intentFilter.addDataAuthority("localhost", "7275");
-            mContext.registerReceiver(mIntentReceiver, intentFilter, null, mHandler);
+        if (!Flags.enableNiSuplMessageInjectionByCarrierConfig()) {
+            updateNiSuplMessageListenerRegistration(
+                    mGnssConfiguration.isNiSuplMessageInjectionEnabled());
         }
 
         mNetworkConnectivityHandler.registerNetworkCallbacks();
@@ -592,6 +579,20 @@
                 case TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED:
                     subscriptionOrCarrierConfigChanged();
                     break;
+            }
+        }
+    };
+
+    private BroadcastReceiver mNiSuplIntentReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            String action = intent.getAction();
+            if (DEBUG) Log.d(TAG, "receive broadcast intent, action: " + action);
+            if (action == null) {
+                return;
+            }
+
+            switch (action) {
                 case Intents.WAP_PUSH_RECEIVED_ACTION:
                 case Intents.DATA_SMS_RECEIVED_ACTION:
                     injectSuplInit(intent);
@@ -1442,6 +1443,46 @@
         mGnssMetrics.logSvStatus(gnssStatus);
     }
 
+    private void updateNiSuplMessageListenerRegistration(boolean shouldRegister) {
+        if (!mNetworkConnectivityHandler.isNativeAgpsRilSupported()) {
+            return;
+        }
+        if (mNiSuplMessageListenerRegistered == shouldRegister) {
+            return;
+        }
+
+        // WAP PUSH NI SUPL message intent filter.
+        // See User Plane Location Protocol Candidate Version 3.0,
+        // OMA-TS-ULP-V3_0-20110920-C, Section 8.3 OMA Push.
+        IntentFilter wapPushNiIntentFilter = new IntentFilter();
+        wapPushNiIntentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION);
+        try {
+            wapPushNiIntentFilter
+                .addDataType("application/vnd.omaloc-supl-init");
+        } catch (IntentFilter.MalformedMimeTypeException e) {
+            Log.w(TAG, "Malformed SUPL init mime type");
+        }
+
+        // MT SMS NI SUPL message intent filter.
+        // See User Plane Location Protocol Candidate Version 3.0,
+        // OMA-TS-ULP-V3_0-20110920-C, Section 8.4 MT SMS.
+        IntentFilter mtSmsNiIntentFilter = new IntentFilter();
+        mtSmsNiIntentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION);
+        mtSmsNiIntentFilter.addDataScheme("sms");
+        mtSmsNiIntentFilter.addDataAuthority("localhost", "7275");
+
+        if (shouldRegister) {
+            mContext.registerReceiver(mNiSuplIntentReceiver,
+                    wapPushNiIntentFilter, null, mHandler);
+            mContext.registerReceiver(mNiSuplIntentReceiver,
+                    mtSmsNiIntentFilter, null, mHandler);
+            mNiSuplMessageListenerRegistered = true;
+        } else {
+            mContext.unregisterReceiver(mNiSuplIntentReceiver);
+            mNiSuplMessageListenerRegistered = false;
+        }
+    }
+
     private void restartLocationRequest() {
         if (DEBUG) Log.d(TAG, "restartLocationRequest");
         setStarted(false);
@@ -1631,6 +1672,10 @@
         if (dumpAll) {
             mNetworkTimeHelper.dump(pw);
             pw.println("mSupportsPsds=" + mSupportsPsds);
+            if (Flags.enableNiSuplMessageInjectionByCarrierConfig()) {
+                pw.println("mNiSuplMessageListenerRegistered="
+                        + mNiSuplMessageListenerRegistered);
+            }
             pw.println(
                     "PsdsServerConfigured=" + mGnssConfiguration.isLongTermPsdsServerConfigured());
             pw.println("native internal state: ");
diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java
index afd5720..287c87f 100644
--- a/telephony/java/android/telephony/CarrierConfigManager.java
+++ b/telephony/java/android/telephony/CarrierConfigManager.java
@@ -5029,6 +5029,18 @@
         public static final String KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY =
                 KEY_PREFIX + "es_supl_data_plane_only_roaming_plmn_string_array";
 
+        /**
+         * Determine whether to enable Net Initiated SUPL (NI SUPL) message injection.
+         * If enabled, the GnssLocationProvider will monitor for WAP PUSH or MT SMS NI SUPL intents
+         * and subsequently inject the NI SUPL packet into the GNSS HAL.
+         * {@code false} - Disable NI SUPL message injection. This is default.
+         * {@code true} - Enable NI SUPL message injection.
+         */
+        @FlaggedApi(android.location.flags.Flags
+                .FLAG_ENABLE_NI_SUPL_MESSAGE_INJECTION_BY_CARRIER_CONFIG)
+        public static final String KEY_ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL =
+                KEY_PREFIX + "enable_ni_supl_message_injection_bool";
+
         private static PersistableBundle getDefaults() {
             PersistableBundle defaults = new PersistableBundle();
             defaults.putBoolean(KEY_PERSIST_LPP_MODE_BOOL, true);
@@ -5046,6 +5058,9 @@
             defaults.putInt(KEY_ES_SUPL_CONTROL_PLANE_SUPPORT_INT,
                     SUPL_EMERGENCY_MODE_TYPE_CP_ONLY);
             defaults.putStringArray(KEY_ES_SUPL_DATA_PLANE_ONLY_ROAMING_PLMN_STRING_ARRAY, null);
+            if (android.location.flags.Flags.enableNiSuplMessageInjectionByCarrierConfig()) {
+                defaults.putBoolean(KEY_ENABLE_NI_SUPL_MESSAGE_INJECTION_BOOL, false);
+            }
             return defaults;
         }
     }