Replace hidden CM#isNetworkSupported() usage
It's a refactor work for connectivity mainline module. The
hidden methods access is no longer allowed, so the usage for
isNetworkSupported() should be replaced. Settings use it to
check if device support telephony, wifi or ethernet service.
There are alternative methods to check if device supports such
features. Replace as it is.
Bug: 172183305
Test: m ; make RunSettingsRoboTests
Change-Id: I8f1d11558b1be575a0777ed195abe027e838cb74
Merged-In: I8f1d11558b1be575a0777ed195abe027e838cb74
diff --git a/src/com/android/settings/SettingsDumpService.java b/src/com/android/settings/SettingsDumpService.java
index 2b6c7d8..5e6ee93 100644
--- a/src/com/android/settings/SettingsDumpService.java
+++ b/src/com/android/settings/SettingsDumpService.java
@@ -14,13 +14,15 @@
package com.android.settings;
+import static android.content.pm.PackageManager.FEATURE_ETHERNET;
+import static android.content.pm.PackageManager.FEATURE_WIFI;
+
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
-import android.net.ConnectivityManager;
import android.net.NetworkTemplate;
import android.net.Uri;
import android.os.IBinder;
@@ -101,10 +103,10 @@
private JSONObject dumpDataUsage() throws JSONException {
JSONObject obj = new JSONObject();
DataUsageController controller = new DataUsageController(this);
- ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
SubscriptionManager manager = this.getSystemService(SubscriptionManager.class);
TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class);
- if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
+ final PackageManager packageManager = this.getPackageManager();
+ if (telephonyManager.isDataCapable()) {
JSONArray array = new JSONArray();
for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
telephonyManager = telephonyManager
@@ -117,10 +119,11 @@
}
obj.put("cell", array);
}
- if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
+ if (packageManager.hasSystemFeature(FEATURE_WIFI)) {
obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
}
- if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
+
+ if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) {
obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
}
return obj;
diff --git a/src/com/android/settings/datausage/DataUsageUtils.java b/src/com/android/settings/datausage/DataUsageUtils.java
index da0ca63..7da69cb 100644
--- a/src/com/android/settings/datausage/DataUsageUtils.java
+++ b/src/com/android/settings/datausage/DataUsageUtils.java
@@ -14,13 +14,14 @@
package com.android.settings.datausage;
-import static android.net.ConnectivityManager.TYPE_MOBILE;
-import static android.net.ConnectivityManager.TYPE_WIFI;
+import static android.content.pm.PackageManager.FEATURE_ETHERNET;
+import static android.content.pm.PackageManager.FEATURE_WIFI;
import static android.telephony.TelephonyManager.SIM_STATE_READY;
import android.app.usage.NetworkStats.Bucket;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
+import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkTemplate;
import android.os.RemoteException;
@@ -69,8 +70,7 @@
return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains(ETHERNET);
}
- final ConnectivityManager conn = context.getSystemService(ConnectivityManager.class);
- if (!conn.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
+ if (!context.getPackageManager().hasSystemFeature(FEATURE_ETHERNET)) {
return false;
}
@@ -96,10 +96,8 @@
* TODO: This is the opposite to Utils.isWifiOnly(), it should be refactored into 1 method.
*/
public static boolean hasMobileData(Context context) {
- final ConnectivityManager connectivityManager =
- context.getSystemService(ConnectivityManager.class);
- return connectivityManager != null && connectivityManager
- .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
+ final TelephonyManager tele = context.getSystemService(TelephonyManager.class);
+ return tele.isDataCapable();
}
/**
@@ -128,12 +126,13 @@
Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
}
}
- final ConnectivityManager conn = context.getSystemService(ConnectivityManager.class);
- final boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
+
+ final boolean isDataCapable = tele.isDataCapable();
+ final boolean retVal = isDataCapable && isReady;
if (LOGD) {
Log.d(TAG, "hasReadyMobileRadio:"
- + " conn.isNetworkSupported(TYPE_MOBILE)="
- + conn.isNetworkSupported(TYPE_MOBILE)
+ + " telephonManager.isDataCapable()="
+ + isDataCapable
+ " isReady=" + isReady);
}
return retVal;
@@ -147,9 +146,8 @@
return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
}
- final ConnectivityManager connectivityManager =
- context.getSystemService(ConnectivityManager.class);
- return connectivityManager != null && connectivityManager.isNetworkSupported(TYPE_WIFI);
+ final PackageManager packageManager = context.getPackageManager();
+ return packageManager != null && packageManager.hasSystemFeature(FEATURE_WIFI);
}
/**
diff --git a/src/com/android/settings/homepage/contextualcards/conditional/CellularDataConditionController.java b/src/com/android/settings/homepage/contextualcards/conditional/CellularDataConditionController.java
index 4c0ddc9..9c936b9 100644
--- a/src/com/android/settings/homepage/contextualcards/conditional/CellularDataConditionController.java
+++ b/src/com/android/settings/homepage/contextualcards/conditional/CellularDataConditionController.java
@@ -19,7 +19,6 @@
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
-import android.net.ConnectivityManager;
import android.telephony.PhoneStateListener;
import android.telephony.PreciseDataConnectionState;
import android.telephony.SubscriptionManager;
@@ -39,7 +38,6 @@
private final Context mAppContext;
private final ConditionManager mConditionManager;
private final GlobalSettingsChangeListener mDefaultDataSubscriptionIdListener;
- private final ConnectivityManager mConnectivityManager;
private int mSubId;
private TelephonyManager mTelephonyManager;
@@ -63,8 +61,6 @@
}
}
};
- mConnectivityManager = appContext.getSystemService(
- ConnectivityManager.class);
}
@Override
@@ -74,7 +70,7 @@
@Override
public boolean isDisplayable() {
- if (!mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)
+ if (!mTelephonyManager.isDataCapable()
|| mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_READY) {
return false;
}
diff --git a/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java b/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java
index 3fdb7b4..861b4e3 100644
--- a/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java
@@ -36,8 +36,8 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
import android.content.res.Resources;
-import android.net.ConnectivityManager;
import android.net.NetworkPolicyManager;
import android.os.Bundle;
@@ -72,9 +72,9 @@
@Mock
private NetworkPolicyEditor mNetworkPolicyEditor;
@Mock
- private ConnectivityManager mConnectivityManager;
- @Mock
private NetworkPolicyManager mNetworkPolicyManager;
+ @Mock
+ private PackageManager mMockPackageManager;
private Context mContext;
@Mock
@@ -157,9 +157,8 @@
.onCreatePreferences(any(Bundle.class), nullable(String.class));
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE))
.thenReturn(mNetworkPolicyManager);
- when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
- .thenReturn(mConnectivityManager);
- when(mConnectivityManager.isNetworkSupported(anyInt())).thenReturn(true);
+ when(mContext.getPackageManager()).thenReturn(mMockPackageManager);
+ when(mMockPackageManager.hasSystemFeature(any())).thenReturn(true);
final SwitchPreference preference = mock(SwitchPreference.class);
when(billingCycleSettings.findPreference(anyString())).thenReturn(preference);
diff --git a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java
index 4a5bc70..6a7f237 100644
--- a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java
@@ -16,13 +16,14 @@
package com.android.settings.datausage;
-import static android.net.ConnectivityManager.TYPE_WIFI;
+import static android.content.pm.PackageManager.FEATURE_WIFI;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -31,7 +32,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
-import android.net.ConnectivityManager;
+import android.content.pm.PackageManager;
import android.net.NetworkTemplate;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -104,7 +105,7 @@
@Mock
private TelephonyManager mTelephonyManager;
@Mock
- private ConnectivityManager mConnectivityManager;
+ private PackageManager mPm;
private DataUsageInfoController mDataInfoController;
@@ -138,10 +139,9 @@
doReturn(mTelephonyManager).when(mActivity).getSystemService(TelephonyManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager)
.createForSubscriptionId(mDefaultSubscriptionId);
- when(mActivity.getSystemService(Context.CONNECTIVITY_SERVICE))
- .thenReturn(mConnectivityManager);
+ doReturn(mPm).when(mActivity).getPackageManager();
+ doReturn(false).when(mPm).hasSystemFeature(eq(FEATURE_WIFI));
doReturn(TelephonyManager.SIM_STATE_READY).when(mTelephonyManager).getSimState();
- when(mConnectivityManager.isNetworkSupported(TYPE_WIFI)).thenReturn(false);
mController = spy(new DataUsageSummaryPreferenceController(
mDataUsageController,
@@ -363,7 +363,7 @@
final int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
mController.init(subscriptionId);
mController.mDataUsageController = mDataUsageController;
- when(mConnectivityManager.isNetworkSupported(TYPE_WIFI)).thenReturn(true);
+ doReturn(true).when(mPm).hasSystemFeature(eq(FEATURE_WIFI));
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
diff --git a/tests/robotests/src/com/android/settings/datausage/DataUsageUtilsTest.java b/tests/robotests/src/com/android/settings/datausage/DataUsageUtilsTest.java
index 21f9d1a..a465d74 100644
--- a/tests/robotests/src/com/android/settings/datausage/DataUsageUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/DataUsageUtilsTest.java
@@ -18,14 +18,15 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.robolectric.Shadows.shadowOf;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
+import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import android.util.DataUnit;
@@ -38,13 +39,12 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.shadows.ShadowPackageManager;
@RunWith(RobolectricTestRunner.class)
public final class DataUsageUtilsTest {
@Mock
- private ConnectivityManager mManager;
- @Mock
private TelephonyManager mTelephonyManager;
@Mock
private NetworkStatsManager mNetworkStatsManager;
@@ -56,21 +56,20 @@
MockitoAnnotations.initMocks(this);
final ShadowApplication shadowContext = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
- shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager);
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
shadowContext.setSystemService(Context.NETWORK_STATS_SERVICE, mNetworkStatsManager);
}
@Test
public void mobileDataStatus_whenNetworkIsSupported() {
- when(mManager.isNetworkSupported(anyInt())).thenReturn(true);
+ when(mTelephonyManager.isDataCapable()).thenReturn(true);
final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
assertThat(hasMobileData).isTrue();
}
@Test
public void mobileDataStatus_whenNetworkIsNotSupported() {
- when(mManager.isNetworkSupported(anyInt())).thenReturn(false);
+ when(mTelephonyManager.isDataCapable()).thenReturn(false);
final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
assertThat(hasMobileData).isFalse();
}
@@ -85,7 +84,8 @@
@Test
public void hasEthernet_shouldQueryEthernetSummaryForUser() throws Exception {
- when(mManager.isNetworkSupported(anyInt())).thenReturn(true);
+ ShadowPackageManager pm = shadowOf(RuntimeEnvironment.application.getPackageManager());
+ pm.setSystemFeature(PackageManager.FEATURE_ETHERNET, true);
final String subscriber = "TestSub";
when(mTelephonyManager.getSubscriberId()).thenReturn(subscriber);
diff --git a/tests/robotests/src/com/android/settings/network/MobileNetworkPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/MobileNetworkPreferenceControllerTest.java
index 6d46f9c..8256f35 100644
--- a/tests/robotests/src/com/android/settings/network/MobileNetworkPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/MobileNetworkPreferenceControllerTest.java
@@ -28,7 +28,6 @@
import static org.robolectric.shadow.api.Shadow.extract;
import android.content.Context;
-import android.net.ConnectivityManager;
import android.os.UserManager;
import android.provider.Settings;
import android.provider.Settings.Global;
@@ -88,9 +87,7 @@
public void secondaryUser_prefIsNotAvailable() {
ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
userManager.setIsAdminUser(false);
- ShadowConnectivityManager connectivityManager =
- extract(mContext.getSystemService(ConnectivityManager.class));
- connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true);
+ when(mTelephonyManager.isDataCapable()).thenReturn(true);
mController = new MobileNetworkPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
@@ -100,10 +97,7 @@
public void wifiOnly_prefIsNotAvailable() {
ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
userManager.setIsAdminUser(true);
- ShadowConnectivityManager connectivityManager =
- extract(mContext.getSystemService(ConnectivityManager.class));
- connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
-
+ when(mTelephonyManager.isDataCapable()).thenReturn(false);
mController = new MobileNetworkPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
}
diff --git a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
index f6bc05a..bf8dcda 100644
--- a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
@@ -31,7 +31,6 @@
import android.content.Context;
import android.content.Intent;
-import android.net.ConnectivityManager;
import android.os.UserManager;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
@@ -107,9 +106,7 @@
@Test
public void isAvailable_wifiOnlyMode_notAvailable() {
- final ConnectivityManager cm = mock(ConnectivityManager.class);
- when(cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
- when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(cm);
+ when(mTelephonyManager.isDataCapable()).thenReturn(false);
when(mUserManager.isAdminUser()).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
@@ -117,11 +114,8 @@
@Test
public void isAvailable_secondaryUser_notAvailable() {
- final ConnectivityManager cm = mock(ConnectivityManager.class);
- when(cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true);
- when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(cm);
+ when(mTelephonyManager.isDataCapable()).thenReturn(true);
when(mUserManager.isAdminUser()).thenReturn(false);
-
assertThat(mController.isAvailable()).isFalse();
}