• 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.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.telephony.CarrierConfigManager;
25 import android.telephony.SubscriptionInfo;
26 import android.telephony.SubscriptionManager;
27 import android.telephony.TelephonyManager;
28 
29 import com.android.settings.core.BasePreferenceController;
30 
31 import java.util.List;
32 
33 public class SimLockPreferenceController extends BasePreferenceController {
34 
35     private static final String KEY_SIM_LOCK = "sim_lock_settings";
36 
37     private final CarrierConfigManager mCarrierConfigManager;
38     private final UserManager mUserManager;
39     private final SubscriptionManager mSubscriptionManager;
40     private final TelephonyManager mTelephonyManager;
41 
SimLockPreferenceController(Context context)42     public SimLockPreferenceController(Context context) {
43         super(context, KEY_SIM_LOCK);
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         final PersistableBundle b = mCarrierConfigManager.getConfig();
55         final boolean IsAdmin = mUserManager.isAdminUser();
56         if (!IsAdmin || !isSimIccReady() ||
57                 b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
58             return DISABLED_FOR_USER;
59         }
60         return AVAILABLE;
61     }
62 
63     @Override
displayPreference(PreferenceScreen screen)64     public void displayPreference(PreferenceScreen screen) {
65         super.displayPreference(screen);
66         final Preference preference = screen.findPreference(getPreferenceKey());
67         if (preference == null) {
68             return;
69         }
70         // Disable SIM lock if there is no ready SIM card.
71         preference.setEnabled(isSimReady());
72     }
73 
74     /* Return true if a SIM is ready for locking.
75      * TODO: consider adding to TelephonyManager or SubscritpionManasger.
76      */
isSimReady()77     private boolean isSimReady() {
78         final List<SubscriptionInfo> subInfoList =
79                 mSubscriptionManager.getActiveSubscriptionInfoList();
80         if (subInfoList != null) {
81             for (SubscriptionInfo subInfo : subInfoList) {
82                 final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex());
83                 if ((simState != TelephonyManager.SIM_STATE_ABSENT) &&
84                         (simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
85                     return true;
86                 }
87             }
88         }
89         return false;
90     }
91 
92     /**
93      * Return true if a there is a Slot that has Icc
94      */
isSimIccReady()95     private boolean isSimIccReady() {
96         final List<SubscriptionInfo> subInfoList =
97                 mSubscriptionManager.getActiveSubscriptionInfoList();
98 
99         if (subInfoList != null) {
100             for (SubscriptionInfo subInfo : subInfoList) {
101                 if (mTelephonyManager.hasIccCard(subInfo.getSimSlotIndex())) {
102                     return true;
103                 }
104             }
105         }
106         return false;
107     }
108 }
109