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 final TelephonyManager mTelephonyManager; 42 SimLockPreferenceController(Context context)43 public SimLockPreferenceController(Context context) { 44 super(context, KEY_SIM_LOCK); 45 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 46 mCarrierConfigManager = (CarrierConfigManager) 47 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 48 mSubscriptionManager = (SubscriptionManager) context 49 .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); 50 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 51 } 52 53 @Override getAvailabilityStatus()54 public int getAvailabilityStatus() { 55 final PersistableBundle b = mCarrierConfigManager.getConfig(); 56 final boolean IsAdmin = mUserManager.isAdminUser(); 57 if (!IsAdmin || !isSimIccReady() || 58 b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) { 59 return DISABLED_FOR_USER; 60 } 61 return AVAILABLE; 62 } 63 64 @Override displayPreference(PreferenceScreen screen)65 public void displayPreference(PreferenceScreen screen) { 66 super.displayPreference(screen); 67 final Preference preference = screen.findPreference(getPreferenceKey()); 68 if (preference == null) { 69 return; 70 } 71 // Disable SIM lock if there is no ready SIM card. 72 preference.setEnabled(isSimReady()); 73 } 74 75 /* Return true if a SIM is ready for locking. 76 * TODO: consider adding to TelephonyManager or SubscritpionManasger. 77 */ isSimReady()78 private boolean isSimReady() { 79 final List<SubscriptionInfo> subInfoList = 80 mSubscriptionManager.getActiveSubscriptionInfoList(true); 81 if (subInfoList != null) { 82 for (SubscriptionInfo subInfo : subInfoList) { 83 final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex()); 84 if ((simState != TelephonyManager.SIM_STATE_ABSENT) && 85 (simState != TelephonyManager.SIM_STATE_UNKNOWN)) { 86 return true; 87 } 88 } 89 } 90 return false; 91 } 92 93 /** 94 * Return true if a there is a Slot that has Icc 95 */ isSimIccReady()96 private boolean isSimIccReady() { 97 final List<SubscriptionInfo> subInfoList = 98 mSubscriptionManager.getActiveSubscriptionInfoList(true); 99 100 if (subInfoList != null) { 101 for (SubscriptionInfo subInfo : subInfoList) { 102 if (mTelephonyManager.hasIccCard(subInfo.getSimSlotIndex())) { 103 return true; 104 } 105 } 106 } 107 return false; 108 } 109 } 110