[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;
-    }
 }