Merge "Introduce Telephony2gUpdater to Apply 2g User Restrictions" am: 4183aedcf6

Original change: https://android-review.googlesource.com/c/platform/packages/services/Telephony/+/2221441

Change-Id: I3326d4fd95f8a4d3ba3fb417f427806edae6c17d
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 4122dfe..b8909ef 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -380,6 +380,7 @@
     private SharedPreferences mTelephonySharedPreferences;
     private PhoneConfigurationManager mPhoneConfigurationManager;
     private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
+    private final Telephony2gUpdater mTelephony2gUpdater;
 
     /** User Activity */
     private AtomicBoolean mNotifyUserActivity;
@@ -2347,6 +2348,9 @@
         mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
         mNotifyUserActivity = new AtomicBoolean(false);
         PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
+        mTelephony2gUpdater = new Telephony2gUpdater(
+                Executors.newSingleThreadExecutor(), mApp);
+        mTelephony2gUpdater.init();
         publish();
     }
 
diff --git a/src/com/android/phone/Telephony2gUpdater.java b/src/com/android/phone/Telephony2gUpdater.java
new file mode 100644
index 0000000..0919385
--- /dev/null
+++ b/src/com/android/phone/Telephony2gUpdater.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2022 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.phone;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.UserManager;
+import android.telephony.RadioAccessFamily;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.telephony.RILConstants;
+
+import java.util.List;
+import java.util.concurrent.Executor;
+
+/**
+ * A {@link BroadcastReceiver} that ensures that user restrictions are correctly applied to
+ * telephony.
+ * This includes handling broadcasts from user restriction state changes, as well as ensuring that
+ * SIM-specific settings are correctly applied when new subscriptions become active.
+ *
+ * Callers are expected to call {@code init()} and keep an instance of this class alive.
+ */
+public class Telephony2gUpdater extends BroadcastReceiver {
+    private static final String TAG = "TelephonyUserManagerReceiver";
+
+    // We can't interact with the HAL on the main thread of the phone process (where
+    // receivers are run by default), so we execute our logic from a separate thread.
+    private final Executor mExecutor;
+    private final Context mContext;
+    private final long mBaseAllowedNetworks;
+
+    public Telephony2gUpdater(Executor executor, Context context) {
+        this(executor, context,
+                RadioAccessFamily.getRafFromNetworkType(RILConstants.PREFERRED_NETWORK_MODE));
+    }
+
+    public Telephony2gUpdater(Executor executor, Context context,
+            long baseAllowedNetworks) {
+        mExecutor = executor;
+        mContext = context;
+        mBaseAllowedNetworks = baseAllowedNetworks;
+    }
+
+    /**
+     * Register the given instance as a {@link BroadcastReceiver} and a {@link
+     * SubscriptionManager.OnSubscriptionsChangedListener}.
+     */
+    public void init() {
+        mContext.getSystemService(SubscriptionManager.class).addOnSubscriptionsChangedListener(
+                mExecutor, new SubscriptionListener());
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED);
+        mContext.registerReceiver(this, filter);
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (context == null || intent == null) return;
+        Log.i(TAG, "Received callback for action " + intent.getAction());
+        final PendingResult result = goAsync();
+        mExecutor.execute(() -> {
+            Log.i(TAG, "Running handler for action " + intent.getAction());
+            handleUserRestrictionsChanged(context);
+            result.finish();
+        });
+    }
+
+    /**
+     * Update all active subscriptions with allowed network types depending on the current state
+     * of the {@link UserManager.DISALLOW_2G}.
+     */
+    @VisibleForTesting
+    public void handleUserRestrictionsChanged(Context context) {
+        UserManager um = context.getSystemService(UserManager.class);
+        TelephonyManager tm = context.getSystemService(TelephonyManager.class);
+        SubscriptionManager sm = context.getSystemService(SubscriptionManager.class);
+        final long twoGBitmask = TelephonyManager.NETWORK_CLASS_BITMASK_2G;
+
+        boolean shouldDisable2g = um.hasUserRestriction(UserManager.DISALLOW_CELLULAR_2G);
+
+        // This is expected when subscription info cannot be determined. We'll get another
+        // callback in the future from our SubscriptionListener once we have valid subscriptions.
+        List<SubscriptionInfo> subscriptionInfoList = sm.getAvailableSubscriptionInfoList();
+        if (subscriptionInfoList == null) {
+            return;
+        }
+
+        long allowedNetworkTypes = mBaseAllowedNetworks;
+
+        // 2G device admin controls are global
+        for (SubscriptionInfo info : subscriptionInfoList) {
+            TelephonyManager telephonyManager = tm.createForSubscriptionId(
+                    info.getSubscriptionId());
+            if (shouldDisable2g) {
+                allowedNetworkTypes &= ~twoGBitmask;
+            } else {
+                allowedNetworkTypes |= twoGBitmask;
+            }
+            telephonyManager.setAllowedNetworkTypesForReason(
+                    TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS,
+                    allowedNetworkTypes);
+        }
+    }
+
+    private class SubscriptionListener extends SubscriptionManager.OnSubscriptionsChangedListener {
+        @Override
+        public void onSubscriptionsChanged() {
+            Log.i(TAG, "Running handler for subscription change.");
+            handleUserRestrictionsChanged(mContext);
+        }
+    }
+
+}
diff --git a/tests/src/com/android/TestContext.java b/tests/src/com/android/TestContext.java
index 7edbed9..7c3a842 100644
--- a/tests/src/com/android/TestContext.java
+++ b/tests/src/com/android/TestContext.java
@@ -31,6 +31,7 @@
 import android.os.Looper;
 import android.os.PersistableBundle;
 import android.os.Process;
+import android.os.UserManager;
 import android.telecom.TelecomManager;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
@@ -58,6 +59,7 @@
     @Mock TelephonyManager mMockTelephonyManager;
     @Mock SubscriptionManager mMockSubscriptionManager;
     @Mock ImsManager mMockImsManager;
+    @Mock UserManager mMockUserManager;
 
     private SparseArray<PersistableBundle> mCarrierConfigs = new SparseArray<>();
 
@@ -147,6 +149,9 @@
             case(Context.TELEPHONY_IMS_SERVICE) : {
                 return mMockImsManager;
             }
+            case(Context.USER_SERVICE) : {
+                return mMockUserManager;
+            }
         }
         return null;
     }
@@ -165,6 +170,9 @@
         if (serviceClass == SubscriptionManager.class) {
             return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
         }
+        if (serviceClass == UserManager.class) {
+            return Context.USER_SERVICE;
+        }
         return null;
     }
 
diff --git a/tests/src/com/android/phone/Telephony2gUpdaterTest.java b/tests/src/com/android/phone/Telephony2gUpdaterTest.java
new file mode 100644
index 0000000..3443767
--- /dev/null
+++ b/tests/src/com/android/phone/Telephony2gUpdaterTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2022 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.phone;
+
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.os.UserManager;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.TelephonyTestBase;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+@RunWith(AndroidJUnit4.class)
+public class Telephony2gUpdaterTest extends TelephonyTestBase {
+    private Telephony2gUpdater mTelephony2gUpdater;
+    private Executor mExecutor;
+
+    private UserManager mMockUserManager;
+    private TelephonyManager mMockTelephonyManager;
+    private SubscriptionManager mMockSubscriptionManager;
+
+    // 2G Bitmask is 0b10000000_01001011
+    private static final long BASE_NETWORK = 0b11111111_11111111;
+    private static final long EXPECTED_DISABLED = 0b01111111_10110100;
+    private static final long EXPECTED_ENABLED = 0b11111111_11111111;
+
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        mMockTelephonyManager = mContext.getSystemService(TelephonyManager.class);
+        mMockUserManager = mContext.getSystemService(UserManager.class);
+        mMockSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
+
+        mExecutor = Executors.newSingleThreadExecutor();
+        mTelephony2gUpdater = new Telephony2gUpdater(mExecutor,
+                getTestContext(), BASE_NETWORK);
+    }
+
+    @Test
+    public void handleUserRestrictionsChanged_noSubscriptions_noAllowedNetworksChanged() {
+        when(mMockSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
+                new ArrayList<>());
+        mTelephony2gUpdater.handleUserRestrictionsChanged(getTestContext());
+        verify(mMockTelephonyManager, never()).setAllowedNetworkTypesForReason(anyInt(), anyInt());
+    }
+
+    @Test
+    public void handleUserRestrictionsChanged_nullSubscriptions_noAllowedNetworksChanged() {
+        when(mMockSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(null);
+        mTelephony2gUpdater.handleUserRestrictionsChanged(getTestContext());
+        verify(mMockTelephonyManager, never()).setAllowedNetworkTypesForReason(anyInt(), anyInt());
+    }
+
+    @Test
+    public void handleUserRestrictionsChanged_oneSubscription_allowedNetworksUpdated() {
+        TelephonyManager tmSubscription1 = mock(TelephonyManager.class);
+        when(mMockSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
+                Collections.singletonList(getSubInfo(1)));
+        when(mMockTelephonyManager.createForSubscriptionId(1)).thenReturn(tmSubscription1);
+        when(mMockUserManager.hasUserRestriction(UserManager.DISALLOW_CELLULAR_2G)).thenReturn(
+                true);
+
+        mTelephony2gUpdater.handleUserRestrictionsChanged(getTestContext());
+
+        System.out.println(TelephonyManager.convertNetworkTypeBitmaskToString(11L));
+        verify(tmSubscription1, times(1)).setAllowedNetworkTypesForReason(
+                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS, EXPECTED_DISABLED);
+    }
+
+    @Test
+    public void handleUserRestrictionsChanged_manySubscriptionsDisallow2g_allowedNetworkUpdated() {
+
+        // Two subscriptions are available
+        when(mMockSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
+                Arrays.asList(getSubInfo(1), getSubInfo(2)));
+        TelephonyManager tmSubscription1 = mock(TelephonyManager.class);
+        TelephonyManager tmSubscription2 = mock(TelephonyManager.class);
+        when(mMockTelephonyManager.createForSubscriptionId(1)).thenReturn(tmSubscription1);
+        when(mMockTelephonyManager.createForSubscriptionId(2)).thenReturn(tmSubscription2);
+        // 2g is disallowed
+        when(mMockUserManager.hasUserRestriction(UserManager.DISALLOW_CELLULAR_2G)).thenReturn(
+                true);
+
+        mTelephony2gUpdater.handleUserRestrictionsChanged(getTestContext());
+
+        verify(tmSubscription1, times(1)).setAllowedNetworkTypesForReason(
+                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS, EXPECTED_DISABLED);
+        verify(tmSubscription1, times(1)).setAllowedNetworkTypesForReason(
+                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS, EXPECTED_DISABLED);
+    }
+
+    @Test
+    public void handleUserRestrictionsChanged_manySubscriptionsAllow2g_allowedNetworkUpdated() {
+
+        // Two subscriptions are available
+        when(mMockSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
+                Arrays.asList(getSubInfo(1), getSubInfo(2)));
+        TelephonyManager tmSubscription1 = mock(TelephonyManager.class);
+        TelephonyManager tmSubscription2 = mock(TelephonyManager.class);
+        when(mMockTelephonyManager.createForSubscriptionId(1)).thenReturn(tmSubscription1);
+        when(mMockTelephonyManager.createForSubscriptionId(2)).thenReturn(tmSubscription2);
+
+        // 2g is allowed
+        when(mMockUserManager.hasUserRestriction(UserManager.DISALLOW_CELLULAR_2G)).thenReturn(
+                false);
+
+        mTelephony2gUpdater.handleUserRestrictionsChanged(getTestContext());
+
+        verify(tmSubscription1, times(1)).setAllowedNetworkTypesForReason(
+                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS, EXPECTED_ENABLED);
+        verify(tmSubscription1, times(1)).setAllowedNetworkTypesForReason(
+                TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS, EXPECTED_ENABLED);
+    }
+
+    private SubscriptionInfo getSubInfo(int id) {
+        return new SubscriptionInfo(id, "890126042XXXXXXXXXXX", 0, "T-mobile", "T-mobile", 0, 255,
+                "12345", 0, null, "310", "260", "156", false, null, null);
+    }
+}