diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7fecac48a99..2792254ff9a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -13642,7 +13642,10 @@
Allow 2G
- Use 2G cellular connections. For emergency calls, 2G is always turned on.
+ 2G is less secure, but may improve your connection in some locations. For emergency calls, 2G is always allowed.
+
+ %1$s requires 2G to be available
+
diff --git a/src/com/android/settings/network/telephony/Enable2gPreferenceController.java b/src/com/android/settings/network/telephony/Enable2gPreferenceController.java
index 5ae04ed9f70..a394133bb94 100644
--- a/src/com/android/settings/network/telephony/Enable2gPreferenceController.java
+++ b/src/com/android/settings/network/telephony/Enable2gPreferenceController.java
@@ -19,10 +19,15 @@ import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
+import androidx.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.network.SubscriptionUtil;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -41,6 +46,7 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
private final MetricsFeatureProvider mMetricsFeatureProvider;
private CarrierConfigManager mCarrierConfigManager;
+ private SubscriptionManager mSubscriptionManager;
private TelephonyManager mTelephonyManager;
/**
@@ -53,6 +59,7 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
super(context, key);
mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+ mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
}
/**
@@ -68,6 +75,36 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
return this;
}
+ @Override
+ public void updateState(Preference preference) {
+ super.updateState(preference);
+ if (preference == null || !SubscriptionManager.isUsableSubscriptionId(mSubId)) {
+ return;
+ }
+ final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
+ boolean isDisabledByCarrier =
+ carrierConfig != null
+ && carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G);
+ preference.setEnabled(!isDisabledByCarrier);
+ String summary;
+ if (isDisabledByCarrier) {
+ summary = mContext.getString(R.string.enable_2g_summary_disabled_carrier,
+ getCarrierName());
+ } else {
+ summary = mContext.getString(R.string.enable_2g_summary);
+ }
+ preference.setSummary(summary);
+ }
+
+ private String getCarrierName() {
+ SubscriptionInfo subInfo = SubscriptionUtil.getSubById(mSubscriptionManager, mSubId);
+ if (subInfo == null) {
+ return "";
+ }
+ final String carrierName = subInfo.getCarrierName().toString();
+ return carrierName;
+ }
+
@Override
public int getAvailabilityStatus(int subId) {
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
@@ -78,7 +115,6 @@ public class Enable2gPreferenceController extends TelephonyTogglePreferenceContr
boolean visible =
SubscriptionManager.isUsableSubscriptionId(subId)
&& carrierConfig != null
- && !carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G)
&& mTelephonyManager.isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK);
return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
diff --git a/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java
index 9fb8a316fe6..ab689e5a7df 100644
--- a/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java
@@ -45,12 +45,12 @@ import org.mockito.MockitoAnnotations;
public final class Enable2gPreferenceControllerTest {
private static final int SUB_ID = 2;
+ @Mock
+ private CarrierConfigManager mCarrierConfigManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
- @Mock
- private CarrierConfigManager mCarrierConfigManager;
private PersistableBundle mPersistableBundle;
private Enable2gPreferenceController mController;