blob: 5537926169857b362dac2540cded02c07753b91c [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
19
Danielle Millett487b16f2011-11-02 11:12:21 -040020import android.app.Activity;
Gilles Debunne2c88a872011-06-21 12:47:17 -070021import android.app.AlertDialog;
Clara Bayarri52681ec2016-01-28 11:11:35 +000022import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
Dianne Hackborn4037c7f2010-02-26 17:26:55 -080025import android.app.admin.DevicePolicyManager;
Ido Ofir524a63b2014-06-12 14:18:23 -070026import android.content.ComponentName;
Dianne Hackbornabc3dc62010-01-20 13:40:19 -080027import android.content.Context;
Gilles Debunne2c88a872011-06-21 12:47:17 -070028import android.content.DialogInterface;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.content.Intent;
rich canningsbfbdcef2012-09-09 12:48:50 -070030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
Clara Bayarri6934a042015-10-14 11:07:35 +010032import android.content.pm.UserInfo;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070033import android.content.res.Resources;
Jason Monk3bcd76c2015-04-21 11:20:20 -040034import android.hardware.fingerprint.FingerprintManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035import android.os.Bundle;
Junda Liu80753bc2015-06-17 10:15:34 -070036import android.os.PersistableBundle;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070037import android.os.UserHandle;
Jim Miller783ea852012-11-01 19:39:21 -070038import android.os.UserManager;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070039import android.provider.SearchIndexableResource;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040import android.provider.Settings;
Chia-chi Yeh9cec6982009-09-22 23:59:01 +080041import android.security.KeyStore;
Ido Ofir6f482442014-05-09 14:47:15 -070042import android.service.trust.TrustAgentService;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.SwitchPreference;
Jason Monk39b46742015-09-10 15:52:51 -040044import android.support.v7.preference.Preference;
45import android.support.v7.preference.Preference.OnPreferenceChangeListener;
46import android.support.v7.preference.Preference.OnPreferenceClickListener;
47import android.support.v7.preference.PreferenceGroup;
48import android.support.v7.preference.PreferenceScreen;
Junda Liu80753bc2015-06-17 10:15:34 -070049import android.telephony.CarrierConfigManager;
Wink Savilleca756612014-11-08 10:47:12 -080050import android.telephony.SubscriptionInfo;
Jason Monk3bcd76c2015-04-21 11:20:20 -040051import android.telephony.SubscriptionManager;
52import android.telephony.TelephonyManager;
Ido Ofir524a63b2014-06-12 14:18:23 -070053import android.text.TextUtils;
Jim Miller86624a22010-08-30 22:07:58 -070054import android.util.Log;
Amith Yamasanif06d8692009-06-11 22:32:33 -070055
Chris Wren9d1bfd12016-01-26 18:04:01 -050056import com.android.internal.logging.MetricsProto.MetricsEvent;
Gilles Debunnea6a8a142011-06-09 11:56:17 -070057import com.android.internal.widget.LockPatternUtils;
Sudheer Shanka550d0682016-01-13 15:16:55 +000058import com.android.settings.RestrictedListPreference;
Jim Millerfe765592014-07-31 15:30:29 -070059import com.android.settings.TrustAgentUtils.TrustAgentComponentInfo;
Jorim Jaggi5ad75f02015-04-22 16:17:23 -070060import com.android.settings.fingerprint.FingerprintSettings;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070061import com.android.settings.search.BaseSearchIndexProvider;
Jim Millerfe765592014-07-31 15:30:29 -070062import com.android.settings.search.Index;
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -070063import com.android.settings.search.Indexable;
64import com.android.settings.search.SearchIndexableRaw;
Sudheer Shanka550d0682016-01-13 15:16:55 +000065import com.android.settingslib.RestrictedLockUtils;
Sudheer Shanka9c324682016-01-18 11:17:23 +000066import com.android.settingslib.RestrictedPreference;
Sudheer Shanka66104352016-01-12 17:43:20 +000067import com.android.settingslib.RestrictedSwitchPreference;
Gilles Debunnea6a8a142011-06-09 11:56:17 -070068
Jim Miller86624a22010-08-30 22:07:58 -070069import java.util.ArrayList;
rich canningsbfbdcef2012-09-09 12:48:50 -070070import java.util.List;
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -040071
Selim Cinekec850042015-06-11 18:59:54 -070072import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
73
Sudheer Shanka550d0682016-01-13 15:16:55 +000074import static com.android.settings.RestrictedListPreference.RestrictedItem;
75import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
76
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077/**
78 * Gesture lock pattern settings.
79 */
Julia Reynolds565653c2014-06-12 11:49:12 -040080public class SecuritySettings extends SettingsPreferenceFragment
Adrian Roos62775bf2016-01-28 13:23:53 -080081 implements OnPreferenceChangeListener, DialogInterface.OnClickListener, Indexable,
82 GearPreference.OnGearClickListener {
Jim Miller92186872015-03-04 18:07:32 -080083
84 private static final String TAG = "SecuritySettings";
Jim Miller250b9be2014-09-05 18:48:18 -070085 private static final String TRUST_AGENT_CLICK_INTENT = "trust_agent_click_intent";
Ido Ofir6f482442014-05-09 14:47:15 -070086 private static final Intent TRUST_AGENT_INTENT =
87 new Intent(TrustAgentService.SERVICE_INTERFACE);
Jason parks6f8fb432011-01-07 09:02:14 -060088
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 // Lock Settings
Chia-chi Yeh91d65a22011-01-20 18:46:01 +080090 private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
Clara Bayarri52681ec2016-01-28 11:11:35 +000091 private static final String KEY_UNLOCK_SET_OR_CHANGE_PROFILE = "unlock_set_or_change_profile";
92 private static final String KEY_VISIBLE_PATTERN_PROFILE = "visiblepattern_profile";
Amith Yamasaniedac9af2010-11-17 09:08:21 -080093 private static final String KEY_SECURITY_CATEGORY = "security_category";
rich canningsf0421142012-09-25 16:20:36 -070094 private static final String KEY_DEVICE_ADMIN_CATEGORY = "device_admin_category";
Jim Millerfe765592014-07-31 15:30:29 -070095 private static final String KEY_ADVANCED_SECURITY = "advanced_security";
96 private static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents";
Clara Bayarri52681ec2016-01-28 11:11:35 +000097 private static final String KEY_UNIFICATION = "unification";
Jim Milleref3e7012013-08-20 19:03:30 -070098
Chia-chi Yeh91d65a22011-01-20 18:46:01 +080099 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
Ido Ofir6f482442014-05-09 14:47:15 -0700100 private static final int CHANGE_TRUST_AGENT_SETTINGS = 126;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000101 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE = 127;
102 private static final int UNIFY_LOCK_CONFIRM_DEVICE_REQUEST = 128;
103 private static final int UNIFY_LOCK_CONFIRM_PROFILE_REQUEST = 129;
104 private static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
Hung-ying Tyan7031ab02009-07-02 00:26:46 +0800105
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800106 // Misc Settings
107 private static final String KEY_SIM_LOCK = "sim_lock";
108 private static final String KEY_SHOW_PASSWORD = "show_password";
Kenny Root028634b2013-03-29 11:12:55 -0700109 private static final String KEY_CREDENTIAL_STORAGE_TYPE = "credential_storage_type";
Julia Reynolds233ccd02014-06-23 09:43:09 -0400110 private static final String KEY_RESET_CREDENTIALS = "credentials_reset";
Geoffrey Borggaard6e1102d2013-08-07 14:57:43 -0400111 private static final String KEY_CREDENTIALS_INSTALL = "credentials_install";
Gilles Debunne2c88a872011-06-21 12:47:17 -0700112 private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications";
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700113 private static final String KEY_CREDENTIALS_MANAGER = "credentials_management";
rich canningsbfbdcef2012-09-09 12:48:50 -0700114 private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
Ido Ofir6f482442014-05-09 14:47:15 -0700115 private static final String KEY_TRUST_AGENT = "trust_agent";
Jason Monk27d7ea52014-07-18 09:55:41 -0400116 private static final String KEY_SCREEN_PINNING = "screen_pinning_settings";
Jim Miller47d380f2010-01-20 13:37:14 -0800117
Jim Miller4a9065e2014-08-08 15:25:31 -0700118 // These switch preferences need special handling since they're not all stored in Settings.
Adrian Roos62775bf2016-01-28 13:23:53 -0800119 private static final String SWITCH_PREFERENCE_KEYS[] = {
Clara Bayarri56462d62016-02-12 13:55:36 +0000120 KEY_SHOW_PASSWORD, KEY_TOGGLE_INSTALL_APPLICATIONS, KEY_UNIFICATION };
Jim Miller4a9065e2014-08-08 15:25:31 -0700121
Jim Millerfe765592014-07-31 15:30:29 -0700122 // Only allow one trust agent on the platform.
123 private static final boolean ONLY_ONE_TRUST_AGENT = true;
124
Adrian Roos54375882015-04-16 17:11:22 -0700125 private static final int MY_USER_ID = UserHandle.myUserId();
126
Maggie Benthallf48206e2013-08-20 11:03:05 -0400127 private DevicePolicyManager mDPM;
Wink Saville0183fb52014-11-22 10:11:39 -0800128 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700129 private UserManager mUm;
Jim Miller122b6c82010-02-10 19:53:58 -0800130
Jim Miller00d24762009-12-22 19:04:57 -0800131 private ChooseLockSettingsHelper mChooseLockSettingsHelper;
Jim Miller122b6c82010-02-10 19:53:58 -0800132 private LockPatternUtils mLockPatternUtils;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800133
Clara Bayarri52681ec2016-01-28 11:11:35 +0000134 private SwitchPreference mVisiblePatternProfile;
Clara Bayarri56462d62016-02-12 13:55:36 +0000135 private SwitchPreference mUnifyProfile;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000136
Jim Miller4a9065e2014-08-08 15:25:31 -0700137 private SwitchPreference mShowPassword;
Chia-chi Yeh91d65a22011-01-20 18:46:01 +0800138
Kenny Root028634b2013-03-29 11:12:55 -0700139 private KeyStore mKeyStore;
Sudheer Shanka9c324682016-01-18 11:17:23 +0000140 private RestrictedPreference mResetCredentials;
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -0400141
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000142 private RestrictedSwitchPreference mToggleAppInstallation;
Gilles Debunne2c88a872011-06-21 12:47:17 -0700143 private DialogInterface mWarnInstallApps;
144
Xiaohui Chen44879a32015-07-22 13:53:22 -0700145 private boolean mIsAdmin;
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700146
Jim Miller250b9be2014-09-05 18:48:18 -0700147 private Intent mTrustAgentClickIntent;
Ido Ofir1682a0d2014-06-24 16:26:09 -0700148
Clara Bayarri6934a042015-10-14 11:07:35 +0100149 private int mProfileChallengeUserId;
150
Clara Bayarri52681ec2016-01-28 11:11:35 +0000151 private String mCurrentDevicePassword;
152 private String mCurrentProfilePassword;
153
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400155 protected int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500156 return MetricsEvent.SECURITY;
Chris Wren8a963ba2015-03-20 10:29:14 -0400157 }
158
159 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700160 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161 super.onCreate(savedInstanceState);
Jim Miller122b6c82010-02-10 19:53:58 -0800162
Wink Saville0183fb52014-11-22 10:11:39 -0800163 mSubscriptionManager = SubscriptionManager.from(getActivity());
164
Amith Yamasanid7993472010-08-18 13:59:28 -0700165 mLockPatternUtils = new LockPatternUtils(getActivity());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800166
Dianne Hackbornabc3dc62010-01-20 13:40:19 -0800167 mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Jim Miller122b6c82010-02-10 19:53:58 -0800168
Xiaohui Chen44879a32015-07-22 13:53:22 -0700169 mUm = UserManager.get(getActivity());
170
Amith Yamasanid7993472010-08-18 13:59:28 -0700171 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity());
Jim Miller250b9be2014-09-05 18:48:18 -0700172
173 if (savedInstanceState != null
174 && savedInstanceState.containsKey(TRUST_AGENT_CLICK_INTENT)) {
175 mTrustAgentClickIntent = savedInstanceState.getParcelable(TRUST_AGENT_CLICK_INTENT);
176 }
Amith Yamasani02cf71a2010-09-21 15:48:52 -0700177 }
Mike Lockwood5ed2c4a2009-06-07 23:07:20 -0400178
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700179 private static int getResIdForLockUnlockScreen(Context context,
Clara Bayarri52681ec2016-01-28 11:11:35 +0000180 LockPatternUtils lockPatternUtils, int userId) {
181 final boolean isMyUser = userId == MY_USER_ID;
Jim Miller36972bb2010-11-30 19:47:38 -0800182 int resid = 0;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000183 if (!lockPatternUtils.isSecure(userId)) {
184 if (!isMyUser) {
185 resid = R.xml.security_settings_lockscreen_profile;
186 } else if (lockPatternUtils.isLockScreenDisabled(userId)) {
Jim Miller36972bb2010-11-30 19:47:38 -0800187 resid = R.xml.security_settings_lockscreen;
188 } else {
189 resid = R.xml.security_settings_chooser;
190 }
Jim Miller122b6c82010-02-10 19:53:58 -0800191 } else {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000192 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(userId)) {
Jim Milleraf366a32010-03-25 18:45:22 -0700193 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000194 resid = isMyUser ? R.xml.security_settings_pattern
195 : R.xml.security_settings_pattern_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700196 break;
197 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Nicolas Prevot8fd852e2014-01-25 01:02:04 +0000198 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000199 resid = isMyUser ? R.xml.security_settings_pin
200 : R.xml.security_settings_pin_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700201 break;
202 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
203 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
Konstantin Lopyrev57fbf692010-05-27 16:01:41 -0700204 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Clara Bayarri52681ec2016-01-28 11:11:35 +0000205 resid = isMyUser ? R.xml.security_settings_password
206 : R.xml.security_settings_password_profile;
Jim Milleraf366a32010-03-25 18:45:22 -0700207 break;
Jim Miller00d24762009-12-22 19:04:57 -0800208 }
Jim Miller122b6c82010-02-10 19:53:58 -0800209 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700210 return resid;
211 }
Jim Miller122b6c82010-02-10 19:53:58 -0800212
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700213 /**
214 * Important!
215 *
Ido Ofir6f482442014-05-09 14:47:15 -0700216 * Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700217 * logic or adding/removing preferences here.
218 */
219 private PreferenceScreen createPreferenceHierarchy() {
220 PreferenceScreen root = getPreferenceScreen();
221 if (root != null) {
222 root.removeAll();
223 }
224 addPreferencesFromResource(R.xml.security_settings);
225 root = getPreferenceScreen();
226
227 // Add options for lock/unlock screen
Clara Bayarri52681ec2016-01-28 11:11:35 +0000228 final int resid = getResIdForLockUnlockScreen(getActivity(), mLockPatternUtils, MY_USER_ID);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700229 addPreferencesFromResource(resid);
Jason parks41121992011-01-25 09:26:55 -0600230
Clara Bayarri462cce12016-02-18 12:09:21 +0000231 mProfileChallengeUserId = Utils.getManagedProfileId(mUm, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000232 if (mProfileChallengeUserId != UserHandle.USER_NULL
233 && mLockPatternUtils.isSeparateProfileChallengeAllowed(mProfileChallengeUserId)) {
234 addPreferencesFromResource(R.xml.security_settings_profile);
235 final int profileResid = getResIdForLockUnlockScreen(
236 getActivity(), mLockPatternUtils, mProfileChallengeUserId);
237 addPreferencesFromResource(profileResid);
238 maybeAddFingerprintPreference(root, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000239 addPreferencesFromResource(R.xml.security_settings_unification);
240 if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileChallengeUserId)) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000241 Preference lockPreference = root.findPreference(KEY_UNLOCK_SET_OR_CHANGE_PROFILE);
242 String summary = getContext().getString(
243 R.string.lock_settings_profile_unified_summary);
244 lockPreference.setSummary(summary);
Clara Bayarri56462d62016-02-12 13:55:36 +0000245 lockPreference.setEnabled(false);
Clara Bayarri6934a042015-10-14 11:07:35 +0100246 }
247 }
248
Adrian Roos62775bf2016-01-28 13:23:53 -0800249 Preference unlockSetOrChange = findPreference(KEY_UNLOCK_SET_OR_CHANGE);
250 if (unlockSetOrChange instanceof GearPreference) {
251 ((GearPreference) unlockSetOrChange).setOnGearClickListener(this);
252 }
253
Jason parks41121992011-01-25 09:26:55 -0600254 // Add options for device encryption
Xiaohui Chen44879a32015-07-22 13:53:22 -0700255 mIsAdmin = mUm.isAdminUser();
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700256
Xiaohui Chen44879a32015-07-22 13:53:22 -0700257 if (mIsAdmin) {
Jim Miller0698a212014-10-16 19:49:07 -0700258 if (LockPatternUtils.isDeviceEncryptionEnabled()) {
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700259 // The device is currently encrypted.
260 addPreferencesFromResource(R.xml.security_settings_encrypted);
Jim Miller0698a212014-10-16 19:49:07 -0700261 } else {
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700262 // This device supports encryption but isn't encrypted.
263 addPreferencesFromResource(R.xml.security_settings_unencrypted);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700264 }
Jason parks41121992011-01-25 09:26:55 -0600265 }
266
Jim Miller94d4bd92015-02-19 21:04:57 -0800267 // Fingerprint and trust agents
Ido Ofir1682a0d2014-06-24 16:26:09 -0700268 PreferenceGroup securityCategory = (PreferenceGroup)
269 root.findPreference(KEY_SECURITY_CATEGORY);
Jim Miller9d250052014-08-21 19:24:47 -0700270 if (securityCategory != null) {
Clara Bayarri52681ec2016-01-28 11:11:35 +0000271 maybeAddFingerprintPreference(securityCategory, UserHandle.myUserId());
Jim Miller94d4bd92015-02-19 21:04:57 -0800272 addTrustAgentSettings(securityCategory);
Ido Ofir1682a0d2014-06-24 16:26:09 -0700273 }
274
Clara Bayarri52681ec2016-01-28 11:11:35 +0000275 mVisiblePatternProfile =
276 (SwitchPreference) root.findPreference(KEY_VISIBLE_PATTERN_PROFILE);
Clara Bayarri56462d62016-02-12 13:55:36 +0000277 mUnifyProfile = (SwitchPreference) root.findPreference(KEY_UNIFICATION);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000278
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700279 // Append the rest of the settings
280 addPreferencesFromResource(R.xml.security_settings_misc);
281
282 // Do not display SIM lock for devices without an Icc card
283 TelephonyManager tm = TelephonyManager.getDefault();
Junda Liu80753bc2015-06-17 10:15:34 -0700284 CarrierConfigManager cfgMgr = (CarrierConfigManager)
285 getActivity().getSystemService(Context.CARRIER_CONFIG_SERVICE);
286 PersistableBundle b = cfgMgr.getConfig();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700287 if (!mIsAdmin || !isSimIccReady() ||
Junda Liu80753bc2015-06-17 10:15:34 -0700288 b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) {
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700289 root.removePreference(root.findPreference(KEY_SIM_LOCK));
290 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700291 // Disable SIM lock if there is no ready SIM card.
292 root.findPreference(KEY_SIM_LOCK).setEnabled(isSimReady());
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700293 }
Jason Monkb08853a2014-08-13 09:43:58 -0400294 if (Settings.System.getInt(getContentResolver(),
295 Settings.System.LOCK_TO_APP_ENABLED, 0) != 0) {
296 root.findPreference(KEY_SCREEN_PINNING).setSummary(
297 getResources().getString(R.string.switch_on_text));
Jason Monk27d7ea52014-07-18 09:55:41 -0400298 }
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700299
300 // Show password
Jim Miller4a9065e2014-08-08 15:25:31 -0700301 mShowPassword = (SwitchPreference) root.findPreference(KEY_SHOW_PASSWORD);
Sudheer Shanka9c324682016-01-18 11:17:23 +0000302 mResetCredentials = (RestrictedPreference) root.findPreference(KEY_RESET_CREDENTIALS);
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700303
Kenny Rootc5550c22013-04-08 09:56:05 -0700304 // Credential storage
Emily Bernier11bd33a2013-04-11 16:24:56 -0400305 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
Maggie Benthallf48206e2013-08-20 11:03:05 -0400306 mKeyStore = KeyStore.getInstance(); // needs to be initialized for onResume()
Kenny Root028634b2013-03-29 11:12:55 -0700307
Sudheer Shanka9e9e63b2016-02-11 18:48:14 +0000308 if (!RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
309 UserManager.DISALLOW_CONFIG_CREDENTIALS, MY_USER_ID)) {
310 RestrictedPreference credentialStorageType = (RestrictedPreference) root.findPreference(
311 KEY_CREDENTIAL_STORAGE_TYPE);
312 credentialStorageType.checkRestrictionAndSetDisabled(
313 UserManager.DISALLOW_CONFIG_CREDENTIALS);
314 RestrictedPreference installCredentials = (RestrictedPreference) root.findPreference(
315 KEY_CREDENTIALS_INSTALL);
316 installCredentials.checkRestrictionAndSetDisabled(
317 UserManager.DISALLOW_CONFIG_CREDENTIALS);
318 mResetCredentials.checkRestrictionAndSetDisabled(
319 UserManager.DISALLOW_CONFIG_CREDENTIALS);
Sudheer Shanka9c324682016-01-18 11:17:23 +0000320
Sudheer Shanka9e9e63b2016-02-11 18:48:14 +0000321 final int storageSummaryRes =
322 mKeyStore.isHardwareBacked() ? R.string.credential_storage_type_hardware
323 : R.string.credential_storage_type_software;
324 credentialStorageType.setSummary(storageSummaryRes);
325 } else {
326 PreferenceGroup credentialsManager = (PreferenceGroup)
327 root.findPreference(KEY_CREDENTIALS_MANAGER);
328 credentialsManager.removePreference(root.findPreference(KEY_RESET_CREDENTIALS));
329 credentialsManager.removePreference(root.findPreference(KEY_CREDENTIALS_INSTALL));
330 credentialsManager.removePreference(root.findPreference(KEY_CREDENTIAL_STORAGE_TYPE));
331 }
Sudheer Shanka9c324682016-01-18 11:17:23 +0000332
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700333
Kenny Rootc5550c22013-04-08 09:56:05 -0700334 // Application install
Ido Ofir6f482442014-05-09 14:47:15 -0700335 PreferenceGroup deviceAdminCategory = (PreferenceGroup)
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400336 root.findPreference(KEY_DEVICE_ADMIN_CATEGORY);
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000337 mToggleAppInstallation = (RestrictedSwitchPreference) findPreference(
Amith Yamasani7fb4f852012-09-25 22:17:33 -0700338 KEY_TOGGLE_INSTALL_APPLICATIONS);
339 mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400340 // Side loading of apps.
Amith Yamasaniceceb2a2014-11-11 15:36:15 -0800341 // Disable for restricted profiles. For others, check if policy disallows it.
Adrian Roos54375882015-04-16 17:11:22 -0700342 mToggleAppInstallation.setEnabled(!um.getUserInfo(MY_USER_ID).isRestricted());
Sudheer Shanka9e9e63b2016-02-11 18:48:14 +0000343 if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
344 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, MY_USER_ID)
345 || RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
346 UserManager.DISALLOW_INSTALL_APPS, MY_USER_ID)) {
347 mToggleAppInstallation.setEnabled(false);
348 }
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000349 if (mToggleAppInstallation.isEnabled()) {
350 mToggleAppInstallation.checkRestrictionAndSetDisabled(
351 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
352 if (!mToggleAppInstallation.isDisabledByAdmin()) {
353 mToggleAppInstallation.checkRestrictionAndSetDisabled(
354 UserManager.DISALLOW_INSTALL_APPS);
355 }
Julia Reynolds565653c2014-06-12 11:49:12 -0400356 }
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400357
Jim Millerfe765592014-07-31 15:30:29 -0700358 // Advanced Security features
359 PreferenceGroup advancedCategory =
360 (PreferenceGroup)root.findPreference(KEY_ADVANCED_SECURITY);
Jim Miller9d250052014-08-21 19:24:47 -0700361 if (advancedCategory != null) {
Jim Millerfe765592014-07-31 15:30:29 -0700362 Preference manageAgents = advancedCategory.findPreference(KEY_MANAGE_TRUST_AGENTS);
Adrian Roos54375882015-04-16 17:11:22 -0700363 if (manageAgents != null && !mLockPatternUtils.isSecure(MY_USER_ID)) {
Jim Miller9d250052014-08-21 19:24:47 -0700364 manageAgents.setEnabled(false);
365 manageAgents.setSummary(R.string.disabled_because_no_backup_security);
366 }
Jim Millerfe765592014-07-31 15:30:29 -0700367 }
368
369 // The above preferences come and go based on security state, so we need to update
370 // the index. This call is expected to be fairly cheap, but we may want to do something
371 // smarter in the future.
372 Index.getInstance(getActivity())
373 .updateFromClassNameResource(SecuritySettings.class.getName(), true, true);
374
Jim Miller4a9065e2014-08-08 15:25:31 -0700375 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
376 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
377 if (pref != null) pref.setOnPreferenceChangeListener(this);
378 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800379 return root;
380 }
381
Clara Bayarri52681ec2016-01-28 11:11:35 +0000382 private void maybeAddFingerprintPreference(PreferenceGroup securityCategory, int userId) {
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000383 Preference fingerprintPreference =
384 FingerprintSettings.getFingerprintPreferenceForUser(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000385 securityCategory.getContext(), userId);
Clara Bayarri40db4bb2016-01-07 17:10:27 +0000386 if (fingerprintPreference != null) {
387 securityCategory.addPreference(fingerprintPreference);
Jim Miller94d4bd92015-02-19 21:04:57 -0800388 }
Jim Miller94d4bd92015-02-19 21:04:57 -0800389 }
390
391 private void addTrustAgentSettings(PreferenceGroup securityCategory) {
Adrian Roos54375882015-04-16 17:11:22 -0700392 final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID);
Jim Miller94d4bd92015-02-19 21:04:57 -0800393 ArrayList<TrustAgentComponentInfo> agents =
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000394 getActiveTrustAgents(getActivity(), mLockPatternUtils, mDPM);
Jim Miller94d4bd92015-02-19 21:04:57 -0800395 for (int i = 0; i < agents.size(); i++) {
396 final TrustAgentComponentInfo agent = agents.get(i);
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000397 RestrictedPreference trustAgentPreference =
398 new RestrictedPreference(securityCategory.getContext());
Jim Miller94d4bd92015-02-19 21:04:57 -0800399 trustAgentPreference.setKey(KEY_TRUST_AGENT);
400 trustAgentPreference.setTitle(agent.title);
401 trustAgentPreference.setSummary(agent.summary);
402 // Create intent for this preference.
403 Intent intent = new Intent();
404 intent.setComponent(agent.componentName);
405 intent.setAction(Intent.ACTION_MAIN);
406 trustAgentPreference.setIntent(intent);
407 // Add preference to the settings menu.
408 securityCategory.addPreference(trustAgentPreference);
Adrian Roos088de472015-04-07 14:09:39 +0200409
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000410 trustAgentPreference.setDisabledByAdmin(agent.admin);
411 if (!trustAgentPreference.isDisabledByAdmin() && !hasSecurity) {
Jim Miller94d4bd92015-02-19 21:04:57 -0800412 trustAgentPreference.setEnabled(false);
413 trustAgentPreference.setSummary(R.string.disabled_because_no_backup_security);
414 }
415 }
416 }
417
PauloftheWest0b8788a2014-12-08 13:49:35 -0800418 /* Return true if a there is a Slot that has Icc.
419 */
420 private boolean isSimIccReady() {
421 TelephonyManager tm = TelephonyManager.getDefault();
422 final List<SubscriptionInfo> subInfoList =
423 mSubscriptionManager.getActiveSubscriptionInfoList();
424
425 if (subInfoList != null) {
426 for (SubscriptionInfo subInfo : subInfoList) {
Sanket Padawe24784202015-01-14 14:48:27 -0800427 if (tm.hasIccCard(subInfo.getSimSlotIndex())) {
PauloftheWest0b8788a2014-12-08 13:49:35 -0800428 return true;
429 }
430 }
431 }
432
433 return false;
434 }
435
PauloftheWest50e6eca2014-10-03 11:07:14 -0700436 /* Return true if a SIM is ready for locking.
437 * TODO: consider adding to TelephonyManager or SubscritpionManasger.
438 */
Wink Saville0183fb52014-11-22 10:11:39 -0800439 private boolean isSimReady() {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700440 int simState = TelephonyManager.SIM_STATE_UNKNOWN;
Wink Saville0183fb52014-11-22 10:11:39 -0800441 final List<SubscriptionInfo> subInfoList =
442 mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700443 if (subInfoList != null) {
Wink Savilleca756612014-11-08 10:47:12 -0800444 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -0700445 simState = TelephonyManager.getDefault().getSimState(subInfo.getSimSlotIndex());
PauloftheWest50e6eca2014-10-03 11:07:14 -0700446 if((simState != TelephonyManager.SIM_STATE_ABSENT) &&
447 (simState != TelephonyManager.SIM_STATE_UNKNOWN)){
448 return true;
449 }
450 }
451 }
452 return false;
453 }
454
Jim Millerfe765592014-07-31 15:30:29 -0700455 private static ArrayList<TrustAgentComponentInfo> getActiveTrustAgents(
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000456 Context context, LockPatternUtils utils, DevicePolicyManager dpm) {
457 PackageManager pm = context.getPackageManager();
Jim Millerfe765592014-07-31 15:30:29 -0700458 ArrayList<TrustAgentComponentInfo> result = new ArrayList<TrustAgentComponentInfo>();
459 List<ResolveInfo> resolveInfos = pm.queryIntentServices(TRUST_AGENT_INTENT,
460 PackageManager.GET_META_DATA);
Adrian Roos54375882015-04-16 17:11:22 -0700461 List<ComponentName> enabledTrustAgents = utils.getEnabledTrustAgents(MY_USER_ID);
Adrian Roos088de472015-04-07 14:09:39 +0200462
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000463 EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(context,
464 DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS, UserHandle.myUserId());
Adrian Roos088de472015-04-07 14:09:39 +0200465
Jim Millerfe765592014-07-31 15:30:29 -0700466 if (enabledTrustAgents != null && !enabledTrustAgents.isEmpty()) {
467 for (int i = 0; i < resolveInfos.size(); i++) {
468 ResolveInfo resolveInfo = resolveInfos.get(i);
469 if (resolveInfo.serviceInfo == null) continue;
470 if (!TrustAgentUtils.checkProvidePermission(resolveInfo, pm)) continue;
471 TrustAgentComponentInfo trustAgentComponentInfo =
472 TrustAgentUtils.getSettingsComponent(pm, resolveInfo);
473 if (trustAgentComponentInfo.componentName == null ||
474 !enabledTrustAgents.contains(
475 TrustAgentUtils.getComponentName(resolveInfo)) ||
476 TextUtils.isEmpty(trustAgentComponentInfo.title)) continue;
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000477 if (admin != null && dpm.getTrustAgentConfiguration(
Adrian Roos088de472015-04-07 14:09:39 +0200478 null, TrustAgentUtils.getComponentName(resolveInfo)) == null) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000479 trustAgentComponentInfo.admin = admin;
Adrian Roos088de472015-04-07 14:09:39 +0200480 }
Jim Millerfe765592014-07-31 15:30:29 -0700481 result.add(trustAgentComponentInfo);
482 if (ONLY_ONE_TRUST_AGENT) break;
483 }
484 }
485 return result;
486 }
487
Gilles Debunne2c88a872011-06-21 12:47:17 -0700488 private boolean isNonMarketAppsAllowed() {
Christopher Tate5a64c732012-09-07 13:35:31 -0700489 return Settings.Global.getInt(getContentResolver(),
490 Settings.Global.INSTALL_NON_MARKET_APPS, 0) > 0;
Gilles Debunne2c88a872011-06-21 12:47:17 -0700491 }
492
493 private void setNonMarketAppsAllowed(boolean enabled) {
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400494 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
495 if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
496 return;
497 }
Gilles Debunne2c88a872011-06-21 12:47:17 -0700498 // Change the system setting
Christopher Tate5a64c732012-09-07 13:35:31 -0700499 Settings.Global.putInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS,
Gilles Debunne2c88a872011-06-21 12:47:17 -0700500 enabled ? 1 : 0);
501 }
502
503 private void warnAppInstallation() {
504 // TODO: DialogFragment?
505 mWarnInstallApps = new AlertDialog.Builder(getActivity()).setTitle(
506 getResources().getString(R.string.error_title))
507 .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
508 .setMessage(getResources().getString(R.string.install_all_warning))
509 .setPositiveButton(android.R.string.yes, this)
arete17224822014-08-14 13:45:36 -0700510 .setNegativeButton(android.R.string.no, this)
Gilles Debunne2c88a872011-06-21 12:47:17 -0700511 .show();
512 }
513
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400514 @Override
Gilles Debunne2c88a872011-06-21 12:47:17 -0700515 public void onClick(DialogInterface dialog, int which) {
arete17224822014-08-14 13:45:36 -0700516 if (dialog == mWarnInstallApps) {
517 boolean turnOn = which == DialogInterface.BUTTON_POSITIVE;
518 setNonMarketAppsAllowed(turnOn);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700519 if (mToggleAppInstallation != null) {
arete17224822014-08-14 13:45:36 -0700520 mToggleAppInstallation.setChecked(turnOn);
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700521 }
Gilles Debunne2c88a872011-06-21 12:47:17 -0700522 }
523 }
524
525 @Override
Adrian Roos62775bf2016-01-28 13:23:53 -0800526 public void onGearClick(GearPreference p) {
527 if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) {
528 startFragment(this, SecuritySubSettings.class.getName(), 0, 0, null);
529 }
530 }
531
532 @Override
Gilles Debunne2c88a872011-06-21 12:47:17 -0700533 public void onDestroy() {
534 super.onDestroy();
535 if (mWarnInstallApps != null) {
536 mWarnInstallApps.dismiss();
537 }
538 }
539
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800540 @Override
Jim Miller250b9be2014-09-05 18:48:18 -0700541 public void onSaveInstanceState(Bundle outState) {
542 super.onSaveInstanceState(outState);
543 if (mTrustAgentClickIntent != null) {
544 outState.putParcelable(TRUST_AGENT_CLICK_INTENT, mTrustAgentClickIntent);
545 }
546 }
547
548 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700549 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800550 super.onResume();
551
Jim Miller86624a22010-08-30 22:07:58 -0700552 // Make sure we reload the preference hierarchy since some of these settings
553 // depend on others...
554 createPreferenceHierarchy();
555
Clara Bayarri52681ec2016-01-28 11:11:35 +0000556 if (mVisiblePatternProfile != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +0000557 mVisiblePatternProfile.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Clara Bayarri52681ec2016-01-28 11:11:35 +0000558 mProfileChallengeUserId));
559 }
560
Clara Bayarri56462d62016-02-12 13:55:36 +0000561 updateUnificationPreference();
562
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700563 if (mShowPassword != null) {
564 mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
565 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
566 }
Hung-ying Tyanb0883cb2009-09-30 11:56:05 +0800567
Sudheer Shanka28eab082016-02-10 21:35:54 +0000568 if (mResetCredentials != null && !mResetCredentials.isDisabledByAdmin()) {
Kenny Rootc4372c32013-04-22 13:41:18 -0700569 mResetCredentials.setEnabled(!mKeyStore.isEmpty());
Amith Yamasanib810a0d2012-03-25 10:12:26 -0700570 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800571 }
572
Clara Bayarri56462d62016-02-12 13:55:36 +0000573 private void updateUnificationPreference() {
574 if (mUnifyProfile != null) {
575 mUnifyProfile.setChecked(!mLockPatternUtils.isSeparateProfileChallengeEnabled(
576 mProfileChallengeUserId));
577 }
578 }
579
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800580 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400581 public boolean onPreferenceTreeClick(Preference preference) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800582 final String key = preference.getKey();
Jim Millerbbb4afa2010-04-08 19:40:19 -0700583 if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
Jim Miller17e9e192010-12-07 20:41:41 -0800584 startFragment(this, "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700585 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
Clara Bayarri6934a042015-10-14 11:07:35 +0100586 } else if (KEY_UNLOCK_SET_OR_CHANGE_PROFILE.equals(key)) {
587 Bundle extras = new Bundle();
588 extras.putInt(Intent.EXTRA_USER_ID, mProfileChallengeUserId);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000589 startFragment(this, "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
590 R.string.lock_settings_picker_title_profile,
591 SET_OR_CHANGE_LOCK_METHOD_REQUEST_PROFILE, extras);
Ido Ofir6f482442014-05-09 14:47:15 -0700592 } else if (KEY_TRUST_AGENT.equals(key)) {
593 ChooseLockSettingsHelper helper =
594 new ChooseLockSettingsHelper(this.getActivity(), this);
Jim Miller250b9be2014-09-05 18:48:18 -0700595 mTrustAgentClickIntent = preference.getIntent();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700596 boolean confirmationLaunched = helper.launchConfirmationActivity(
597 CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle());
598 if (!confirmationLaunched&& mTrustAgentClickIntent != null) {
Ido Ofir6f482442014-05-09 14:47:15 -0700599 // If this returns false, it means no password confirmation is required.
Jim Miller250b9be2014-09-05 18:48:18 -0700600 startActivity(mTrustAgentClickIntent);
601 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700602 }
Jim Miller36972bb2010-11-30 19:47:38 -0800603 } else {
604 // If we didn't handle it, let preferences handle it.
Jason Monk39b46742015-09-10 15:52:51 -0400605 return super.onPreferenceTreeClick(preference);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800606 }
Jim Miller36972bb2010-11-30 19:47:38 -0800607 return true;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800608 }
609
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800610 /**
Gilles Debunnea6a8a142011-06-09 11:56:17 -0700611 * see confirmPatternThenDisableAndClear
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800612 */
613 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700614 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800615 super.onActivityResult(requestCode, resultCode, data);
Adrian Roosf7887182015-01-07 20:51:57 +0100616 if (requestCode == CHANGE_TRUST_AGENT_SETTINGS && resultCode == Activity.RESULT_OK) {
Jim Miller250b9be2014-09-05 18:48:18 -0700617 if (mTrustAgentClickIntent != null) {
618 startActivity(mTrustAgentClickIntent);
619 mTrustAgentClickIntent = null;
Ido Ofir6f482442014-05-09 14:47:15 -0700620 }
Jim Miller250b9be2014-09-05 18:48:18 -0700621 return;
Clara Bayarri52681ec2016-01-28 11:11:35 +0000622 } else if (requestCode == UNIFY_LOCK_CONFIRM_DEVICE_REQUEST
623 && resultCode == Activity.RESULT_OK) {
624 mCurrentDevicePassword =
625 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
626 launchConfirmProfileLockForUnification();
627 return;
628 } else if (requestCode == UNIFY_LOCK_CONFIRM_PROFILE_REQUEST
629 && resultCode == Activity.RESULT_OK) {
630 mCurrentProfilePassword =
631 data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
632 unifyLocks();
633 return;
Danielle Millett487b16f2011-11-02 11:12:21 -0400634 }
Jim Miller122b6c82010-02-10 19:53:58 -0800635 createPreferenceHierarchy();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800636 }
Amith Yamasanif06d8692009-06-11 22:32:33 -0700637
Clara Bayarri52681ec2016-01-28 11:11:35 +0000638 private void launchConfirmDeviceLockForUnification() {
639 final String title = getActivity().getString(
640 R.string.lock_settings_profile_screen_lock_title);
641 final ChooseLockSettingsHelper helper =
642 new ChooseLockSettingsHelper(getActivity(), this);
643 if (!helper.launchConfirmationActivity(
644 UNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, UserHandle.myUserId())) {
645 launchConfirmProfileLockForUnification();
646 }
647 }
648
649 private void launchConfirmProfileLockForUnification() {
650 final String title = getActivity().getString(
651 R.string.lock_settings_profile_screen_lock_title);
652 final ChooseLockSettingsHelper helper =
653 new ChooseLockSettingsHelper(getActivity(), this);
654 if (!helper.launchConfirmationActivity(
655 UNIFY_LOCK_CONFIRM_PROFILE_REQUEST, title, true, mProfileChallengeUserId)) {
656 unifyLocks();
657 createPreferenceHierarchy();
658 }
659 }
660
661 private void unifyLocks() {
662 int profileQuality =
663 mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId);
664 mLockPatternUtils.clearLock(mProfileChallengeUserId);
665 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false);
666 if (profileQuality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
667 mLockPatternUtils.saveLockPattern(
668 LockPatternUtils.stringToPattern(mCurrentProfilePassword),
669 mCurrentDevicePassword, UserHandle.myUserId());
670 } else {
671 mLockPatternUtils.saveLockPassword(
672 mCurrentProfilePassword, mCurrentDevicePassword,
673 profileQuality, UserHandle.myUserId());
674 }
675 mCurrentDevicePassword = null;
676 mCurrentProfilePassword = null;
677 }
678
Clara Bayarri56462d62016-02-12 13:55:36 +0000679 private void unifyUncompliantLocks() {
680 mLockPatternUtils.clearLock(mProfileChallengeUserId);
681 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, false);
682 startFragment(this, "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
683 R.string.lock_settings_picker_title, SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
684 }
685
Maggie Benthall0c5a4012013-03-14 17:41:27 -0400686 @Override
Jim Miller86624a22010-08-30 22:07:58 -0700687 public boolean onPreferenceChange(Preference preference, Object value) {
arete300e9202014-08-15 12:04:48 -0700688 boolean result = true;
Jim Miller4a9065e2014-08-08 15:25:31 -0700689 final String key = preference.getKey();
690 final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
Clara Bayarri52681ec2016-01-28 11:11:35 +0000691 if (KEY_VISIBLE_PATTERN_PROFILE.equals(key)) {
692 lockPatternUtils.setVisiblePatternEnabled((Boolean) value, mProfileChallengeUserId);
Clara Bayarri56462d62016-02-12 13:55:36 +0000693 } else if (KEY_UNIFICATION.equals(key)) {
694 if ((Boolean) value) {
695 final boolean compliantForDevice =
696 (mLockPatternUtils.getKeyguardStoredPasswordQuality(mProfileChallengeUserId)
697 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
698 && mLockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
699 mProfileChallengeUserId));
700 UnificationConfirmationDialog dialog =
701 UnificationConfirmationDialog.newIntance(compliantForDevice);
702 dialog.show(getChildFragmentManager(), TAG_UNIFICATION_DIALOG);
703 } else {
704 mLockPatternUtils.setSeparateProfileChallengeEnabled(mProfileChallengeUserId, true);
705 createPreferenceHierarchy();
706 }
Clara Bayarri52681ec2016-01-28 11:11:35 +0000707 } else if (KEY_SHOW_PASSWORD.equals(key)) {
Jim Miller4a9065e2014-08-08 15:25:31 -0700708 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
709 ((Boolean) value) ? 1 : 0);
Paul Lawrencec04420c2015-05-18 13:25:01 -0700710 lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID);
Jim Miller4a9065e2014-08-08 15:25:31 -0700711 } else if (KEY_TOGGLE_INSTALL_APPLICATIONS.equals(key)) {
712 if ((Boolean) value) {
713 mToggleAppInstallation.setChecked(false);
714 warnAppInstallation();
arete300e9202014-08-15 12:04:48 -0700715 // Don't change Switch status until user makes choice in dialog, so return false.
716 result = false;
Jim Miller4a9065e2014-08-08 15:25:31 -0700717 } else {
718 setNonMarketAppsAllowed(false);
719 }
Jim Miller86624a22010-08-30 22:07:58 -0700720 }
arete300e9202014-08-15 12:04:48 -0700721 return result;
Jim Miller86624a22010-08-30 22:07:58 -0700722 }
Danielle Millett487b16f2011-11-02 11:12:21 -0400723
rich cannings93c0ee52012-09-30 13:54:26 -0700724 @Override
725 protected int getHelpResource() {
726 return R.string.help_url_security;
727 }
728
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700729 /**
730 * For Search. Please keep it in sync when updating "createPreferenceHierarchy()"
731 */
732 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
733 new SecuritySearchIndexProvider();
734
Jim Millerfe765592014-07-31 15:30:29 -0700735 private static class SecuritySearchIndexProvider extends BaseSearchIndexProvider {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700736
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700737 @Override
738 public List<SearchIndexableResource> getXmlResourcesToIndex(
739 Context context, boolean enabled) {
740
741 List<SearchIndexableResource> result = new ArrayList<SearchIndexableResource>();
742
743 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
744 // Add options for lock/unlock screen
Clara Bayarri52681ec2016-01-28 11:11:35 +0000745 int resId = getResIdForLockUnlockScreen(context, lockPatternUtils, MY_USER_ID);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700746
747 SearchIndexableResource sir = new SearchIndexableResource(context);
748 sir.xmlResId = resId;
749 result.add(sir);
750
Xiaohui Chen44879a32015-07-22 13:53:22 -0700751 final UserManager um = UserManager.get(context);
Clara Bayarri462cce12016-02-18 12:09:21 +0000752 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000753 if (profileUserId != UserHandle.USER_NULL
754 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)) {
755 sir = new SearchIndexableResource(context);
756 sir.xmlResId = getResIdForLockUnlockScreen(
757 context, lockPatternUtils, profileUserId);
758 result.add(sir);
Clara Bayarri6934a042015-10-14 11:07:35 +0100759 }
760
Xiaohui Chen44879a32015-07-22 13:53:22 -0700761 if (um.isAdminUser()) {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700762 DevicePolicyManager dpm = (DevicePolicyManager)
763 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
764
765 switch (dpm.getStorageEncryptionStatus()) {
766 case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE:
767 // The device is currently encrypted.
768 resId = R.xml.security_settings_encrypted;
769 break;
770 case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE:
771 // This device supports encryption but isn't encrypted.
772 resId = R.xml.security_settings_unencrypted;
773 break;
774 }
775
776 sir = new SearchIndexableResource(context);
777 sir.xmlResId = resId;
778 result.add(sir);
779 }
780
Adrian Roos62775bf2016-01-28 13:23:53 -0800781 sir = new SearchIndexableResource(context);
782 sir.xmlResId = SecuritySubSettings.getResIdForLockUnlockSubScreen(context,
783 lockPatternUtils);
784 sir.className = SecuritySubSettings.class.getName();
785 result.add(sir);
786
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700787 // Append the rest of the settings
788 sir = new SearchIndexableResource(context);
789 sir.xmlResId = R.xml.security_settings_misc;
790 result.add(sir);
791
792 return result;
793 }
794
795 @Override
796 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
797 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
798 final Resources res = context.getResources();
799
800 final String screenTitle = res.getString(R.string.security_settings_title);
801
Fabrice Di Megliod7290292014-07-01 14:32:03 -0700802 SearchIndexableRaw data = new SearchIndexableRaw(context);
803 data.title = screenTitle;
804 data.screenTitle = screenTitle;
805 result.add(data);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700806
Xiaohui Chen44879a32015-07-22 13:53:22 -0700807 final UserManager um = UserManager.get(context);
808 if (!um.isAdminUser()) {
809 int resId = um.isLinkedUser() ?
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700810 R.string.profile_info_settings_title : R.string.user_info_settings_title;
811
812 data = new SearchIndexableRaw(context);
813 data.title = res.getString(resId);
814 data.screenTitle = screenTitle;
815 result.add(data);
816 }
817
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400818 // Fingerprint
819 FingerprintManager fpm =
820 (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
Jim Millerb528b262016-02-04 17:10:44 -0800821 if (fpm != null && fpm.isHardwareDetected()) {
Jim Millerc5fa6e22015-09-22 18:30:13 -0700822 // This catches the title which can be overloaded in an overlay
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400823 data = new SearchIndexableRaw(context);
824 data.title = res.getString(R.string.security_settings_fingerprint_preference_title);
825 data.screenTitle = screenTitle;
826 result.add(data);
Jim Millerc5fa6e22015-09-22 18:30:13 -0700827 // Fallback for when the above doesn't contain "fingerprint"
828 data = new SearchIndexableRaw(context);
829 data.title = res.getString(R.string.fingerprint_manage_category_title);
830 data.screenTitle = screenTitle;
831 result.add(data);
Julia Reynoldsf516e7d2015-07-16 15:33:22 -0400832 }
833
Clara Bayarri52681ec2016-01-28 11:11:35 +0000834 final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Clara Bayarri462cce12016-02-18 12:09:21 +0000835 final int profileUserId = Utils.getManagedProfileId(um, MY_USER_ID);
Clara Bayarri52681ec2016-01-28 11:11:35 +0000836 if (profileUserId != UserHandle.USER_NULL
837 && lockPatternUtils.isSeparateProfileChallengeAllowed(profileUserId)) {
838 if (lockPatternUtils.getKeyguardStoredPasswordQuality(profileUserId)
839 >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING
840 && lockPatternUtils.isSeparateProfileChallengeAllowedToUnify(
841 profileUserId)) {
842 data = new SearchIndexableRaw(context);
843 data.title = res.getString(R.string.lock_settings_profile_unification_title);
844 data.screenTitle = screenTitle;
845 result.add(data);
846 }
847 }
848
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700849 // Credential storage
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700850 if (!um.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
851 KeyStore keyStore = KeyStore.getInstance();
852
853 final int storageSummaryRes = keyStore.isHardwareBacked() ?
854 R.string.credential_storage_type_hardware :
855 R.string.credential_storage_type_software;
856
857 data = new SearchIndexableRaw(context);
858 data.title = res.getString(storageSummaryRes);
859 data.screenTitle = screenTitle;
860 result.add(data);
861 }
862
Jim Millerfe765592014-07-31 15:30:29 -0700863 // Advanced
Adrian Roos54375882015-04-16 17:11:22 -0700864 if (lockPatternUtils.isSecure(MY_USER_ID)) {
Jim Millerfe765592014-07-31 15:30:29 -0700865 ArrayList<TrustAgentComponentInfo> agents =
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000866 getActiveTrustAgents(context, lockPatternUtils,
Adrian Roos088de472015-04-07 14:09:39 +0200867 context.getSystemService(DevicePolicyManager.class));
Jim Millerfe765592014-07-31 15:30:29 -0700868 for (int i = 0; i < agents.size(); i++) {
869 final TrustAgentComponentInfo agent = agents.get(i);
870 data = new SearchIndexableRaw(context);
871 data.title = agent.title;
872 data.screenTitle = screenTitle;
873 result.add(data);
874 }
875 }
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700876 return result;
877 }
878
879 @Override
880 public List<String> getNonIndexableKeys(Context context) {
881 final List<String> keys = new ArrayList<String>();
882
883 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700884
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700885 // Do not display SIM lock for devices without an Icc card
Xiaohui Chen44879a32015-07-22 13:53:22 -0700886 final UserManager um = UserManager.get(context);
887 final TelephonyManager tm = TelephonyManager.from(context);
888 if (!um.isAdminUser() || !tm.hasIccCard()) {
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700889 keys.add(KEY_SIM_LOCK);
890 }
891
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700892 if (um.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
893 keys.add(KEY_CREDENTIALS_MANAGER);
894 }
895
Jim Millerfe765592014-07-31 15:30:29 -0700896 // TrustAgent settings disappear when the user has no primary security.
Adrian Roos54375882015-04-16 17:11:22 -0700897 if (!lockPatternUtils.isSecure(MY_USER_ID)) {
Jim Millerfe765592014-07-31 15:30:29 -0700898 keys.add(KEY_TRUST_AGENT);
899 keys.add(KEY_MANAGE_TRUST_AGENTS);
900 }
901
Adrian Roos62775bf2016-01-28 13:23:53 -0800902 if (!SecuritySubSettings.canChangeRequireCredentialBeforeStartup(context)) {
903 keys.add(SecuritySubSettings.KEY_REQUIRE_CRED_BEFORE_STARTUP);
904 }
905
Fabrice Di Megliod1d2f022014-04-11 16:09:11 -0700906 return keys;
907 }
908 }
909
Adrian Roos62775bf2016-01-28 13:23:53 -0800910 public static class SecuritySubSettings extends SettingsPreferenceFragment
911 implements OnPreferenceChangeListener {
912
913 private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
914 private static final String KEY_LOCK_AFTER_TIMEOUT = "lock_after_timeout";
915 private static final String KEY_OWNER_INFO_SETTINGS = "owner_info_settings";
916 private static final String KEY_POWER_INSTANTLY_LOCKS = "power_button_instantly_locks";
917 private static final String KEY_REQUIRE_CRED_BEFORE_STARTUP = "require_cred_before_startup";
918
919 public static final int REQUEST_CHANGE_REQUIRE_CRED_FOR_START = 2;
920
921 // These switch preferences need special handling since they're not all stored in Settings.
922 private static final String SWITCH_PREFERENCE_KEYS[] = { KEY_LOCK_AFTER_TIMEOUT,
923 KEY_VISIBLE_PATTERN, KEY_POWER_INSTANTLY_LOCKS, KEY_REQUIRE_CRED_BEFORE_STARTUP };
924
925 private RestrictedListPreference mLockAfter;
926 private SwitchPreference mVisiblePattern;
927 private SwitchPreference mPowerButtonInstantlyLocks;
Sudheer Shankaec1052d2016-02-03 00:24:52 +0000928 private RestrictedPreference mOwnerInfoPref;
Adrian Roos62775bf2016-01-28 13:23:53 -0800929
930 private LockPatternUtils mLockPatternUtils;
931 private DevicePolicyManager mDPM;
932
933 @Override
934 protected int getMetricsCategory() {
935 return MetricsEvent.SECURITY;
936 }
937
938 @Override
939 public void onCreate(Bundle icicle) {
940 super.onCreate(icicle);
941 mLockPatternUtils = new LockPatternUtils(getContext());
942 mDPM = getContext().getSystemService(DevicePolicyManager.class);
943 }
944
945 @Override
946 public void onResume() {
947 super.onResume();
948
949 createPreferenceHierarchy();
950
Adrian Roos62775bf2016-01-28 13:23:53 -0800951 if (mVisiblePattern != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +0000952 mVisiblePattern.setChecked(mLockPatternUtils.isVisiblePatternEnabled(
Adrian Roos62775bf2016-01-28 13:23:53 -0800953 MY_USER_ID));
954 }
955 if (mPowerButtonInstantlyLocks != null) {
Clara Bayarri56462d62016-02-12 13:55:36 +0000956 mPowerButtonInstantlyLocks.setChecked(mLockPatternUtils.getPowerButtonInstantlyLocks(
Adrian Roos62775bf2016-01-28 13:23:53 -0800957 MY_USER_ID));
958 }
959
960 updateOwnerInfo();
961 }
962
963 @Override
964 public void onActivityResult(int requestCode, int resultCode, Intent data) {
965 super.onActivityResult(requestCode, resultCode, data);
966
967 createPreferenceHierarchy();
968 }
969
970 private void createPreferenceHierarchy() {
971 PreferenceScreen root = getPreferenceScreen();
972 if (root != null) {
973 root.removeAll();
974 }
975 root = null;
976
977 final int resid = getResIdForLockUnlockSubScreen(getActivity(),
978 new LockPatternUtils(getContext()));
979 addPreferencesFromResource(resid);
980
981 // lock after preference
982 mLockAfter = (RestrictedListPreference) findPreference(KEY_LOCK_AFTER_TIMEOUT);
983 if (mLockAfter != null) {
984 setupLockAfterPreference();
985 updateLockAfterPreferenceSummary();
986 }
987
988 // visible pattern
989 mVisiblePattern = (SwitchPreference) findPreference(KEY_VISIBLE_PATTERN);
990
991 // lock instantly on power key press
992 mPowerButtonInstantlyLocks = (SwitchPreference) findPreference(
993 KEY_POWER_INSTANTLY_LOCKS);
994 Preference trustAgentPreference = findPreference(KEY_TRUST_AGENT);
995 if (mPowerButtonInstantlyLocks != null &&
996 trustAgentPreference != null &&
997 trustAgentPreference.getTitle().length() > 0) {
998 mPowerButtonInstantlyLocks.setSummary(getString(
999 R.string.lockpattern_settings_power_button_instantly_locks_summary,
1000 trustAgentPreference.getTitle()));
1001 }
1002
Sudheer Shankaec1052d2016-02-03 00:24:52 +00001003 mOwnerInfoPref = (RestrictedPreference) findPreference(KEY_OWNER_INFO_SETTINGS);
Adrian Roos62775bf2016-01-28 13:23:53 -08001004 if (mOwnerInfoPref != null) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +00001005 if (mLockPatternUtils.isDeviceOwnerInfoEnabled()) {
1006 EnforcedAdmin admin = RestrictedLockUtils.getDeviceOwner(getActivity());
1007 mOwnerInfoPref.setDisabledByAdmin(admin);
1008 } else {
1009 mOwnerInfoPref.setDisabledByAdmin(null);
1010 mOwnerInfoPref.setEnabled(!mLockPatternUtils.isLockScreenDisabled(MY_USER_ID));
1011 if (mOwnerInfoPref.isEnabled()) {
1012 mOwnerInfoPref.setOnPreferenceClickListener(
1013 new OnPreferenceClickListener() {
1014 @Override
1015 public boolean onPreferenceClick(Preference preference) {
1016 OwnerInfoSettings.show(SecuritySubSettings.this);
1017 return true;
1018 }
1019 });
1020 }
Adrian Roos62775bf2016-01-28 13:23:53 -08001021 }
1022 }
1023
1024 Preference requireCredForStartup = findPreference(KEY_REQUIRE_CRED_BEFORE_STARTUP);
1025 if (requireCredForStartup instanceof SwitchPreference) {
1026 ((SwitchPreference) requireCredForStartup).setChecked(
1027 mLockPatternUtils.isCredentialRequiredToDecrypt(false));
1028 if (!canChangeRequireCredentialBeforeStartup(getContext())) {
1029 removePreference(KEY_REQUIRE_CRED_BEFORE_STARTUP);
1030 }
1031 }
1032
1033 for (int i = 0; i < SWITCH_PREFERENCE_KEYS.length; i++) {
1034 final Preference pref = findPreference(SWITCH_PREFERENCE_KEYS[i]);
1035 if (pref != null) pref.setOnPreferenceChangeListener(this);
1036 }
1037 }
1038
1039 static boolean canChangeRequireCredentialBeforeStartup(Context context) {
1040 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
1041 return UserManager.get(context).isAdminUser()
Adrian Roos77181e92016-02-25 15:43:24 -08001042 && UserManager.get(context).isPrimaryUser()
Adrian Roos62775bf2016-01-28 13:23:53 -08001043 && LockPatternUtils.isDeviceEncryptionEnabled()
1044 && !dpm.getDoNotAskCredentialsOnBoot();
1045 }
1046
1047 private void setupLockAfterPreference() {
1048 // Compatible with pre-Froyo
1049 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1050 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1051 mLockAfter.setValue(String.valueOf(currentTimeout));
1052 mLockAfter.setOnPreferenceChangeListener(this);
1053 final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
1054 getActivity());
1055 if (admin != null) {
1056 final long adminTimeout = (mDPM != null ? mDPM.getMaximumTimeToLock(null) : 0);
1057 final long displayTimeout = Math.max(0,
1058 Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0));
1059 if (adminTimeout > 0) {
1060 // This setting is a slave to display timeout when a device policy is enforced.
1061 // As such, maxLockTimeout = adminTimeout - displayTimeout.
1062 // If there isn't enough time, shows "immediately" setting.
1063 disableUnusableTimeouts(Math.max(0, adminTimeout - displayTimeout), admin);
1064 }
1065 }
1066 }
1067
1068 private void updateLockAfterPreferenceSummary() {
1069 final String summary;
1070 if (mLockAfter.isDisabledByAdmin()) {
1071 summary = getString(R.string.disabled_by_policy_title);
1072 } else {
1073 // Update summary message with current value
1074 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
1075 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
1076 final CharSequence[] entries = mLockAfter.getEntries();
1077 final CharSequence[] values = mLockAfter.getEntryValues();
1078 int best = 0;
1079 for (int i = 0; i < values.length; i++) {
1080 if (mLockAfter.isRestrictedForEntry(entries[i])) {
1081 break;
1082 }
1083 long timeout = Long.valueOf(values[i].toString());
1084 if (currentTimeout >= timeout) {
1085 best = i;
1086 }
1087 }
1088
1089 Preference preference = findPreference(KEY_TRUST_AGENT);
1090 if (preference != null && preference.getTitle().length() > 0) {
1091 if (Long.valueOf(values[best].toString()) == 0) {
1092 summary = getString(R.string.lock_immediately_summary_with_exception,
1093 preference.getTitle());
1094 } else {
1095 summary = getString(R.string.lock_after_timeout_summary_with_exception,
1096 entries[best], preference.getTitle());
1097 }
1098 } else {
1099 summary = getString(R.string.lock_after_timeout_summary, entries[best]);
1100 }
1101 }
1102 mLockAfter.setSummary(summary);
1103 }
1104
1105 private void disableUnusableTimeouts(long maxTimeout, EnforcedAdmin admin) {
1106 final CharSequence[] entries = mLockAfter.getEntries();
1107 final CharSequence[] values = mLockAfter.getEntryValues();
1108 long maxTimeoutSelectable = 0;
1109 int maxTimeoutEntryIndex = -1;
1110 for (int i = 0; i < values.length; i++) {
1111 long timeout = Long.parseLong(values[i].toString());
1112 if (timeout > maxTimeout) {
1113 break;
1114 }
1115 maxTimeoutSelectable = timeout;
1116 maxTimeoutEntryIndex = i;
1117 }
1118 // If there are no possible options for the user, then set this preference as
1119 // disabled by admin, otherwise remove the padlock in case it was set earlier.
1120 if (maxTimeoutSelectable == 0) {
1121 mLockAfter.setDisabledByAdmin(admin);
1122 return;
1123 } else {
1124 mLockAfter.setDisabledByAdmin(null);
1125 }
1126
1127 mLockAfter.clearRestrictedItems();
1128 // Set all the entries after the maximum selectable timeout as disabled by admin.
1129 for (int i = maxTimeoutEntryIndex + 1; i < values.length; i++) {
1130 mLockAfter.addRestrictedItem(
1131 new RestrictedItem(entries[i], values[i], admin));
1132 }
1133
1134 final int userPreference = Integer.valueOf(mLockAfter.getValue());
1135 if (userPreference <= maxTimeout) {
1136 mLockAfter.setValue(String.valueOf(userPreference));
1137 } else if (maxTimeoutSelectable == maxTimeout) {
1138 mLockAfter.setValue(String.valueOf(maxTimeout));
1139 } else {
1140 // There will be no highlighted selection since nothing in the list matches
1141 // maxTimeout. The user can still select anything less than maxTimeout.
1142 // TODO: maybe append maxTimeout to the list and mark selected.
1143 }
1144 }
1145
1146 public void updateOwnerInfo() {
1147 if (mOwnerInfoPref != null) {
1148 if (mLockPatternUtils.isDeviceOwnerInfoEnabled()) {
Sudheer Shankaec1052d2016-02-03 00:24:52 +00001149 mOwnerInfoPref.setSummary(
1150 mLockPatternUtils.getDeviceOwnerInfo());
Adrian Roos62775bf2016-01-28 13:23:53 -08001151 } else {
1152 mOwnerInfoPref.setSummary(mLockPatternUtils.isOwnerInfoEnabled(MY_USER_ID)
1153 ? mLockPatternUtils.getOwnerInfo(MY_USER_ID)
1154 : getString(R.string.owner_info_settings_summary));
1155 }
1156 }
1157 }
1158
1159 private static int getResIdForLockUnlockSubScreen(Context context,
1160 LockPatternUtils lockPatternUtils) {
1161 if (lockPatternUtils.isSecure(MY_USER_ID)) {
1162 switch (lockPatternUtils.getKeyguardStoredPasswordQuality(MY_USER_ID)) {
1163 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
1164 return R.xml.security_settings_pattern_sub;
1165 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
1166 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
1167 return R.xml.security_settings_pin_sub;
1168 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
1169 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
1170 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
1171 return R.xml.security_settings_password_sub;
1172 }
1173 } else if (!lockPatternUtils.isLockScreenDisabled(MY_USER_ID)) {
1174 return R.xml.security_settings_slide_sub;
1175 }
1176 return 0;
1177 }
1178
1179 @Override
1180 public boolean onPreferenceChange(Preference preference, Object value) {
1181 String key = preference.getKey();
1182 if (KEY_POWER_INSTANTLY_LOCKS.equals(key)) {
1183 mLockPatternUtils.setPowerButtonInstantlyLocks((Boolean) value, MY_USER_ID);
1184 } else if (KEY_LOCK_AFTER_TIMEOUT.equals(key)) {
1185 int timeout = Integer.parseInt((String) value);
1186 try {
1187 Settings.Secure.putInt(getContentResolver(),
1188 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, timeout);
1189 } catch (NumberFormatException e) {
1190 Log.e("SecuritySettings", "could not persist lockAfter timeout setting", e);
1191 }
1192 updateLockAfterPreferenceSummary();
1193 } else if (KEY_VISIBLE_PATTERN.equals(key)) {
1194 mLockPatternUtils.setVisiblePatternEnabled((Boolean) value, MY_USER_ID);
1195 } else if (KEY_REQUIRE_CRED_BEFORE_STARTUP.equals(key)) {
1196 Bundle extras = new Bundle();
1197 extras.putBoolean(
1198 ChooseLockSettingsHelper.EXTRA_KEY_FOR_CHANGE_CRED_REQUIRED_FOR_BOOT, true);
1199 startFragment(this,
1200 "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
1201 R.string.lock_settings_picker_title, REQUEST_CHANGE_REQUIRE_CRED_FOR_START,
1202 extras);
1203 return false;
1204 }
1205 return true;
1206 }
1207 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001208
1209 public static class UnificationConfirmationDialog extends DialogFragment {
Clara Bayarri56462d62016-02-12 13:55:36 +00001210 private static final String EXTRA_COMPLIANT = "compliant";
Clara Bayarri52681ec2016-01-28 11:11:35 +00001211
Clara Bayarri56462d62016-02-12 13:55:36 +00001212 public static UnificationConfirmationDialog newIntance(boolean compliant) {
Clara Bayarri52681ec2016-01-28 11:11:35 +00001213 UnificationConfirmationDialog dialog = new UnificationConfirmationDialog();
Clara Bayarri56462d62016-02-12 13:55:36 +00001214 Bundle args = new Bundle();
1215 args.putBoolean(EXTRA_COMPLIANT, compliant);
1216 dialog.setArguments(args);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001217 return dialog;
1218 }
1219
1220 @Override
1221 public void show(FragmentManager manager, String tag) {
1222 if (manager.findFragmentByTag(tag) == null) {
1223 // Prevent opening multiple dialogs if tapped on button quickly
1224 super.show(manager, tag);
1225 }
1226 }
1227
1228 @Override
1229 public Dialog onCreateDialog(Bundle savedInstanceState) {
1230 final SecuritySettings parentFragment = ((SecuritySettings) getParentFragment());
Clara Bayarri56462d62016-02-12 13:55:36 +00001231 final boolean compliant = getArguments().getBoolean(EXTRA_COMPLIANT);
Clara Bayarri52681ec2016-01-28 11:11:35 +00001232 return new AlertDialog.Builder(getActivity())
1233 .setTitle(R.string.lock_settings_profile_unification_dialog_title)
Clara Bayarri56462d62016-02-12 13:55:36 +00001234 .setMessage(compliant ? R.string.lock_settings_profile_unification_dialog_body
1235 : R.string.lock_settings_profile_unification_dialog_uncompliant_body)
1236 .setPositiveButton(compliant ? R.string.okay
1237 : R.string.lock_settings_profile_unification_dialog_uncompliant_confirm,
Clara Bayarri52681ec2016-01-28 11:11:35 +00001238 new DialogInterface.OnClickListener() {
1239 @Override
1240 public void onClick(DialogInterface dialog, int whichButton) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001241 if (compliant) {
1242 parentFragment.launchConfirmDeviceLockForUnification();
1243 } else {
1244 parentFragment.unifyUncompliantLocks();
1245 }
Clara Bayarri52681ec2016-01-28 11:11:35 +00001246 }
1247 }
1248 )
1249 .setNegativeButton(R.string.cancel,
1250 new DialogInterface.OnClickListener() {
1251 @Override
1252 public void onClick(DialogInterface dialog, int whichButton) {
Clara Bayarri56462d62016-02-12 13:55:36 +00001253 parentFragment.updateUnificationPreference();
Clara Bayarri52681ec2016-01-28 11:11:35 +00001254 dismiss();
1255 }
1256 }
1257 )
1258 .create();
1259 }
1260 }
1261
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001262}