Merge "Fix the notification issue after pSIM is auto enabled" into sc-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 764bd48..07386c1 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -12782,4 +12782,7 @@
     <string name="smart_forwarding_missing_mdn_text">Phone number is missing.</string>
     <!-- Alert Dialog text when user didn't input the phone number [CHAR LIMIT=50]-->
     <string name="smart_forwarding_missing_alert_dialog_text">OK</string>
+
+    <!-- Enable 2G: Title for if user wants to enable 2G [CHAR LIMIT=40] -->
+    <string name="enable_2g_title">Allow 2G</string>
 </resources>
diff --git a/res/xml/mobile_network_settings.xml b/res/xml/mobile_network_settings.xml
index a100d6b..86f5a83 100644
--- a/res/xml/mobile_network_settings.xml
+++ b/res/xml/mobile_network_settings.xml
@@ -255,4 +255,8 @@
         settings:searchable="false"
         settings:controller="com.android.settings.network.telephony.NrDisabledInDsdsFooterPreferenceController"/>
 
+    <SwitchPreference
+        android:key="enable_2g"
+        android:title="@string/enable_2g_title"
+        settings:controller="com.android.settings.network.telephony.Enable2gPreferenceController" />
 </PreferenceScreen>
diff --git a/src/com/android/settings/fuelgauge/OWNERS b/src/com/android/settings/fuelgauge/OWNERS
index 4e5ee0e..ab3da89 100644
--- a/src/com/android/settings/fuelgauge/OWNERS
+++ b/src/com/android/settings/fuelgauge/OWNERS
@@ -1,3 +1,4 @@
 # Default reviewers for this and subdirectories.
-millchen@google.com
-tmfang@google.com
+tifn@google.com
+wesleycwwang@google.com
+ykhung@google.com
diff --git a/src/com/android/settings/network/telephony/Enable2gPreferenceController.java b/src/com/android/settings/network/telephony/Enable2gPreferenceController.java
new file mode 100644
index 0000000..1fa4300
--- /dev/null
+++ b/src/com/android/settings/network/telephony/Enable2gPreferenceController.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.network.telephony;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+
+/**
+ * Preference controller for "Enable 2G"
+ */
+public class Enable2gPreferenceController extends TelephonyTogglePreferenceController {
+
+    private static final String LOG_TAG = "Enable2gPreferenceController";
+    private static final long BITMASK_2G =  TelephonyManager.NETWORK_TYPE_BITMASK_GSM
+                | TelephonyManager.NETWORK_TYPE_BITMASK_GPRS
+                | TelephonyManager.NETWORK_TYPE_BITMASK_EDGE
+                | TelephonyManager.NETWORK_TYPE_BITMASK_CDMA;
+
+    private CarrierConfigManager mCarrierConfigManager;
+    private TelephonyManager mTelephonyManager;
+
+    /**
+     * Class constructor of "Enable 2G" toggle.
+     *
+     * @param context of settings
+     * @param key assigned within UI entry of XML file
+     */
+    public Enable2gPreferenceController(Context context, String key) {
+        super(context, key);
+        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
+    }
+
+    /**
+     * Initialization based on a given subscription id.
+     *
+     * @param subId is the subscription id
+     * @return this instance after initialization
+     */
+    public Enable2gPreferenceController init(int subId) {
+        mSubId = subId;
+        mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
+              .createForSubscriptionId(mSubId);
+        return this;
+    }
+
+    @Override
+    public int getAvailabilityStatus(int subId) {
+        final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
+        boolean visible =
+                subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                && carrierConfig != null
+                && !carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G)
+                && mTelephonyManager.isRadioInterfaceCapabilitySupported(
+                    mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
+        return visible ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
+    }
+
+    @Override
+    public boolean isChecked() {
+        long currentlyAllowedNetworkTypes = mTelephonyManager.getAllowedNetworkTypesForReason(
+                mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G);
+        return (currentlyAllowedNetworkTypes & BITMASK_2G) != 0;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        long currentlyAllowedNetworkTypes = mTelephonyManager.getAllowedNetworkTypesForReason(
+                mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G);
+        boolean enabled = (currentlyAllowedNetworkTypes & BITMASK_2G) != 0;
+        if (enabled == isChecked) {
+            return false;
+        }
+        long newAllowedNetworkTypes = currentlyAllowedNetworkTypes;
+        if (isChecked) {
+            newAllowedNetworkTypes = currentlyAllowedNetworkTypes | BITMASK_2G;
+            Log.i(LOG_TAG, "Enabling 2g. Allowed network types: " + newAllowedNetworkTypes);
+        } else {
+            newAllowedNetworkTypes = currentlyAllowedNetworkTypes & ~BITMASK_2G;
+            Log.i(LOG_TAG, "Disabling 2g. Allowed network types: " + newAllowedNetworkTypes);
+        }
+        mTelephonyManager.setAllowedNetworkTypesForReason(
+                mTelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G, newAllowedNetworkTypes);
+        return true;
+    }
+}
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
index 0d842ef..9a4cb40 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
@@ -158,6 +158,7 @@
         use(PreferredNetworkModePreferenceController.class).init(mSubId);
         use(EnabledNetworkModePreferenceController.class).init(getLifecycle(), mSubId);
         use(DataServiceSetupPreferenceController.class).init(mSubId);
+        use(Enable2gPreferenceController.class).init(mSubId);
 
         final WifiCallingPreferenceController wifiCallingPreferenceController =
                 use(WifiCallingPreferenceController.class).init(mSubId);
diff --git a/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java
new file mode 100644
index 0000000..9b9bb11
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/Enable2gPreferenceControllerTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.network.telephony;
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public final class Enable2gPreferenceControllerTest {
+    private static final int SUB_ID = 2;
+
+    @Mock
+    private TelephonyManager mTelephonyManager;
+    @Mock
+    private CarrierConfigManager mCarrierConfigManager;
+
+    private PersistableBundle mPersistableBundle;
+    private Enable2gPreferenceController mController;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(ApplicationProvider.getApplicationContext());
+        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
+        when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+        when(mContext.getSystemService(CarrierConfigManager.class))
+              .thenReturn(mCarrierConfigManager);
+
+        doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+
+        mPersistableBundle = new PersistableBundle();
+        doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+        doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfigForSubId(
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        mController = new Enable2gPreferenceController(mContext, "mobile_data");
+        mController.init(SUB_ID);
+    }
+
+    @Test
+    public void getAvailabilityStatus_invalidSubId_returnUnavailable() {
+        mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_hideEnable2g_returnUnavailable() {
+        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
+                true);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_nullCarrierConfig_returnUnavailable() {
+        doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
+                mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
+        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
+                false);
+        doReturn(null).when(mCarrierConfigManager);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_capabilityNotSupported_returnUnavailable() {
+        doReturn(false).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
+                mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
+        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
+                false);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_returnAvailable() {
+        doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
+                mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
+        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
+                false);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+}