Introduce Telephony2gUpdater to Apply 2g User Restrictions

Listens for the following actions and ensures that 2g is enabled
or disabled based on the state of the `DISALLOW_2G` user
restriction. 2g is enabled or disabled for all active subscriptions
on the device.

* A broadcast for action `android.os.action.USER_RESTRICTIONS_CHANGED`
* A callback from `SubscriptionManager` informing us of a subscription
state change

Test: atest Telephony2gUpdaterTest
Bug: 230669497
Change-Id: I819d412bc14cb98a5874d2a42b50d4e4483b4b02
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 66cef64..837df86 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);
+        }
+    }
+
+}