blob: 55f21fd22a4d00e947dd90e8f9ce2dcf233ec5e4 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2007 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
17package com.android.settings;
18
Charles Hefc6c0162017-04-24 18:05:48 +010019import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
Maurice Lam2eb170c2017-04-28 16:18:47 -070020
Charles Hefc6c0162017-04-24 18:05:48 +010021import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
22
Danielle Millett487b16f2011-11-02 11:12:21 -040023import android.app.Activity;
Gilles Debunne2c88a872011-06-21 12:47:17 -070024import android.app.AlertDialog;
Clara Bayarri52681ec2016-01-28 11:11:35 +000025import android.app.Dialog;
Clara Bayarri52681ec2016-01-28 11:11:35 +000026import android.app.FragmentManager;
Dianne Hackborn4037c7f2010-02-26 17:26:55 -080027import android.app.admin.DevicePolicyManager;
Ido Ofir524a63b2014-06-12 14:18:23 -070028import android.content.ComponentName;
Dianne Hackbornabc3dc62010-01-20 13:40:19 -080029import android.content.Context;
Gilles Debunne2c88a872011-06-21 12:47:17 -070030import android.content.DialogInterface;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.content.Intent;
rich canningsbfbdcef2012-09-09 12:48:50 -070032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070034import android.content.res.Resources;
Jason Monk3bcd76c2015-04-21 11:20:20 -040035import android.hardware.fingerprint.FingerprintManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036import android.os.Bundle;
Junda Liu80753bc2015-06-17 10:15:34 -070037import android.os.PersistableBundle;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070038import android.os.UserHandle;
Jim Miller783ea852012-11-01 19:39:21 -070039import android.os.UserManager;
Charles Hefc6c0162017-04-24 18:05:48 +010040import android.os.storage.StorageManager;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070041import android.provider.SearchIndexableResource;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042import android.provider.Settings;
Ido Ofir6f482442014-05-09 14:47:15 -070043import android.service.trust.TrustAgentService;
Shahriyar Amini5c1ad612017-01-11 11:30:13 -080044import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040045import android.support.v14.preference.SwitchPreference;
Jason Monk39b46742015-09-10 15:52:51 -040046import android.support.v7.preference.Preference;
47import android.support.v7.preference.Preference.OnPreferenceChangeListener;
Jason Monk39b46742015-09-10 15:52:51 -040048import android.support.v7.preference.PreferenceGroup;
49import android.support.v7.preference.PreferenceScreen;
Junda Liu80753bc2015-06-17 10:15:34 -070050import android.telephony.CarrierConfigManager;
Wink Savilleca756612014-11-08 10:47:12 -080051import android.telephony.SubscriptionInfo;
Jason Monk3bcd76c2015-04-21 11:20:20 -040052import android.telephony.SubscriptionManager;
53import android.telephony.TelephonyManager;
Ido Ofir524a63b2014-06-12 14:18:23 -070054import android.text.TextUtils;
Jim Miller86624a22010-08-30 22:07:58 -070055import android.util.Log;
Fan Zhang301fe802016-10-26 10:44:45 -070056
Tamas Berghammer265d3c22016-06-22 15:34:45 +010057import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Gilles Debunnea6a8a142011-06-09 11:56:17 -070058import com.android.internal.widget.LockPatternUtils;
Jim Millerfe765592014-07-31 15:30:29 -070059import com.android.settings.TrustAgentUtils.TrustAgentComponentInfo;
Fan Zhang1e516282016-09-16 12:45:07 -070060import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
Fan Zhang301fe802016-10-26 10:44:45 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
Shahriyar Amini5c1ad612017-01-11 11:30:13 -080062import com.android.settings.dashboard.SummaryLoader;
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +010063import com.android.settings.enterprise.EnterprisePrivacyPreferenceController;
64import com.android.settings.enterprise.ManageDeviceAdminPreferenceController;
Jorim Jaggi5ad75f02015-04-22 16:17:23 -070065import com.android.settings.fingerprint.FingerprintSettings;
Doris Ling81610542017-02-15 17:49:18 -080066import com.android.settings.location.LocationPreferenceController;
Doris Lingcee83e42017-04-06 16:01:37 -070067import com.android.settings.notification.LockScreenNotificationPreferenceController;
Fan Zhang301fe802016-10-26 10:44:45 -070068import com.android.settings.overlay.FeatureFactory;
Maurice Lam2eb170c2017-04-28 16:18:47 -070069import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
70import com.android.settings.password.ChooseLockSettingsHelper;
71import com.android.settings.password.ManagedLockPasswordProvider;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070072import com.android.settings.search.BaseSearchIndexProvider;
73import com.android.settings.search.Indexable;
74import com.android.settings.search.SearchIndexableRaw;
Doris Ling93582992017-03-16 13:20:15 -070075import com.android.settings.security.OwnerInfoPreferenceController;
Shahriyar Amini4774b582016-12-28 11:15:44 -080076import com.android.settings.security.SecurityFeatureProvider;
Zachary Iqbalccae73f2017-01-17 14:55:49 -080077import com.android.settings.trustagent.TrustAgentManager;
Fan Zhang62e66c92017-02-08 16:00:56 -080078import com.android.settings.widget.GearPreference;
Sudheer Shanka550d0682016-01-13 15:16:55 +000079import com.android.settingslib.RestrictedLockUtils;
Sudheer Shanka9c324682016-01-18 11:17:23 +000080import com.android.settingslib.RestrictedPreference;
Fan Zhang301fe802016-10-26 10:44:45 -070081import com.android.settingslib.drawer.CategoryKey;
Gilles Debunnea6a8a142011-06-09 11:56:17 -070082
Jim Miller86624a22010-08-30 22:07:58 -070083import java.util.ArrayList;
rich canningsbfbdcef2012-09-09 12:48:50 -070084import java.util.List;
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -040085
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086/**
87 * Gesture lock pattern settings.
88 */
Julia Reynolds565653c2014-06-12 11:49:12 -040089public class SecuritySettings extends SettingsPreferenceFragment
Suprabh Shuklaf61f11b2017-02-06 15:25:29 -080090 implements OnPreferenceChangeListener, Indexable,
Adrian Roos62775bf2016-01-28 13:23:53 -080091 GearPreference.OnGearClickListener {
Jim Miller92186872015-03-04 18:07:32 -080092
93 private static final String TAG = "SecuritySettings";
Fan Zhang301fe802016-10-26 10:44:45 -070094
Jim Miller250b9be2014-09-05 18:48:18 -070095 private static final String TRUST_AGENT_CLICK_INTENT = "trust_agent_click_intent";
Ido Ofir6f482442014-05-09 14:47:15 -070096 private static final Intent TRUST_AGENT_INTENT =
97 new Intent(TrustAgentService.SERVICE_INTERFACE);
Jason parks6f8fb432011-01-07 09:02:14 -060098
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099 // Lock Settings
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800100 private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
Clara Bayarri52681ec2016-01-28 11:11:35 +0000101 private static final String KEY_UNLOCK_SET_OR_CHANGE_PROFILE = "unlock_set_or_change_profile";
102 private static final String KEY_VISIBLE_PATTERN_PROFILE = "visiblepattern_profile";
Amith Yamasaniedac9af2010-11-17 09:08:21 -0800103 private static final String KEY_SECURITY_CATEGORY = "security_category";
Doris Ling2db8c862017-04-06 14:22:18 -0700104 @VisibleForTesting
105 static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
Clara Bayarri52681ec2016-01-28 11:11:35 +0000106 private static final String KEY_UNIFICATION = "unification";
Doris Lingcee83e42017-04-06 16:01:37 -0700107 @VisibleForTesting
108 static final String KEY_LOCKSCREEN_PREFERENCES = "lockscreen_preferences";
Matthew Fritze2e34a642017-05-11 11:56:27 -0700109 private static final String KEY_ENCRYPTION_AND_CREDENTIALS = "encryption_and_credential";
110 private static final String KEY_LOCATION_SCANNING = "location_scanning";
111 private static final String KEY_LOCATION = "location";
Jim Milleref3e7012013-08-20 19:03:30 -0700112
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800113 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
Ido Ofir6f482442014-05-09 14:47:15 -0700114 private static final int CHANGE_TRUST_AGENT_SETTINGS = 126;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000115 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE = 127;
116 private static final int UNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 128;
117 private static final int UNIFY_LOCK_CONFIRM_PROFILE_REQUEST = 129;
Mahaver Chopra26632092016-03-18 19:08:05 +0000118 private static final int UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 130;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000119 private static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
Hung-ying Tyan7031ab02009-07-02 00:26:46 +0800120
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800121 // Misc Settings
Doris Ling81610542017-02-15 17:49:18 -0800122 private static final String KEY_SIM_LOCK = "sim_lock_settings";
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800123 private static final String KEY_SHOW_PASSWORD = "show_password";
Ido Ofir6f482442014-05-09 14:47:15 -0700124 private static final String KEY_TRUST_AGENT = "trust_agent";
Jason Monk27d7ea52014-07-18 09:55:41 -0400125 private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
Jim Miller47d380f2010-01-20 13:37:14 -0800126
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800127 // Security status
128 private static final String KEY_SECURITY_STATUS = "security_status";
129 private static final String SECURITY_STATUS_KEY_PREFIX = "security_status_";
130
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800131 // Package verifier Settings
132 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800133 static final String KEY_PACKAGE_VERIFIER_STATUS = "security_status_package_verifier";
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800134 private static final int PACKAGE_VERIFIER_STATE_ENABLED = 1;
135
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100136 // Device management settings
137 private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
138 private static final String KEY_MANAGE_DEVICE_ADMIN = "manage_device_admin";
139
Jim Miller4a9065e2014-08-08 15:25:31 -0700140 // These switch preferences need special handling since they're not all stored in Settings.
Adrian Roos62775bf2016-01-28 13:23:53 -0800141 private static final String SWITCH_PREFERENCE_KEYS[] = {
Suprabh Shuklaf61f11b2017-02-06 15:25:29 -0800142 KEY_SHOW_PASSWORD, KEY_UNIFICATION, KEY_VISIBLE_PATTERN_PROFILE
Mahaver Chopraa1276e42016-03-04 17:17:59 +0000143 };
Jim Miller4a9065e2014-08-08 15:25:31 -0700144
Jim Millerfe765592014-07-31 15:30:29 -0700145 // Only allow one trust agent on the platform.
146 private static final boolean ONLY_ONE_TRUST_AGENT = true;
147
Adrian Roos54375882015-04-16 17:11:22 -0700148 private static final int MY_USER_ID = UserHandle.myUserId();
149
Fan Zhang301fe802016-10-26 10:44:45 -0700150 private DashboardFeatureProvider mDashboardFeatureProvider;
Maggie Benthallf48206e2013-08-20 11:03:05 -0400151 private DevicePolicyManager mDPM;
Shahriyar Amini4774b582016-12-28 11:15:44 -0800152 private SecurityFeatureProvider mSecurityFeatureProvider;
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800153 private TrustAgentManager mTrustAgentManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800154 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700155 private UserManager mUm;
Jim Miller122b6c82010-02-10 19:53:58 -0800156
Jim Miller00d24762009-12-22 19:04:57 -0800157 private ChooseLockSettingsHelper mChooseLockSettingsHelper;
Jim Miller122b6c82010-02-10 19:53:58 -0800158 private LockPatternUtils mLockPatternUtils;
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700159 private ManagedLockPasswordProvider mManagedPasswordProvider;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800160
Clara Bayarri52681ec2016-01-28 11:11:35 +0000161 private SwitchPreference mVisiblePatternProfile;
Clara Bayarri56462d62016-02-12 13:55:36 +0000162 private SwitchPreference mUnifyProfile;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000163
Jim Miller4a9065e2014-08-08 15:25:31 -0700164 private SwitchPreference mShowPassword;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800165
Xiaohui Chen44879a32015-07-22 13:53:22 -0700166 private boolean mIsAdmin;
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700167
Jim Miller250b9be2014-09-05 18:48:18 -0700168 private Intent mTrustAgentClickIntent;
Ido Ofir1682a0d2014-06-24 16:26:09 -0700169
Clara Bayarri6934a042015-10-14 11:07:35 +0100170 private int mProfileChallengeUserId;
171
Clara Bayarri52681ec2016-01-28 11:11:35 +0000172 private String mCurrentDevicePassword;
173 private String mCurrentProfilePassword;
174
Doris Ling81610542017-02-15 17:49:18 -0800175 private LocationPreferenceController mLocationcontroller;
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100176 private ManageDeviceAdminPreferenceController mManageDeviceAdminPreferenceController;
177 private EnterprisePrivacyPreferenceController mEnterprisePrivacyPreferenceController;
Doris Lingcee83e42017-04-06 16:01:37 -0700178 private LockScreenNotificationPreferenceController mLockScreenNotificationPreferenceController;
Doris Ling81610542017-02-15 17:49:18 -0800179
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700181 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500182 return MetricsEvent.SECURITY;
Chris Wren8a963ba2015-03-20 10:29:14 -0400183 }
184
185 @Override
Alex Salo2e52b422017-05-05 15:36:27 -0700186 public void onAttach(Context context) {
187 super.onAttach(context);
188 mLocationcontroller = new LocationPreferenceController(context, getLifecycle());
189 }
190
191 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700192 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800193 super.onCreate(savedInstanceState);
Jim Miller122b6c82010-02-10 19:53:58 -0800194
Fan Zhang301fe802016-10-26 10:44:45 -0700195 final Activity activity = getActivity();
Wink Saville0183fb52014-11-22 10:11:39 -0800196
Fan Zhang301fe802016-10-26 10:44:45 -0700197 mSubscriptionManager = SubscriptionManager.from(activity);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198
Fan Zhang301fe802016-10-26 10:44:45 -0700199 mLockPatternUtils = new LockPatternUtils(activity);
200
201 mManagedPasswordProvider = ManagedLockPasswordProvider.get(activity, MY_USER_ID);
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700202
Dianne Hackbornabc3dc62010-01-20 13:40:19 -0800203 mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Jim Miller122b6c82010-02-10 19:53:58 -0800204
Fan Zhang301fe802016-10-26 10:44:45 -0700205 mUm = UserManager.get(activity);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700206
Fan Zhang301fe802016-10-26 10:44:45 -0700207 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(activity);
208
209 mDashboardFeatureProvider = FeatureFactory.getFactory(activity)
210 .getDashboardFeatureProvider(activity);
Jim Miller250b9be2014-09-05 18:48:18 -0700211
Shahriyar Amini4774b582016-12-28 11:15:44 -0800212 mSecurityFeatureProvider = FeatureFactory.getFactory(activity).getSecurityFeatureProvider();
213
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800214 mTrustAgentManager = mSecurityFeatureProvider.getTrustAgentManager();
215
Jim Miller250b9be2014-09-05 18:48:18 -0700216 if (savedInstanceState != null
217 && savedInstanceState.containsKey(TRUST_AGENT_CLICK_INTENT)) {
218 mTrustAgentClickIntent = savedInstanceState.getParcelable(TRUST_AGENT_CLICK_INTENT);
219 }
Doris Ling81610542017-02-15 17:49:18 -0800220
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100221 mManageDeviceAdminPreferenceController
222 = new ManageDeviceAdminPreferenceController(activity);
223 mEnterprisePrivacyPreferenceController
224 = new EnterprisePrivacyPreferenceController(activity, null /* lifecycle */);
Doris Lingcee83e42017-04-06 16:01:37 -0700225 mLockScreenNotificationPreferenceController
226 = new LockScreenNotificationPreferenceController(activity);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700227 }
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -0400228
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700229 private static int getResIdForLockUnlockScreen(Context context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700230 LockPatternUtils lockPatternUtils, ManagedLockPasswordProvider managedPasswordProvider,
231 int userId) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000232 final boolean isMyUser = userId == MY_USER_ID;
Jim Miller36972bb2010-11-30 19:47:38 -0800233 int resid = 0;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000234 if (!lockPatternUtils.isSecure(userId)) {
235 if (!isMyUser) {
236 resid = R.xml.security_settings_lockscreen_profile;
237 } else if (lockPatternUtils.isLockScreenDisabled(userId)) {
Jim Miller36972bb2010-11-30 19:47:38 -0800238 resid = R.xml.security_settings_lockscreen;
239 } else {
240 resid = R.xml.security_settings_chooser;
241 }
Jim Miller122b6c82010-02-10 19:53:58 -0800242 } else {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000243 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(userId)) {
Jim Milleraf366a32010-03-25 18:45:22 -0700244 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000245 resid = isMyUser ? R.xml.security_settings_pattern
246 : R.xml.security_settings_pattern_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700247 break;
248 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Nicolas Prevot8fd852e2014-01-25 01:02:04 +0000249 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000250 resid = isMyUser ? R.xml.security_settings_pin
251 : R.xml.security_settings_pin_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700252 break;
253 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
254 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
Konstantin Lopyrev57fbf692010-05-27 16:01:41 -0700255 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000256 resid = isMyUser ? R.xml.security_settings_password
257 : R.xml.security_settings_password_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700258 break;
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700259 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
260 resid = managedPasswordProvider.getResIdForLockUnlockScreen(!isMyUser);
261 break;
Jim Miller00d24762009-12-22 19:04:57 -0800262 }
Jim Miller122b6c82010-02-10 19:53:58 -0800263 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700264 return resid;
265 }
Jim Miller122b6c82010-02-10 19:53:58 -0800266
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700267 /**
268 * Important!
269 *
Ido Ofir6f482442014-05-09 14:47:15 -0700270 * Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700271 * logic or adding/removing preferences here.
272 */
273 private PreferenceScreen createPreferenceHierarchy() {
274 PreferenceScreen root = getPreferenceScreen();
275 if (root != null) {
276 root.removeAll();
277 }
278 addPreferencesFromResource(R.xml.security_settings);
279 root = getPreferenceScreen();
280
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800281 // Add category for security status
282 addPreferencesFromResource(R.xml.security_settings_status);
283
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700284 // Add options for lock/unlock screen
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700285 final int resid = getResIdForLockUnlockScreen(getActivity(), mLockPatternUtils,
286 mManagedPasswordProvider, MY_USER_ID);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700287 addPreferencesFromResource(resid);
Jason parks41121992011-01-25 09:26:55 -0600288
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100289 // DO or PO installed in the user may disallow to change password.
290 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100291
Clara Bayarri462cce12016-02-18 12:09:21 +0000292 mProfileChallengeUserId = Utils.getManagedProfileId(mUm, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000293 if (mProfileChallengeUserId != UserHandle.USER_NULL
294 && mLockPatternUtils.isSeparateProfileChallengeAllowed(mProfileChallengeUserId)) {
295 addPreferencesFromResource(R.xml.security_settings_profile);
Mahaver Chopra26632092016-03-18 19:08:05 +0000296 addPreferencesFromResource(R.xml.security_settings_unification);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000297 final int profileResid = getResIdForLockUnlockScreen(
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700298 getActivity(), mLockPatternUtils, mManagedPasswordProvider,
299 mProfileChallengeUserId);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000300 addPreferencesFromResource(profileResid);
301 maybeAddFingerprintPreference(root, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000302 if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100303 final Preference lockPreference =
304 root.findPreference(KEY_UNLOCK_SET_OR_CHANGE_PROFILE);
305 final String summary = getContext().getString(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000306 R.string.lock_settings_profile_unified_summary);
307 lockPreference.setSummary(summary);
Clara Bayarri56462d62016-02-12 13:55:36 +0000308 lockPreference.setEnabled(false);
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100309 // PO may disallow to change password for the profile, but screen lock and managed
310 // profile's lock is the same. Disable main "Screen lock" menu.
311 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE, mProfileChallengeUserId);
312 } else {
313 // PO may disallow to change profile password, and the profile's password is
314 // separated from screen lock password. Disable profile specific "Screen lock" menu.
315 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE_PROFILE,
316 mProfileChallengeUserId);
Clara Bayarri6934a042015-10-14 11:07:35 +0100317 }
318 }
319
Adrian Roos62775bf2016-01-28 13:23:53 -0800320 Preference unlockSetOrChange = findPreference(KEY_UNLOCK_SET_OR_CHANGE);
321 if (unlockSetOrChange instanceof GearPreference) {
322 ((GearPreference) unlockSetOrChange).setOnGearClickListener(this);
323 }
324
Xiaohui Chen44879a32015-07-22 13:53:22 -0700325 mIsAdmin = mUm.isAdminUser();
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700326
Jim Miller94d4bd92015-02-19 21:04:57 -0800327 // Fingerprint and trust agents
Doris Ling2db8c862017-04-06 14:22:18 -0700328 int numberOfTrustAgent = 0;
Ido Ofir1682a0d2014-06-24 16:26:09 -0700329 PreferenceGroup securityCategory = (PreferenceGroup)
330 root.findPreference(KEY_SECURITY_CATEGORY);
Jim Miller9d250052014-08-21 19:24:47 -0700331 if (securityCategory != null) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000332 maybeAddFingerprintPreference(securityCategory, UserHandle.myUserId());
Doris Ling2db8c862017-04-06 14:22:18 -0700333 numberOfTrustAgent = addTrustAgentSettings(securityCategory);
Doris Lingcee83e42017-04-06 16:01:37 -0700334 setLockscreenPreferencesSummary(securityCategory);
Ido Ofir1682a0d2014-06-24 16:26:09 -0700335 }
336
Clara Bayarri52681ec2016-01-28 11:11:35 +0000337 mVisiblePatternProfile =
338 (SwitchPreference) root.findPreference(KEY_VISIBLE_PATTERN_PROFILE);
Clara Bayarri56462d62016-02-12 13:55:36 +0000339 mUnifyProfile = (SwitchPreference) root.findPreference(KEY_UNIFICATION);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000340
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700341 // Append the rest of the settings
342 addPreferencesFromResource(R.xml.security_settings_misc);
343
344 // Do not display SIM lock for devices without an Icc card
345 TelephonyManager tm = TelephonyManager.getDefault();
Junda Liu80753bc2015-06-17 10:15:34 -0700346 CarrierConfigManager cfgMgr = (CarrierConfigManager)
347 getActivity().getSystemService(Context.CARRIER_CONFIG_SERVICE);
348 PersistableBundle b = cfgMgr.getConfig();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700349 if (!mIsAdmin || !isSimIccReady() ||
Junda Liu80753bc2015-06-17 10:15:34 -0700350 b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700351 root.removePreference(root.findPreference(KEY_SIM_LOCK));
352 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700353 // Disable SIM lock if there is no ready SIM card.
354 root.findPreference(KEY_SIM_LOCK).setEnabled(isSimReady());
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700355 }
Jason Monkb08853a2014-08-13 09:43:58 -0400356 if (Settings.System.getInt(getContentResolver(),
357 Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
358 root.findPreference(KEY_SCREEN_PINNING).setSummary(
359 getResources().getString(R.string.switch_on_text));
Jason Monk27d7ea52014-07-18 09:55:41 -0400360 }
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700361
Ng Zhi Anf0c03202017-09-20 17:51:28 -0700362 // Encryption status of device
363 if (LockPatternUtils.isDeviceEncryptionEnabled()) {
364 root.findPreference(KEY_ENCRYPTION_AND_CREDENTIALS).setSummary(
365 R.string.encryption_and_credential_settings_summary);
366 } else {
367 root.findPreference(KEY_ENCRYPTION_AND_CREDENTIALS).setSummary(
368 R.string.summary_placeholder);
369 }
370
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700371 // Show password
Jim Miller4a9065e2014-08-08 15:25:31 -0700372 mShowPassword = (SwitchPreference) root.findPreference(KEY_SHOW_PASSWORD);
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700373
Kenny Rootc5550c22013-04-08 09:56:05 -0700374 // Credential storage
Emily Bernier11bd33a2013-04-11 16:24:56 -0400375 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700376
Jim Millerfe765592014-07-31 15:30:29 -0700377 // Advanced Security features
Doris Ling2db8c862017-04-06 14:22:18 -0700378 initTrustAgentPreference(root, numberOfTrustAgent);
Jim Millerfe765592014-07-31 15:30:29 -0700379
380 // The above preferences come and go based on security state, so we need to update
381 // the index. This call is expected to be fairly cheap, but we may want to do something
382 // smarter in the future.
Matthew Fritze5c83cfa2017-03-16 13:33:18 -0700383 final Activity activity = getActivity();
384 FeatureFactory.getFactory(activity).getSearchFeatureProvider().getIndexingManager(activity)
Matthew Fritze8a42b072017-03-27 17:42:36 -0700385 .updateFromClassNameResource(SecuritySettings.class.getName(),
386 true /* includeInSearchResults */);
Jim Millerfe765592014-07-31 15:30:29 -0700387
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800388 PreferenceGroup securityStatusPreferenceGroup =
389 (PreferenceGroup) root.findPreference(KEY_SECURITY_STATUS);
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800390 final List<Preference> tilePrefs = mDashboardFeatureProvider.getPreferencesForCategory(
391 getActivity(), getPrefContext(), getMetricsCategory(),
392 CategoryKey.CATEGORY_SECURITY);
393 int numSecurityStatusPrefs = 0;
394 if (tilePrefs != null && !tilePrefs.isEmpty()) {
395 for (Preference preference : tilePrefs) {
396 if (!TextUtils.isEmpty(preference.getKey())
397 && preference.getKey().startsWith(SECURITY_STATUS_KEY_PREFIX)) {
398 // Injected security status settings are placed under the Security status
399 // category.
400 securityStatusPreferenceGroup.addPreference(preference);
401 numSecurityStatusPrefs++;
402 } else {
403 // Other injected settings are placed under the Security preference screen.
404 root.addPreference(preference);
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800405 }
Fan Zhang301fe802016-10-26 10:44:45 -0700406 }
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800407 }
Fan Zhang301fe802016-10-26 10:44:45 -0700408
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800409 if (numSecurityStatusPrefs == 0) {
410 root.removePreference(securityStatusPreferenceGroup);
411 } else if (numSecurityStatusPrefs > 0) {
412 // Update preference data with tile data. Security feature provider only updates the
413 // data if it actually needs to be changed.
414 mSecurityFeatureProvider.updatePreferences(getActivity(), root,
415 mDashboardFeatureProvider.getTilesForCategory(
416 CategoryKey.CATEGORY_SECURITY));
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800417 }
Shahriyar Amini4774b582016-12-28 11:15:44 -0800418
Jim Miller4a9065e2014-08-08 15:25:31 -0700419 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
420 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
421 if (pref != null) pref.setOnPreferenceChangeListener(this);
422 }
Doris Ling81610542017-02-15 17:49:18 -0800423
424 mLocationcontroller.displayPreference(root);
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100425 mManageDeviceAdminPreferenceController.updateState(
426 root.findPreference(KEY_MANAGE_DEVICE_ADMIN));
427 mEnterprisePrivacyPreferenceController.displayPreference(root);
428 mEnterprisePrivacyPreferenceController.onResume();
429
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800430 return root;
431 }
432
Doris Ling2db8c862017-04-06 14:22:18 -0700433 @VisibleForTesting
434 void initTrustAgentPreference(PreferenceScreen root, int numberOfTrustAgent) {
435 Preference manageAgents = root.findPreference(KEY_MANAGE_TRUST_AGENTS);
436 if (manageAgents != null) {
437 if (!mLockPatternUtils.isSecure(MY_USER_ID)) {
438 manageAgents.setEnabled(false);
439 manageAgents.setSummary(R.string.disabled_because_no_backup_security);
440 } else if (numberOfTrustAgent > 0) {
441 manageAgents.setSummary(getActivity().getResources().getQuantityString(
442 R.plurals.manage_trust_agents_summary_on,
443 numberOfTrustAgent, numberOfTrustAgent));
444 } else {
445 manageAgents.setSummary(R.string.manage_trust_agents_summary);
446 }
447 }
448 }
449
Doris Lingcee83e42017-04-06 16:01:37 -0700450 @VisibleForTesting
451 void setLockscreenPreferencesSummary(PreferenceGroup group) {
452 final Preference lockscreenPreferences = group.findPreference(KEY_LOCKSCREEN_PREFERENCES);
453 if (lockscreenPreferences != null) {
454 lockscreenPreferences.setSummary(
455 mLockScreenNotificationPreferenceController.getSummaryResource());
456 }
457 }
458
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100459 /*
460 * Sets the preference as disabled by admin if PASSWORD_QUALITY_MANAGED is set.
461 * The preference must be a RestrictedPreference.
462 */
463 private void disableIfPasswordQualityManaged(String preferenceKey, int userId) {
464 final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
465 getActivity(), userId);
466 if (admin != null && mDPM.getPasswordQuality(admin.component, userId) ==
467 DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
468 final RestrictedPreference pref =
469 (RestrictedPreference) getPreferenceScreen().findPreference(preferenceKey);
470 pref.setDisabledByAdmin(admin);
471 }
472 }
473
Clara Bayarri52681ec2016-01-28 11:11:35 +0000474 private void maybeAddFingerprintPreference(PreferenceGroup securityCategory, int userId) {
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000475 Preference fingerprintPreference =
476 FingerprintSettings.getFingerprintPreferenceForUser(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000477 securityCategory.getContext(), userId);
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000478 if (fingerprintPreference != null) {
479 securityCategory.addPreference(fingerprintPreference);
Jim Miller94d4bd92015-02-19 21:04:57 -0800480 }
Jim Miller94d4bd92015-02-19 21:04:57 -0800481 }
482
Doris Ling2db8c862017-04-06 14:22:18 -0700483 // Return the number of trust agents being added
484 private int addTrustAgentSettings(PreferenceGroup securityCategory) {
Adrian Roos54375882015-04-16 17:11:22 -0700485 final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800486 ArrayList<TrustAgentComponentInfo> agents = getActiveTrustAgents(
487 getActivity(), mTrustAgentManager, mLockPatternUtils, mDPM);
Jim Miller94d4bd92015-02-19 21:04:57 -0800488 for (int i = 0; i < agents.size(); i++) {
489 final TrustAgentComponentInfo agent = agents.get(i);
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000490 RestrictedPreference trustAgentPreference =
491 new RestrictedPreference(securityCategory.getContext());
Jim Miller94d4bd92015-02-19 21:04:57 -0800492 trustAgentPreference.setKey(KEY_TRUST_AGENT);
493 trustAgentPreference.setTitle(agent.title);
494 trustAgentPreference.setSummary(agent.summary);
495 // Create intent for this preference.
496 Intent intent = new Intent();
497 intent.setComponent(agent.componentName);
498 intent.setAction(Intent.ACTION_MAIN);
499 trustAgentPreference.setIntent(intent);
500 // Add preference to the settings menu.
501 securityCategory.addPreference(trustAgentPreference);
Adrian Roos088de472015-04-07 14:09:39 +0200502
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000503 trustAgentPreference.setDisabledByAdmin(agent.admin);
504 if (!trustAgentPreference.isDisabledByAdmin() && !hasSecurity) {
Jim Miller94d4bd92015-02-19 21:04:57 -0800505 trustAgentPreference.setEnabled(false);
506 trustAgentPreference.setSummary(R.string.disabled_because_no_backup_security);
507 }
508 }
Doris Ling2db8c862017-04-06 14:22:18 -0700509 return agents.size();
Jim Miller94d4bd92015-02-19 21:04:57 -0800510 }
511
PauloftheWest0b8788a2014-12-08 13:49:35 -0800512 /* Return true if a there is a Slot that has Icc.
513 */
514 private boolean isSimIccReady() {
515 TelephonyManager tm = TelephonyManager.getDefault();
516 final List<SubscriptionInfo> subInfoList =
517 mSubscriptionManager.getActiveSubscriptionInfoList();
518
519 if (subInfoList != null) {
520 for (SubscriptionInfo subInfo : subInfoList) {
Sanket Padawe24784202015-01-14 14:48:27 -0800521 if (tm.hasIccCard(subInfo.getSimSlotIndex())) {
PauloftheWest0b8788a2014-12-08 13:49:35 -0800522 return true;
523 }
524 }
525 }
526
527 return false;
528 }
529
PauloftheWest50e6eca2014-10-03 11:07:14 -0700530 /* Return true if a SIM is ready for locking.
531 * TODO: consider adding to TelephonyManager or SubscritpionManasger.
532 */
Wink Saville0183fb52014-11-22 10:11:39 -0800533 private boolean isSimReady() {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700534 int simState = TelephonyManager.SIM_STATE_UNKNOWN;
Wink Saville0183fb52014-11-22 10:11:39 -0800535 final List<SubscriptionInfo> subInfoList =
536 mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700537 if (subInfoList != null) {
Wink Savilleca756612014-11-08 10:47:12 -0800538 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -0700539 simState = TelephonyManager.getDefault().getSimState(subInfo.getSimSlotIndex());
PauloftheWest50e6eca2014-10-03 11:07:14 -0700540 if((simState != TelephonyManager.SIM_STATE_ABSENT) &&
541 (simState != TelephonyManager.SIM_STATE_UNKNOWN)){
542 return true;
543 }
544 }
545 }
546 return false;
547 }
548
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800549 private static ArrayList<TrustAgentComponentInfo> getActiveTrustAgents(Context context,
550 TrustAgentManager trustAgentManager, LockPatternUtils utils,
551 DevicePolicyManager dpm) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000552 PackageManager pm = context.getPackageManager();
Jim Millerfe765592014-07-31 15:30:29 -0700553 ArrayList<TrustAgentComponentInfo> result = new ArrayList<TrustAgentComponentInfo>();
554 List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
555 PackageManager.GET_META_DATA);
Adrian Roos54375882015-04-16 17:11:22 -0700556 List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(MY_USER_ID);
Adrian Roos088de472015-04-07 14:09:39 +0200557
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000558 EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
559 DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
Adrian Roos088de472015-04-07 14:09:39 +0200560
Jim Millerfe765592014-07-31 15:30:29 -0700561 if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
562 for (int i = 0; i < resolveInfos.size(); i++) {
563 ResolveInfo resolveInfo = resolveInfos.get(i);
564 if (resolveInfo.serviceInfo == null) continue;
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800565 if (!trustAgentManager.shouldProvideTrust(resolveInfo, pm)) {
566 continue;
567 }
Jim Millerfe765592014-07-31 15:30:29 -0700568 TrustAgentComponentInfo trustAgentComponentInfo =
569 TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
570 if (trustAgentComponentInfo.componentName == null ||
571 !enabledTrustAgents.contains(
572 TrustAgentUtils.getComponentName(resolveInfo)) ||
573 TextUtils.isEmpty(trustAgentComponentInfo.title)) continue;
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000574 if (admin != null && dpm.getTrustAgentConfiguration(
Adrian Roos088de472015-04-07 14:09:39 +0200575 null, TrustAgentUtils.getComponentName(resolveInfo)) == null) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000576 trustAgentComponentInfo.admin = admin;
Adrian Roos088de472015-04-07 14:09:39 +0200577 }
Jim Millerfe765592014-07-31 15:30:29 -0700578 result.add(trustAgentComponentInfo);
579 if (ONLY_ONE_TRUST_AGENT) break;
580 }
581 }
582 return result;
583 }
584
Adrian Roos73c52f22017-02-28 15:59:01 -0800585 private static CharSequence getActiveTrustAgentLabel(Context context,
586 TrustAgentManager trustAgentManager, LockPatternUtils utils,
587 DevicePolicyManager dpm) {
588 ArrayList<TrustAgentComponentInfo> agents = getActiveTrustAgents(context,
589 trustAgentManager, utils, dpm);
590 return agents.isEmpty() ? null : agents.get(0).title;
591 }
592
Gilles Debunne2c88a872011-06-21 12:47:17 -0700593 @Override
Adrian Roos62775bf2016-01-28 13:23:53 -0800594 public void onGearClick(GearPreference p) {
595 if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) {
596 startFragment(this, SecuritySubSettings.class.getName(), 0, 0, null);
597 }
598 }
599
600 @Override
Jim Miller250b9be2014-09-05 18:48:18 -0700601 public void onSaveInstanceState(Bundle outState) {
602 super.onSaveInstanceState(outState);
603 if (mTrustAgentClickIntent != null) {
604 outState.putParcelable(TRUST_AGENT_CLICK_INTENT, mTrustAgentClickIntent);
605 }
606 }
607
608 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700609 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800610 super.onResume();
611
Jim Miller86624a22010-08-30 22:07:58 -0700612 // Make sure we reload the preference hierarchy since some of these settings
613 // depend on others...
614 createPreferenceHierarchy();
615
Clara Bayarri52681ec2016-01-28 11:11:35 +0000616 if (mVisiblePatternProfile != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +0000617 mVisiblePatternProfile.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000618 mProfileChallengeUserId));
619 }
620
Clara Bayarri56462d62016-02-12 13:55:36 +0000621 updateUnificationPreference();
622
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700623 if (mShowPassword != null) {
624 mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
625 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
626 }
Hung-ying Tyanb0883cb2009-09-30 11:56:05 +0800627
Doris Ling81610542017-02-15 17:49:18 -0800628 mLocationcontroller.updateSummary();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800629 }
630
Clara Bayarri56462d62016-02-12 13:55:36 +0000631 private void updateUnificationPreference() {
632 if (mUnifyProfile != null) {
633 mUnifyProfile.setChecked(!mLockPatternUtils.isSeparateProfileChallengeEnabled(
634 mProfileChallengeUserId));
635 }
636 }
637
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800638 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400639 public boolean onPreferenceTreeClick(Preference preference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800640 final String key = preference.getKey();
Jim Millerbbb4afa2010-04-08 19:40:19 -0700641 if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
Charles Hefc6c0162017-04-24 18:05:48 +0100642 // TODO(b/35930129): Remove once existing password can be passed into vold directly.
643 // Currently we need this logic to ensure that the QUIET_MODE is off for any work
644 // profile with unified challenge on FBE-enabled devices. Otherwise, vold would not be
645 // able to complete the operation due to the lack of (old) encryption key.
646 if (mProfileChallengeUserId != UserHandle.USER_NULL
647 && !mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)
648 && StorageManager.isFileEncryptedNativeOnly()) {
649 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
650 mProfileChallengeUserId)) {
651 return false;
652 }
653 }
Maurice Lam2eb170c2017-04-28 16:18:47 -0700654 startFragment(this, ChooseLockGenericFragment.class.getName(),
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700655 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
Clara Bayarri6934a042015-10-14 11:07:35 +0100656 } else if (KEY_UNLOCK_SET_OR_CHANGE_PROFILE.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100657 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100658 mProfileChallengeUserId)) {
659 return false;
660 }
Clara Bayarri6934a042015-10-14 11:07:35 +0100661 Bundle extras = new Bundle();
662 extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
Maurice Lam2eb170c2017-04-28 16:18:47 -0700663 startFragment(this, ChooseLockGenericFragment.class.getName(),
Clara Bayarri52681ec2016-01-28 11:11:35 +0000664 R.string.lock_settings_picker_title_profile,
665 SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
Ido Ofir6f482442014-05-09 14:47:15 -0700666 } else if (KEY_TRUST_AGENT.equals(key)) {
667 ChooseLockSettingsHelper helper =
668 new ChooseLockSettingsHelper(this.getActivity(), this);
Jim Miller250b9be2014-09-05 18:48:18 -0700669 mTrustAgentClickIntent = preference.getIntent();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700670 boolean confirmationLaunched = helper.launchConfirmationActivity(
671 CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle());
672 if (!confirmationLaunched&& mTrustAgentClickIntent != null) {
Ido Ofir6f482442014-05-09 14:47:15 -0700673 // If this returns false, it means no password confirmation is required.
Jim Miller250b9be2014-09-05 18:48:18 -0700674 startActivity(mTrustAgentClickIntent);
675 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700676 }
Jim Miller36972bb2010-11-30 19:47:38 -0800677 } else {
678 // If we didn't handle it, let preferences handle it.
Jason Monk39b46742015-09-10 15:52:51 -0400679 return super.onPreferenceTreeClick(preference);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800680 }
Jim Miller36972bb2010-11-30 19:47:38 -0800681 return true;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800682 }
683
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800684 /**
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700685 * see confirmPatternThenDisableAndClear
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800686 */
687 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700688 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800689 super.onActivityResult(requestCode, resultCode, data);
Adrian Roosf7887182015-01-07 20:51:57 +0100690 if (requestCode == CHANGE_TRUST_AGENT_SETTINGS && resultCode == Activity.RESULT_OK) {
Jim Miller250b9be2014-09-05 18:48:18 -0700691 if (mTrustAgentClickIntent != null) {
692 startActivity(mTrustAgentClickIntent);
693 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700694 }
Jim Miller250b9be2014-09-05 18:48:18 -0700695 return;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000696 } else if (requestCode == UNIFY_LOCK_CONFIRM_DEVICE_REQUEST
697 && resultCode == Activity.RESULT_OK) {
698 mCurrentDevicePassword =
699 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
700 launchConfirmProfileLockForUnification();
701 return;
702 } else if (requestCode == UNIFY_LOCK_CONFIRM_PROFILE_REQUEST
703 && resultCode == Activity.RESULT_OK) {
704 mCurrentProfilePassword =
705 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
706 unifyLocks();
707 return;
Mahaver Chopra26632092016-03-18 19:08:05 +0000708 } else if (requestCode == UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST
709 && resultCode == Activity.RESULT_OK) {
710 ununifyLocks();
711 return;
Danielle Millett487b16f2011-11-02 11:12:21 -0400712 }
Jim Miller122b6c82010-02-10 19:53:58 -0800713 createPreferenceHierarchy();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800714 }
Amith Yamasanif06d8692009-06-11 22:32:33 -0700715
Clara Bayarri52681ec2016-01-28 11:11:35 +0000716 private void launchConfirmDeviceLockForUnification() {
717 final String title = getActivity().getString(
Mahaver Chopra26632092016-03-18 19:08:05 +0000718 R.string.unlock_set_unlock_launch_picker_title);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000719 final ChooseLockSettingsHelper helper =
720 new ChooseLockSettingsHelper(getActivity(), this);
721 if (!helper.launchConfirmationActivity(
Mahaver Chopra26632092016-03-18 19:08:05 +0000722 UNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000723 launchConfirmProfileLockForUnification();
724 }
725 }
726
727 private void launchConfirmProfileLockForUnification() {
728 final String title = getActivity().getString(
Mahaver Chopra26632092016-03-18 19:08:05 +0000729 R.string.unlock_set_unlock_launch_picker_title_profile);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000730 final ChooseLockSettingsHelper helper =
731 new ChooseLockSettingsHelper(getActivity(), this);
732 if (!helper.launchConfirmationActivity(
733 UNIFY_LOCK_CONFIRM_PROFILE_REQUEST, title, true, mProfileChallengeUserId)) {
734 unifyLocks();
735 createPreferenceHierarchy();
736 }
737 }
738
739 private void unifyLocks() {
740 int profileQuality =
741 mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000742 if (profileQuality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
743 mLockPatternUtils.saveLockPattern(
744 LockPatternUtils.stringToPattern(mCurrentProfilePassword),
Mahaver Chopra26632092016-03-18 19:08:05 +0000745 mCurrentDevicePassword, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000746 } else {
747 mLockPatternUtils.saveLockPassword(
748 mCurrentProfilePassword, mCurrentDevicePassword,
Mahaver Chopra26632092016-03-18 19:08:05 +0000749 profileQuality, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000750 }
Ricky Waia0a07382016-04-12 17:34:59 +0100751 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false,
752 mCurrentProfilePassword);
Daniel U4af16612016-03-24 17:34:20 +0000753 final boolean profilePatternVisibility =
754 mLockPatternUtils.isVisiblePatternEnabled(mProfileChallengeUserId);
755 mLockPatternUtils.setVisiblePatternEnabled(profilePatternVisibility, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000756 mCurrentDevicePassword = null;
757 mCurrentProfilePassword = null;
758 }
759
Clara Bayarri56462d62016-02-12 13:55:36 +0000760 private void unifyUncompliantLocks() {
Ricky Wai4bbf0652016-04-01 16:31:56 +0100761 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false,
762 mCurrentProfilePassword);
Maurice Lam2eb170c2017-04-28 16:18:47 -0700763 startFragment(this, ChooseLockGenericFragment.class.getName(),
Clara Bayarri56462d62016-02-12 13:55:36 +0000764 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
765 }
766
Mahaver Chopra26632092016-03-18 19:08:05 +0000767 private void ununifyLocks() {
Mahaver Chopra26632092016-03-18 19:08:05 +0000768 Bundle extras = new Bundle();
769 extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
770 startFragment(this,
Maurice Lam2eb170c2017-04-28 16:18:47 -0700771 ChooseLockGenericFragment.class.getName(),
Mahaver Chopra26632092016-03-18 19:08:05 +0000772 R.string.lock_settings_picker_title_profile,
773 SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
774 }
775
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400776 @Override
Jim Miller86624a22010-08-30 22:07:58 -0700777 public boolean onPreferenceChange(Preference preference, Object value) {
arete300e9202014-08-15 12:04:48 -0700778 boolean result = true;
Jim Miller4a9065e2014-08-08 15:25:31 -0700779 final String key = preference.getKey();
780 final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
Clara Bayarri52681ec2016-01-28 11:11:35 +0000781 if (KEY_VISIBLE_PATTERN_PROFILE.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100782 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100783 mProfileChallengeUserId)) {
784 return false;
785 }
Clara Bayarri52681ec2016-01-28 11:11:35 +0000786 lockPatternUtils.setVisiblePatternEnabled((Boolean) value, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000787 } else if (KEY_UNIFICATION.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100788 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100789 mProfileChallengeUserId)) {
790 return false;
791 }
Clara Bayarri56462d62016-02-12 13:55:36 +0000792 if ((Boolean) value) {
793 final boolean compliantForDevice =
794 (mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId)
795 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
796 && mLockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
797 mProfileChallengeUserId));
798 UnificationConfirmationDialog dialog =
799 UnificationConfirmationDialog.newIntance(compliantForDevice);
800 dialog.show(getChildFragmentManager(), TAG_UNIFICATION_DIALOG);
801 } else {
Mahaver Chopra26632092016-03-18 19:08:05 +0000802 final String title = getActivity().getString(
803 R.string.unlock_set_unlock_launch_picker_title);
804 final ChooseLockSettingsHelper helper =
805 new ChooseLockSettingsHelper(getActivity(), this);
806 if(!helper.launchConfirmationActivity(
807 UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) {
808 ununifyLocks();
809 }
Clara Bayarri56462d62016-02-12 13:55:36 +0000810 }
Clara Bayarri52681ec2016-01-28 11:11:35 +0000811 } else if (KEY_SHOW_PASSWORD.equals(key)) {
Jim Miller4a9065e2014-08-08 15:25:31 -0700812 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
813 ((Boolean) value) ? 1 : 0);
Paul Lawrencec04420c2015-05-18 13:25:01 -0700814 lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID);
Jim Miller86624a22010-08-30 22:07:58 -0700815 }
arete300e9202014-08-15 12:04:48 -0700816 return result;
Jim Miller86624a22010-08-30 22:07:58 -0700817 }
Danielle Millett487b16f2011-11-02 11:12:21 -0400818
rich cannings93c0ee52012-09-30 13:54:26 -0700819 @Override
820 protected int getHelpResource() {
821 return R.string.help_url_security;
822 }
823
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700824 /**
825 * For Search. Please keep it in sync when updating "createPreferenceHierarchy()"
826 */
827 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
828 new SecuritySearchIndexProvider();
829
Jim Millerfe765592014-07-31 15:30:29 -0700830 private static class SecuritySearchIndexProvider extends BaseSearchIndexProvider {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700831
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700832 @Override
833 public List<SearchIndexableResource> getXmlResourcesToIndex(
834 Context context, boolean enabled) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100835 final List<SearchIndexableResource> index = new ArrayList<SearchIndexableResource>();
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700836
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100837 final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700838 final ManagedLockPasswordProvider managedPasswordProvider =
839 ManagedLockPasswordProvider.get(context, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100840 final DevicePolicyManager dpm = (DevicePolicyManager)
841 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700842 final UserManager um = UserManager.get(context);
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100843 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100844
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100845 // To add option for unlock screen, user's password must not be managed and
846 // must not be unified with managed profile, whose password is managed.
847 if (!isPasswordManaged(MY_USER_ID, context, dpm)
848 && (profileUserId == UserHandle.USER_NULL
849 || lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)
850 || !isPasswordManaged(profileUserId, context, dpm))) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100851 // Add options for lock/unlock screen
852 final int resId = getResIdForLockUnlockScreen(context, lockPatternUtils,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700853 managedPasswordProvider, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100854 index.add(getSearchResource(context, resId));
855 }
856
Clara Bayarri52681ec2016-01-28 11:11:35 +0000857 if (profileUserId != UserHandle.USER_NULL
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100858 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)
859 && !isPasswordManaged(profileUserId, context, dpm)) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100860 index.add(getSearchResource(context, getResIdForLockUnlockScreen(context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700861 lockPatternUtils, managedPasswordProvider, profileUserId)));
Clara Bayarri6934a042015-10-14 11:07:35 +0100862 }
863
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100864 final SearchIndexableResource sir = getSearchResource(context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700865 SecuritySubSettings.getResIdForLockUnlockSubScreen(context, lockPatternUtils,
866 managedPasswordProvider));
Adrian Roos62775bf2016-01-28 13:23:53 -0800867 sir.className = SecuritySubSettings.class.getName();
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100868 index.add(sir);
Adrian Roos62775bf2016-01-28 13:23:53 -0800869
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700870 // Append the rest of the settings
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100871 index.add(getSearchResource(context, R.xml.security_settings_misc));
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700872
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100873 return index;
874 }
875
876 private SearchIndexableResource getSearchResource(Context context, int xmlResId) {
877 final SearchIndexableResource sir = new SearchIndexableResource(context);
878 sir.xmlResId = xmlResId;
879 return sir;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700880 }
881
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100882 private boolean isPasswordManaged(int userId, Context context, DevicePolicyManager dpm) {
883 final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
884 context, userId);
885 return admin != null && dpm.getPasswordQuality(admin.component, userId) ==
886 DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
887 }
888
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700889 @Override
890 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
891 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
892 final Resources res = context.getResources();
893
894 final String screenTitle = res.getString(R.string.security_settings_title);
895
Fabrice Di Megliod7290292014-07-01 14:32:03 -0700896 SearchIndexableRaw data = new SearchIndexableRaw(context);
897 data.title = screenTitle;
898 data.screenTitle = screenTitle;
899 result.add(data);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700900
Xiaohui Chen44879a32015-07-22 13:53:22 -0700901 final UserManager um = UserManager.get(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700902
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400903 // Fingerprint
Jeff Sharkeyab508072016-10-11 14:25:22 -0600904 final FingerprintManager fpm = Utils.getFingerprintManagerOrNull(context);
Jim Millerb528b262016-02-04 17:10:44 -0800905 if (fpm != null && fpm.isHardwareDetected()) {
Jim Millerc5fa6e22015-09-22 18:30:13 -0700906 // This catches the title which can be overloaded in an overlay
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400907 data = new SearchIndexableRaw(context);
908 data.title = res.getString(R.string.security_settings_fingerprint_preference_title);
909 data.screenTitle = screenTitle;
910 result.add(data);
Jim Millerc5fa6e22015-09-22 18:30:13 -0700911 // Fallback for when the above doesn't contain "fingerprint"
912 data = new SearchIndexableRaw(context);
913 data.title = res.getString(R.string.fingerprint_manage_category_title);
914 data.screenTitle = screenTitle;
915 result.add(data);
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400916 }
917
Clara Bayarri52681ec2016-01-28 11:11:35 +0000918 final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Clara Bayarri462cce12016-02-18 12:09:21 +0000919 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000920 if (profileUserId != UserHandle.USER_NULL
921 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)) {
922 if (lockPatternUtils.getKeyguardStoredPasswordQuality(profileUserId)
923 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
924 && lockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
925 profileUserId)) {
926 data = new SearchIndexableRaw(context);
927 data.title = res.getString(R.string.lock_settings_profile_unification_title);
928 data.screenTitle = screenTitle;
929 result.add(data);
930 }
931 }
932
Jim Millerfe765592014-07-31 15:30:29 -0700933 // Advanced
Adrian Roos54375882015-04-16 17:11:22 -0700934 if (lockPatternUtils.isSecure(MY_USER_ID)) {
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800935 final TrustAgentManager trustAgentManager =
936 FeatureFactory.getFactory(context).getSecurityFeatureProvider()
937 .getTrustAgentManager();
938 final List<TrustAgentComponentInfo> agents =
939 getActiveTrustAgents(context, trustAgentManager, lockPatternUtils,
Adrian Roos088de472015-04-07 14:09:39 +0200940 context.getSystemService(DevicePolicyManager.class));
Jim Millerfe765592014-07-31 15:30:29 -0700941 for (int i = 0; i < agents.size(); i++) {
942 final TrustAgentComponentInfo agent = agents.get(i);
943 data = new SearchIndexableRaw(context);
944 data.title = agent.title;
945 data.screenTitle = screenTitle;
946 result.add(data);
947 }
948 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700949 return result;
950 }
951
952 @Override
953 public List<String> getNonIndexableKeys(Context context) {
Matthew Fritze2e34a642017-05-11 11:56:27 -0700954 final List<String> keys = super.getNonIndexableKeys(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700955
956 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700957
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700958 // Do not display SIM lock for devices without an Icc card
Xiaohui Chen44879a32015-07-22 13:53:22 -0700959 final UserManager um = UserManager.get(context);
960 final TelephonyManager tm = TelephonyManager.from(context);
961 if (!um.isAdminUser() || !tm.hasIccCard()) {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700962 keys.add(KEY_SIM_LOCK);
963 }
964
Jim Millerfe765592014-07-31 15:30:29 -0700965 // TrustAgent settings disappear when the user has no primary security.
Adrian Roos54375882015-04-16 17:11:22 -0700966 if (!lockPatternUtils.isSecure(MY_USER_ID)) {
Jim Millerfe765592014-07-31 15:30:29 -0700967 keys.add(KEY_TRUST_AGENT);
968 keys.add(KEY_MANAGE_TRUST_AGENTS);
969 }
970
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100971 if (!(new EnterprisePrivacyPreferenceController(context, null /* lifecycle */))
972 .isAvailable()) {
973 keys.add(KEY_ENTERPRISE_PRIVACY);
974 }
975
Matthew Fritze2e34a642017-05-11 11:56:27 -0700976 // Duplicate in special app access
977 keys.add(KEY_MANAGE_DEVICE_ADMIN);
978 // Duplicates between parent-child
979 keys.add(KEY_LOCATION);
980 keys.add(KEY_ENCRYPTION_AND_CREDENTIALS);
981 keys.add(KEY_SCREEN_PINNING);
982 keys.add(KEY_LOCATION_SCANNING);
983
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700984 return keys;
985 }
986 }
987
Adrian Roos62775bf2016-01-28 13:23:53 -0800988 public static class SecuritySubSettings extends SettingsPreferenceFragment
Doris Ling93582992017-03-16 13:20:15 -0700989 implements OnPreferenceChangeListener, OwnerInfoPreferenceController.OwnerInfoCallback {
Adrian Roos62775bf2016-01-28 13:23:53 -0800990
991 private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
992 private static final String KEY_LOCK_AFTER_TIMEOUT = "lock_after_timeout";
Adrian Roos62775bf2016-01-28 13:23:53 -0800993 private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
Adrian Roos62775bf2016-01-28 13:23:53 -0800994
995 // These switch preferences need special handling since they're not all stored in Settings.
996 private static final String SWITCH_PREFERENCE_KEYS[] = { KEY_LOCK_AFTER_TIMEOUT,
Clara Bayarrib4d55a22016-04-20 15:02:44 +0100997 KEY_VISIBLE_PATTERN, KEY_POWER_INSTANTLY_LOCKS };
Adrian Roos62775bf2016-01-28 13:23:53 -0800998
Sudheer Shanka900adce2016-03-04 16:58:34 -0800999 private TimeoutListPreference mLockAfter;
Adrian Roos62775bf2016-01-28 13:23:53 -08001000 private SwitchPreference mVisiblePattern;
1001 private SwitchPreference mPowerButtonInstantlyLocks;
Adrian Roos62775bf2016-01-28 13:23:53 -08001002
Adrian Roos73c52f22017-02-28 15:59:01 -08001003 private TrustAgentManager mTrustAgentManager;
Adrian Roos62775bf2016-01-28 13:23:53 -08001004 private LockPatternUtils mLockPatternUtils;
1005 private DevicePolicyManager mDPM;
Doris Ling93582992017-03-16 13:20:15 -07001006 private OwnerInfoPreferenceController mOwnerInfoPreferenceController;
Adrian Roos62775bf2016-01-28 13:23:53 -08001007
1008 @Override
Fan Zhang65076132016-08-08 10:25:13 -07001009 public int getMetricsCategory() {
Adrian Roos62775bf2016-01-28 13:23:53 -08001010 return MetricsEvent.SECURITY;
1011 }
1012
1013 @Override
1014 public void onCreate(Bundle icicle) {
1015 super.onCreate(icicle);
Adrian Roos73c52f22017-02-28 15:59:01 -08001016 SecurityFeatureProvider securityFeatureProvider =
1017 FeatureFactory.getFactory(getActivity()).getSecurityFeatureProvider();
1018 mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
Adrian Roos62775bf2016-01-28 13:23:53 -08001019 mLockPatternUtils = new LockPatternUtils(getContext());
1020 mDPM = getContext().getSystemService(DevicePolicyManager.class);
Doris Ling93582992017-03-16 13:20:15 -07001021 mOwnerInfoPreferenceController =
1022 new OwnerInfoPreferenceController(getContext(), this, null /* lifecycle */);
Adrian Roos3686ef52016-04-27 13:58:22 -07001023 createPreferenceHierarchy();
Adrian Roos62775bf2016-01-28 13:23:53 -08001024 }
1025
1026 @Override
1027 public void onResume() {
1028 super.onResume();
1029
1030 createPreferenceHierarchy();
1031
Adrian Roos62775bf2016-01-28 13:23:53 -08001032 if (mVisiblePattern != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001033 mVisiblePattern.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Adrian Roos62775bf2016-01-28 13:23:53 -08001034 MY_USER_ID));
1035 }
1036 if (mPowerButtonInstantlyLocks != null) {
William Luh4f80f222017-02-24 09:49:37 -08001037 mPowerButtonInstantlyLocks.setChecked(
1038 mLockPatternUtils.getPowerButtonInstantlyLocks(MY_USER_ID));
Adrian Roos62775bf2016-01-28 13:23:53 -08001039 }
1040
Doris Ling93582992017-03-16 13:20:15 -07001041 mOwnerInfoPreferenceController.updateSummary();
Adrian Roos62775bf2016-01-28 13:23:53 -08001042 }
1043
1044 @Override
1045 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1046 super.onActivityResult(requestCode, resultCode, data);
1047
1048 createPreferenceHierarchy();
1049 }
1050
1051 private void createPreferenceHierarchy() {
1052 PreferenceScreen root = getPreferenceScreen();
1053 if (root != null) {
1054 root.removeAll();
1055 }
Adrian Roos62775bf2016-01-28 13:23:53 -08001056
1057 final int resid = getResIdForLockUnlockSubScreen(getActivity(),
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001058 new LockPatternUtils(getContext()),
1059 ManagedLockPasswordProvider.get(getContext(), MY_USER_ID));
Adrian Roos62775bf2016-01-28 13:23:53 -08001060 addPreferencesFromResource(resid);
1061
1062 // lock after preference
Sudheer Shanka900adce2016-03-04 16:58:34 -08001063 mLockAfter = (TimeoutListPreference) findPreference(KEY_LOCK_AFTER_TIMEOUT);
Adrian Roos62775bf2016-01-28 13:23:53 -08001064 if (mLockAfter != null) {
1065 setupLockAfterPreference();
1066 updateLockAfterPreferenceSummary();
1067 }
1068
1069 // visible pattern
1070 mVisiblePattern = (SwitchPreference) findPreference(KEY_VISIBLE_PATTERN);
1071
1072 // lock instantly on power key press
1073 mPowerButtonInstantlyLocks = (SwitchPreference) findPreference(
1074 KEY_POWER_INSTANTLY_LOCKS);
Adrian Roos73c52f22017-02-28 15:59:01 -08001075 CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
1076 mTrustAgentManager, mLockPatternUtils, mDPM);
1077 if (mPowerButtonInstantlyLocks != null && !TextUtils.isEmpty(trustAgentLabel)) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001078 mPowerButtonInstantlyLocks.setSummary(getString(
1079 R.string.lockpattern_settings_power_button_instantly_locks_summary,
Adrian Roos73c52f22017-02-28 15:59:01 -08001080 trustAgentLabel));
Adrian Roos62775bf2016-01-28 13:23:53 -08001081 }
1082
Doris Ling93582992017-03-16 13:20:15 -07001083 mOwnerInfoPreferenceController.displayPreference(getPreferenceScreen());
1084 mOwnerInfoPreferenceController.updateEnableState();
Adrian Roos62775bf2016-01-28 13:23:53 -08001085
Adrian Roos62775bf2016-01-28 13:23:53 -08001086 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
1087 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
1088 if (pref != null) pref.setOnPreferenceChangeListener(this);
1089 }
1090 }
1091
Adrian Roos62775bf2016-01-28 13:23:53 -08001092 private void setupLockAfterPreference() {
1093 // Compatible with pre-Froyo
1094 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1095 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1096 mLockAfter.setValue(String.valueOf(currentTimeout));
1097 mLockAfter.setOnPreferenceChangeListener(this);
Sudheer Shanka900adce2016-03-04 16:58:34 -08001098 if (mDPM != null) {
1099 final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
1100 getActivity());
Ricky Wai36cce832016-03-18 16:26:35 +00001101 final long adminTimeout = mDPM
1102 .getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId());
Adrian Roos62775bf2016-01-28 13:23:53 -08001103 final long displayTimeout = Math.max(0,
1104 Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0));
Sudheer Shanka900adce2016-03-04 16:58:34 -08001105 // This setting is a slave to display timeout when a device policy is enforced.
1106 // As such, maxLockTimeout = adminTimeout - displayTimeout.
1107 // If there isn't enough time, shows "immediately" setting.
1108 final long maxTimeout = Math.max(0, adminTimeout - displayTimeout);
1109 mLockAfter.removeUnusableTimeouts(maxTimeout, admin);
Adrian Roos62775bf2016-01-28 13:23:53 -08001110 }
1111 }
1112
1113 private void updateLockAfterPreferenceSummary() {
1114 final String summary;
1115 if (mLockAfter.isDisabledByAdmin()) {
1116 summary = getString(R.string.disabled_by_policy_title);
1117 } else {
1118 // Update summary message with current value
1119 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1120 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1121 final CharSequence[] entries = mLockAfter.getEntries();
1122 final CharSequence[] values = mLockAfter.getEntryValues();
1123 int best = 0;
1124 for (int i = 0; i < values.length; i++) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001125 long timeout = Long.valueOf(values[i].toString());
1126 if (currentTimeout >= timeout) {
1127 best = i;
1128 }
1129 }
1130
Adrian Roos73c52f22017-02-28 15:59:01 -08001131 CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
1132 mTrustAgentManager, mLockPatternUtils, mDPM);
1133 if (!TextUtils.isEmpty(trustAgentLabel)) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001134 if (Long.valueOf(values[best].toString()) == 0) {
1135 summary = getString(R.string.lock_immediately_summary_with_exception,
Adrian Roos73c52f22017-02-28 15:59:01 -08001136 trustAgentLabel);
Adrian Roos62775bf2016-01-28 13:23:53 -08001137 } else {
1138 summary = getString(R.string.lock_after_timeout_summary_with_exception,
Adrian Roos73c52f22017-02-28 15:59:01 -08001139 entries[best], trustAgentLabel);
Adrian Roos62775bf2016-01-28 13:23:53 -08001140 }
1141 } else {
1142 summary = getString(R.string.lock_after_timeout_summary, entries[best]);
1143 }
1144 }
1145 mLockAfter.setSummary(summary);
1146 }
1147
Doris Ling93582992017-03-16 13:20:15 -07001148 @Override
1149 public void onOwnerInfoUpdated() {
1150 mOwnerInfoPreferenceController.updateSummary();
Adrian Roos62775bf2016-01-28 13:23:53 -08001151 }
1152
1153 private static int getResIdForLockUnlockSubScreen(Context context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001154 LockPatternUtils lockPatternUtils,
1155 ManagedLockPasswordProvider managedPasswordProvider) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001156 if (lockPatternUtils.isSecure(MY_USER_ID)) {
1157 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(MY_USER_ID)) {
1158 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
1159 return R.xml.security_settings_pattern_sub;
1160 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
1161 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
1162 return R.xml.security_settings_pin_sub;
1163 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
1164 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
1165 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
1166 return R.xml.security_settings_password_sub;
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001167 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
1168 return managedPasswordProvider.getResIdForLockUnlockSubScreen();
Adrian Roos62775bf2016-01-28 13:23:53 -08001169 }
1170 } else if (!lockPatternUtils.isLockScreenDisabled(MY_USER_ID)) {
1171 return R.xml.security_settings_slide_sub;
1172 }
1173 return 0;
1174 }
1175
1176 @Override
1177 public boolean onPreferenceChange(Preference preference, Object value) {
1178 String key = preference.getKey();
1179 if (KEY_POWER_INSTANTLY_LOCKS.equals(key)) {
1180 mLockPatternUtils.setPowerButtonInstantlyLocks((Boolean) value, MY_USER_ID);
1181 } else if (KEY_LOCK_AFTER_TIMEOUT.equals(key)) {
1182 int timeout = Integer.parseInt((String) value);
1183 try {
1184 Settings.Secure.putInt(getContentResolver(),
1185 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, timeout);
1186 } catch (NumberFormatException e) {
1187 Log.e("SecuritySettings", "could not persist lockAfter timeout setting", e);
1188 }
Julia Reynoldsde4f4712016-05-12 13:48:34 -04001189 setupLockAfterPreference();
Adrian Roos62775bf2016-01-28 13:23:53 -08001190 updateLockAfterPreferenceSummary();
1191 } else if (KEY_VISIBLE_PATTERN.equals(key)) {
1192 mLockPatternUtils.setVisiblePatternEnabled((Boolean) value, MY_USER_ID);
Adrian Roos62775bf2016-01-28 13:23:53 -08001193 }
1194 return true;
1195 }
1196 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001197
Fan Zhang1e516282016-09-16 12:45:07 -07001198 public static class UnificationConfirmationDialog extends InstrumentedDialogFragment {
Clara Bayarri56462d62016-02-12 13:55:36 +00001199 private static final String EXTRA_COMPLIANT = "compliant";
Clara Bayarri52681ec2016-01-28 11:11:35 +00001200
Clara Bayarri56462d62016-02-12 13:55:36 +00001201 public static UnificationConfirmationDialog newIntance(boolean compliant) {
Clara Bayarri52681ec2016-01-28 11:11:35 +00001202 UnificationConfirmationDialog dialog = new UnificationConfirmationDialog();
Clara Bayarri56462d62016-02-12 13:55:36 +00001203 Bundle args = new Bundle();
1204 args.putBoolean(EXTRA_COMPLIANT, compliant);
1205 dialog.setArguments(args);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001206 return dialog;
1207 }
1208
1209 @Override
1210 public void show(FragmentManager manager, String tag) {
1211 if (manager.findFragmentByTag(tag) == null) {
1212 // Prevent opening multiple dialogs if tapped on button quickly
1213 super.show(manager, tag);
1214 }
1215 }
1216
1217 @Override
1218 public Dialog onCreateDialog(Bundle savedInstanceState) {
1219 final SecuritySettings parentFragment = ((SecuritySettings) getParentFragment());
Clara Bayarri56462d62016-02-12 13:55:36 +00001220 final boolean compliant = getArguments().getBoolean(EXTRA_COMPLIANT);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001221 return new AlertDialog.Builder(getActivity())
1222 .setTitle(R.string.lock_settings_profile_unification_dialog_title)
Clara Bayarri56462d62016-02-12 13:55:36 +00001223 .setMessage(compliant ? R.string.lock_settings_profile_unification_dialog_body
1224 : R.string.lock_settings_profile_unification_dialog_uncompliant_body)
Mahaver Chopra26632092016-03-18 19:08:05 +00001225 .setPositiveButton(
1226 compliant ? R.string.lock_settings_profile_unification_dialog_confirm
Clara Bayarri56462d62016-02-12 13:55:36 +00001227 : R.string.lock_settings_profile_unification_dialog_uncompliant_confirm,
Clara Bayarri52681ec2016-01-28 11:11:35 +00001228 new DialogInterface.OnClickListener() {
1229 @Override
1230 public void onClick(DialogInterface dialog, int whichButton) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001231 if (compliant) {
1232 parentFragment.launchConfirmDeviceLockForUnification();
1233 } else {
1234 parentFragment.unifyUncompliantLocks();
1235 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001236 }
1237 }
1238 )
Ricky Waif6fc2202016-04-12 18:35:41 +01001239 .setNegativeButton(R.string.cancel, null)
Clara Bayarri52681ec2016-01-28 11:11:35 +00001240 .create();
1241 }
Ricky Waif6fc2202016-04-12 18:35:41 +01001242
1243 @Override
1244 public void onDismiss(DialogInterface dialog) {
1245 super.onDismiss(dialog);
1246 ((SecuritySettings) getParentFragment()).updateUnificationPreference();
1247 }
Fan Zhang1e516282016-09-16 12:45:07 -07001248
1249 @Override
1250 public int getMetricsCategory() {
1251 return MetricsEvent.DIALOG_UNIFICATION_CONFIRMATION;
1252 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001253 }
1254
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001255 static class SummaryProvider implements SummaryLoader.SummaryProvider {
1256
1257 private final Context mContext;
1258 private final SummaryLoader mSummaryLoader;
1259
1260 public SummaryProvider(Context context, SummaryLoader summaryLoader) {
1261 mContext = context;
1262 mSummaryLoader = summaryLoader;
1263 }
1264
1265 @Override
1266 public void setListening(boolean listening) {
Doris Ling48ffe672017-04-10 15:14:22 -07001267 if (listening) {
1268 final FingerprintManager fpm =
1269 Utils.getFingerprintManagerOrNull(mContext);
Fan Zhang2cab2982017-02-14 11:58:25 -08001270 if (fpm != null && fpm.isHardwareDetected()) {
1271 mSummaryLoader.setSummary(this,
Doris Ling48ffe672017-04-10 15:14:22 -07001272 mContext.getString(R.string.security_dashboard_summary));
Fan Zhang2cab2982017-02-14 11:58:25 -08001273 } else {
Doris Ling48ffe672017-04-10 15:14:22 -07001274 mSummaryLoader.setSummary(this, mContext.getString(
1275 R.string.security_dashboard_summary_no_fingerprint));
Fan Zhang2cab2982017-02-14 11:58:25 -08001276 }
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001277 }
1278 }
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001279 }
1280
1281 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
1282 new SummaryLoader.SummaryProviderFactory() {
1283 @Override
1284 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
1285 SummaryLoader summaryLoader) {
1286 return new SummaryProvider(activity, summaryLoader);
1287 }
1288 };
1289
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001290}