blob: 0919385dccc456c3bfc199e034f763a15afcd1bb [file] [log] [blame]
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00001/*
2 * Copyright (C) 2022 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
17package com.android.phone;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.UserManager;
24import android.telephony.RadioAccessFamily;
25import android.telephony.SubscriptionInfo;
26import android.telephony.SubscriptionManager;
27import android.telephony.TelephonyManager;
28import android.util.Log;
29
30import com.android.internal.annotations.VisibleForTesting;
31import com.android.internal.telephony.RILConstants;
32
33import java.util.List;
34import java.util.concurrent.Executor;
35
36/**
37 * A {@link BroadcastReceiver} that ensures that user restrictions are correctly applied to
38 * telephony.
39 * This includes handling broadcasts from user restriction state changes, as well as ensuring that
40 * SIM-specific settings are correctly applied when new subscriptions become active.
41 *
42 * Callers are expected to call {@code init()} and keep an instance of this class alive.
43 */
44public class Telephony2gUpdater extends BroadcastReceiver {
45 private static final String TAG = "TelephonyUserManagerReceiver";
46
47 // We can't interact with the HAL on the main thread of the phone process (where
48 // receivers are run by default), so we execute our logic from a separate thread.
49 private final Executor mExecutor;
50 private final Context mContext;
51 private final long mBaseAllowedNetworks;
52
53 public Telephony2gUpdater(Executor executor, Context context) {
54 this(executor, context,
55 RadioAccessFamily.getRafFromNetworkType(RILConstants.PREFERRED_NETWORK_MODE));
56 }
57
58 public Telephony2gUpdater(Executor executor, Context context,
59 long baseAllowedNetworks) {
60 mExecutor = executor;
61 mContext = context;
62 mBaseAllowedNetworks = baseAllowedNetworks;
63 }
64
65 /**
66 * Register the given instance as a {@link BroadcastReceiver} and a {@link
67 * SubscriptionManager.OnSubscriptionsChangedListener}.
68 */
69 public void init() {
70 mContext.getSystemService(SubscriptionManager.class).addOnSubscriptionsChangedListener(
71 mExecutor, new SubscriptionListener());
72 IntentFilter filter = new IntentFilter();
73 filter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED);
74 mContext.registerReceiver(this, filter);
75 }
76
77 @Override
78 public void onReceive(Context context, Intent intent) {
79 if (context == null || intent == null) return;
80 Log.i(TAG, "Received callback for action " + intent.getAction());
81 final PendingResult result = goAsync();
82 mExecutor.execute(() -> {
83 Log.i(TAG, "Running handler for action " + intent.getAction());
84 handleUserRestrictionsChanged(context);
85 result.finish();
86 });
87 }
88
89 /**
90 * Update all active subscriptions with allowed network types depending on the current state
91 * of the {@link UserManager.DISALLOW_2G}.
92 */
93 @VisibleForTesting
94 public void handleUserRestrictionsChanged(Context context) {
95 UserManager um = context.getSystemService(UserManager.class);
96 TelephonyManager tm = context.getSystemService(TelephonyManager.class);
97 SubscriptionManager sm = context.getSystemService(SubscriptionManager.class);
98 final long twoGBitmask = TelephonyManager.NETWORK_CLASS_BITMASK_2G;
99
100 boolean shouldDisable2g = um.hasUserRestriction(UserManager.DISALLOW_CELLULAR_2G);
101
102 // This is expected when subscription info cannot be determined. We'll get another
103 // callback in the future from our SubscriptionListener once we have valid subscriptions.
104 List<SubscriptionInfo> subscriptionInfoList = sm.getAvailableSubscriptionInfoList();
105 if (subscriptionInfoList == null) {
106 return;
107 }
108
109 long allowedNetworkTypes = mBaseAllowedNetworks;
110
111 // 2G device admin controls are global
112 for (SubscriptionInfo info : subscriptionInfoList) {
113 TelephonyManager telephonyManager = tm.createForSubscriptionId(
114 info.getSubscriptionId());
115 if (shouldDisable2g) {
116 allowedNetworkTypes &= ~twoGBitmask;
117 } else {
118 allowedNetworkTypes |= twoGBitmask;
119 }
120 telephonyManager.setAllowedNetworkTypesForReason(
121 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS,
122 allowedNetworkTypes);
123 }
124 }
125
126 private class SubscriptionListener extends SubscriptionManager.OnSubscriptionsChangedListener {
127 @Override
128 public void onSubscriptionsChanged() {
129 Log.i(TAG, "Running handler for subscription change.");
130 handleUserRestrictionsChanged(mContext);
131 }
132 }
133
134}