Merge "Fix IMS conference merge failed"
diff --git a/src/java/com/android/ims/ImsManager.java b/src/java/com/android/ims/ImsManager.java
index 5996c9a..cad8283 100644
--- a/src/java/com/android/ims/ImsManager.java
+++ b/src/java/com/android/ims/ImsManager.java
@@ -19,6 +19,7 @@
 import android.annotation.Nullable;
 import android.app.PendingIntent;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.HandlerExecutor;
@@ -308,8 +309,14 @@
          * Start the creation of a connection to the underlying ImsService implementation. When the
          * service is connected, {@link Listener#connectionReady(ImsManager)} will be called with
          * an active ImsManager instance.
+         *
+         * If this device does not support an ImsStack (i.e. doesn't support
+         * {@link PackageManager#FEATURE_TELEPHONY_IMS} feature), this method will do nothing.
          */
         public void connect() {
+            if (!ImsManager.isImsSupportedOnDevice(mContext)) {
+                return;
+            }
             mRetryCount = 0;
             // Send a message to connect to the Ims Service and open a connection through
             // getImsService().
@@ -448,6 +455,10 @@
         }
     }
 
+    public static boolean isImsSupportedOnDevice(Context context) {
+        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS);
+    }
+
     /**
      * Returns the user configuration of Enhanced 4G LTE Mode setting.
      *
@@ -1084,13 +1095,18 @@
             }
             if (DBG) log("getWfcMode - setting=" + setting);
         } else {
-            // The WFC roaming mode is set in the Settings UI to be the same as the WFC mode if the
-            // roaming mode is set to not "editable" (see
-            // CarrierConfigManager.KEY_EDITABLE_WFC_ROAMING_MODE_BOOL for explanation), so can't
-            // override those settings here by setting the WFC roaming mode to default, like above.
-            setting = getSettingFromSubscriptionManager(
-                    SubscriptionManager.WFC_IMS_ROAMING_MODE,
-                    CarrierConfigManager.KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT);
+            if (getBooleanCarrierConfig(
+                    CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)) {
+                setting = getWfcMode(false);
+            } else if (!getBooleanCarrierConfig(
+                    CarrierConfigManager.KEY_EDITABLE_WFC_ROAMING_MODE_BOOL)) {
+                setting = getIntCarrierConfig(
+                        CarrierConfigManager.KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT);
+            } else {
+                setting = getSettingFromSubscriptionManager(
+                        SubscriptionManager.WFC_IMS_ROAMING_MODE,
+                        CarrierConfigManager.KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT);
+            }
             if (DBG) log("getWfcMode (roaming) - setting=" + setting);
         }
         return setting;
@@ -2364,6 +2380,10 @@
      */
     private void checkAndThrowExceptionIfServiceUnavailable()
             throws ImsException {
+        if (!isImsSupportedOnDevice(mContext)) {
+            throw new ImsException("IMS not supported on device.",
+                    ImsReasonInfo.CODE_LOCAL_IMS_NOT_SUPPORTED_ON_DEVICE);
+        }
         if (mMmTelFeatureConnection == null || !mMmTelFeatureConnection.isBinderAlive()) {
             createImsService();
 
@@ -2753,6 +2773,7 @@
 
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         pw.println("ImsManager:");
+        pw.println("  device supports IMS = " + isImsSupportedOnDevice(mContext));
         pw.println("  mPhoneId = " + mPhoneId);
         pw.println("  mConfigUpdated = " + mConfigUpdated);
         pw.println("  mImsServiceProxy = " + mMmTelFeatureConnection);
diff --git a/src/java/com/android/ims/MmTelFeatureConnection.java b/src/java/com/android/ims/MmTelFeatureConnection.java
index 86ef876..50dc91e 100644
--- a/src/java/com/android/ims/MmTelFeatureConnection.java
+++ b/src/java/com/android/ims/MmTelFeatureConnection.java
@@ -16,6 +16,7 @@
 
 package com.android.ims;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.Context;
 import android.os.Handler;
@@ -100,7 +101,7 @@
                         Log.w(TAG, "onSubscriptionsChanged: could not find SubscriptionManager.");
                         return;
                     }
-                    List<SubscriptionInfo> subInfos = manager.getActiveSubscriptionInfoList();
+                    List<SubscriptionInfo> subInfos = manager.getActiveSubscriptionInfoList(false);
                     if (subInfos == null) {
                         subInfos = Collections.emptyList();
                     }
@@ -297,7 +298,7 @@
             IImsRegistration imsRegistration = getRegistration();
             if (imsRegistration != null) {
                 try {
-                    getRegistration().addRegistrationCallback(localCallback);
+                    imsRegistration.addRegistrationCallback(localCallback);
                 } catch (RemoteException e) {
                     throw new IllegalStateException("ImsRegistrationCallbackAdapter: MmTelFeature"
                             + " binder is dead.");
@@ -314,7 +315,7 @@
             IImsRegistration imsRegistration = getRegistration();
             if (imsRegistration != null) {
                 try {
-                    getRegistration().removeRegistrationCallback(localCallback);
+                    imsRegistration.removeRegistrationCallback(localCallback);
                 } catch (RemoteException e) {
                     Log.w(TAG, "ImsRegistrationCallbackAdapter - unregisterCallback: couldn't"
                             + " remove registration callback");
@@ -429,6 +430,7 @@
     private final Object mLock = new Object();
     // Updated by IImsServiceFeatureCallback when FEATURE_EMERGENCY_MMTEL is sent.
     private boolean mSupportsEmergencyCalling = false;
+    private static boolean sImsSupportedOnDevice = true;
 
     // Cache the Registration and Config interfaces as long as the MmTel feature is connected. If
     // it becomes disconnected, invalidate.
@@ -451,8 +453,13 @@
     private final CapabilityCallbackManager mCapabilityCallbackManager;
     private final ProvisioningCallbackManager mProvisioningCallbackManager;
 
-    public static MmTelFeatureConnection create(Context context , int slotId) {
+    public static @NonNull MmTelFeatureConnection create(Context context , int slotId) {
         MmTelFeatureConnection serviceProxy = new MmTelFeatureConnection(context, slotId);
+        if (!ImsManager.isImsSupportedOnDevice(context)) {
+            // Return empty service proxy in the case that IMS is not supported.
+            sImsSupportedOnDevice = false;
+            return serviceProxy;
+        }
 
         TelephonyManager tm  = getTelephonyManager(context);
         if (tm == null) {
@@ -946,7 +953,10 @@
         return mIsAvailable && mBinder != null && mBinder.isBinderAlive();
     }
 
-    protected void checkServiceIsReady() throws RemoteException {
+    private void checkServiceIsReady() throws RemoteException {
+        if (!sImsSupportedOnDevice) {
+            throw new RemoteException("IMS is not supported on this device.");
+        }
         if (!isBinderReady()) {
             throw new RemoteException("ImsServiceProxy is not ready to accept commands.");
         }
@@ -955,10 +965,4 @@
     private IImsMmTelFeature getServiceInterface(IBinder b) {
         return IImsMmTelFeature.Stub.asInterface(b);
     }
-
-    protected void checkBinderConnection() throws RemoteException {
-        if (!isBinderAlive()) {
-            throw new RemoteException("ImsServiceProxy is not available for that feature.");
-        }
-    }
 }