[Provider Model] Add new API to detect

1. current connection is mobile data
 - remove the SubscriptionsPreferenceController#activeNetworkIsCellular() and move into MobileNetworkUtils
2. isProviderModel API
 - Add it in Util for Provider Model
3. isWiFiCallingEnabled
 - remove WifiCallingPreferenceController#isWifiCallingEnabled() and move into MobileNetworkUtils
4. Add nes test case for MobileNetworkUtils

Bug: 171873895
Test: atest -c MobileNetworkUtilsTest
Change-Id: I4bfdf0537fe07d064d6c0ba4a2c44b4a4f158d91
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index f9359a2..b6a9f59 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -83,6 +83,7 @@
 import android.text.format.DateUtils;
 import android.text.style.TtsSpan;
 import android.util.ArraySet;
+import android.util.FeatureFlagUtils;
 import android.util.IconDrawableFactory;
 import android.util.Log;
 import android.view.LayoutInflater;
@@ -1146,4 +1147,9 @@
         drawable.draw(canvas);
         return roundedBitmap;
     }
+
+    public static boolean isProviderModelEnabled(Context context) {
+        return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SETTINGS_PROVIDER_MODEL);
+    }
+
 }
diff --git a/src/com/android/settings/network/SubscriptionsPreferenceController.java b/src/com/android/settings/network/SubscriptionsPreferenceController.java
index 82df4aa..53d6c30 100644
--- a/src/com/android/settings/network/SubscriptionsPreferenceController.java
+++ b/src/com/android/settings/network/SubscriptionsPreferenceController.java
@@ -24,9 +24,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
-import android.net.ConnectivityManager;
-import android.net.Network;
-import android.net.NetworkCapabilities;
 import android.provider.Settings;
 import android.telephony.SignalStrength;
 import android.telephony.SubscriptionInfo;
@@ -71,7 +68,6 @@
     private String mPreferenceGroupKey;
     private PreferenceGroup mPreferenceGroup;
     private SubscriptionManager mManager;
-    private ConnectivityManager mConnectivityManager;
     private SubscriptionsChangeListener mSubscriptionsListener;
     private MobileDataEnabledListener mDataEnabledListener;
     private DataConnectivityListener mConnectivityListener;
@@ -112,7 +108,6 @@
         mPreferenceGroupKey = preferenceGroupKey;
         mStartOrder = startOrder;
         mManager = context.getSystemService(SubscriptionManager.class);
-        mConnectivityManager = mContext.getSystemService(ConnectivityManager.class);
         mSubscriptionPreferences = new ArrayMap<>();
         mSubscriptionsListener = new SubscriptionsChangeListener(context, this);
         mDataEnabledListener = new MobileDataEnabledListener(context, this);
@@ -229,19 +224,6 @@
                 NO_CELL_DATA_TYPE_ICON, cutOut);
     }
 
-    private boolean activeNetworkIsCellular() {
-        final Network activeNetwork = mConnectivityManager.getActiveNetwork();
-        if (activeNetwork == null) {
-            return false;
-        }
-        final NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(
-                activeNetwork);
-        if (networkCapabilities == null) {
-            return false;
-        }
-        return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
-    }
-
     /**
      * The summary can have either 1 or 2 lines depending on which services (calls, SMS, data) this
      * subscription is the default for.
@@ -271,7 +253,7 @@
             final TelephonyManager telMgrForSub = mContext.getSystemService(
                     TelephonyManager.class).createForSubscriptionId(subId);
             final boolean dataEnabled = telMgrForSub.isDataEnabled();
-            if (dataEnabled && activeNetworkIsCellular()) {
+            if (dataEnabled && MobileNetworkUtils.activeNetworkIsCellular(mContext)) {
                 line2 = mContext.getString(R.string.mobile_data_active);
             } else if (!dataEnabled) {
                 line2 = mContext.getString(R.string.mobile_data_off);
diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
index 99e69c7..b300c66 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
@@ -31,6 +31,7 @@
 import static com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO;
 import static com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA;
 
+import android.annotation.Nullable;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
@@ -41,6 +42,9 @@
 import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
+import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
 import android.os.PersistableBundle;
 import android.os.SystemClock;
 import android.os.SystemProperties;
@@ -69,6 +73,7 @@
 import com.android.settings.R;
 import com.android.settings.Utils;
 import com.android.settings.core.BasePreferenceController;
+import com.android.settings.network.ims.WifiCallingQueryImsState;
 import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
 import com.android.settingslib.development.DevelopmentSettingsEnabler;
 import com.android.settingslib.graph.SignalDrawable;
@@ -202,8 +207,7 @@
         return bundle.getBoolean(CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false /*default*/);
     }
 
-    @VisibleForTesting
-    static Intent buildPhoneAccountConfigureIntent(
+    public static Intent buildPhoneAccountConfigureIntent(
             Context context, PhoneAccountHandle accountHandle) {
         Intent intent = buildConfigureIntent(
                 context, accountHandle, TelecomManager.ACTION_CONFIGURE_PHONE_ACCOUNT);
@@ -854,4 +858,45 @@
         raf = ((NR & raf) > 0) ? (NR | raf) : raf;
         return raf;
     }
+
+    /**
+     * Copied from SubscriptionsPreferenceController#activeNetworkIsCellular()
+     */
+    public static boolean activeNetworkIsCellular(Context context) {
+        final ConnectivityManager connectivityManager =
+                context.getSystemService(ConnectivityManager.class);
+        final Network activeNetwork = connectivityManager.getActiveNetwork();
+        if (activeNetwork == null) {
+            return false;
+        }
+        final NetworkCapabilities networkCapabilities =
+                connectivityManager.getNetworkCapabilities(activeNetwork);
+        if (networkCapabilities == null) {
+            return false;
+        }
+        return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
+    }
+
+    /**
+     * Copied from WifiCallingPreferenceController#isWifiCallingEnabled()
+     */
+    public static boolean isWifiCallingEnabled(Context context, int subId,
+            @Nullable WifiCallingQueryImsState queryImsState,
+            @Nullable PhoneAccountHandle phoneAccountHandle) {
+        if (phoneAccountHandle == null){
+            phoneAccountHandle = context.getSystemService(TelecomManager.class)
+                    .getSimCallManagerForSubscription(subId);
+        }
+        boolean isWifiCallingEnabled;
+        if (phoneAccountHandle != null) {
+            final Intent intent = buildPhoneAccountConfigureIntent(context, phoneAccountHandle);
+            isWifiCallingEnabled = intent != null;
+        } else {
+            if (queryImsState == null) {
+                queryImsState = new WifiCallingQueryImsState(context, subId);
+            }
+            isWifiCallingEnabled = queryImsState.isReadyToWifiCalling();
+        }
+        return isWifiCallingEnabled;
+    }
 }
diff --git a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
index b38fe07..6bfd650 100644
--- a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
+++ b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
@@ -71,7 +71,7 @@
     @Override
     public int getAvailabilityStatus(int subId) {
         return SubscriptionManager.isValidSubscriptionId(subId)
-                && isWifiCallingEnabled(mContext, subId)
+                && MobileNetworkUtils.isWifiCallingEnabled(mContext, subId, null, null)
                 ? AVAILABLE
                 : UNSUPPORTED_ON_DEVICE;
     }
@@ -221,23 +221,4 @@
             mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
         }
     }
-
-    private boolean isWifiCallingEnabled(Context context, int subId) {
-        final PhoneAccountHandle simCallManager =
-                context.getSystemService(TelecomManager.class)
-                       .getSimCallManagerForSubscription(subId);
-        final int phoneId = SubscriptionManager.getSlotIndex(subId);
-
-        boolean isWifiCallingEnabled;
-        if (simCallManager != null) {
-            final Intent intent = MobileNetworkUtils.buildPhoneAccountConfigureIntent(
-                    context, simCallManager);
-
-            isWifiCallingEnabled = intent != null;
-        } else {
-            isWifiCallingEnabled = queryImsState(subId).isReadyToWifiCalling();
-        }
-
-        return isWifiCallingEnabled;
-    }
 }
diff --git a/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
index ee8157d..9c0a082 100644
--- a/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
+++ b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
@@ -17,10 +17,13 @@
 package com.android.settings.network.telephony;
 
 import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 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;
@@ -31,9 +34,13 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
 import android.os.PersistableBundle;
 import android.provider.Settings;
 import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
@@ -42,6 +49,7 @@
 import androidx.test.core.app.ApplicationProvider;
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
+import com.android.settings.network.ims.MockWfcQueryImsState;
 import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
 
 import org.junit.Before;
@@ -79,11 +87,18 @@
     private ResolveInfo mResolveInfo;
     @Mock
     private CarrierConfigManager mCarrierConfigManager;
+    @Mock
+    private ConnectivityManager mConnectivityManager;
+    @Mock
+    private TelecomManager mTelecomManager;
 
     private Context mContext;
     private PersistableBundle mCarrierConfig;
     private PhoneAccountHandle mPhoneAccountHandle;
     private ComponentName mComponentName;
+    private NetworkCapabilities mNetworkCapabilities;
+    private Network mNetwork;
+    private MockWfcQueryImsState mMockQueryWfcState;
 
     @Before
     public void setUp() {
@@ -92,7 +107,6 @@
         mContext = spy(ApplicationProvider.getApplicationContext());
         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
         when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
-        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
         when(mTelephonyManager.createForSubscriptionId(SUB_ID_1)).thenReturn(mTelephonyManager);
         when(mTelephonyManager.createForSubscriptionId(SUB_ID_2)).thenReturn(mTelephonyManager2);
         when(mContext.getPackageManager()).thenReturn(mPackageManager);
@@ -103,6 +117,10 @@
         mCarrierConfig = new PersistableBundle();
         when(mCarrierConfigManager.getConfigForSubId(SUB_ID_1)).thenReturn(mCarrierConfig);
 
+        mNetwork = new Network(anyInt());
+        when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager);
+        when(mConnectivityManager.getActiveNetwork()).thenReturn(mNetwork);
+
         when(mSubscriptionInfo1.getSubscriptionId()).thenReturn(SUB_ID_1);
         when(mSubscriptionInfo1.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_1);
         when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(SUB_ID_2);
@@ -115,6 +133,11 @@
 
         when(mTelephonyManager.getNetworkOperatorName()).thenReturn(
                 PLMN_FROM_TELEPHONY_MANAGER_API);
+
+        when(mContext.getSystemService(TelecomManager.class)).thenReturn(mTelecomManager);
+        when(mTelecomManager.getSimCallManagerForSubscription(SUB_ID_1))
+                .thenReturn(mPhoneAccountHandle);
+        mMockQueryWfcState = new MockWfcQueryImsState(mContext, SUB_ID_1);
     }
 
     @Test
@@ -148,8 +171,7 @@
 
     @Test
     public void buildConfigureIntent_noActivityHandleIntent_returnNull() {
-        when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
-                .thenReturn(new ArrayList<>());
+        buildPhoneAccountConfigureIntent(false);
 
         assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
                 mPhoneAccountHandle)).isNull();
@@ -157,10 +179,7 @@
 
     @Test
     public void buildConfigureIntent_hasActivityHandleIntent_returnIntent() {
-        mComponentName = new ComponentName(PACKAGE_NAME, "testClass");
-        mPhoneAccountHandle = new PhoneAccountHandle(mComponentName, "");
-        when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
-                .thenReturn(Arrays.asList(mResolveInfo));
+        buildPhoneAccountConfigureIntent(true);
 
         assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
                 mPhoneAccountHandle)).isNotNull();
@@ -333,4 +352,71 @@
         assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
                 mContext)).isNotNull();
     }
+
+    @Test
+    public void isCellularNetwork_withCellularNetwork_returnTrue() {
+        addNetworkTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
+
+        assertTrue(MobileNetworkUtils.activeNetworkIsCellular(mContext));
+    }
+
+    @Test
+    public void isCellularNetwork_withWifiNetwork_returnFalse() {
+        addNetworkTransportType(NetworkCapabilities.TRANSPORT_WIFI);
+
+        assertFalse(MobileNetworkUtils.activeNetworkIsCellular(mContext));
+    }
+
+    private void addNetworkTransportType (int networkType) {
+        mNetworkCapabilities = new NetworkCapabilities().addTransportType(networkType);
+        when(mConnectivityManager.getNetworkCapabilities(mNetwork)).thenReturn(
+                mNetworkCapabilities);
+    }
+
+    @Test
+    public void isWifiCallingEnabled_hasPhoneAccountHandleAndHasActivityHandleIntent_returnTrue() {
+        buildPhoneAccountConfigureIntent(true);
+
+        assertTrue(MobileNetworkUtils.isWifiCallingEnabled(mContext, SUB_ID_1,
+                null, mPhoneAccountHandle));
+    }
+
+    @Test
+    public void isWifiCallingEnabled_hasPhoneAccountHandleAndNoActivityHandleIntent_returnFalse() {
+        buildPhoneAccountConfigureIntent(false);
+
+        assertFalse(MobileNetworkUtils.isWifiCallingEnabled(mContext, SUB_ID_1,
+                null, mPhoneAccountHandle));
+    }
+
+    @Test
+    public void isWifiCallingEnabled_noPhoneAccountHandleAndWifiCallingIsReady_returnTrue() {
+        setWifiCallingEnabled(true);
+
+        assertTrue(MobileNetworkUtils.isWifiCallingEnabled(mContext, SUB_ID_1,
+                mMockQueryWfcState, null));
+    }
+
+    @Test
+    public void isWifiCallingEnabled_noPhoneAccountHandleAndWifiCallingNotReady_returnFalse() {
+        setWifiCallingEnabled(false);
+
+        assertFalse(MobileNetworkUtils.isWifiCallingEnabled(mContext, SUB_ID_1,
+                mMockQueryWfcState, null));
+    }
+
+    private void setWifiCallingEnabled(boolean enabled){
+        mMockQueryWfcState.setIsEnabledByUser(enabled);
+        mMockQueryWfcState.setServiceStateReady(enabled);
+        mMockQueryWfcState.setIsEnabledByPlatform(enabled);
+        mMockQueryWfcState.setIsProvisionedOnDevice(enabled);
+    }
+
+    private void buildPhoneAccountConfigureIntent(boolean hasActivityHandleIntent) {
+        mComponentName = new ComponentName(PACKAGE_NAME, "testClass");
+        mPhoneAccountHandle = new PhoneAccountHandle(mComponentName, "");
+        when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
+                .thenReturn(
+                        hasActivityHandleIntent ? Arrays.asList(mResolveInfo) : new ArrayList<>());
+    }
 }