blob: 26a9ecbf355836d81d906997cc189be20420531e [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;
Fan Zhang06983002017-10-16 17:16:53 -070062import com.android.settings.dashboard.ProgressiveDisclosureMixin;
Shahriyar Amini5c1ad612017-01-11 11:30:13 -080063import com.android.settings.dashboard.SummaryLoader;
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +010064import com.android.settings.enterprise.EnterprisePrivacyPreferenceController;
65import com.android.settings.enterprise.ManageDeviceAdminPreferenceController;
Jorim Jaggi5ad75f02015-04-22 16:17:23 -070066import com.android.settings.fingerprint.FingerprintSettings;
Doris Ling81610542017-02-15 17:49:18 -080067import com.android.settings.location.LocationPreferenceController;
Doris Lingcee83e42017-04-06 16:01:37 -070068import com.android.settings.notification.LockScreenNotificationPreferenceController;
Fan Zhang301fe802016-10-26 10:44:45 -070069import com.android.settings.overlay.FeatureFactory;
Maurice Lam2eb170c2017-04-28 16:18:47 -070070import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
71import com.android.settings.password.ChooseLockSettingsHelper;
72import com.android.settings.password.ManagedLockPasswordProvider;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070073import com.android.settings.search.BaseSearchIndexProvider;
74import com.android.settings.search.Indexable;
75import com.android.settings.search.SearchIndexableRaw;
Doris Ling93582992017-03-16 13:20:15 -070076import com.android.settings.security.OwnerInfoPreferenceController;
Shahriyar Amini4774b582016-12-28 11:15:44 -080077import com.android.settings.security.SecurityFeatureProvider;
Zachary Iqbalccae73f2017-01-17 14:55:49 -080078import com.android.settings.trustagent.TrustAgentManager;
Fan Zhang62e66c92017-02-08 16:00:56 -080079import com.android.settings.widget.GearPreference;
Sudheer Shanka550d0682016-01-13 15:16:55 +000080import com.android.settingslib.RestrictedLockUtils;
Sudheer Shanka9c324682016-01-18 11:17:23 +000081import com.android.settingslib.RestrictedPreference;
Fan Zhang301fe802016-10-26 10:44:45 -070082import com.android.settingslib.drawer.CategoryKey;
Gilles Debunnea6a8a142011-06-09 11:56:17 -070083
Jim Miller86624a22010-08-30 22:07:58 -070084import java.util.ArrayList;
rich canningsbfbdcef2012-09-09 12:48:50 -070085import java.util.List;
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -040086
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087/**
88 * Gesture lock pattern settings.
89 */
Julia Reynolds565653c2014-06-12 11:49:12 -040090public class SecuritySettings extends SettingsPreferenceFragment
Suprabh Shuklaf61f11b2017-02-06 15:25:29 -080091 implements OnPreferenceChangeListener, Indexable,
Adrian Roos62775bf2016-01-28 13:23:53 -080092 GearPreference.OnGearClickListener {
Jim Miller92186872015-03-04 18:07:32 -080093
94 private static final String TAG = "SecuritySettings";
Fan Zhang301fe802016-10-26 10:44:45 -070095
Jim Miller250b9be2014-09-05 18:48:18 -070096 private static final String TRUST_AGENT_CLICK_INTENT = "trust_agent_click_intent";
Ido Ofir6f482442014-05-09 14:47:15 -070097 private static final Intent TRUST_AGENT_INTENT =
98 new Intent(TrustAgentService.SERVICE_INTERFACE);
Jason parks6f8fb432011-01-07 09:02:14 -060099
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 // Lock Settings
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800101 private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
Clara Bayarri52681ec2016-01-28 11:11:35 +0000102 private static final String KEY_UNLOCK_SET_OR_CHANGE_PROFILE = "unlock_set_or_change_profile";
103 private static final String KEY_VISIBLE_PATTERN_PROFILE = "visiblepattern_profile";
Amith Yamasaniedac9af2010-11-17 09:08:21 -0800104 private static final String KEY_SECURITY_CATEGORY = "security_category";
Doris Ling2db8c862017-04-06 14:22:18 -0700105 @VisibleForTesting
106 static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
Clara Bayarri52681ec2016-01-28 11:11:35 +0000107 private static final String KEY_UNIFICATION = "unification";
Doris Lingcee83e42017-04-06 16:01:37 -0700108 @VisibleForTesting
109 static final String KEY_LOCKSCREEN_PREFERENCES = "lockscreen_preferences";
Matthew Fritze2e34a642017-05-11 11:56:27 -0700110 private static final String KEY_ENCRYPTION_AND_CREDENTIALS = "encryption_and_credential";
111 private static final String KEY_LOCATION_SCANNING = "location_scanning";
112 private static final String KEY_LOCATION = "location";
Jim Milleref3e7012013-08-20 19:03:30 -0700113
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800114 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
Ido Ofir6f482442014-05-09 14:47:15 -0700115 private static final int CHANGE_TRUST_AGENT_SETTINGS = 126;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000116 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE = 127;
117 private static final int UNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 128;
118 private static final int UNIFY_LOCK_CONFIRM_PROFILE_REQUEST = 129;
Mahaver Chopra26632092016-03-18 19:08:05 +0000119 private static final int UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 130;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000120 private static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
Hung-ying Tyan7031ab02009-07-02 00:26:46 +0800121
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800122 // Misc Settings
Doris Ling81610542017-02-15 17:49:18 -0800123 private static final String KEY_SIM_LOCK = "sim_lock_settings";
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800124 private static final String KEY_SHOW_PASSWORD = "show_password";
Ido Ofir6f482442014-05-09 14:47:15 -0700125 private static final String KEY_TRUST_AGENT = "trust_agent";
Jason Monk27d7ea52014-07-18 09:55:41 -0400126 private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
Jim Miller47d380f2010-01-20 13:37:14 -0800127
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800128 // Security status
129 private static final String KEY_SECURITY_STATUS = "security_status";
130 private static final String SECURITY_STATUS_KEY_PREFIX = "security_status_";
131
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800132 // Package verifier Settings
133 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800134 static final String KEY_PACKAGE_VERIFIER_STATUS = "security_status_package_verifier";
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800135 private static final int PACKAGE_VERIFIER_STATE_ENABLED = 1;
136
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100137 // Device management settings
138 private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
139 private static final String KEY_MANAGE_DEVICE_ADMIN = "manage_device_admin";
140
Jim Miller4a9065e2014-08-08 15:25:31 -0700141 // These switch preferences need special handling since they're not all stored in Settings.
Adrian Roos62775bf2016-01-28 13:23:53 -0800142 private static final String SWITCH_PREFERENCE_KEYS[] = {
Suprabh Shuklaf61f11b2017-02-06 15:25:29 -0800143 KEY_SHOW_PASSWORD, KEY_UNIFICATION, KEY_VISIBLE_PATTERN_PROFILE
Mahaver Chopraa1276e42016-03-04 17:17:59 +0000144 };
Jim Miller4a9065e2014-08-08 15:25:31 -0700145
Jim Millerfe765592014-07-31 15:30:29 -0700146 // Only allow one trust agent on the platform.
147 private static final boolean ONLY_ONE_TRUST_AGENT = true;
148
Adrian Roos54375882015-04-16 17:11:22 -0700149 private static final int MY_USER_ID = UserHandle.myUserId();
150
Fan Zhang301fe802016-10-26 10:44:45 -0700151 private DashboardFeatureProvider mDashboardFeatureProvider;
Maggie Benthallf48206e2013-08-20 11:03:05 -0400152 private DevicePolicyManager mDPM;
Shahriyar Amini4774b582016-12-28 11:15:44 -0800153 private SecurityFeatureProvider mSecurityFeatureProvider;
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800154 private TrustAgentManager mTrustAgentManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800155 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700156 private UserManager mUm;
Jim Miller122b6c82010-02-10 19:53:58 -0800157
Jim Miller00d24762009-12-22 19:04:57 -0800158 private ChooseLockSettingsHelper mChooseLockSettingsHelper;
Jim Miller122b6c82010-02-10 19:53:58 -0800159 private LockPatternUtils mLockPatternUtils;
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700160 private ManagedLockPasswordProvider mManagedPasswordProvider;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800161
Clara Bayarri52681ec2016-01-28 11:11:35 +0000162 private SwitchPreference mVisiblePatternProfile;
Clara Bayarri56462d62016-02-12 13:55:36 +0000163 private SwitchPreference mUnifyProfile;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000164
Jim Miller4a9065e2014-08-08 15:25:31 -0700165 private SwitchPreference mShowPassword;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800166
Xiaohui Chen44879a32015-07-22 13:53:22 -0700167 private boolean mIsAdmin;
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700168
Jim Miller250b9be2014-09-05 18:48:18 -0700169 private Intent mTrustAgentClickIntent;
Ido Ofir1682a0d2014-06-24 16:26:09 -0700170
Clara Bayarri6934a042015-10-14 11:07:35 +0100171 private int mProfileChallengeUserId;
172
Clara Bayarri52681ec2016-01-28 11:11:35 +0000173 private String mCurrentDevicePassword;
174 private String mCurrentProfilePassword;
175
Doris Ling81610542017-02-15 17:49:18 -0800176 private LocationPreferenceController mLocationcontroller;
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100177 private ManageDeviceAdminPreferenceController mManageDeviceAdminPreferenceController;
178 private EnterprisePrivacyPreferenceController mEnterprisePrivacyPreferenceController;
Doris Lingcee83e42017-04-06 16:01:37 -0700179 private LockScreenNotificationPreferenceController mLockScreenNotificationPreferenceController;
Doris Ling81610542017-02-15 17:49:18 -0800180
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800181 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700182 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500183 return MetricsEvent.SECURITY;
Chris Wren8a963ba2015-03-20 10:29:14 -0400184 }
185
186 @Override
Alex Salo2e52b422017-05-05 15:36:27 -0700187 public void onAttach(Context context) {
188 super.onAttach(context);
189 mLocationcontroller = new LocationPreferenceController(context, getLifecycle());
190 }
191
192 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700193 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 super.onCreate(savedInstanceState);
Jim Miller122b6c82010-02-10 19:53:58 -0800195
Fan Zhang301fe802016-10-26 10:44:45 -0700196 final Activity activity = getActivity();
Wink Saville0183fb52014-11-22 10:11:39 -0800197
Fan Zhang301fe802016-10-26 10:44:45 -0700198 mSubscriptionManager = SubscriptionManager.from(activity);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800199
Fan Zhang301fe802016-10-26 10:44:45 -0700200 mLockPatternUtils = new LockPatternUtils(activity);
201
202 mManagedPasswordProvider = ManagedLockPasswordProvider.get(activity, MY_USER_ID);
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700203
Dianne Hackbornabc3dc62010-01-20 13:40:19 -0800204 mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Jim Miller122b6c82010-02-10 19:53:58 -0800205
Fan Zhang301fe802016-10-26 10:44:45 -0700206 mUm = UserManager.get(activity);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700207
Fan Zhang301fe802016-10-26 10:44:45 -0700208 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(activity);
209
210 mDashboardFeatureProvider = FeatureFactory.getFactory(activity)
211 .getDashboardFeatureProvider(activity);
Jim Miller250b9be2014-09-05 18:48:18 -0700212
Shahriyar Amini4774b582016-12-28 11:15:44 -0800213 mSecurityFeatureProvider = FeatureFactory.getFactory(activity).getSecurityFeatureProvider();
214
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800215 mTrustAgentManager = mSecurityFeatureProvider.getTrustAgentManager();
216
Jim Miller250b9be2014-09-05 18:48:18 -0700217 if (savedInstanceState != null
218 && savedInstanceState.containsKey(TRUST_AGENT_CLICK_INTENT)) {
219 mTrustAgentClickIntent = savedInstanceState.getParcelable(TRUST_AGENT_CLICK_INTENT);
220 }
Doris Ling81610542017-02-15 17:49:18 -0800221
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100222 mManageDeviceAdminPreferenceController
223 = new ManageDeviceAdminPreferenceController(activity);
224 mEnterprisePrivacyPreferenceController
225 = new EnterprisePrivacyPreferenceController(activity, null /* lifecycle */);
Doris Lingcee83e42017-04-06 16:01:37 -0700226 mLockScreenNotificationPreferenceController
227 = new LockScreenNotificationPreferenceController(activity);
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700228 }
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -0400229
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700230 private static int getResIdForLockUnlockScreen(Context context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700231 LockPatternUtils lockPatternUtils, ManagedLockPasswordProvider managedPasswordProvider,
232 int userId) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000233 final boolean isMyUser = userId == MY_USER_ID;
Jim Miller36972bb2010-11-30 19:47:38 -0800234 int resid = 0;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000235 if (!lockPatternUtils.isSecure(userId)) {
236 if (!isMyUser) {
237 resid = R.xml.security_settings_lockscreen_profile;
238 } else if (lockPatternUtils.isLockScreenDisabled(userId)) {
Jim Miller36972bb2010-11-30 19:47:38 -0800239 resid = R.xml.security_settings_lockscreen;
240 } else {
241 resid = R.xml.security_settings_chooser;
242 }
Jim Miller122b6c82010-02-10 19:53:58 -0800243 } else {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000244 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(userId)) {
Jim Milleraf366a32010-03-25 18:45:22 -0700245 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000246 resid = isMyUser ? R.xml.security_settings_pattern
247 : R.xml.security_settings_pattern_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700248 break;
249 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Nicolas Prevot8fd852e2014-01-25 01:02:04 +0000250 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000251 resid = isMyUser ? R.xml.security_settings_pin
252 : R.xml.security_settings_pin_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700253 break;
254 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
255 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
Konstantin Lopyrev57fbf692010-05-27 16:01:41 -0700256 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000257 resid = isMyUser ? R.xml.security_settings_password
258 : R.xml.security_settings_password_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700259 break;
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700260 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
261 resid = managedPasswordProvider.getResIdForLockUnlockScreen(!isMyUser);
262 break;
Jim Miller00d24762009-12-22 19:04:57 -0800263 }
Jim Miller122b6c82010-02-10 19:53:58 -0800264 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700265 return resid;
266 }
Jim Miller122b6c82010-02-10 19:53:58 -0800267
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700268 /**
269 * Important!
270 *
Ido Ofir6f482442014-05-09 14:47:15 -0700271 * Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700272 * logic or adding/removing preferences here.
273 */
274 private PreferenceScreen createPreferenceHierarchy() {
275 PreferenceScreen root = getPreferenceScreen();
276 if (root != null) {
277 root.removeAll();
278 }
Fan Zhang06983002017-10-16 17:16:53 -0700279 if (ProgressiveDisclosureMixin.isV2Enabled()) {
Fan Zhang06983002017-10-16 17:16:53 -0700280 addPreferencesFromResource(R.xml.security_settings_advanced_button);
Fan Zhangaee17312017-10-17 14:49:04 -0700281 } else {
282 addPreferencesFromResource(R.xml.security_settings);
Fan Zhang06983002017-10-16 17:16:53 -0700283 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700284 root = getPreferenceScreen();
285
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800286 // Add category for security status
287 addPreferencesFromResource(R.xml.security_settings_status);
288
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700289 // Add options for lock/unlock screen
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700290 final int resid = getResIdForLockUnlockScreen(getActivity(), mLockPatternUtils,
291 mManagedPasswordProvider, MY_USER_ID);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700292 addPreferencesFromResource(resid);
Jason parks41121992011-01-25 09:26:55 -0600293
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100294 // DO or PO installed in the user may disallow to change password.
295 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100296
Clara Bayarri462cce12016-02-18 12:09:21 +0000297 mProfileChallengeUserId = Utils.getManagedProfileId(mUm, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000298 if (mProfileChallengeUserId != UserHandle.USER_NULL
299 && mLockPatternUtils.isSeparateProfileChallengeAllowed(mProfileChallengeUserId)) {
300 addPreferencesFromResource(R.xml.security_settings_profile);
Mahaver Chopra26632092016-03-18 19:08:05 +0000301 addPreferencesFromResource(R.xml.security_settings_unification);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000302 final int profileResid = getResIdForLockUnlockScreen(
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700303 getActivity(), mLockPatternUtils, mManagedPasswordProvider,
304 mProfileChallengeUserId);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000305 addPreferencesFromResource(profileResid);
306 maybeAddFingerprintPreference(root, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000307 if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100308 final Preference lockPreference =
309 root.findPreference(KEY_UNLOCK_SET_OR_CHANGE_PROFILE);
310 final String summary = getContext().getString(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000311 R.string.lock_settings_profile_unified_summary);
312 lockPreference.setSummary(summary);
Clara Bayarri56462d62016-02-12 13:55:36 +0000313 lockPreference.setEnabled(false);
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100314 // PO may disallow to change password for the profile, but screen lock and managed
315 // profile's lock is the same. Disable main "Screen lock" menu.
316 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE, mProfileChallengeUserId);
317 } else {
318 // PO may disallow to change profile password, and the profile's password is
319 // separated from screen lock password. Disable profile specific "Screen lock" menu.
320 disableIfPasswordQualityManaged(KEY_UNLOCK_SET_OR_CHANGE_PROFILE,
321 mProfileChallengeUserId);
Clara Bayarri6934a042015-10-14 11:07:35 +0100322 }
323 }
324
Adrian Roos62775bf2016-01-28 13:23:53 -0800325 Preference unlockSetOrChange = findPreference(KEY_UNLOCK_SET_OR_CHANGE);
326 if (unlockSetOrChange instanceof GearPreference) {
327 ((GearPreference) unlockSetOrChange).setOnGearClickListener(this);
328 }
329
Xiaohui Chen44879a32015-07-22 13:53:22 -0700330 mIsAdmin = mUm.isAdminUser();
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700331
Jim Miller94d4bd92015-02-19 21:04:57 -0800332 // Fingerprint and trust agents
Doris Ling2db8c862017-04-06 14:22:18 -0700333 int numberOfTrustAgent = 0;
Ido Ofir1682a0d2014-06-24 16:26:09 -0700334 PreferenceGroup securityCategory = (PreferenceGroup)
335 root.findPreference(KEY_SECURITY_CATEGORY);
Jim Miller9d250052014-08-21 19:24:47 -0700336 if (securityCategory != null) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000337 maybeAddFingerprintPreference(securityCategory, UserHandle.myUserId());
Doris Ling2db8c862017-04-06 14:22:18 -0700338 numberOfTrustAgent = addTrustAgentSettings(securityCategory);
Doris Lingcee83e42017-04-06 16:01:37 -0700339 setLockscreenPreferencesSummary(securityCategory);
Ido Ofir1682a0d2014-06-24 16:26:09 -0700340 }
341
Clara Bayarri52681ec2016-01-28 11:11:35 +0000342 mVisiblePatternProfile =
343 (SwitchPreference) root.findPreference(KEY_VISIBLE_PATTERN_PROFILE);
Clara Bayarri56462d62016-02-12 13:55:36 +0000344 mUnifyProfile = (SwitchPreference) root.findPreference(KEY_UNIFICATION);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000345
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700346 // Append the rest of the settings
347 addPreferencesFromResource(R.xml.security_settings_misc);
348
349 // Do not display SIM lock for devices without an Icc card
350 TelephonyManager tm = TelephonyManager.getDefault();
Junda Liu80753bc2015-06-17 10:15:34 -0700351 CarrierConfigManager cfgMgr = (CarrierConfigManager)
352 getActivity().getSystemService(Context.CARRIER_CONFIG_SERVICE);
353 PersistableBundle b = cfgMgr.getConfig();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700354 if (!mIsAdmin || !isSimIccReady() ||
Junda Liu80753bc2015-06-17 10:15:34 -0700355 b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700356 root.removePreference(root.findPreference(KEY_SIM_LOCK));
357 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700358 // Disable SIM lock if there is no ready SIM card.
359 root.findPreference(KEY_SIM_LOCK).setEnabled(isSimReady());
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700360 }
Jason Monkb08853a2014-08-13 09:43:58 -0400361 if (Settings.System.getInt(getContentResolver(),
362 Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
363 root.findPreference(KEY_SCREEN_PINNING).setSummary(
364 getResources().getString(R.string.switch_on_text));
Jason Monk27d7ea52014-07-18 09:55:41 -0400365 }
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700366
Ng Zhi Anf0c03202017-09-20 17:51:28 -0700367 // Encryption status of device
368 if (LockPatternUtils.isDeviceEncryptionEnabled()) {
369 root.findPreference(KEY_ENCRYPTION_AND_CREDENTIALS).setSummary(
370 R.string.encryption_and_credential_settings_summary);
371 } else {
372 root.findPreference(KEY_ENCRYPTION_AND_CREDENTIALS).setSummary(
373 R.string.summary_placeholder);
374 }
375
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700376 // Show password
Jim Miller4a9065e2014-08-08 15:25:31 -0700377 mShowPassword = (SwitchPreference) root.findPreference(KEY_SHOW_PASSWORD);
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700378
Kenny Rootc5550c22013-04-08 09:56:05 -0700379 // Credential storage
Emily Bernier11bd33a2013-04-11 16:24:56 -0400380 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700381
Jim Millerfe765592014-07-31 15:30:29 -0700382 // Advanced Security features
Doris Ling2db8c862017-04-06 14:22:18 -0700383 initTrustAgentPreference(root, numberOfTrustAgent);
Jim Millerfe765592014-07-31 15:30:29 -0700384
385 // The above preferences come and go based on security state, so we need to update
386 // the index. This call is expected to be fairly cheap, but we may want to do something
387 // smarter in the future.
Matthew Fritze5c83cfa2017-03-16 13:33:18 -0700388 final Activity activity = getActivity();
389 FeatureFactory.getFactory(activity).getSearchFeatureProvider().getIndexingManager(activity)
Matthew Fritze8a42b072017-03-27 17:42:36 -0700390 .updateFromClassNameResource(SecuritySettings.class.getName(),
391 true /* includeInSearchResults */);
Jim Millerfe765592014-07-31 15:30:29 -0700392
Shahriyar Aminic60464e2017-01-23 20:27:34 -0800393 PreferenceGroup securityStatusPreferenceGroup =
394 (PreferenceGroup) root.findPreference(KEY_SECURITY_STATUS);
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800395 final List<Preference> tilePrefs = mDashboardFeatureProvider.getPreferencesForCategory(
396 getActivity(), getPrefContext(), getMetricsCategory(),
397 CategoryKey.CATEGORY_SECURITY);
398 int numSecurityStatusPrefs = 0;
399 if (tilePrefs != null && !tilePrefs.isEmpty()) {
400 for (Preference preference : tilePrefs) {
401 if (!TextUtils.isEmpty(preference.getKey())
402 && preference.getKey().startsWith(SECURITY_STATUS_KEY_PREFIX)) {
403 // Injected security status settings are placed under the Security status
404 // category.
405 securityStatusPreferenceGroup.addPreference(preference);
406 numSecurityStatusPrefs++;
407 } else {
408 // Other injected settings are placed under the Security preference screen.
409 root.addPreference(preference);
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800410 }
Fan Zhang301fe802016-10-26 10:44:45 -0700411 }
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800412 }
Fan Zhang301fe802016-10-26 10:44:45 -0700413
Doris Lingf2cf2ae2017-03-03 17:12:47 -0800414 if (numSecurityStatusPrefs == 0) {
415 root.removePreference(securityStatusPreferenceGroup);
416 } else if (numSecurityStatusPrefs > 0) {
417 // Update preference data with tile data. Security feature provider only updates the
418 // data if it actually needs to be changed.
419 mSecurityFeatureProvider.updatePreferences(getActivity(), root,
420 mDashboardFeatureProvider.getTilesForCategory(
421 CategoryKey.CATEGORY_SECURITY));
Shahriyar Amini5c1ad612017-01-11 11:30:13 -0800422 }
Shahriyar Amini4774b582016-12-28 11:15:44 -0800423
Jim Miller4a9065e2014-08-08 15:25:31 -0700424 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
425 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
426 if (pref != null) pref.setOnPreferenceChangeListener(this);
427 }
Doris Ling81610542017-02-15 17:49:18 -0800428
429 mLocationcontroller.displayPreference(root);
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100430 mManageDeviceAdminPreferenceController.updateState(
431 root.findPreference(KEY_MANAGE_DEVICE_ADMIN));
432 mEnterprisePrivacyPreferenceController.displayPreference(root);
433 mEnterprisePrivacyPreferenceController.onResume();
434
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800435 return root;
436 }
437
Doris Ling2db8c862017-04-06 14:22:18 -0700438 @VisibleForTesting
439 void initTrustAgentPreference(PreferenceScreen root, int numberOfTrustAgent) {
440 Preference manageAgents = root.findPreference(KEY_MANAGE_TRUST_AGENTS);
441 if (manageAgents != null) {
442 if (!mLockPatternUtils.isSecure(MY_USER_ID)) {
443 manageAgents.setEnabled(false);
444 manageAgents.setSummary(R.string.disabled_because_no_backup_security);
445 } else if (numberOfTrustAgent > 0) {
446 manageAgents.setSummary(getActivity().getResources().getQuantityString(
447 R.plurals.manage_trust_agents_summary_on,
448 numberOfTrustAgent, numberOfTrustAgent));
449 } else {
450 manageAgents.setSummary(R.string.manage_trust_agents_summary);
451 }
452 }
453 }
454
Doris Lingcee83e42017-04-06 16:01:37 -0700455 @VisibleForTesting
456 void setLockscreenPreferencesSummary(PreferenceGroup group) {
457 final Preference lockscreenPreferences = group.findPreference(KEY_LOCKSCREEN_PREFERENCES);
458 if (lockscreenPreferences != null) {
459 lockscreenPreferences.setSummary(
460 mLockScreenNotificationPreferenceController.getSummaryResource());
461 }
462 }
463
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100464 /*
465 * Sets the preference as disabled by admin if PASSWORD_QUALITY_MANAGED is set.
466 * The preference must be a RestrictedPreference.
467 */
468 private void disableIfPasswordQualityManaged(String preferenceKey, int userId) {
469 final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
470 getActivity(), userId);
471 if (admin != null && mDPM.getPasswordQuality(admin.component, userId) ==
472 DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
473 final RestrictedPreference pref =
474 (RestrictedPreference) getPreferenceScreen().findPreference(preferenceKey);
475 pref.setDisabledByAdmin(admin);
476 }
477 }
478
Clara Bayarri52681ec2016-01-28 11:11:35 +0000479 private void maybeAddFingerprintPreference(PreferenceGroup securityCategory, int userId) {
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000480 Preference fingerprintPreference =
481 FingerprintSettings.getFingerprintPreferenceForUser(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000482 securityCategory.getContext(), userId);
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000483 if (fingerprintPreference != null) {
484 securityCategory.addPreference(fingerprintPreference);
Jim Miller94d4bd92015-02-19 21:04:57 -0800485 }
Jim Miller94d4bd92015-02-19 21:04:57 -0800486 }
487
Doris Ling2db8c862017-04-06 14:22:18 -0700488 // Return the number of trust agents being added
489 private int addTrustAgentSettings(PreferenceGroup securityCategory) {
Adrian Roos54375882015-04-16 17:11:22 -0700490 final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800491 ArrayList<TrustAgentComponentInfo> agents = getActiveTrustAgents(
492 getActivity(), mTrustAgentManager, mLockPatternUtils, mDPM);
Jim Miller94d4bd92015-02-19 21:04:57 -0800493 for (int i = 0; i < agents.size(); i++) {
494 final TrustAgentComponentInfo agent = agents.get(i);
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000495 RestrictedPreference trustAgentPreference =
496 new RestrictedPreference(securityCategory.getContext());
Jim Miller94d4bd92015-02-19 21:04:57 -0800497 trustAgentPreference.setKey(KEY_TRUST_AGENT);
498 trustAgentPreference.setTitle(agent.title);
499 trustAgentPreference.setSummary(agent.summary);
500 // Create intent for this preference.
501 Intent intent = new Intent();
502 intent.setComponent(agent.componentName);
503 intent.setAction(Intent.ACTION_MAIN);
504 trustAgentPreference.setIntent(intent);
505 // Add preference to the settings menu.
506 securityCategory.addPreference(trustAgentPreference);
Adrian Roos088de472015-04-07 14:09:39 +0200507
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000508 trustAgentPreference.setDisabledByAdmin(agent.admin);
509 if (!trustAgentPreference.isDisabledByAdmin() && !hasSecurity) {
Jim Miller94d4bd92015-02-19 21:04:57 -0800510 trustAgentPreference.setEnabled(false);
511 trustAgentPreference.setSummary(R.string.disabled_because_no_backup_security);
512 }
513 }
Doris Ling2db8c862017-04-06 14:22:18 -0700514 return agents.size();
Jim Miller94d4bd92015-02-19 21:04:57 -0800515 }
516
PauloftheWest0b8788a2014-12-08 13:49:35 -0800517 /* Return true if a there is a Slot that has Icc.
518 */
519 private boolean isSimIccReady() {
520 TelephonyManager tm = TelephonyManager.getDefault();
521 final List<SubscriptionInfo> subInfoList =
522 mSubscriptionManager.getActiveSubscriptionInfoList();
523
524 if (subInfoList != null) {
525 for (SubscriptionInfo subInfo : subInfoList) {
Sanket Padawe24784202015-01-14 14:48:27 -0800526 if (tm.hasIccCard(subInfo.getSimSlotIndex())) {
PauloftheWest0b8788a2014-12-08 13:49:35 -0800527 return true;
528 }
529 }
530 }
531
532 return false;
533 }
534
PauloftheWest50e6eca2014-10-03 11:07:14 -0700535 /* Return true if a SIM is ready for locking.
536 * TODO: consider adding to TelephonyManager or SubscritpionManasger.
537 */
Wink Saville0183fb52014-11-22 10:11:39 -0800538 private boolean isSimReady() {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700539 int simState = TelephonyManager.SIM_STATE_UNKNOWN;
Wink Saville0183fb52014-11-22 10:11:39 -0800540 final List<SubscriptionInfo> subInfoList =
541 mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700542 if (subInfoList != null) {
Wink Savilleca756612014-11-08 10:47:12 -0800543 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -0700544 simState = TelephonyManager.getDefault().getSimState(subInfo.getSimSlotIndex());
PauloftheWest50e6eca2014-10-03 11:07:14 -0700545 if((simState != TelephonyManager.SIM_STATE_ABSENT) &&
546 (simState != TelephonyManager.SIM_STATE_UNKNOWN)){
547 return true;
548 }
549 }
550 }
551 return false;
552 }
553
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800554 private static ArrayList<TrustAgentComponentInfo> getActiveTrustAgents(Context context,
555 TrustAgentManager trustAgentManager, LockPatternUtils utils,
556 DevicePolicyManager dpm) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000557 PackageManager pm = context.getPackageManager();
Jim Millerfe765592014-07-31 15:30:29 -0700558 ArrayList<TrustAgentComponentInfo> result = new ArrayList<TrustAgentComponentInfo>();
559 List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
560 PackageManager.GET_META_DATA);
Adrian Roos54375882015-04-16 17:11:22 -0700561 List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(MY_USER_ID);
Adrian Roos088de472015-04-07 14:09:39 +0200562
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000563 EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
564 DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
Adrian Roos088de472015-04-07 14:09:39 +0200565
Jim Millerfe765592014-07-31 15:30:29 -0700566 if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
567 for (int i = 0; i < resolveInfos.size(); i++) {
568 ResolveInfo resolveInfo = resolveInfos.get(i);
569 if (resolveInfo.serviceInfo == null) continue;
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800570 if (!trustAgentManager.shouldProvideTrust(resolveInfo, pm)) {
571 continue;
572 }
Jim Millerfe765592014-07-31 15:30:29 -0700573 TrustAgentComponentInfo trustAgentComponentInfo =
574 TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
575 if (trustAgentComponentInfo.componentName == null ||
576 !enabledTrustAgents.contains(
577 TrustAgentUtils.getComponentName(resolveInfo)) ||
578 TextUtils.isEmpty(trustAgentComponentInfo.title)) continue;
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000579 if (admin != null && dpm.getTrustAgentConfiguration(
Adrian Roos088de472015-04-07 14:09:39 +0200580 null, TrustAgentUtils.getComponentName(resolveInfo)) == null) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000581 trustAgentComponentInfo.admin = admin;
Adrian Roos088de472015-04-07 14:09:39 +0200582 }
Jim Millerfe765592014-07-31 15:30:29 -0700583 result.add(trustAgentComponentInfo);
584 if (ONLY_ONE_TRUST_AGENT) break;
585 }
586 }
587 return result;
588 }
589
Adrian Roos73c52f22017-02-28 15:59:01 -0800590 private static CharSequence getActiveTrustAgentLabel(Context context,
591 TrustAgentManager trustAgentManager, LockPatternUtils utils,
592 DevicePolicyManager dpm) {
593 ArrayList<TrustAgentComponentInfo> agents = getActiveTrustAgents(context,
594 trustAgentManager, utils, dpm);
595 return agents.isEmpty() ? null : agents.get(0).title;
596 }
597
Gilles Debunne2c88a872011-06-21 12:47:17 -0700598 @Override
Adrian Roos62775bf2016-01-28 13:23:53 -0800599 public void onGearClick(GearPreference p) {
600 if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) {
601 startFragment(this, SecuritySubSettings.class.getName(), 0, 0, null);
602 }
603 }
604
605 @Override
Jim Miller250b9be2014-09-05 18:48:18 -0700606 public void onSaveInstanceState(Bundle outState) {
607 super.onSaveInstanceState(outState);
608 if (mTrustAgentClickIntent != null) {
609 outState.putParcelable(TRUST_AGENT_CLICK_INTENT, mTrustAgentClickIntent);
610 }
611 }
612
613 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700614 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800615 super.onResume();
616
Jim Miller86624a22010-08-30 22:07:58 -0700617 // Make sure we reload the preference hierarchy since some of these settings
618 // depend on others...
619 createPreferenceHierarchy();
620
Clara Bayarri52681ec2016-01-28 11:11:35 +0000621 if (mVisiblePatternProfile != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +0000622 mVisiblePatternProfile.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000623 mProfileChallengeUserId));
624 }
625
Clara Bayarri56462d62016-02-12 13:55:36 +0000626 updateUnificationPreference();
627
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700628 if (mShowPassword != null) {
629 mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
630 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
631 }
Hung-ying Tyanb0883cb2009-09-30 11:56:05 +0800632
Doris Ling81610542017-02-15 17:49:18 -0800633 mLocationcontroller.updateSummary();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800634 }
635
Clara Bayarri56462d62016-02-12 13:55:36 +0000636 private void updateUnificationPreference() {
637 if (mUnifyProfile != null) {
638 mUnifyProfile.setChecked(!mLockPatternUtils.isSeparateProfileChallengeEnabled(
639 mProfileChallengeUserId));
640 }
641 }
642
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800643 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400644 public boolean onPreferenceTreeClick(Preference preference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800645 final String key = preference.getKey();
Jim Millerbbb4afa2010-04-08 19:40:19 -0700646 if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
Charles Hefc6c0162017-04-24 18:05:48 +0100647 // TODO(b/35930129): Remove once existing password can be passed into vold directly.
648 // Currently we need this logic to ensure that the QUIET_MODE is off for any work
649 // profile with unified challenge on FBE-enabled devices. Otherwise, vold would not be
650 // able to complete the operation due to the lack of (old) encryption key.
651 if (mProfileChallengeUserId != UserHandle.USER_NULL
652 && !mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)
653 && StorageManager.isFileEncryptedNativeOnly()) {
654 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
655 mProfileChallengeUserId)) {
656 return false;
657 }
658 }
Maurice Lam2eb170c2017-04-28 16:18:47 -0700659 startFragment(this, ChooseLockGenericFragment.class.getName(),
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700660 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
Clara Bayarri6934a042015-10-14 11:07:35 +0100661 } else if (KEY_UNLOCK_SET_OR_CHANGE_PROFILE.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100662 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100663 mProfileChallengeUserId)) {
664 return false;
665 }
Clara Bayarri6934a042015-10-14 11:07:35 +0100666 Bundle extras = new Bundle();
667 extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
Maurice Lam2eb170c2017-04-28 16:18:47 -0700668 startFragment(this, ChooseLockGenericFragment.class.getName(),
Clara Bayarri52681ec2016-01-28 11:11:35 +0000669 R.string.lock_settings_picker_title_profile,
670 SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
Ido Ofir6f482442014-05-09 14:47:15 -0700671 } else if (KEY_TRUST_AGENT.equals(key)) {
672 ChooseLockSettingsHelper helper =
673 new ChooseLockSettingsHelper(this.getActivity(), this);
Jim Miller250b9be2014-09-05 18:48:18 -0700674 mTrustAgentClickIntent = preference.getIntent();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700675 boolean confirmationLaunched = helper.launchConfirmationActivity(
676 CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle());
677 if (!confirmationLaunched&& mTrustAgentClickIntent != null) {
Ido Ofir6f482442014-05-09 14:47:15 -0700678 // If this returns false, it means no password confirmation is required.
Jim Miller250b9be2014-09-05 18:48:18 -0700679 startActivity(mTrustAgentClickIntent);
680 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700681 }
Jim Miller36972bb2010-11-30 19:47:38 -0800682 } else {
683 // If we didn't handle it, let preferences handle it.
Jason Monk39b46742015-09-10 15:52:51 -0400684 return super.onPreferenceTreeClick(preference);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800685 }
Jim Miller36972bb2010-11-30 19:47:38 -0800686 return true;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800687 }
688
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800689 /**
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700690 * see confirmPatternThenDisableAndClear
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800691 */
692 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700693 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800694 super.onActivityResult(requestCode, resultCode, data);
Adrian Roosf7887182015-01-07 20:51:57 +0100695 if (requestCode == CHANGE_TRUST_AGENT_SETTINGS && resultCode == Activity.RESULT_OK) {
Jim Miller250b9be2014-09-05 18:48:18 -0700696 if (mTrustAgentClickIntent != null) {
697 startActivity(mTrustAgentClickIntent);
698 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700699 }
Jim Miller250b9be2014-09-05 18:48:18 -0700700 return;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000701 } else if (requestCode == UNIFY_LOCK_CONFIRM_DEVICE_REQUEST
702 && resultCode == Activity.RESULT_OK) {
703 mCurrentDevicePassword =
704 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
705 launchConfirmProfileLockForUnification();
706 return;
707 } else if (requestCode == UNIFY_LOCK_CONFIRM_PROFILE_REQUEST
708 && resultCode == Activity.RESULT_OK) {
709 mCurrentProfilePassword =
710 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
711 unifyLocks();
712 return;
Mahaver Chopra26632092016-03-18 19:08:05 +0000713 } else if (requestCode == UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST
714 && resultCode == Activity.RESULT_OK) {
715 ununifyLocks();
716 return;
Danielle Millett487b16f2011-11-02 11:12:21 -0400717 }
Jim Miller122b6c82010-02-10 19:53:58 -0800718 createPreferenceHierarchy();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800719 }
Amith Yamasanif06d8692009-06-11 22:32:33 -0700720
Clara Bayarri52681ec2016-01-28 11:11:35 +0000721 private void launchConfirmDeviceLockForUnification() {
722 final String title = getActivity().getString(
Mahaver Chopra26632092016-03-18 19:08:05 +0000723 R.string.unlock_set_unlock_launch_picker_title);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000724 final ChooseLockSettingsHelper helper =
725 new ChooseLockSettingsHelper(getActivity(), this);
726 if (!helper.launchConfirmationActivity(
Mahaver Chopra26632092016-03-18 19:08:05 +0000727 UNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000728 launchConfirmProfileLockForUnification();
729 }
730 }
731
732 private void launchConfirmProfileLockForUnification() {
733 final String title = getActivity().getString(
Mahaver Chopra26632092016-03-18 19:08:05 +0000734 R.string.unlock_set_unlock_launch_picker_title_profile);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000735 final ChooseLockSettingsHelper helper =
736 new ChooseLockSettingsHelper(getActivity(), this);
737 if (!helper.launchConfirmationActivity(
738 UNIFY_LOCK_CONFIRM_PROFILE_REQUEST, title, true, mProfileChallengeUserId)) {
739 unifyLocks();
740 createPreferenceHierarchy();
741 }
742 }
743
744 private void unifyLocks() {
745 int profileQuality =
746 mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000747 if (profileQuality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
748 mLockPatternUtils.saveLockPattern(
749 LockPatternUtils.stringToPattern(mCurrentProfilePassword),
Mahaver Chopra26632092016-03-18 19:08:05 +0000750 mCurrentDevicePassword, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000751 } else {
752 mLockPatternUtils.saveLockPassword(
753 mCurrentProfilePassword, mCurrentDevicePassword,
Mahaver Chopra26632092016-03-18 19:08:05 +0000754 profileQuality, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000755 }
Ricky Waia0a07382016-04-12 17:34:59 +0100756 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false,
757 mCurrentProfilePassword);
Daniel U4af16612016-03-24 17:34:20 +0000758 final boolean profilePatternVisibility =
759 mLockPatternUtils.isVisiblePatternEnabled(mProfileChallengeUserId);
760 mLockPatternUtils.setVisiblePatternEnabled(profilePatternVisibility, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000761 mCurrentDevicePassword = null;
762 mCurrentProfilePassword = null;
763 }
764
Clara Bayarri56462d62016-02-12 13:55:36 +0000765 private void unifyUncompliantLocks() {
Ricky Wai4bbf0652016-04-01 16:31:56 +0100766 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false,
767 mCurrentProfilePassword);
Maurice Lam2eb170c2017-04-28 16:18:47 -0700768 startFragment(this, ChooseLockGenericFragment.class.getName(),
Clara Bayarri56462d62016-02-12 13:55:36 +0000769 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
770 }
771
Mahaver Chopra26632092016-03-18 19:08:05 +0000772 private void ununifyLocks() {
Mahaver Chopra26632092016-03-18 19:08:05 +0000773 Bundle extras = new Bundle();
774 extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
775 startFragment(this,
Maurice Lam2eb170c2017-04-28 16:18:47 -0700776 ChooseLockGenericFragment.class.getName(),
Mahaver Chopra26632092016-03-18 19:08:05 +0000777 R.string.lock_settings_picker_title_profile,
778 SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
779 }
780
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400781 @Override
Jim Miller86624a22010-08-30 22:07:58 -0700782 public boolean onPreferenceChange(Preference preference, Object value) {
arete300e9202014-08-15 12:04:48 -0700783 boolean result = true;
Jim Miller4a9065e2014-08-08 15:25:31 -0700784 final String key = preference.getKey();
785 final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
Clara Bayarri52681ec2016-01-28 11:11:35 +0000786 if (KEY_VISIBLE_PATTERN_PROFILE.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100787 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100788 mProfileChallengeUserId)) {
789 return false;
790 }
Clara Bayarri52681ec2016-01-28 11:11:35 +0000791 lockPatternUtils.setVisiblePatternEnabled((Boolean) value, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000792 } else if (KEY_UNIFICATION.equals(key)) {
Bernard Chau88d523b2016-04-14 15:08:28 +0100793 if (Utils.startQuietModeDialogIfNecessary(this.getActivity(), mUm,
Ricky Wai616342b2016-04-13 10:40:22 +0100794 mProfileChallengeUserId)) {
795 return false;
796 }
Clara Bayarri56462d62016-02-12 13:55:36 +0000797 if ((Boolean) value) {
798 final boolean compliantForDevice =
799 (mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId)
800 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
801 && mLockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
802 mProfileChallengeUserId));
803 UnificationConfirmationDialog dialog =
804 UnificationConfirmationDialog.newIntance(compliantForDevice);
805 dialog.show(getChildFragmentManager(), TAG_UNIFICATION_DIALOG);
806 } else {
Mahaver Chopra26632092016-03-18 19:08:05 +0000807 final String title = getActivity().getString(
808 R.string.unlock_set_unlock_launch_picker_title);
809 final ChooseLockSettingsHelper helper =
810 new ChooseLockSettingsHelper(getActivity(), this);
811 if(!helper.launchConfirmationActivity(
812 UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) {
813 ununifyLocks();
814 }
Clara Bayarri56462d62016-02-12 13:55:36 +0000815 }
Clara Bayarri52681ec2016-01-28 11:11:35 +0000816 } else if (KEY_SHOW_PASSWORD.equals(key)) {
Jim Miller4a9065e2014-08-08 15:25:31 -0700817 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
818 ((Boolean) value) ? 1 : 0);
Paul Lawrencec04420c2015-05-18 13:25:01 -0700819 lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID);
Jim Miller86624a22010-08-30 22:07:58 -0700820 }
arete300e9202014-08-15 12:04:48 -0700821 return result;
Jim Miller86624a22010-08-30 22:07:58 -0700822 }
Danielle Millett487b16f2011-11-02 11:12:21 -0400823
rich cannings93c0ee52012-09-30 13:54:26 -0700824 @Override
825 protected int getHelpResource() {
826 return R.string.help_url_security;
827 }
828
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700829 /**
830 * For Search. Please keep it in sync when updating "createPreferenceHierarchy()"
831 */
832 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
833 new SecuritySearchIndexProvider();
834
Jim Millerfe765592014-07-31 15:30:29 -0700835 private static class SecuritySearchIndexProvider extends BaseSearchIndexProvider {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700836
Matthew Fritze9c3c7d82017-10-19 10:38:08 -0700837 // TODO (b/68001777) Refactor indexing to include all XML and block other settings.
838
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700839 @Override
840 public List<SearchIndexableResource> getXmlResourcesToIndex(
841 Context context, boolean enabled) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100842 final List<SearchIndexableResource> index = new ArrayList<SearchIndexableResource>();
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700843
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100844 final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700845 final ManagedLockPasswordProvider managedPasswordProvider =
846 ManagedLockPasswordProvider.get(context, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100847 final DevicePolicyManager dpm = (DevicePolicyManager)
848 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700849 final UserManager um = UserManager.get(context);
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100850 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100851
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100852 // To add option for unlock screen, user's password must not be managed and
853 // must not be unified with managed profile, whose password is managed.
854 if (!isPasswordManaged(MY_USER_ID, context, dpm)
855 && (profileUserId == UserHandle.USER_NULL
856 || lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)
857 || !isPasswordManaged(profileUserId, context, dpm))) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100858 // Add options for lock/unlock screen
859 final int resId = getResIdForLockUnlockScreen(context, lockPatternUtils,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700860 managedPasswordProvider, MY_USER_ID);
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100861 index.add(getSearchResource(context, resId));
862 }
863
Clara Bayarri52681ec2016-01-28 11:11:35 +0000864 if (profileUserId != UserHandle.USER_NULL
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100865 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)
866 && !isPasswordManaged(profileUserId, context, dpm)) {
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100867 index.add(getSearchResource(context, getResIdForLockUnlockScreen(context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700868 lockPatternUtils, managedPasswordProvider, profileUserId)));
Clara Bayarri6934a042015-10-14 11:07:35 +0100869 }
870
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100871 final SearchIndexableResource sir = getSearchResource(context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -0700872 SecuritySubSettings.getResIdForLockUnlockSubScreen(context, lockPatternUtils,
873 managedPasswordProvider));
Adrian Roos62775bf2016-01-28 13:23:53 -0800874 sir.className = SecuritySubSettings.class.getName();
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100875 index.add(sir);
Adrian Roos62775bf2016-01-28 13:23:53 -0800876
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700877 // Append the rest of the settings
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100878 index.add(getSearchResource(context, R.xml.security_settings_misc));
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700879
Oleksandr Peletskyi41d99402016-02-29 16:12:04 +0100880 return index;
881 }
882
883 private SearchIndexableResource getSearchResource(Context context, int xmlResId) {
884 final SearchIndexableResource sir = new SearchIndexableResource(context);
885 sir.xmlResId = xmlResId;
886 return sir;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700887 }
888
Oleksandr Peletskyi7aeb56c2016-03-10 11:59:22 +0100889 private boolean isPasswordManaged(int userId, Context context, DevicePolicyManager dpm) {
890 final EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
891 context, userId);
892 return admin != null && dpm.getPasswordQuality(admin.component, userId) ==
893 DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
894 }
895
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700896 @Override
897 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
898 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
899 final Resources res = context.getResources();
900
901 final String screenTitle = res.getString(R.string.security_settings_title);
902
Fabrice Di Megliod7290292014-07-01 14:32:03 -0700903 SearchIndexableRaw data = new SearchIndexableRaw(context);
904 data.title = screenTitle;
905 data.screenTitle = screenTitle;
906 result.add(data);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700907
Xiaohui Chen44879a32015-07-22 13:53:22 -0700908 final UserManager um = UserManager.get(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700909
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400910 // Fingerprint
Jeff Sharkeyab508072016-10-11 14:25:22 -0600911 final FingerprintManager fpm = Utils.getFingerprintManagerOrNull(context);
Jim Millerb528b262016-02-04 17:10:44 -0800912 if (fpm != null && fpm.isHardwareDetected()) {
Jim Millerc5fa6e22015-09-22 18:30:13 -0700913 // This catches the title which can be overloaded in an overlay
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400914 data = new SearchIndexableRaw(context);
915 data.title = res.getString(R.string.security_settings_fingerprint_preference_title);
916 data.screenTitle = screenTitle;
917 result.add(data);
Jim Millerc5fa6e22015-09-22 18:30:13 -0700918 // Fallback for when the above doesn't contain "fingerprint"
919 data = new SearchIndexableRaw(context);
920 data.title = res.getString(R.string.fingerprint_manage_category_title);
921 data.screenTitle = screenTitle;
922 result.add(data);
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400923 }
924
Clara Bayarri52681ec2016-01-28 11:11:35 +0000925 final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Clara Bayarri462cce12016-02-18 12:09:21 +0000926 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000927 if (profileUserId != UserHandle.USER_NULL
928 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)) {
929 if (lockPatternUtils.getKeyguardStoredPasswordQuality(profileUserId)
930 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
931 && lockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
932 profileUserId)) {
933 data = new SearchIndexableRaw(context);
934 data.title = res.getString(R.string.lock_settings_profile_unification_title);
935 data.screenTitle = screenTitle;
936 result.add(data);
937 }
938 }
939
Jim Millerfe765592014-07-31 15:30:29 -0700940 // Advanced
Adrian Roos54375882015-04-16 17:11:22 -0700941 if (lockPatternUtils.isSecure(MY_USER_ID)) {
Zachary Iqbalccae73f2017-01-17 14:55:49 -0800942 final TrustAgentManager trustAgentManager =
943 FeatureFactory.getFactory(context).getSecurityFeatureProvider()
944 .getTrustAgentManager();
945 final List<TrustAgentComponentInfo> agents =
946 getActiveTrustAgents(context, trustAgentManager, lockPatternUtils,
Adrian Roos088de472015-04-07 14:09:39 +0200947 context.getSystemService(DevicePolicyManager.class));
Jim Millerfe765592014-07-31 15:30:29 -0700948 for (int i = 0; i < agents.size(); i++) {
949 final TrustAgentComponentInfo agent = agents.get(i);
950 data = new SearchIndexableRaw(context);
951 data.title = agent.title;
952 data.screenTitle = screenTitle;
953 result.add(data);
954 }
955 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700956 return result;
957 }
958
959 @Override
960 public List<String> getNonIndexableKeys(Context context) {
Matthew Fritze2e34a642017-05-11 11:56:27 -0700961 final List<String> keys = super.getNonIndexableKeys(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700962
963 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700964
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700965 // Do not display SIM lock for devices without an Icc card
Xiaohui Chen44879a32015-07-22 13:53:22 -0700966 final UserManager um = UserManager.get(context);
967 final TelephonyManager tm = TelephonyManager.from(context);
968 if (!um.isAdminUser() || !tm.hasIccCard()) {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700969 keys.add(KEY_SIM_LOCK);
970 }
971
Jim Millerfe765592014-07-31 15:30:29 -0700972 // TrustAgent settings disappear when the user has no primary security.
Adrian Roos54375882015-04-16 17:11:22 -0700973 if (!lockPatternUtils.isSecure(MY_USER_ID)) {
Jim Millerfe765592014-07-31 15:30:29 -0700974 keys.add(KEY_TRUST_AGENT);
975 keys.add(KEY_MANAGE_TRUST_AGENTS);
976 }
977
Bartosz Fabianowskiba66a0e2017-03-10 15:47:14 +0100978 if (!(new EnterprisePrivacyPreferenceController(context, null /* lifecycle */))
979 .isAvailable()) {
980 keys.add(KEY_ENTERPRISE_PRIVACY);
981 }
982
Matthew Fritze2e34a642017-05-11 11:56:27 -0700983 // Duplicate in special app access
984 keys.add(KEY_MANAGE_DEVICE_ADMIN);
985 // Duplicates between parent-child
986 keys.add(KEY_LOCATION);
987 keys.add(KEY_ENCRYPTION_AND_CREDENTIALS);
988 keys.add(KEY_SCREEN_PINNING);
989 keys.add(KEY_LOCATION_SCANNING);
990
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700991 return keys;
992 }
993 }
994
Adrian Roos62775bf2016-01-28 13:23:53 -0800995 public static class SecuritySubSettings extends SettingsPreferenceFragment
Doris Ling93582992017-03-16 13:20:15 -0700996 implements OnPreferenceChangeListener, OwnerInfoPreferenceController.OwnerInfoCallback {
Adrian Roos62775bf2016-01-28 13:23:53 -0800997
998 private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
999 private static final String KEY_LOCK_AFTER_TIMEOUT = "lock_after_timeout";
Adrian Roos62775bf2016-01-28 13:23:53 -08001000 private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
Adrian Roos62775bf2016-01-28 13:23:53 -08001001
1002 // These switch preferences need special handling since they're not all stored in Settings.
1003 private static final String SWITCH_PREFERENCE_KEYS[] = { KEY_LOCK_AFTER_TIMEOUT,
Clara Bayarrib4d55a22016-04-20 15:02:44 +01001004 KEY_VISIBLE_PATTERN, KEY_POWER_INSTANTLY_LOCKS };
Adrian Roos62775bf2016-01-28 13:23:53 -08001005
Sudheer Shanka900adce2016-03-04 16:58:34 -08001006 private TimeoutListPreference mLockAfter;
Adrian Roos62775bf2016-01-28 13:23:53 -08001007 private SwitchPreference mVisiblePattern;
1008 private SwitchPreference mPowerButtonInstantlyLocks;
Adrian Roos62775bf2016-01-28 13:23:53 -08001009
Adrian Roos73c52f22017-02-28 15:59:01 -08001010 private TrustAgentManager mTrustAgentManager;
Adrian Roos62775bf2016-01-28 13:23:53 -08001011 private LockPatternUtils mLockPatternUtils;
1012 private DevicePolicyManager mDPM;
Doris Ling93582992017-03-16 13:20:15 -07001013 private OwnerInfoPreferenceController mOwnerInfoPreferenceController;
Adrian Roos62775bf2016-01-28 13:23:53 -08001014
1015 @Override
Fan Zhang65076132016-08-08 10:25:13 -07001016 public int getMetricsCategory() {
Adrian Roos62775bf2016-01-28 13:23:53 -08001017 return MetricsEvent.SECURITY;
1018 }
1019
1020 @Override
1021 public void onCreate(Bundle icicle) {
1022 super.onCreate(icicle);
Adrian Roos73c52f22017-02-28 15:59:01 -08001023 SecurityFeatureProvider securityFeatureProvider =
1024 FeatureFactory.getFactory(getActivity()).getSecurityFeatureProvider();
1025 mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
Adrian Roos62775bf2016-01-28 13:23:53 -08001026 mLockPatternUtils = new LockPatternUtils(getContext());
1027 mDPM = getContext().getSystemService(DevicePolicyManager.class);
Doris Ling93582992017-03-16 13:20:15 -07001028 mOwnerInfoPreferenceController =
1029 new OwnerInfoPreferenceController(getContext(), this, null /* lifecycle */);
Adrian Roos3686ef52016-04-27 13:58:22 -07001030 createPreferenceHierarchy();
Adrian Roos62775bf2016-01-28 13:23:53 -08001031 }
1032
1033 @Override
1034 public void onResume() {
1035 super.onResume();
1036
1037 createPreferenceHierarchy();
1038
Adrian Roos62775bf2016-01-28 13:23:53 -08001039 if (mVisiblePattern != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001040 mVisiblePattern.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Adrian Roos62775bf2016-01-28 13:23:53 -08001041 MY_USER_ID));
1042 }
1043 if (mPowerButtonInstantlyLocks != null) {
William Luh4f80f222017-02-24 09:49:37 -08001044 mPowerButtonInstantlyLocks.setChecked(
1045 mLockPatternUtils.getPowerButtonInstantlyLocks(MY_USER_ID));
Adrian Roos62775bf2016-01-28 13:23:53 -08001046 }
1047
Doris Ling93582992017-03-16 13:20:15 -07001048 mOwnerInfoPreferenceController.updateSummary();
Adrian Roos62775bf2016-01-28 13:23:53 -08001049 }
1050
1051 @Override
1052 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1053 super.onActivityResult(requestCode, resultCode, data);
1054
1055 createPreferenceHierarchy();
1056 }
1057
1058 private void createPreferenceHierarchy() {
1059 PreferenceScreen root = getPreferenceScreen();
1060 if (root != null) {
1061 root.removeAll();
1062 }
Adrian Roos62775bf2016-01-28 13:23:53 -08001063
1064 final int resid = getResIdForLockUnlockSubScreen(getActivity(),
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001065 new LockPatternUtils(getContext()),
1066 ManagedLockPasswordProvider.get(getContext(), MY_USER_ID));
Adrian Roos62775bf2016-01-28 13:23:53 -08001067 addPreferencesFromResource(resid);
1068
1069 // lock after preference
Sudheer Shanka900adce2016-03-04 16:58:34 -08001070 mLockAfter = (TimeoutListPreference) findPreference(KEY_LOCK_AFTER_TIMEOUT);
Adrian Roos62775bf2016-01-28 13:23:53 -08001071 if (mLockAfter != null) {
1072 setupLockAfterPreference();
1073 updateLockAfterPreferenceSummary();
1074 }
1075
1076 // visible pattern
1077 mVisiblePattern = (SwitchPreference) findPreference(KEY_VISIBLE_PATTERN);
1078
1079 // lock instantly on power key press
1080 mPowerButtonInstantlyLocks = (SwitchPreference) findPreference(
1081 KEY_POWER_INSTANTLY_LOCKS);
Adrian Roos73c52f22017-02-28 15:59:01 -08001082 CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
1083 mTrustAgentManager, mLockPatternUtils, mDPM);
1084 if (mPowerButtonInstantlyLocks != null && !TextUtils.isEmpty(trustAgentLabel)) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001085 mPowerButtonInstantlyLocks.setSummary(getString(
1086 R.string.lockpattern_settings_power_button_instantly_locks_summary,
Adrian Roos73c52f22017-02-28 15:59:01 -08001087 trustAgentLabel));
Adrian Roos62775bf2016-01-28 13:23:53 -08001088 }
1089
Doris Ling93582992017-03-16 13:20:15 -07001090 mOwnerInfoPreferenceController.displayPreference(getPreferenceScreen());
1091 mOwnerInfoPreferenceController.updateEnableState();
Adrian Roos62775bf2016-01-28 13:23:53 -08001092
Adrian Roos62775bf2016-01-28 13:23:53 -08001093 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
1094 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
1095 if (pref != null) pref.setOnPreferenceChangeListener(this);
1096 }
1097 }
1098
Adrian Roos62775bf2016-01-28 13:23:53 -08001099 private void setupLockAfterPreference() {
1100 // Compatible with pre-Froyo
1101 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1102 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1103 mLockAfter.setValue(String.valueOf(currentTimeout));
1104 mLockAfter.setOnPreferenceChangeListener(this);
Sudheer Shanka900adce2016-03-04 16:58:34 -08001105 if (mDPM != null) {
1106 final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
1107 getActivity());
Ricky Wai36cce832016-03-18 16:26:35 +00001108 final long adminTimeout = mDPM
1109 .getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId());
Adrian Roos62775bf2016-01-28 13:23:53 -08001110 final long displayTimeout = Math.max(0,
1111 Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0));
Sudheer Shanka900adce2016-03-04 16:58:34 -08001112 // This setting is a slave to display timeout when a device policy is enforced.
1113 // As such, maxLockTimeout = adminTimeout - displayTimeout.
1114 // If there isn't enough time, shows "immediately" setting.
1115 final long maxTimeout = Math.max(0, adminTimeout - displayTimeout);
1116 mLockAfter.removeUnusableTimeouts(maxTimeout, admin);
Adrian Roos62775bf2016-01-28 13:23:53 -08001117 }
1118 }
1119
1120 private void updateLockAfterPreferenceSummary() {
1121 final String summary;
1122 if (mLockAfter.isDisabledByAdmin()) {
1123 summary = getString(R.string.disabled_by_policy_title);
1124 } else {
1125 // Update summary message with current value
1126 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1127 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1128 final CharSequence[] entries = mLockAfter.getEntries();
1129 final CharSequence[] values = mLockAfter.getEntryValues();
1130 int best = 0;
1131 for (int i = 0; i < values.length; i++) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001132 long timeout = Long.valueOf(values[i].toString());
1133 if (currentTimeout >= timeout) {
1134 best = i;
1135 }
1136 }
1137
Adrian Roos73c52f22017-02-28 15:59:01 -08001138 CharSequence trustAgentLabel = getActiveTrustAgentLabel(getContext(),
1139 mTrustAgentManager, mLockPatternUtils, mDPM);
1140 if (!TextUtils.isEmpty(trustAgentLabel)) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001141 if (Long.valueOf(values[best].toString()) == 0) {
1142 summary = getString(R.string.lock_immediately_summary_with_exception,
Adrian Roos73c52f22017-02-28 15:59:01 -08001143 trustAgentLabel);
Adrian Roos62775bf2016-01-28 13:23:53 -08001144 } else {
1145 summary = getString(R.string.lock_after_timeout_summary_with_exception,
Adrian Roos73c52f22017-02-28 15:59:01 -08001146 entries[best], trustAgentLabel);
Adrian Roos62775bf2016-01-28 13:23:53 -08001147 }
1148 } else {
1149 summary = getString(R.string.lock_after_timeout_summary, entries[best]);
1150 }
1151 }
1152 mLockAfter.setSummary(summary);
1153 }
1154
Doris Ling93582992017-03-16 13:20:15 -07001155 @Override
1156 public void onOwnerInfoUpdated() {
1157 mOwnerInfoPreferenceController.updateSummary();
Adrian Roos62775bf2016-01-28 13:23:53 -08001158 }
1159
1160 private static int getResIdForLockUnlockSubScreen(Context context,
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001161 LockPatternUtils lockPatternUtils,
1162 ManagedLockPasswordProvider managedPasswordProvider) {
Adrian Roos62775bf2016-01-28 13:23:53 -08001163 if (lockPatternUtils.isSecure(MY_USER_ID)) {
1164 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(MY_USER_ID)) {
1165 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
1166 return R.xml.security_settings_pattern_sub;
1167 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
1168 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
1169 return R.xml.security_settings_pin_sub;
1170 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
1171 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
1172 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
1173 return R.xml.security_settings_password_sub;
Toni Barzice7d6e4d2016-03-30 11:43:19 -07001174 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
1175 return managedPasswordProvider.getResIdForLockUnlockSubScreen();
Adrian Roos62775bf2016-01-28 13:23:53 -08001176 }
1177 } else if (!lockPatternUtils.isLockScreenDisabled(MY_USER_ID)) {
1178 return R.xml.security_settings_slide_sub;
1179 }
1180 return 0;
1181 }
1182
1183 @Override
1184 public boolean onPreferenceChange(Preference preference, Object value) {
1185 String key = preference.getKey();
1186 if (KEY_POWER_INSTANTLY_LOCKS.equals(key)) {
1187 mLockPatternUtils.setPowerButtonInstantlyLocks((Boolean) value, MY_USER_ID);
1188 } else if (KEY_LOCK_AFTER_TIMEOUT.equals(key)) {
1189 int timeout = Integer.parseInt((String) value);
1190 try {
1191 Settings.Secure.putInt(getContentResolver(),
1192 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, timeout);
1193 } catch (NumberFormatException e) {
1194 Log.e("SecuritySettings", "could not persist lockAfter timeout setting", e);
1195 }
Julia Reynoldsde4f4712016-05-12 13:48:34 -04001196 setupLockAfterPreference();
Adrian Roos62775bf2016-01-28 13:23:53 -08001197 updateLockAfterPreferenceSummary();
1198 } else if (KEY_VISIBLE_PATTERN.equals(key)) {
1199 mLockPatternUtils.setVisiblePatternEnabled((Boolean) value, MY_USER_ID);
Adrian Roos62775bf2016-01-28 13:23:53 -08001200 }
1201 return true;
1202 }
1203 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001204
Fan Zhang1e516282016-09-16 12:45:07 -07001205 public static class UnificationConfirmationDialog extends InstrumentedDialogFragment {
Clara Bayarri56462d62016-02-12 13:55:36 +00001206 private static final String EXTRA_COMPLIANT = "compliant";
Clara Bayarri52681ec2016-01-28 11:11:35 +00001207
Clara Bayarri56462d62016-02-12 13:55:36 +00001208 public static UnificationConfirmationDialog newIntance(boolean compliant) {
Clara Bayarri52681ec2016-01-28 11:11:35 +00001209 UnificationConfirmationDialog dialog = new UnificationConfirmationDialog();
Clara Bayarri56462d62016-02-12 13:55:36 +00001210 Bundle args = new Bundle();
1211 args.putBoolean(EXTRA_COMPLIANT, compliant);
1212 dialog.setArguments(args);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001213 return dialog;
1214 }
1215
1216 @Override
1217 public void show(FragmentManager manager, String tag) {
1218 if (manager.findFragmentByTag(tag) == null) {
1219 // Prevent opening multiple dialogs if tapped on button quickly
1220 super.show(manager, tag);
1221 }
1222 }
1223
1224 @Override
1225 public Dialog onCreateDialog(Bundle savedInstanceState) {
1226 final SecuritySettings parentFragment = ((SecuritySettings) getParentFragment());
Clara Bayarri56462d62016-02-12 13:55:36 +00001227 final boolean compliant = getArguments().getBoolean(EXTRA_COMPLIANT);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001228 return new AlertDialog.Builder(getActivity())
1229 .setTitle(R.string.lock_settings_profile_unification_dialog_title)
Clara Bayarri56462d62016-02-12 13:55:36 +00001230 .setMessage(compliant ? R.string.lock_settings_profile_unification_dialog_body
1231 : R.string.lock_settings_profile_unification_dialog_uncompliant_body)
Mahaver Chopra26632092016-03-18 19:08:05 +00001232 .setPositiveButton(
1233 compliant ? R.string.lock_settings_profile_unification_dialog_confirm
Clara Bayarri56462d62016-02-12 13:55:36 +00001234 : R.string.lock_settings_profile_unification_dialog_uncompliant_confirm,
Clara Bayarri52681ec2016-01-28 11:11:35 +00001235 new DialogInterface.OnClickListener() {
1236 @Override
1237 public void onClick(DialogInterface dialog, int whichButton) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001238 if (compliant) {
1239 parentFragment.launchConfirmDeviceLockForUnification();
1240 } else {
1241 parentFragment.unifyUncompliantLocks();
1242 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001243 }
1244 }
1245 )
Ricky Waif6fc2202016-04-12 18:35:41 +01001246 .setNegativeButton(R.string.cancel, null)
Clara Bayarri52681ec2016-01-28 11:11:35 +00001247 .create();
1248 }
Ricky Waif6fc2202016-04-12 18:35:41 +01001249
1250 @Override
1251 public void onDismiss(DialogInterface dialog) {
1252 super.onDismiss(dialog);
1253 ((SecuritySettings) getParentFragment()).updateUnificationPreference();
1254 }
Fan Zhang1e516282016-09-16 12:45:07 -07001255
1256 @Override
1257 public int getMetricsCategory() {
1258 return MetricsEvent.DIALOG_UNIFICATION_CONFIRMATION;
1259 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001260 }
1261
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001262 static class SummaryProvider implements SummaryLoader.SummaryProvider {
1263
1264 private final Context mContext;
1265 private final SummaryLoader mSummaryLoader;
1266
1267 public SummaryProvider(Context context, SummaryLoader summaryLoader) {
1268 mContext = context;
1269 mSummaryLoader = summaryLoader;
1270 }
1271
1272 @Override
1273 public void setListening(boolean listening) {
Doris Ling48ffe672017-04-10 15:14:22 -07001274 if (listening) {
1275 final FingerprintManager fpm =
1276 Utils.getFingerprintManagerOrNull(mContext);
Fan Zhang2cab2982017-02-14 11:58:25 -08001277 if (fpm != null && fpm.isHardwareDetected()) {
1278 mSummaryLoader.setSummary(this,
Doris Ling48ffe672017-04-10 15:14:22 -07001279 mContext.getString(R.string.security_dashboard_summary));
Fan Zhang2cab2982017-02-14 11:58:25 -08001280 } else {
Doris Ling48ffe672017-04-10 15:14:22 -07001281 mSummaryLoader.setSummary(this, mContext.getString(
1282 R.string.security_dashboard_summary_no_fingerprint));
Fan Zhang2cab2982017-02-14 11:58:25 -08001283 }
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001284 }
1285 }
Shahriyar Amini5c1ad612017-01-11 11:30:13 -08001286 }
1287
1288 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
1289 new SummaryLoader.SummaryProviderFactory() {
1290 @Override
1291 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
1292 SummaryLoader summaryLoader) {
1293 return new SummaryProvider(activity, summaryLoader);
1294 }
1295 };
1296
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001297}