Convert some security setting logic to PreferenceController

- Manage trust agent
- Show password
- Sim lock
- Screen pinning

Bug: 32953042
Test: robotests
Change-Id: I0c781a505238cae7a6643b701b750ca63a87a8a5
This commit is contained in:
Fan Zhang
2018-01-02 14:17:40 -08:00
parent f314494f32
commit 0b47bb4bf3
13 changed files with 840 additions and 137 deletions

View File

@@ -14,8 +14,8 @@
package com.android.settings.core;
import android.content.Context;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
/**
* Abstract class that consolidates logic for updating toggle controllers.
@@ -45,7 +45,7 @@ public abstract class TogglePreferenceController extends BasePreferenceControlle
@Override
public final void updateState(Preference preference) {
((SwitchPreference) preference).setChecked(isChecked());
((TwoStatePreference) preference).setChecked(isChecked());
}
@Override

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class ScreenPinningPreferenceController extends BasePreferenceController {
private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
public ScreenPinningPreferenceController(Context context) {
super(context, KEY_SCREEN_PINNING);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference preference = screen.findPreference(getPreferenceKey());
if (preference == null) {
return;
}
if (Settings.System.getInt(mContext.getContentResolver(),
Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
preference.setSummary(
mContext.getString(R.string.switch_on_text));
} else {
preference.setSummary(
mContext.getString(R.string.switch_off_text));
}
}
}

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.support.v7.preference.PreferenceScreen;
import android.util.FeatureFlagUtils;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.FeatureFlags;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settingslib.drawer.DashboardCategory;
@@ -38,4 +39,9 @@ public interface SecurityFeatureProvider {
/** Returns the {@link TrustAgentManager} bound to this {@link SecurityFeatureProvider}. */
TrustAgentManager getTrustAgentManager();
/**
* Returns a {@link LockPatternUtils} instance bound to application context.
*/
LockPatternUtils getLockPatternUtils(Context context);
}

View File

@@ -29,6 +29,7 @@ import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Pair;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settingslib.drawer.DashboardCategory;
@@ -43,6 +44,7 @@ import java.util.TreeMap;
public class SecurityFeatureProviderImpl implements SecurityFeatureProvider {
private TrustAgentManager mTrustAgentManager;
private LockPatternUtils mLockPatternUtils;
@VisibleForTesting
static final Drawable DEFAULT_ICON = null;
@@ -195,4 +197,12 @@ public class SecurityFeatureProviderImpl implements SecurityFeatureProvider {
}
return mTrustAgentManager;
}
@Override
public LockPatternUtils getLockPatternUtils(Context context) {
if (mLockPatternUtils == null) {
mLockPatternUtils = new LockPatternUtils(context.getApplicationContext());
}
return mLockPatternUtils;
}
}

View File

@@ -7,21 +7,15 @@ import android.content.Intent;
import android.content.res.Resources;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.StorageManager;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.android.internal.logging.nano.MetricsProto;
@@ -43,6 +37,7 @@ import com.android.settings.password.ManagedLockPasswordProvider;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.security.screenlock.ScreenLockSettings;
import com.android.settings.security.trustagent.ManageTrustAgentsPreferenceController;
import com.android.settings.security.trustagent.TrustAgentManager;
import com.android.settings.widget.GearPreference;
import com.android.settingslib.RestrictedLockUtils;
@@ -67,13 +62,11 @@ public class SecuritySettingsV2 extends DashboardFragment
private static final String KEY_UNLOCK_SET_OR_CHANGE_PROFILE = "unlock_set_or_change_profile";
private static final String KEY_VISIBLE_PATTERN_PROFILE = "visiblepattern_profile";
private static final String KEY_SECURITY_CATEGORY = "security_category";
@VisibleForTesting
static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
private static final String KEY_UNIFICATION = "unification";
@VisibleForTesting
static final String KEY_LOCKSCREEN_PREFERENCES = "lockscreen_preferences";
private static final String KEY_ENCRYPTION_AND_CREDENTIALS = "encryption_and_credential";
private static final String KEY_LOCATION_SCANNING = "location_scanning";
private static final String KEY_LOCATION_SCANNING = "location_scanning";
private static final String KEY_LOCATION = "location";
private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
@@ -85,10 +78,7 @@ public class SecuritySettingsV2 extends DashboardFragment
private static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
// Misc Settings
private static final String KEY_SIM_LOCK = "sim_lock_settings";
private static final String KEY_SHOW_PASSWORD = "show_password";
private static final String KEY_TRUST_AGENT = "trust_agent";
private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
// Security status
private static final String KEY_SECURITY_STATUS = "security_status";
@@ -100,7 +90,7 @@ public class SecuritySettingsV2 extends DashboardFragment
// These switch preferences need special handling since they're not all stored in Settings.
private static final String SWITCH_PREFERENCE_KEYS[] = {
KEY_SHOW_PASSWORD, KEY_UNIFICATION, KEY_VISIBLE_PATTERN_PROFILE
KEY_UNIFICATION, KEY_VISIBLE_PATTERN_PROFILE
};
private static final int MY_USER_ID = UserHandle.myUserId();
@@ -109,7 +99,6 @@ public class SecuritySettingsV2 extends DashboardFragment
private DevicePolicyManager mDPM;
private SecurityFeatureProvider mSecurityFeatureProvider;
private TrustAgentManager mTrustAgentManager;
private SubscriptionManager mSubscriptionManager;
private UserManager mUm;
private ChooseLockSettingsHelper mChooseLockSettingsHelper;
@@ -119,10 +108,6 @@ public class SecuritySettingsV2 extends DashboardFragment
private SwitchPreference mVisiblePatternProfile;
private RestrictedSwitchPreference mUnifyProfile;
private SwitchPreference mShowPassword;
private boolean mIsAdmin;
private Intent mTrustAgentClickIntent;
private int mProfileChallengeUserId;
@@ -134,6 +119,10 @@ public class SecuritySettingsV2 extends DashboardFragment
private ManageDeviceAdminPreferenceController mManageDeviceAdminPreferenceController;
private EnterprisePrivacyPreferenceController mEnterprisePrivacyPreferenceController;
private LockScreenNotificationPreferenceController mLockScreenNotificationPreferenceController;
private ManageTrustAgentsPreferenceController mManageTrustAgentsPreferenceController;
private ScreenPinningPreferenceController mScreenPinningPreferenceController;
private SimLockPreferenceController mSimLockPreferenceController;
private ShowPasswordPreferenceController mShowPasswordPreferenceController;
@Override
public int getMetricsCategory() {
@@ -143,15 +132,15 @@ public class SecuritySettingsV2 extends DashboardFragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
mSecurityFeatureProvider = FeatureFactory.getFactory(context).getSecurityFeatureProvider();
mLocationController = new LocationPreferenceController(context, getLifecycle());
mSubscriptionManager = SubscriptionManager.from(context);
mLockPatternUtils = new LockPatternUtils(context);
mLockPatternUtils = mSecurityFeatureProvider.getLockPatternUtils(context);
mManagedPasswordProvider = ManagedLockPasswordProvider.get(context, MY_USER_ID);
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mUm = UserManager.get(context);
mDashboardFeatureProvider = FeatureFactory.getFactory(context)
.getDashboardFeatureProvider(context);
mSecurityFeatureProvider = FeatureFactory.getFactory(context).getSecurityFeatureProvider();
mTrustAgentManager = mSecurityFeatureProvider.getTrustAgentManager();
}
@@ -173,6 +162,10 @@ public class SecuritySettingsV2 extends DashboardFragment
= new EnterprisePrivacyPreferenceController(context);
mLockScreenNotificationPreferenceController
= new LockScreenNotificationPreferenceController(context);
mManageTrustAgentsPreferenceController = new ManageTrustAgentsPreferenceController(context);
mScreenPinningPreferenceController = new ScreenPinningPreferenceController(context);
mSimLockPreferenceController = new SimLockPreferenceController(context);
mShowPasswordPreferenceController = new ShowPasswordPreferenceController(context);
return null;
}
@@ -182,7 +175,6 @@ public class SecuritySettingsV2 extends DashboardFragment
mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity());
if (savedInstanceState != null
&& savedInstanceState.containsKey(TRUST_AGENT_CLICK_INTENT)) {
mTrustAgentClickIntent = savedInstanceState.getParcelable(TRUST_AGENT_CLICK_INTENT);
@@ -280,15 +272,13 @@ public class SecuritySettingsV2 extends DashboardFragment
((GearPreference) unlockSetOrChange).setOnGearClickListener(this);
}
mIsAdmin = mUm.isAdminUser();
// Fingerprint and trust agents
int numberOfTrustAgent = 0;
PreferenceGroup securityCategory = (PreferenceGroup)
root.findPreference(KEY_SECURITY_CATEGORY);
if (securityCategory != null) {
maybeAddFingerprintPreference(securityCategory, UserHandle.myUserId());
numberOfTrustAgent = addTrustAgentSettings(securityCategory);
addTrustAgentSettings(securityCategory);
setLockscreenPreferencesSummary(securityCategory);
}
@@ -296,24 +286,8 @@ public class SecuritySettingsV2 extends DashboardFragment
(SwitchPreference) root.findPreference(KEY_VISIBLE_PATTERN_PROFILE);
mUnifyProfile = (RestrictedSwitchPreference) root.findPreference(KEY_UNIFICATION);
// Do not display SIM lock for devices without an Icc card
TelephonyManager tm = TelephonyManager.getDefault();
CarrierConfigManager cfgMgr = (CarrierConfigManager)
getActivity().getSystemService(Context.CARRIER_CONFIG_SERVICE);
PersistableBundle b = cfgMgr.getConfig();
if (!mIsAdmin || !isSimIccReady() ||
b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
root.removePreference(root.findPreference(KEY_SIM_LOCK));
} else {
// Disable SIM lock if there is no ready SIM card.
root.findPreference(KEY_SIM_LOCK).setEnabled(isSimReady());
}
if (Settings.System.getInt(getContentResolver(),
Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
root.findPreference(KEY_SCREEN_PINNING).setSummary(
getResources().getString(R.string.switch_on_text));
}
mSimLockPreferenceController.displayPreference(root);
mScreenPinningPreferenceController.displayPreference(root);
// Encryption status of device
if (LockPatternUtils.isDeviceEncryptionEnabled()) {
@@ -324,14 +298,8 @@ public class SecuritySettingsV2 extends DashboardFragment
R.string.summary_placeholder);
}
// Show password
mShowPassword = (SwitchPreference) root.findPreference(KEY_SHOW_PASSWORD);
// Credential storage
final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
// Advanced Security features
initTrustAgentPreference(root, numberOfTrustAgent);
mManageTrustAgentsPreferenceController.displayPreference(root);
PreferenceGroup securityStatusPreferenceGroup =
(PreferenceGroup) root.findPreference(KEY_SECURITY_STATUS);
@@ -380,23 +348,6 @@ public class SecuritySettingsV2 extends DashboardFragment
return root;
}
@VisibleForTesting
void initTrustAgentPreference(PreferenceScreen root, int numberOfTrustAgent) {
Preference manageAgents = root.findPreference(KEY_MANAGE_TRUST_AGENTS);
if (manageAgents != null) {
if (!mLockPatternUtils.isSecure(MY_USER_ID)) {
manageAgents.setEnabled(false);
manageAgents.setSummary(R.string.disabled_because_no_backup_security);
} else if (numberOfTrustAgent > 0) {
manageAgents.setSummary(getActivity().getResources().getQuantityString(
R.plurals.manage_trust_agents_summary_on,
numberOfTrustAgent, numberOfTrustAgent));
} else {
manageAgents.setSummary(R.string.manage_trust_agents_summary);
}
}
}
@VisibleForTesting
void setLockscreenPreferencesSummary(PreferenceGroup group) {
final Preference lockscreenPreferences = group.findPreference(KEY_LOCKSCREEN_PREFERENCES);
@@ -411,8 +362,9 @@ public class SecuritySettingsV2 extends DashboardFragment
* The preference must be a RestrictedPreference.
*/
private void disableIfPasswordQualityManaged(String preferenceKey, int userId) {
final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
getActivity(), userId);
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtils.checkIfPasswordQualityIsSet(
getActivity(), userId);
if (admin != null && mDPM.getPasswordQuality(admin.component, userId) ==
DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
final RestrictedPreference pref =
@@ -433,8 +385,8 @@ public class SecuritySettingsV2 extends DashboardFragment
// Return the number of trust agents being added
private int addTrustAgentSettings(PreferenceGroup securityCategory) {
final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
final List<TrustAgentManager.TrustAgentComponentInfo> agents = mTrustAgentManager.getActiveTrustAgents(
getActivity(), mLockPatternUtils);
final List<TrustAgentManager.TrustAgentComponentInfo> agents =
mTrustAgentManager.getActiveTrustAgents(getActivity(), mLockPatternUtils);
for (TrustAgentManager.TrustAgentComponentInfo agent : agents) {
final RestrictedPreference trustAgentPreference =
new RestrictedPreference(securityCategory.getContext());
@@ -458,43 +410,6 @@ public class SecuritySettingsV2 extends DashboardFragment
return agents.size();
}
/* Return true if a there is a Slot that has Icc.
*/
private boolean isSimIccReady() {
TelephonyManager tm = TelephonyManager.getDefault();
final List<SubscriptionInfo> subInfoList =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList != null) {
for (SubscriptionInfo subInfo : subInfoList) {
if (tm.hasIccCard(subInfo.getSimSlotIndex())) {
return true;
}
}
}
return false;
}
/* Return true if a SIM is ready for locking.
* TODO: consider adding to TelephonyManager or SubscritpionManasger.
*/
private boolean isSimReady() {
final List<SubscriptionInfo> subInfoList =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList != null) {
for (SubscriptionInfo subInfo : subInfoList) {
final int simState = TelephonyManager.getDefault()
.getSimState(subInfo.getSimSlotIndex());
if ((simState != TelephonyManager.SIM_STATE_ABSENT) &&
(simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
return true;
}
}
}
return false;
}
@Override
public void onGearClick(GearPreference p) {
if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) {
@@ -524,12 +439,10 @@ public class SecuritySettingsV2 extends DashboardFragment
}
updateUnificationPreference();
if (mShowPassword != null) {
mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
}
final Preference showPasswordPref = getPreferenceScreen().findPreference(
mShowPasswordPreferenceController.getPreferenceKey());
showPasswordPref.setOnPreferenceChangeListener(mShowPasswordPreferenceController);
mShowPasswordPreferenceController.updateState(showPasswordPref);
mLocationController.updateSummary();
}
@@ -581,7 +494,7 @@ public class SecuritySettingsV2 extends DashboardFragment
mTrustAgentClickIntent = preference.getIntent();
boolean confirmationLaunched = helper.launchConfirmationActivity(
CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle());
if (!confirmationLaunched&& mTrustAgentClickIntent != null) {
if (!confirmationLaunched && mTrustAgentClickIntent != null) {
// If this returns false, it means no password confirmation is required.
startActivity(mTrustAgentClickIntent);
mTrustAgentClickIntent = null;
@@ -715,15 +628,11 @@ public class SecuritySettingsV2 extends DashboardFragment
R.string.unlock_set_unlock_launch_picker_title);
final ChooseLockSettingsHelper helper =
new ChooseLockSettingsHelper(getActivity(), this);
if(!helper.launchConfirmationActivity(
if (!helper.launchConfirmationActivity(
UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) {
ununifyLocks();
}
}
} else if (KEY_SHOW_PASSWORD.equals(key)) {
Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
((Boolean) value) ? 1 : 0);
lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID);
}
return result;
}
@@ -788,8 +697,9 @@ public class SecuritySettingsV2 extends DashboardFragment
}
private boolean isPasswordManaged(int userId, Context context, DevicePolicyManager dpm) {
final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
context, userId);
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtils.checkIfPasswordQualityIsSet(
context, userId);
return admin != null && dpm.getPasswordQuality(admin.component, userId) ==
DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
}
@@ -847,20 +757,13 @@ public class SecuritySettingsV2 extends DashboardFragment
@Override
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
// Do not display SIM lock for devices without an Icc card
final UserManager um = UserManager.get(context);
final TelephonyManager tm = TelephonyManager.from(context);
if (!um.isAdminUser() || !tm.hasIccCard()) {
keys.add(KEY_SIM_LOCK);
}
new SimLockPreferenceController(context).updateNonIndexableKeys(keys);
// TrustAgent settings disappear when the user has no primary security.
if (!lockPatternUtils.isSecure(MY_USER_ID)) {
keys.add(KEY_TRUST_AGENT);
keys.add(KEY_MANAGE_TRUST_AGENTS);
}
if (!(new EnterprisePrivacyPreferenceController(context))
@@ -873,7 +776,6 @@ public class SecuritySettingsV2 extends DashboardFragment
// Duplicates between parent-child
keys.add(KEY_LOCATION);
keys.add(KEY_ENCRYPTION_AND_CREDENTIALS);
keys.add(KEY_SCREEN_PINNING);
keys.add(KEY_LOCATION_SCANNING);
return keys;

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
public class ShowPasswordPreferenceController extends TogglePreferenceController {
private static final String KEY_SHOW_PASSWORD = "show_password";
private static final int MY_USER_ID = UserHandle.myUserId();
private final LockPatternUtils mLockPatternUtils;
public ShowPasswordPreferenceController(Context context) {
super(context, KEY_SHOW_PASSWORD);
mLockPatternUtils = FeatureFactory.getFactory(context)
.getSecurityFeatureProvider()
.getLockPatternUtils(context);
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
Settings.System.TEXT_SHOW_PASSWORD, 1) != 0;
}
@Override
public void setChecked(boolean isChecked) {
Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
isChecked ? 1 : 0);
mLockPatternUtils.setVisiblePasswordEnabled(isChecked, MY_USER_ID);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security;
import android.content.Context;
import android.os.PersistableBundle;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.settings.core.BasePreferenceController;
import java.util.List;
public class SimLockPreferenceController extends BasePreferenceController {
private static final String KEY_SIM_LOCK = "sim_lock_settings";
private final CarrierConfigManager mCarrierConfigManager;
private final UserManager mUserManager;
private final SubscriptionManager mSubscriptionManager;
private final TelephonyManager mTelephonyManager;
public SimLockPreferenceController(Context context) {
super(context, KEY_SIM_LOCK);
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mCarrierConfigManager = (CarrierConfigManager)
mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
mSubscriptionManager = (SubscriptionManager) context
.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
@Override
public int getAvailabilityStatus() {
final PersistableBundle b = mCarrierConfigManager.getConfig();
final boolean IsAdmin = mUserManager.isAdminUser();
if (!IsAdmin || !isSimIccReady() ||
b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
return DISABLED_FOR_USER;
}
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference preference = screen.findPreference(getPreferenceKey());
if (preference == null) {
return;
}
// Disable SIM lock if there is no ready SIM card.
preference.setEnabled(isSimReady());
}
/* Return true if a SIM is ready for locking.
* TODO: consider adding to TelephonyManager or SubscritpionManasger.
*/
private boolean isSimReady() {
final List<SubscriptionInfo> subInfoList =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList != null) {
for (SubscriptionInfo subInfo : subInfoList) {
final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex());
if ((simState != TelephonyManager.SIM_STATE_ABSENT) &&
(simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
return true;
}
}
}
return false;
}
/**
* Return true if a there is a Slot that has Icc
*/
private boolean isSimIccReady() {
final List<SubscriptionInfo> subInfoList =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList != null) {
for (SubscriptionInfo subInfo : subInfoList) {
if (mTelephonyManager.hasIccCard(subInfo.getSimSlotIndex())) {
return true;
}
}
}
return false;
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.security.trustagent;
import android.content.Context;
import android.os.UserHandle;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.security.SecurityFeatureProvider;
public class ManageTrustAgentsPreferenceController extends BasePreferenceController {
@VisibleForTesting
static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
private static final int MY_USER_ID = UserHandle.myUserId();
private final LockPatternUtils mLockPatternUtils;
private TrustAgentManager mTrustAgentManager;
public ManageTrustAgentsPreferenceController(Context context) {
super(context, KEY_MANAGE_TRUST_AGENTS);
final SecurityFeatureProvider securityFeatureProvider = FeatureFactory.getFactory(context)
.getSecurityFeatureProvider();
mLockPatternUtils = securityFeatureProvider.getLockPatternUtils(context);
mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference preference = screen.findPreference(getPreferenceKey());
if (preference == null) {
return;
}
final int numberOfTrustAgent = getTrustAgentCount();
if (!mLockPatternUtils.isSecure(MY_USER_ID)) {
preference.setEnabled(false);
preference.setSummary(R.string.disabled_because_no_backup_security);
} else if (numberOfTrustAgent > 0) {
preference.setSummary(mContext.getResources().getQuantityString(
R.plurals.manage_trust_agents_summary_on,
numberOfTrustAgent, numberOfTrustAgent));
} else {
preference.setSummary(R.string.manage_trust_agents_summary);
}
}
private int getTrustAgentCount() {
return mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils).size();
}
}