Use new KEY_ENABLE_PRESENCE_PUBLISH_BOOL in RcsService

Uses new KEY_ENABLE_PRESENCE_PUBLISH_BOOL in the RcsService to
bring up/down the presence service connection to the RCS stack.

If KEY_ENABLE_PRESENCE_PUBLISH_BOOL is disabled, the service
connection is completely brought down (contact capability
exchange will not work either).

Bug: 173115560
Test: change carrier config & ensure service is brought up/down
Merged-In: Ib4a52e5baef5a9467e02bb3d95b3a1df2c1d4b41
Change-Id: Ib4a52e5baef5a9467e02bb3d95b3a1df2c1d4b41
diff --git a/rcs/rcsservice/src/com/android/service/ims/RcsService.java b/rcs/rcsservice/src/com/android/service/ims/RcsService.java
index cbd4a8b..08fcaab 100644
--- a/rcs/rcsservice/src/com/android/service/ims/RcsService.java
+++ b/rcs/rcsservice/src/com/android/service/ims/RcsService.java
@@ -275,15 +275,12 @@
             return;
         }
         int defaultSub = RcsSettingUtils.getDefaultSubscriptionId(this);
-        if (!SubscriptionManager.isValidSubscriptionId(defaultSub)) {
-            mAssociatedSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
-            handleAssociatedSubscriptionChanged(mAssociatedSubscription);
-            return;
+        // If the presence SIP PUBLISH procedure is not supported, treat it as if there is no valid
+        // associated sub
+        if (!RcsSettingUtils.isPublishEnabled(this, defaultSub)) {
+            defaultSub = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         }
 
-        ImsMmTelManager imsManager = ImsMmTelManager.createForSubscriptionId(defaultSub);
-        ProvisioningManager provisioningManager =
-                ProvisioningManager.createForSubscriptionId(defaultSub);
         try {
             if (defaultSub == mAssociatedSubscription) {
                 // Don't register duplicate callbacks for the same subscription.
@@ -301,15 +298,23 @@
                         mProvisioningChangedCallback);
                 logger.print("callbacks unregistered for sub " + mAssociatedSubscription);
             }
-            // move over registrations.
-            imsManager.registerImsRegistrationCallback(getMainExecutor(), mImsRegistrationCallback);
-            imsManager.registerMmTelCapabilityCallback(getMainExecutor(), mCapabilityCallback);
-            provisioningManager.registerProvisioningChangedCallback(getMainExecutor(),
-                    mProvisioningChangedCallback);
-            mAssociatedSubscription = defaultSub;
+            if (SubscriptionManager.isValidSubscriptionId(defaultSub)) {
+                ImsMmTelManager imsManager = ImsMmTelManager.createForSubscriptionId(defaultSub);
+                ProvisioningManager provisioningManager =
+                        ProvisioningManager.createForSubscriptionId(defaultSub);
+                // move over registrations if the new sub id is valid.
+                imsManager.registerImsRegistrationCallback(getMainExecutor(),
+                        mImsRegistrationCallback);
+                imsManager.registerMmTelCapabilityCallback(getMainExecutor(), mCapabilityCallback);
+                provisioningManager.registerProvisioningChangedCallback(getMainExecutor(),
+                        mProvisioningChangedCallback);
+                mAssociatedSubscription = defaultSub;
+                logger.print("callbacks registered for sub " + mAssociatedSubscription);
+            } else {
+                mAssociatedSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+            }
             logger.print("registerImsCallbacksAndSetAssociatedSubscription: new default="
-                    + defaultSub);
-            logger.print("callbacks registered for sub " + mAssociatedSubscription);
+                    + mAssociatedSubscription);
             handleAssociatedSubscriptionChanged(mAssociatedSubscription);
         } catch (ImsException e) {
             logger.info("Couldn't register callbacks for " + defaultSub + ": "
diff --git a/rcs/rcsservice/src/com/android/service/ims/RcsSettingUtils.java b/rcs/rcsservice/src/com/android/service/ims/RcsSettingUtils.java
index 056e4d0..d39952f 100644
--- a/rcs/rcsservice/src/com/android/service/ims/RcsSettingUtils.java
+++ b/rcs/rcsservice/src/com/android/service/ims/RcsSettingUtils.java
@@ -134,6 +134,21 @@
         return isProvisioned;
     }
 
+    public static boolean isPublishEnabled(Context context, int subId) {
+        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            logger.debug("isPublishEnabled: no valid subscriptions!");
+            return false;
+        }
+        CarrierConfigManager configManager = (CarrierConfigManager)
+                context.getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        if (configManager != null) {
+            PersistableBundle config = configManager.getConfigForSubId(subId);
+            return (config != null) && config.getBoolean(
+                    CarrierConfigManager.Ims.KEY_ENABLE_PRESENCE_PUBLISH_BOOL, false);
+        }
+        return false;
+    }
+
     public static boolean hasUserEnabledContactDiscovery(Context context, int subId) {
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             logger.debug("hasUserEnabledContactDiscovery: no valid subscriptions!");