• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.settings.network.SubscriptionUtil;
32 
33 import java.util.List;
34 
35 public class SimLockPreferenceController extends BasePreferenceController {
36 
37     private final CarrierConfigManager mCarrierConfigManager;
38     private final UserManager mUserManager;
39     private final SubscriptionManager mSubscriptionManager;
40     private TelephonyManager mTelephonyManager;
41 
SimLockPreferenceController(Context context, String key)42     public SimLockPreferenceController(Context context, String key) {
43         super(context, key);
44         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
45         mCarrierConfigManager = (CarrierConfigManager)
46                 mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
47         mSubscriptionManager = ((SubscriptionManager) context
48                 .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
49                 .createForAllUserProfiles();
50         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         if (!SubscriptionUtil.isSimHardwareVisible(mContext)) {
56             return UNSUPPORTED_ON_DEVICE;
57         }
58 
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                     && (simState != TelephonyManager.SIM_STATE_PERM_DISABLED)) {
100                 return true;
101             }
102         }
103         return false;
104     }
105 
isHideSimLockSetting(List<SubscriptionInfo> subInfoList)106     private boolean isHideSimLockSetting(List<SubscriptionInfo> subInfoList) {
107         if (subInfoList == null) {
108             return true;
109         }
110 
111         for (SubscriptionInfo subInfo : subInfoList) {
112             final TelephonyManager telephonyManager = mTelephonyManager
113                     .createForSubscriptionId(subInfo.getSubscriptionId());
114             final PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(
115                     subInfo.getSubscriptionId());
116             if (telephonyManager.hasIccCard() && bundle != null
117                     && !bundle.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
118                 // one or more sims show sim lock setting UI.
119                 return false;
120             }
121         }
122 
123         return true;
124     }
125 }
126