1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.network.telephony; 17 18 import android.app.settings.SettingsEnums; 19 import android.content.Context; 20 import android.os.PersistableBundle; 21 import android.telephony.CarrierConfigManager; 22 import android.telephony.SubscriptionManager; 23 import android.telephony.TelephonyManager; 24 import android.util.Log; 25 26 import com.android.settings.overlay.FeatureFactory; 27 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 28 29 /** 30 * Preference controller for "Enable 2G" 31 */ 32 public class Enable2gPreferenceController extends TelephonyTogglePreferenceController { 33 34 private static final String LOG_TAG = "Enable2gPreferenceController"; 35 private static final long BITMASK_2G = TelephonyManager.NETWORK_TYPE_BITMASK_GSM 36 | TelephonyManager.NETWORK_TYPE_BITMASK_GPRS 37 | TelephonyManager.NETWORK_TYPE_BITMASK_EDGE 38 | TelephonyManager.NETWORK_TYPE_BITMASK_CDMA 39 | TelephonyManager.NETWORK_TYPE_BITMASK_1xRTT; 40 41 private final MetricsFeatureProvider mMetricsFeatureProvider; 42 43 private CarrierConfigManager mCarrierConfigManager; 44 private TelephonyManager mTelephonyManager; 45 46 /** 47 * Class constructor of "Enable 2G" toggle. 48 * 49 * @param context of settings 50 * @param key assigned within UI entry of XML file 51 */ Enable2gPreferenceController(Context context, String key)52 public Enable2gPreferenceController(Context context, String key) { 53 super(context, key); 54 mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); 55 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 56 } 57 58 /** 59 * Initialization based on a given subscription id. 60 * 61 * @param subId is the subscription id 62 * @return this instance after initialization 63 */ init(int subId)64 public Enable2gPreferenceController init(int subId) { 65 mSubId = subId; 66 mTelephonyManager = mContext.getSystemService(TelephonyManager.class) 67 .createForSubscriptionId(mSubId); 68 return this; 69 } 70 71 @Override getAvailabilityStatus(int subId)72 public int getAvailabilityStatus(int subId) { 73 final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId); 74 if (mTelephonyManager == null) { 75 Log.w(LOG_TAG, "Telephony manager not yet initialized"); 76 mTelephonyManager = mContext.getSystemService(TelephonyManager.class); 77 } 78 boolean visible = 79 SubscriptionManager.isUsableSubscriptionId(subId) 80 && carrierConfig != null 81 && !carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G) 82 && mTelephonyManager.isRadioInterfaceCapabilitySupported( 83 mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK); 84 return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 85 } 86 87 @Override isChecked()88 public boolean isChecked() { 89 long currentlyAllowedNetworkTypes = mTelephonyManager.getAllowedNetworkTypesForReason( 90 mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G); 91 return (currentlyAllowedNetworkTypes & BITMASK_2G) != 0; 92 } 93 94 @Override setChecked(boolean isChecked)95 public boolean setChecked(boolean isChecked) { 96 if (!SubscriptionManager.isUsableSubscriptionId(mSubId)) { 97 return false; 98 } 99 long currentlyAllowedNetworkTypes = mTelephonyManager.getAllowedNetworkTypesForReason( 100 mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G); 101 boolean enabled = (currentlyAllowedNetworkTypes & BITMASK_2G) != 0; 102 if (enabled == isChecked) { 103 return false; 104 } 105 long newAllowedNetworkTypes = currentlyAllowedNetworkTypes; 106 if (isChecked) { 107 newAllowedNetworkTypes = currentlyAllowedNetworkTypes | BITMASK_2G; 108 Log.i(LOG_TAG, "Enabling 2g. Allowed network types: " + newAllowedNetworkTypes); 109 } else { 110 newAllowedNetworkTypes = currentlyAllowedNetworkTypes & ~BITMASK_2G; 111 Log.i(LOG_TAG, "Disabling 2g. Allowed network types: " + newAllowedNetworkTypes); 112 } 113 mTelephonyManager.setAllowedNetworkTypesForReason( 114 mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G, newAllowedNetworkTypes); 115 mMetricsFeatureProvider.action( 116 mContext, SettingsEnums.ACTION_2G_ENABLED, isChecked); 117 return true; 118 } 119 } 120