Merge "Copy wifiCalling method from telephony to settings"
diff --git a/src/com/android/settings/mobilenetwork/MobileNetworkUtils.java b/src/com/android/settings/mobilenetwork/MobileNetworkUtils.java
index 44b1cef..d5ab909 100644
--- a/src/com/android/settings/mobilenetwork/MobileNetworkUtils.java
+++ b/src/com/android/settings/mobilenetwork/MobileNetworkUtils.java
@@ -22,6 +22,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.database.Cursor;
 import android.os.PersistableBundle;
 import android.os.SystemProperties;
@@ -36,6 +37,8 @@
 import android.text.TextUtils;
 import android.util.Log;
 
+import androidx.annotation.VisibleForTesting;
+
 import com.android.ims.ImsException;
 import com.android.ims.ImsManager;
 
@@ -54,6 +57,8 @@
     // the default value is false.
     private static final String KEY_ENABLE_ESIM_UI_BY_DEFAULT =
             "esim.enable_esim_system_ui_by_default";
+    private static final String LEGACY_ACTION_CONFIGURE_PHONE_ACCOUNT =
+            "android.telecom.action.CONNECTION_SERVICE_CONFIGURE";
 
     /**
      * Returns if DPC APNs are enforced.
@@ -91,11 +96,10 @@
 
         boolean isWifiCallingEnabled;
         if (simCallManager != null) {
-            //TODO(b/114749736): build intent to query wifi calling feature
-            final Intent intent = null;
-            PackageManager pm = context.getPackageManager();
-            isWifiCallingEnabled = intent != null
-                    && !pm.queryIntentActivities(intent, 0 /* flags */).isEmpty();
+            Intent intent = buildPhoneAccountConfigureIntent(
+                    context, simCallManager);
+
+            isWifiCallingEnabled = intent != null;
         } else {
             ImsManager imsMgr = ImsManager.getInstance(context, phoneId);
             isWifiCallingEnabled = imsMgr != null
@@ -107,6 +111,43 @@
         return isWifiCallingEnabled;
     }
 
+    @VisibleForTesting
+    static Intent buildPhoneAccountConfigureIntent(
+            Context context, PhoneAccountHandle accountHandle) {
+        Intent intent = buildConfigureIntent(
+                context, accountHandle, TelecomManager.ACTION_CONFIGURE_PHONE_ACCOUNT);
+
+        if (intent == null) {
+            // If the new configuration didn't work, try the old configuration intent.
+            intent = buildConfigureIntent(context, accountHandle,
+                    LEGACY_ACTION_CONFIGURE_PHONE_ACCOUNT);
+        }
+        return intent;
+    }
+
+    private static Intent buildConfigureIntent(
+            Context context, PhoneAccountHandle accountHandle, String actionStr) {
+        if (accountHandle == null || accountHandle.getComponentName() == null
+                || TextUtils.isEmpty(accountHandle.getComponentName().getPackageName())) {
+            return null;
+        }
+
+        // Build the settings intent.
+        Intent intent = new Intent(actionStr);
+        intent.setPackage(accountHandle.getComponentName().getPackageName());
+        intent.addCategory(Intent.CATEGORY_DEFAULT);
+        intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle);
+
+        // Check to see that the phone account package can handle the setting intent.
+        PackageManager pm = context.getPackageManager();
+        List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
+        if (resolutions.size() == 0) {
+            intent = null;  // set no intent if the package cannot handle it.
+        }
+
+        return intent;
+    }
+
     public static boolean isImsServiceStateReady(ImsManager imsMgr) {
         boolean isImsServiceStateReady = false;
 
diff --git a/tests/robotests/src/com/android/settings/mobilenetwork/MobileNetworkUtilsTest.java b/tests/robotests/src/com/android/settings/mobilenetwork/MobileNetworkUtilsTest.java
index 2b21f1f..4b91f6d 100644
--- a/tests/robotests/src/com/android/settings/mobilenetwork/MobileNetworkUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/mobilenetwork/MobileNetworkUtilsTest.java
@@ -16,13 +16,24 @@
 
 package com.android.settings.mobilenetwork;
 
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyObject;
+import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.telecom.PhoneAccountHandle;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
@@ -36,10 +47,12 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 
 @RunWith(SettingsRobolectricTestRunner.class)
 public class MobileNetworkUtilsTest {
+    private static final String PACKAGE_NAME = "com.android.app";
     private static final int SUB_ID_1 = 1;
     private static final int SUB_ID_2 = 2;
 
@@ -53,6 +66,14 @@
     private SubscriptionInfo mSubscriptionInfo1;
     @Mock
     private SubscriptionInfo mSubscriptionInfo2;
+    @Mock
+    private PackageManager mPackageManager;
+    @Mock
+    private PhoneAccountHandle mPhoneAccountHandle;
+    @Mock
+    private ComponentName mComponentName;
+    @Mock
+    private ResolveInfo mResolveInfo;
 
     private Context mContext;
 
@@ -65,6 +86,9 @@
         doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
         doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID_1);
         doReturn(mTelephonyManager2).when(mTelephonyManager).createForSubscriptionId(SUB_ID_2);
+        doReturn(mPackageManager).when(mContext).getPackageManager();
+        doReturn(mComponentName).when(mPhoneAccountHandle).getComponentName();
+        doReturn(PACKAGE_NAME).when(mComponentName).getPackageName();
 
         doReturn(SUB_ID_1).when(mSubscriptionInfo1).getSubscriptionId();
         doReturn(SUB_ID_2).when(mSubscriptionInfo2).getSubscriptionId();
@@ -96,4 +120,27 @@
         verify(mTelephonyManager).setDataEnabled(true);
         verify(mTelephonyManager2).setDataEnabled(false);
     }
+
+    @Test
+    public void buildConfigureIntent_nullHandle_returnNull() {
+        assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, null)).isNull();
+    }
+
+    @Test
+    public void buildConfigureIntent_noActivityHandleIntent_returnNull() {
+        doReturn(new ArrayList<ResolveInfo>()).when(mPackageManager).queryIntentActivities(
+                nullable(Intent.class), anyInt());
+
+        assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
+                mPhoneAccountHandle)).isNull();
+    }
+
+    @Test
+    public void buildConfigureIntent_hasActivityHandleIntent_returnIntent() {
+        doReturn(Arrays.asList(mResolveInfo)).when(mPackageManager).queryIntentActivities(
+                nullable(Intent.class), anyInt());
+
+        assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
+                mPhoneAccountHandle)).isNotNull();
+    }
 }