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         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 List<SubscriptionInfo> subInfoList =
56                 mSubscriptionManager.getActiveSubscriptionInfoList();
57 
58         if (subInfoList == null) {
59             return DISABLED_FOR_USER;
60         }
61 
62         final boolean isAdmin = mUserManager.isAdminUser();
63         if (isAdmin && (!isHideSimLockSetting(subInfoList))) {
64             return AVAILABLE;
65         }
66 
67         return DISABLED_FOR_USER;
68     }
69 
70     @Override
displayPreference(PreferenceScreen screen)71     public void displayPreference(PreferenceScreen screen) {
72         super.displayPreference(screen);
73         final Preference preference = screen.findPreference(getPreferenceKey());
74         if (preference == null) {
75             return;
76         }
77         // Disable SIM lock if there is no ready SIM card.
78         preference.setEnabled(isSimReady());
79     }
80 
81     /* Return true if a SIM is ready for locking.
82      * TODO: consider adding to TelephonyManager or SubscritpionManasger.
83      */
isSimReady()84     private boolean isSimReady() {
85         final List<SubscriptionInfo> subInfoList =
86                 mSubscriptionManager.getActiveSubscriptionInfoList();
87         if (subInfoList == null) {
88             return false;
89         }
90 
91         for (SubscriptionInfo subInfo : subInfoList) {
92             final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex());
93             if ((simState != TelephonyManager.SIM_STATE_ABSENT)
94                     && (simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
95                 return true;
96             }
97         }
98         return false;
99     }
100 
isHideSimLockSetting(List<SubscriptionInfo> subInfoList)101     private boolean isHideSimLockSetting(List<SubscriptionInfo> subInfoList) {
102         if (subInfoList == null) {
103             return true;
104         }
105 
106         for (SubscriptionInfo subInfo : subInfoList) {
107             final TelephonyManager telephonyManager = mTelephonyManager
108                     .createForSubscriptionId(subInfo.getSubscriptionId());
109             final PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(
110                     subInfo.getSubscriptionId());
111             if (telephonyManager.hasIccCard() && bundle != null
112                     && !bundle.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
113                 // one or more sims show sim lock setting UI.
114                 return false;
115             }
116         }
117 
118         return true;
119     }
120 }
121