• 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         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
50     }
51 
52     @Override
getAvailabilityStatus()53     public int getAvailabilityStatus() {
54         if (!SubscriptionUtil.isSimHardwareVisible(mContext)) {
55             return UNSUPPORTED_ON_DEVICE;
56         }
57 
58         final List<SubscriptionInfo> subInfoList =
59                 mSubscriptionManager.getActiveSubscriptionInfoList();
60 
61         if (subInfoList == null) {
62             return DISABLED_FOR_USER;
63         }
64 
65         final boolean isAdmin = mUserManager.isAdminUser();
66         if (isAdmin && (!isHideSimLockSetting(subInfoList))) {
67             return AVAILABLE;
68         }
69 
70         return DISABLED_FOR_USER;
71     }
72 
73     @Override
displayPreference(PreferenceScreen screen)74     public void displayPreference(PreferenceScreen screen) {
75         super.displayPreference(screen);
76         final Preference preference = screen.findPreference(getPreferenceKey());
77         if (preference == null) {
78             return;
79         }
80         // Disable SIM lock if there is no ready SIM card.
81         preference.setEnabled(isSimReady());
82     }
83 
84     /* Return true if a SIM is ready for locking.
85      * TODO: consider adding to TelephonyManager or SubscritpionManasger.
86      */
isSimReady()87     private boolean isSimReady() {
88         final List<SubscriptionInfo> subInfoList =
89                 mSubscriptionManager.getActiveSubscriptionInfoList();
90         if (subInfoList == null) {
91             return false;
92         }
93 
94         for (SubscriptionInfo subInfo : subInfoList) {
95             final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex());
96             if ((simState != TelephonyManager.SIM_STATE_ABSENT)
97                     && (simState != TelephonyManager.SIM_STATE_UNKNOWN)
98                     && (simState != TelephonyManager.SIM_STATE_PERM_DISABLED)) {
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