1 /* 2 * Copyright (C) 2023 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 17 package com.android.internal.telephony; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.UserManager; 24 import android.telephony.Rlog; 25 26 /** 27 * A BroadcastReceiver for device administration events. 28 */ 29 public class TelephonyAdminReceiver extends BroadcastReceiver { 30 private static final String TAG = "TelephonyAdminReceiver"; 31 private final Phone mPhone; 32 // We keep track of the last value to avoid updating when unrelated user restrictions change 33 private boolean mDisallowCellular2gRestriction = false; 34 private final Context mContext; 35 private UserManager mUserManager; 36 TelephonyAdminReceiver(Context context, Phone phone)37 public TelephonyAdminReceiver(Context context, Phone phone) { 38 mContext = context; 39 mPhone = phone; 40 mUserManager = null; 41 if (ensureUserManagerExists()) { 42 mDisallowCellular2gRestriction = mUserManager.hasUserRestriction( 43 UserManager.DISALLOW_CELLULAR_2G); 44 } 45 IntentFilter filter = new IntentFilter(); 46 filter.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED); 47 context.registerReceiver(this, filter); 48 } 49 50 @Override onReceive(Context context, Intent intent)51 public void onReceive(Context context, Intent intent) { 52 Rlog.d(TAG, "Processing onReceive"); 53 if (context == null || intent == null) return; 54 if (!intent.getAction().equals(UserManager.ACTION_USER_RESTRICTIONS_CHANGED)) { 55 Rlog.d(TAG, "Ignoring unexpected action: " + intent.getAction()); 56 return; 57 } 58 if (!ensureUserManagerExists()) { 59 return; 60 } 61 boolean shouldDisallow2g = mUserManager.hasUserRestriction( 62 UserManager.DISALLOW_CELLULAR_2G); 63 64 if (shouldDisallow2g != mDisallowCellular2gRestriction) { 65 Rlog.i(TAG, 66 "Updating allowed network types with new admin 2g restriction. no_cellular_2g: " 67 + shouldDisallow2g); 68 mDisallowCellular2gRestriction = shouldDisallow2g; 69 mPhone.sendSubscriptionSettings(false); 70 } else { 71 Rlog.i(TAG, "Skipping update of allowed network types. Restriction no_cellular_2g " 72 + "unchanged: " + mDisallowCellular2gRestriction); 73 } 74 } 75 76 /** 77 * Returns the current state of the {@link UserManager#DISALLOW_CELLULAR_2G} user restriction. 78 */ isCellular2gDisabled()79 public boolean isCellular2gDisabled() { 80 return mDisallowCellular2gRestriction; 81 } 82 83 /** 84 * Tries to resolve the user manager system service. Returns true if successful, false 85 * otherwise. 86 */ ensureUserManagerExists()87 private boolean ensureUserManagerExists() { 88 if (mUserManager == null) { 89 Rlog.d(TAG, "No user manager. Attempting to resolve one."); 90 mUserManager = mContext.getSystemService(UserManager.class); 91 } 92 if (mUserManager == null) { 93 Rlog.e(TAG, 94 "Could not get a user manager instance. All operations will be no-ops until " 95 + "one is resolved"); 96 return false; 97 } 98 return true; 99 } 100 } 101