1 /* 2 * Copyright (C) 2018 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.settings.security; 18 19 import android.content.Context; 20 import android.os.PersistableBundle; 21 import android.os.UserManager; 22 import android.telephony.CarrierConfigManager; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 import android.telephony.TelephonyManager; 26 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 31 32 import java.util.List; 33 34 public class SimLockPreferenceController extends BasePreferenceController { 35 36 private static final String KEY_SIM_LOCK = "sim_lock_settings"; 37 38 private final CarrierConfigManager mCarrierConfigManager; 39 private final UserManager mUserManager; 40 private final SubscriptionManager mSubscriptionManager; 41 private TelephonyManager mTelephonyManager; 42 SimLockPreferenceController(Context context)43 public SimLockPreferenceController(Context context) { 44 this(context, KEY_SIM_LOCK); 45 } 46 SimLockPreferenceController(Context context, String key)47 public SimLockPreferenceController(Context context, String key) { 48 super(context, key); 49 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 50 mCarrierConfigManager = (CarrierConfigManager) 51 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 52 mSubscriptionManager = (SubscriptionManager) context 53 .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); 54 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 55 } 56 57 @Override getAvailabilityStatus()58 public int getAvailabilityStatus() { 59 final List<SubscriptionInfo> subInfoList = 60 mSubscriptionManager.getActiveSubscriptionInfoList(); 61 62 if (subInfoList == null) { 63 return DISABLED_FOR_USER; 64 } 65 66 final boolean isAdmin = mUserManager.isAdminUser(); 67 if (isAdmin && (!isHideSimLockSetting(subInfoList))) { 68 return AVAILABLE; 69 } 70 71 return DISABLED_FOR_USER; 72 } 73 74 @Override displayPreference(PreferenceScreen screen)75 public void displayPreference(PreferenceScreen screen) { 76 super.displayPreference(screen); 77 final Preference preference = screen.findPreference(getPreferenceKey()); 78 if (preference == null) { 79 return; 80 } 81 // Disable SIM lock if there is no ready SIM card. 82 preference.setEnabled(isSimReady()); 83 } 84 85 /* Return true if a SIM is ready for locking. 86 * TODO: consider adding to TelephonyManager or SubscritpionManasger. 87 */ isSimReady()88 private boolean isSimReady() { 89 final List<SubscriptionInfo> subInfoList = 90 mSubscriptionManager.getActiveSubscriptionInfoList(); 91 if (subInfoList == null) { 92 return false; 93 } 94 95 for (SubscriptionInfo subInfo : subInfoList) { 96 final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex()); 97 if ((simState != TelephonyManager.SIM_STATE_ABSENT) 98 && (simState != TelephonyManager.SIM_STATE_UNKNOWN)) { 99 return true; 100 } 101 } 102 return false; 103 } 104 isHideSimLockSetting(List<SubscriptionInfo> subInfoList)105 private boolean isHideSimLockSetting(List<SubscriptionInfo> subInfoList) { 106 if (subInfoList == null) { 107 return true; 108 } 109 110 for (SubscriptionInfo subInfo : subInfoList) { 111 final TelephonyManager telephonyManager = mTelephonyManager 112 .createForSubscriptionId(subInfo.getSubscriptionId()); 113 final PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId( 114 subInfo.getSubscriptionId()); 115 if (telephonyManager.hasIccCard() && bundle != null 116 && !bundle.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) { 117 // one or more sims show sim lock setting UI. 118 return false; 119 } 120 } 121 122 return true; 123 } 124 } 125