OSDN Git Service

Merge tag 'android-7.1.2_r36' into nougat-x86
[android-x86/packages-apps-Settings.git] / src / com / android / settings / ChooseLockGeneric.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings;
18
19 import static android.app.admin.DevicePolicyManager.ACTION_SET_NEW_PARENT_PROFILE_PASSWORD;
20 import static android.app.admin.DevicePolicyManager.ACTION_SET_NEW_PASSWORD;
21 import static com.android.settings.ChooseLockPassword.ChooseLockPasswordFragment.RESULT_FINISHED;
22 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
23
24 import android.accessibilityservice.AccessibilityServiceInfo;
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.app.Dialog;
28 import android.app.DialogFragment;
29 import android.app.Fragment;
30 import android.app.FragmentManager;
31 import android.app.admin.DevicePolicyManager;
32 import android.content.Context;
33 import android.content.DialogInterface;
34 import android.content.Intent;
35 import android.content.pm.UserInfo;
36 import android.hardware.fingerprint.Fingerprint;
37 import android.hardware.fingerprint.FingerprintManager;
38 import android.hardware.fingerprint.FingerprintManager.RemovalCallback;
39 import android.os.Bundle;
40 import android.os.UserHandle;
41 import android.os.UserManager;
42 import android.os.storage.StorageManager;
43 import android.security.KeyStore;
44 import android.support.v7.preference.Preference;
45 import android.support.v7.preference.PreferenceScreen;
46 import android.text.TextUtils;
47 import android.util.EventLog;
48 import android.util.Log;
49 import android.view.accessibility.AccessibilityManager;
50 import android.widget.TextView;
51
52 import com.android.internal.logging.MetricsProto.MetricsEvent;
53 import com.android.internal.widget.LockPatternUtils;
54 import com.android.settings.fingerprint.FingerprintEnrollBase;
55 import com.android.settings.fingerprint.FingerprintEnrollFindSensor;
56 import com.android.settingslib.RestrictedLockUtils;
57 import com.android.settingslib.RestrictedPreference;
58
59 import java.util.List;
60
61 public class ChooseLockGeneric extends SettingsActivity {
62     public static final String CONFIRM_CREDENTIALS = "confirm_credentials";
63
64     @Override
65     public Intent getIntent() {
66         Intent modIntent = new Intent(super.getIntent());
67         modIntent.putExtra(EXTRA_SHOW_FRAGMENT, getFragmentClass().getName());
68
69         String action = modIntent.getAction();
70         if (ACTION_SET_NEW_PASSWORD.equals(action)
71                 || ACTION_SET_NEW_PARENT_PROFILE_PASSWORD.equals(action)) {
72             modIntent.putExtra(EXTRA_HIDE_DRAWER, true);
73         }
74         return modIntent;
75     }
76
77     @Override
78     protected boolean isValidFragment(String fragmentName) {
79         if (ChooseLockGenericFragment.class.getName().equals(fragmentName)) return true;
80         return false;
81     }
82
83     /* package */ Class<? extends Fragment> getFragmentClass() {
84         return ChooseLockGenericFragment.class;
85     }
86
87     public static class InternalActivity extends ChooseLockGeneric {
88     }
89
90     public static class ChooseLockGenericFragment extends SettingsPreferenceFragment {
91         private static final String TAG = "ChooseLockGenericFragment";
92         private static final int MIN_PASSWORD_LENGTH = 4;
93         private static final String KEY_UNLOCK_SET_OFF = "unlock_set_off";
94         private static final String KEY_UNLOCK_SET_NONE = "unlock_set_none";
95         private static final String KEY_UNLOCK_SET_PIN = "unlock_set_pin";
96         private static final String KEY_UNLOCK_SET_PASSWORD = "unlock_set_password";
97         private static final String KEY_UNLOCK_SET_PATTERN = "unlock_set_pattern";
98         private static final String KEY_UNLOCK_SET_MANAGED = "unlock_set_managed";
99         private static final String KEY_SKIP_FINGERPRINT = "unlock_skip_fingerprint";
100         private static final String PASSWORD_CONFIRMED = "password_confirmed";
101         private static final String WAITING_FOR_CONFIRMATION = "waiting_for_confirmation";
102         public static final String MINIMUM_QUALITY_KEY = "minimum_quality";
103         public static final String HIDE_DISABLED_PREFS = "hide_disabled_prefs";
104         public static final String ENCRYPT_REQUESTED_QUALITY = "encrypt_requested_quality";
105         public static final String ENCRYPT_REQUESTED_DISABLED = "encrypt_requested_disabled";
106         public static final String TAG_FRP_WARNING_DIALOG = "frp_warning_dialog";
107
108         private static final int CONFIRM_EXISTING_REQUEST = 100;
109         private static final int ENABLE_ENCRYPTION_REQUEST = 101;
110         private static final int CHOOSE_LOCK_REQUEST = 102;
111         private static final int CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST = 103;
112         private static final int SKIP_FINGERPRINT_REQUEST = 104;
113
114         private ChooseLockSettingsHelper mChooseLockSettingsHelper;
115         private DevicePolicyManager mDPM;
116         private KeyStore mKeyStore;
117         private boolean mHasChallenge = false;
118         private long mChallenge;
119         private boolean mPasswordConfirmed = false;
120         private boolean mWaitingForConfirmation = false;
121         private int mEncryptionRequestQuality;
122         private boolean mEncryptionRequestDisabled;
123         private boolean mRequirePassword;
124         private boolean mForChangeCredRequiredForBoot = false;
125         private String mUserPassword;
126         private LockPatternUtils mLockPatternUtils;
127         private FingerprintManager mFingerprintManager;
128         private int mUserId;
129         private boolean mHideDrawer = false;
130         private ManagedLockPasswordProvider mManagedPasswordProvider;
131         private boolean mIsSetNewPassword = false;
132
133         protected boolean mForFingerprint = false;
134
135         @Override
136         protected int getMetricsCategory() {
137             return MetricsEvent.CHOOSE_LOCK_GENERIC;
138         }
139
140         @Override
141         public void onCreate(Bundle savedInstanceState) {
142             super.onCreate(savedInstanceState);
143
144             String chooseLockAction = getActivity().getIntent().getAction();
145             mFingerprintManager =
146                 (FingerprintManager) getActivity().getSystemService(Context.FINGERPRINT_SERVICE);
147             mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
148             mKeyStore = KeyStore.getInstance();
149             mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this.getActivity());
150             mLockPatternUtils = new LockPatternUtils(getActivity());
151             mIsSetNewPassword = ACTION_SET_NEW_PARENT_PROFILE_PASSWORD.equals(chooseLockAction)
152                     || ACTION_SET_NEW_PASSWORD.equals(chooseLockAction);
153
154             // Defaults to needing to confirm credentials
155             final boolean confirmCredentials = getActivity().getIntent()
156                 .getBooleanExtra(CONFIRM_CREDENTIALS, true);
157             if (getActivity() instanceof ChooseLockGeneric.InternalActivity) {
158                 mPasswordConfirmed = !confirmCredentials;
159             }
160             mHideDrawer = getActivity().getIntent().getBooleanExtra(EXTRA_HIDE_DRAWER, false);
161
162             mHasChallenge = getActivity().getIntent().getBooleanExtra(
163                     ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false);
164             mChallenge = getActivity().getIntent().getLongExtra(
165                     ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0);
166             mForFingerprint = getActivity().getIntent().getBooleanExtra(
167                     ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, false);
168             mForChangeCredRequiredForBoot = getArguments() != null && getArguments().getBoolean(
169                     ChooseLockSettingsHelper.EXTRA_KEY_FOR_CHANGE_CRED_REQUIRED_FOR_BOOT);
170
171             if (savedInstanceState != null) {
172                 mPasswordConfirmed = savedInstanceState.getBoolean(PASSWORD_CONFIRMED);
173                 mWaitingForConfirmation = savedInstanceState.getBoolean(WAITING_FOR_CONFIRMATION);
174                 mEncryptionRequestQuality = savedInstanceState.getInt(ENCRYPT_REQUESTED_QUALITY);
175                 mEncryptionRequestDisabled = savedInstanceState.getBoolean(
176                         ENCRYPT_REQUESTED_DISABLED);
177             }
178
179             // a) If this is started from other user, use that user id.
180             // b) If this is started from the same user, read the extra if this is launched
181             //    from Settings app itself.
182             // c) Otherwise, use UserHandle.myUserId().
183             mUserId = Utils.getSecureTargetUser(
184                     getActivity().getActivityToken(),
185                     UserManager.get(getActivity()),
186                     getArguments(),
187                     getActivity().getIntent().getExtras()).getIdentifier();
188             if (ACTION_SET_NEW_PASSWORD.equals(chooseLockAction)
189                     && Utils.isManagedProfile(UserManager.get(getActivity()), mUserId)
190                     && mLockPatternUtils.isSeparateProfileChallengeEnabled(mUserId)) {
191                 getActivity().setTitle(R.string.lock_settings_picker_title_profile);
192             }
193
194             mManagedPasswordProvider = ManagedLockPasswordProvider.get(getActivity(), mUserId);
195
196             if (mPasswordConfirmed) {
197                 updatePreferencesOrFinish();
198                 if (mForChangeCredRequiredForBoot) {
199                     maybeEnableEncryption(mLockPatternUtils.getKeyguardStoredPasswordQuality(
200                             mUserId), false);
201                 }
202             } else if (!mWaitingForConfirmation) {
203                 ChooseLockSettingsHelper helper =
204                         new ChooseLockSettingsHelper(this.getActivity(), this);
205                 boolean managedProfileWithUnifiedLock = Utils
206                         .isManagedProfile(UserManager.get(getActivity()), mUserId)
207                         && !mLockPatternUtils.isSeparateProfileChallengeEnabled(mUserId);
208                 if (managedProfileWithUnifiedLock
209                         || !helper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST,
210                         getString(R.string.unlock_set_unlock_launch_picker_title), true, mUserId)) {
211                     mPasswordConfirmed = true; // no password set, so no need to confirm
212                     updatePreferencesOrFinish();
213                 } else {
214                     mWaitingForConfirmation = true;
215                 }
216             }
217             addHeaderView();
218         }
219
220         protected void addHeaderView() {
221             if (mForFingerprint) {
222                 setHeaderView(R.layout.choose_lock_generic_fingerprint_header);
223                 if (mIsSetNewPassword) {
224                     ((TextView) getHeaderView().findViewById(R.id.fingerprint_header_description))
225                             .setText(R.string.fingerprint_unlock_title);
226                 }
227             }
228         }
229
230         @Override
231         public boolean onPreferenceTreeClick(Preference preference) {
232             final String key = preference.getKey();
233
234             if (!isUnlockMethodSecure(key) && mLockPatternUtils.isSecure(mUserId)) {
235                 // Show the disabling FRP warning only when the user is switching from a secure
236                 // unlock method to an insecure one
237                 showFactoryResetProtectionWarningDialog(key);
238                 return true;
239             } else if (KEY_SKIP_FINGERPRINT.equals(key)) {
240                 Intent chooseLockGenericIntent = new Intent(getActivity(),
241                     ChooseLockGeneric.InternalActivity.class);
242                 chooseLockGenericIntent.setAction(getIntent().getAction());
243                 // Forward the target user id to  ChooseLockGeneric.
244                 chooseLockGenericIntent.putExtra(Intent.EXTRA_USER_ID, mUserId);
245                 chooseLockGenericIntent.putExtra(CONFIRM_CREDENTIALS, !mPasswordConfirmed);
246                 startActivityForResult(chooseLockGenericIntent, SKIP_FINGERPRINT_REQUEST);
247                 return true;
248             } else {
249                 return setUnlockMethod(key);
250             }
251         }
252
253         /**
254          * If the device has encryption already enabled, then ask the user if they
255          * also want to encrypt the phone with this password.
256          *
257          * @param quality
258          * @param disabled
259          */
260         // TODO: why does this take disabled, its always called with a quality higher than
261         // what makes sense with disabled == true
262         private void maybeEnableEncryption(int quality, boolean disabled) {
263             DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
264             if (UserManager.get(getActivity()).isAdminUser()
265                     && mUserId == UserHandle.myUserId()
266                     && LockPatternUtils.isDeviceEncryptionEnabled()
267                     && !LockPatternUtils.isFileEncryptionEnabled()
268                     && !dpm.getDoNotAskCredentialsOnBoot()) {
269                 mEncryptionRequestQuality = quality;
270                 mEncryptionRequestDisabled = disabled;
271                 // Get the intent that the encryption interstitial should start for creating
272                 // the new unlock method.
273                 Intent unlockMethodIntent = getIntentForUnlockMethod(quality, disabled);
274                 unlockMethodIntent.putExtra(
275                         ChooseLockSettingsHelper.EXTRA_KEY_FOR_CHANGE_CRED_REQUIRED_FOR_BOOT,
276                         mForChangeCredRequiredForBoot);
277                 final Context context = getActivity();
278                 // If accessibility is enabled and the user hasn't seen this dialog before, set the
279                 // default state to agree with that which is compatible with accessibility
280                 // (password not required).
281                 final boolean accEn = AccessibilityManager.getInstance(context).isEnabled();
282                 final boolean required = mLockPatternUtils.isCredentialRequiredToDecrypt(!accEn);
283                 Intent intent = getEncryptionInterstitialIntent(context, quality, required,
284                         unlockMethodIntent);
285                 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT,
286                         mForFingerprint);
287                 intent.putExtra(EXTRA_HIDE_DRAWER, mHideDrawer);
288                 startActivityForResult(
289                         intent,
290                         mIsSetNewPassword && mHasChallenge
291                                 ? CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST
292                                 : ENABLE_ENCRYPTION_REQUEST);
293             } else {
294                 if (mForChangeCredRequiredForBoot) {
295                     // Welp, couldn't change it. Oh well.
296                     finish();
297                     return;
298                 }
299                 mRequirePassword = false; // device encryption not enabled or not device owner.
300                 updateUnlockMethodAndFinish(quality, disabled);
301             }
302         }
303
304         @Override
305         public void onActivityResult(int requestCode, int resultCode, Intent data) {
306             super.onActivityResult(requestCode, resultCode, data);
307             mWaitingForConfirmation = false;
308             if (requestCode == CONFIRM_EXISTING_REQUEST && resultCode == Activity.RESULT_OK) {
309                 mPasswordConfirmed = true;
310                 mUserPassword = data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
311                 updatePreferencesOrFinish();
312                 if (mForChangeCredRequiredForBoot) {
313                     if (!TextUtils.isEmpty(mUserPassword)) {
314                         maybeEnableEncryption(
315                                 mLockPatternUtils.getKeyguardStoredPasswordQuality(mUserId), false);
316                     } else {
317                         finish();
318                     }
319                 }
320             } else if (requestCode == CHOOSE_LOCK_REQUEST
321                     || requestCode == ENABLE_ENCRYPTION_REQUEST) {
322                 if (resultCode != RESULT_CANCELED || mForChangeCredRequiredForBoot) {
323                     getActivity().setResult(resultCode, data);
324                     finish();
325                 }
326             } else if (requestCode == CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST
327                     && resultCode == FingerprintEnrollBase.RESULT_FINISHED) {
328                 Intent intent = new Intent(getActivity(), FingerprintEnrollFindSensor.class);
329                 if (data != null) {
330                     intent.putExtras(data.getExtras());
331                 }
332                 // Forward the target user id to fingerprint setup page.
333                 intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
334                 startActivity(intent);
335                 finish();
336             } else if (requestCode == SKIP_FINGERPRINT_REQUEST) {
337                 if (resultCode != RESULT_CANCELED) {
338                     getActivity().setResult(
339                             resultCode == RESULT_FINISHED ? RESULT_OK : resultCode, data);
340                     finish();
341                 }
342             } else {
343                 getActivity().setResult(Activity.RESULT_CANCELED);
344                 finish();
345             }
346             if (requestCode == Activity.RESULT_CANCELED && mForChangeCredRequiredForBoot) {
347                 finish();
348             }
349         }
350
351         @Override
352         public void onSaveInstanceState(Bundle outState) {
353             super.onSaveInstanceState(outState);
354             // Saved so we don't force user to re-enter their password if configuration changes
355             outState.putBoolean(PASSWORD_CONFIRMED, mPasswordConfirmed);
356             outState.putBoolean(WAITING_FOR_CONFIRMATION, mWaitingForConfirmation);
357             outState.putInt(ENCRYPT_REQUESTED_QUALITY, mEncryptionRequestQuality);
358             outState.putBoolean(ENCRYPT_REQUESTED_DISABLED, mEncryptionRequestDisabled);
359         }
360
361         private void updatePreferencesOrFinish() {
362             Intent intent = getActivity().getIntent();
363             int quality = intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1);
364             if (quality == -1) {
365                 // If caller didn't specify password quality, show UI and allow the user to choose.
366                 quality = intent.getIntExtra(MINIMUM_QUALITY_KEY, -1);
367                 quality = upgradeQuality(quality);
368                 final boolean hideDisabledPrefs = intent.getBooleanExtra(
369                         HIDE_DISABLED_PREFS, false);
370                 final PreferenceScreen prefScreen = getPreferenceScreen();
371                 if (prefScreen != null) {
372                     prefScreen.removeAll();
373                 }
374                 addPreferences();
375                 disableUnusablePreferences(quality, hideDisabledPrefs);
376                 updatePreferenceText();
377                 updateCurrentPreference();
378                 updatePreferenceSummaryIfNeeded();
379             } else {
380                 updateUnlockMethodAndFinish(quality, false);
381             }
382         }
383
384         protected void addPreferences() {
385             addPreferencesFromResource(R.xml.security_settings_picker);
386
387             // Used for testing purposes
388             findPreference(KEY_UNLOCK_SET_NONE).setViewId(R.id.lock_none);
389             findPreference(KEY_SKIP_FINGERPRINT).setViewId(R.id.lock_none);
390             findPreference(KEY_UNLOCK_SET_PIN).setViewId(R.id.lock_pin);
391             findPreference(KEY_UNLOCK_SET_PASSWORD).setViewId(R.id.lock_password);
392         }
393
394         private void updatePreferenceText() {
395             if (mForFingerprint) {
396                 final String key[] = { KEY_UNLOCK_SET_PATTERN,
397                         KEY_UNLOCK_SET_PIN,
398                         KEY_UNLOCK_SET_PASSWORD };
399                 final int res[] = { R.string.fingerprint_unlock_set_unlock_pattern,
400                         R.string.fingerprint_unlock_set_unlock_pin,
401                         R.string.fingerprint_unlock_set_unlock_password };
402                 for (int i = 0; i < key.length; i++) {
403                     Preference pref = findPreference(key[i]);
404                     if (pref != null) { // can be removed by device admin
405                         pref.setTitle(res[i]);
406                     }
407                 }
408             }
409
410             if (mManagedPasswordProvider.isSettingManagedPasswordSupported()) {
411                 Preference managed = findPreference(KEY_UNLOCK_SET_MANAGED);
412                 managed.setTitle(mManagedPasswordProvider.getPickerOptionTitle(mForFingerprint));
413             } else {
414                 removePreference(KEY_UNLOCK_SET_MANAGED);
415             }
416
417             if (!(mForFingerprint && mIsSetNewPassword)) {
418                 removePreference(KEY_SKIP_FINGERPRINT);
419             }
420         }
421
422         private void updateCurrentPreference() {
423             String currentKey = getKeyForCurrent();
424             Preference preference = findPreference(currentKey);
425             if (preference != null) {
426                 preference.setSummary(R.string.current_screen_lock);
427             }
428         }
429
430         private String getKeyForCurrent() {
431             final int credentialOwner = UserManager.get(getContext())
432                     .getCredentialOwnerProfile(mUserId);
433             if (mLockPatternUtils.isLockScreenDisabled(credentialOwner)) {
434                 return KEY_UNLOCK_SET_OFF;
435             }
436             switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(credentialOwner)) {
437                 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
438                     return KEY_UNLOCK_SET_PATTERN;
439                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
440                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
441                     return KEY_UNLOCK_SET_PIN;
442                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
443                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
444                 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
445                     return KEY_UNLOCK_SET_PASSWORD;
446                 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
447                     return KEY_UNLOCK_SET_MANAGED;
448                 case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
449                     return KEY_UNLOCK_SET_NONE;
450             }
451             return null;
452         }
453
454         /** increases the quality if necessary */
455         private int upgradeQuality(int quality) {
456             quality = upgradeQualityForDPM(quality);
457             return quality;
458         }
459
460         private int upgradeQualityForDPM(int quality) {
461             // Compare min allowed password quality
462             int minQuality = mDPM.getPasswordQuality(null, mUserId);
463             if (quality < minQuality) {
464                 quality = minQuality;
465             }
466             return quality;
467         }
468
469         /***
470          * Disables preferences that are less secure than required quality. The actual
471          * implementation is in disableUnusablePreferenceImpl.
472          *
473          * @param quality the requested quality.
474          * @param hideDisabledPrefs if false preferences show why they were disabled; otherwise
475          * they're not shown at all.
476          */
477         protected void disableUnusablePreferences(final int quality, boolean hideDisabledPrefs) {
478             disableUnusablePreferencesImpl(quality, hideDisabledPrefs);
479         }
480
481         /***
482          * Disables preferences that are less secure than required quality.
483          *
484          * @param quality the requested quality.
485          * @param hideDisabled whether to hide disable screen lock options.
486          */
487         protected void disableUnusablePreferencesImpl(final int quality,
488                 boolean hideDisabled) {
489             final PreferenceScreen entries = getPreferenceScreen();
490
491             int adminEnforcedQuality = mDPM.getPasswordQuality(null, mUserId);
492             EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfPasswordQualityIsSet(
493                     getActivity(), mUserId);
494             for (int i = entries.getPreferenceCount() - 1; i >= 0; --i) {
495                 Preference pref = entries.getPreference(i);
496                 if (pref instanceof RestrictedPreference) {
497                     final String key = pref.getKey();
498                     boolean enabled = true;
499                     boolean visible = true;
500                     boolean disabledByAdmin = false;
501                     if (KEY_UNLOCK_SET_OFF.equals(key)) {
502                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
503                         if (getResources().getBoolean(R.bool.config_hide_none_security_option)) {
504                             enabled = false;
505                             visible = false;
506                         }
507                         disabledByAdmin = adminEnforcedQuality
508                                 > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
509                     } else if (KEY_UNLOCK_SET_NONE.equals(key)) {
510                         if (mUserId != UserHandle.myUserId()) {
511                             // Swipe doesn't make sense for profiles.
512                             visible = false;
513                         }
514                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
515                         disabledByAdmin = adminEnforcedQuality
516                                 > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
517                     } else if (KEY_UNLOCK_SET_PATTERN.equals(key)) {
518                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
519                         disabledByAdmin = adminEnforcedQuality
520                                 > DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
521                     } else if (KEY_UNLOCK_SET_PIN.equals(key)) {
522                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
523                         disabledByAdmin = adminEnforcedQuality
524                                 > DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
525                     } else if (KEY_UNLOCK_SET_PASSWORD.equals(key)) {
526                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
527                         disabledByAdmin = adminEnforcedQuality
528                                 > DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
529                     } else if (KEY_UNLOCK_SET_MANAGED.equals(key)) {
530                         enabled = quality <= DevicePolicyManager.PASSWORD_QUALITY_MANAGED
531                                 && mManagedPasswordProvider.isManagedPasswordChoosable();
532                         disabledByAdmin = adminEnforcedQuality
533                                 > DevicePolicyManager.PASSWORD_QUALITY_MANAGED;
534                     }
535                     if (hideDisabled) {
536                         visible = enabled;
537                     }
538                     if (!visible) {
539                         entries.removePreference(pref);
540                     } else if (disabledByAdmin && enforcedAdmin != null) {
541                         ((RestrictedPreference) pref).setDisabledByAdmin(enforcedAdmin);
542                     } else if (!enabled) {
543                         // we need to setDisabledByAdmin to null first to disable the padlock
544                         // in case it was set earlier.
545                         ((RestrictedPreference) pref).setDisabledByAdmin(null);
546                         pref.setSummary(R.string.unlock_set_unlock_disabled_summary);
547                         pref.setEnabled(false);
548                     } else {
549                         ((RestrictedPreference) pref).setDisabledByAdmin(null);
550                     }
551                 }
552             }
553         }
554
555         private void updatePreferenceSummaryIfNeeded() {
556             // On a default block encrypted device with accessibility, add a warning
557             // that your data is not credential encrypted
558             if (!StorageManager.isBlockEncrypted()) {
559                 return;
560             }
561
562             if (StorageManager.isNonDefaultBlockEncrypted()) {
563                 return;
564             }
565
566             if (AccessibilityManager.getInstance(getActivity()).getEnabledAccessibilityServiceList(
567                     AccessibilityServiceInfo.FEEDBACK_ALL_MASK).isEmpty()) {
568                 return;
569             }
570
571             CharSequence summary = getString(R.string.secure_lock_encryption_warning);
572
573             PreferenceScreen screen = getPreferenceScreen();
574             final int preferenceCount = screen.getPreferenceCount();
575             for (int i = 0; i < preferenceCount; i++) {
576                 Preference preference = screen.getPreference(i);
577                 switch (preference.getKey()) {
578                     case KEY_UNLOCK_SET_PATTERN:
579                     case KEY_UNLOCK_SET_PIN:
580                     case KEY_UNLOCK_SET_PASSWORD:
581                     case KEY_UNLOCK_SET_MANAGED: {
582                         preference.setSummary(summary);
583                     } break;
584                 }
585             }
586         }
587
588         protected Intent getLockManagedPasswordIntent(boolean requirePassword, String password) {
589             return mManagedPasswordProvider.createIntent(requirePassword, password);
590         }
591
592         protected Intent getLockPasswordIntent(Context context, int quality,
593                 int minLength, final int maxLength,
594                 boolean requirePasswordToDecrypt, boolean confirmCredentials, int userId) {
595             return ChooseLockPassword.createIntent(context, quality, minLength,
596                     maxLength, requirePasswordToDecrypt, confirmCredentials, userId);
597         }
598
599         protected Intent getLockPasswordIntent(Context context, int quality,
600                 int minLength, final int maxLength,
601                 boolean requirePasswordToDecrypt, long challenge, int userId) {
602             return ChooseLockPassword.createIntent(context, quality, minLength,
603                     maxLength, requirePasswordToDecrypt, challenge, userId);
604         }
605
606         protected Intent getLockPasswordIntent(Context context, int quality, int minLength,
607                 int maxLength, boolean requirePasswordToDecrypt, String password, int userId) {
608             return ChooseLockPassword.createIntent(context, quality, minLength, maxLength,
609                     requirePasswordToDecrypt, password, userId);
610         }
611
612         protected Intent getLockPatternIntent(Context context, final boolean requirePassword,
613                 final boolean confirmCredentials, int userId) {
614             return ChooseLockPattern.createIntent(context, requirePassword,
615                     confirmCredentials, userId);
616         }
617
618         protected Intent getLockPatternIntent(Context context, final boolean requirePassword,
619                long challenge, int userId) {
620             return ChooseLockPattern.createIntent(context, requirePassword, challenge, userId);
621         }
622
623         protected Intent getLockPatternIntent(Context context, final boolean requirePassword,
624                 final String pattern, int userId) {
625             return ChooseLockPattern.createIntent(context, requirePassword, pattern, userId);
626         }
627
628         protected Intent getEncryptionInterstitialIntent(Context context, int quality,
629                 boolean required, Intent unlockMethodIntent) {
630             return EncryptionInterstitial.createStartIntent(context, quality, required,
631                     unlockMethodIntent);
632         }
633
634         /**
635          * Invokes an activity to change the user's pattern, password or PIN based on given quality
636          * and minimum quality specified by DevicePolicyManager. If quality is
637          * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}, password is cleared.
638          *
639          * @param quality the desired quality. Ignored if DevicePolicyManager requires more security
640          * @param disabled whether or not to show LockScreen at all. Only meaningful when quality is
641          * {@link DevicePolicyManager#PASSWORD_QUALITY_UNSPECIFIED}
642          */
643         void updateUnlockMethodAndFinish(int quality, boolean disabled) {
644             // Sanity check. We should never get here without confirming user's existing password.
645             if (!mPasswordConfirmed) {
646                 throw new IllegalStateException("Tried to update password without confirming it");
647             }
648
649             quality = upgradeQuality(quality);
650             Intent intent = getIntentForUnlockMethod(quality, disabled);
651             if (intent != null) {
652                 startActivityForResult(intent,
653                         mIsSetNewPassword && mHasChallenge
654                                 ? CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST
655                                 : CHOOSE_LOCK_REQUEST);
656                 return;
657             }
658
659             if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
660                 mLockPatternUtils.setSeparateProfileChallengeEnabled(mUserId, true, mUserPassword);
661                 mChooseLockSettingsHelper.utils().clearLock(mUserId);
662                 mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled, mUserId);
663                 getActivity().setResult(Activity.RESULT_OK);
664                 removeAllFingerprintForUserAndFinish(mUserId);
665             } else {
666                 removeAllFingerprintForUserAndFinish(mUserId);
667             }
668         }
669
670         private Intent getIntentForUnlockMethod(int quality, boolean disabled) {
671             Intent intent = null;
672             final Context context = getActivity();
673             if (quality >= DevicePolicyManager.PASSWORD_QUALITY_MANAGED) {
674                 intent = getLockManagedPasswordIntent(mRequirePassword, mUserPassword);
675             } else if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
676                 int minLength = mDPM.getPasswordMinimumLength(null, mUserId);
677                 if (minLength < MIN_PASSWORD_LENGTH) {
678                     minLength = MIN_PASSWORD_LENGTH;
679                 }
680                 final int maxLength = mDPM.getPasswordMaximumLength(quality);
681                 if (mHasChallenge) {
682                     intent = getLockPasswordIntent(context, quality, minLength,
683                             maxLength, mRequirePassword, mChallenge, mUserId);
684                 } else {
685                     intent = getLockPasswordIntent(context, quality, minLength,
686                             maxLength, mRequirePassword, mUserPassword, mUserId);
687                 }
688             } else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
689                 if (mHasChallenge) {
690                     intent = getLockPatternIntent(context, mRequirePassword,
691                             mChallenge, mUserId);
692                 } else {
693                     intent = getLockPatternIntent(context, mRequirePassword,
694                             mUserPassword, mUserId);
695                 }
696             }
697             if (intent != null) {
698                 intent.putExtra(EXTRA_HIDE_DRAWER, mHideDrawer);
699             }
700             return intent;
701         }
702
703         private void removeAllFingerprintForUserAndFinish(final int userId) {
704             if (mFingerprintManager != null && mFingerprintManager.isHardwareDetected()) {
705                 if (mFingerprintManager.hasEnrolledFingerprints(userId)) {
706                     mFingerprintManager.setActiveUser(userId);
707                     // For the purposes of M and N, groupId is the same as userId.
708                     final int groupId = userId;
709                     Fingerprint finger = new Fingerprint(null, groupId, 0, 0);
710                     mFingerprintManager.remove(finger, userId,
711                             new RemovalCallback() {
712                                 @Override
713                                 public void onRemovalError(Fingerprint fp, int errMsgId,
714                                         CharSequence errString) {
715                                     Log.v(TAG, "Fingerprint removed: " + fp.getFingerId());
716                                     if (fp.getFingerId() == 0) {
717                                         removeManagedProfileFingerprintsAndFinishIfNecessary(userId);
718                                     }
719                                 }
720
721                                 @Override
722                                 public void onRemovalSucceeded(Fingerprint fingerprint) {
723                                     if (fingerprint.getFingerId() == 0) {
724                                         removeManagedProfileFingerprintsAndFinishIfNecessary(userId);
725                                     }
726                                 }
727                             });
728                 } else {
729                     // No fingerprints in this user, we may also want to delete managed profile
730                     // fingerprints
731                     removeManagedProfileFingerprintsAndFinishIfNecessary(userId);
732                 }
733             } else {
734                 // The removal callback will call finish, once all fingerprints are removed.
735                 // We need to wait for that to occur, otherwise, the UI will still show that
736                 // fingerprints exist even though they are (about to) be removed depending on
737                 // the race condition.
738                 finish();
739             }
740         }
741
742         private void removeManagedProfileFingerprintsAndFinishIfNecessary(final int parentUserId) {
743             mFingerprintManager.setActiveUser(UserHandle.myUserId());
744             final UserManager um = UserManager.get(getActivity());
745             boolean hasChildProfile = false;
746             if (!um.getUserInfo(parentUserId).isManagedProfile()) {
747                 // Current user is primary profile, remove work profile fingerprints if necessary
748                 final List<UserInfo> profiles = um.getProfiles(parentUserId);
749                 final int profilesSize = profiles.size();
750                 for (int i = 0; i < profilesSize; i++) {
751                     final UserInfo userInfo = profiles.get(i);
752                     if (userInfo.isManagedProfile() && !mLockPatternUtils
753                             .isSeparateProfileChallengeEnabled(userInfo.id)) {
754                         removeAllFingerprintForUserAndFinish(userInfo.id);
755                         hasChildProfile = true;
756                         break;
757                     }
758                 }
759             }
760             if (!hasChildProfile) {
761                 finish();
762             }
763         }
764
765         @Override
766         public void onDestroy() {
767             super.onDestroy();
768         }
769
770         @Override
771         protected int getHelpResource() {
772             return R.string.help_url_choose_lockscreen;
773         }
774
775         private int getResIdForFactoryResetProtectionWarningTitle() {
776             boolean isProfile = Utils.isManagedProfile(UserManager.get(getActivity()), mUserId);
777             return isProfile ? R.string.unlock_disable_frp_warning_title_profile
778                     : R.string.unlock_disable_frp_warning_title;
779         }
780
781         private int getResIdForFactoryResetProtectionWarningMessage() {
782             boolean hasFingerprints = mFingerprintManager.hasEnrolledFingerprints(mUserId);
783             boolean isProfile = Utils.isManagedProfile(UserManager.get(getActivity()), mUserId);
784             switch (mLockPatternUtils.getKeyguardStoredPasswordQuality(mUserId)) {
785                 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
786                     if (hasFingerprints && isProfile) {
787                         return R.string
788                                 .unlock_disable_frp_warning_content_pattern_fingerprint_profile;
789                     } else if (hasFingerprints && !isProfile) {
790                         return R.string.unlock_disable_frp_warning_content_pattern_fingerprint;
791                     } else if (isProfile) {
792                         return R.string.unlock_disable_frp_warning_content_pattern_profile;
793                     } else {
794                         return R.string.unlock_disable_frp_warning_content_pattern;
795                     }
796                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
797                 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
798                     if (hasFingerprints && isProfile) {
799                         return R.string.unlock_disable_frp_warning_content_pin_fingerprint_profile;
800                     } else if (hasFingerprints && !isProfile) {
801                         return R.string.unlock_disable_frp_warning_content_pin_fingerprint;
802                     } else if (isProfile) {
803                         return R.string.unlock_disable_frp_warning_content_pin_profile;
804                     } else {
805                         return R.string.unlock_disable_frp_warning_content_pin;
806                     }
807                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
808                 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
809                 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
810                 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
811                     if (hasFingerprints && isProfile) {
812                         return R.string
813                                 .unlock_disable_frp_warning_content_password_fingerprint_profile;
814                     } else if (hasFingerprints && !isProfile) {
815                         return R.string.unlock_disable_frp_warning_content_password_fingerprint;
816                     } else if (isProfile) {
817                         return R.string.unlock_disable_frp_warning_content_password_profile;
818                     } else {
819                         return R.string.unlock_disable_frp_warning_content_password;
820                     }
821                 default:
822                     if (hasFingerprints && isProfile) {
823                         return R.string
824                                 .unlock_disable_frp_warning_content_unknown_fingerprint_profile;
825                     } else if (hasFingerprints && !isProfile) {
826                         return R.string.unlock_disable_frp_warning_content_unknown_fingerprint;
827                     } else if (isProfile) {
828                         return R.string.unlock_disable_frp_warning_content_unknown_profile;
829                     } else {
830                         return R.string.unlock_disable_frp_warning_content_unknown;
831                     }
832             }
833         }
834
835         private boolean isUnlockMethodSecure(String unlockMethod) {
836             return !(KEY_UNLOCK_SET_OFF.equals(unlockMethod) ||
837                     KEY_UNLOCK_SET_NONE.equals(unlockMethod));
838         }
839
840         private boolean setUnlockMethod(String unlockMethod) {
841             EventLog.writeEvent(EventLogTags.LOCK_SCREEN_TYPE, unlockMethod);
842
843             if (KEY_UNLOCK_SET_OFF.equals(unlockMethod)) {
844                 updateUnlockMethodAndFinish(
845                         DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, true /* disabled */ );
846             } else if (KEY_UNLOCK_SET_NONE.equals(unlockMethod)) {
847                 updateUnlockMethodAndFinish(
848                         DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, false /* disabled */ );
849             } else if (KEY_UNLOCK_SET_MANAGED.equals(unlockMethod)) {
850                 maybeEnableEncryption(DevicePolicyManager.PASSWORD_QUALITY_MANAGED, false);
851             } else if (KEY_UNLOCK_SET_PATTERN.equals(unlockMethod)) {
852                 maybeEnableEncryption(
853                         DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, false);
854             } else if (KEY_UNLOCK_SET_PIN.equals(unlockMethod)) {
855                 maybeEnableEncryption(
856                         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, false);
857             } else if (KEY_UNLOCK_SET_PASSWORD.equals(unlockMethod)) {
858                 maybeEnableEncryption(
859                         DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC, false);
860             } else {
861                 Log.e(TAG, "Encountered unknown unlock method to set: " + unlockMethod);
862                 return false;
863             }
864             return true;
865         }
866
867         private void showFactoryResetProtectionWarningDialog(String unlockMethodToSet) {
868             int title = getResIdForFactoryResetProtectionWarningTitle();
869             int message = getResIdForFactoryResetProtectionWarningMessage();
870             FactoryResetProtectionWarningDialog dialog =
871                     FactoryResetProtectionWarningDialog.newInstance(
872                             title, message, unlockMethodToSet);
873             dialog.show(getChildFragmentManager(), TAG_FRP_WARNING_DIALOG);
874         }
875
876         public static class FactoryResetProtectionWarningDialog extends DialogFragment {
877
878             private static final String ARG_TITLE_RES = "titleRes";
879             private static final String ARG_MESSAGE_RES = "messageRes";
880             private static final String ARG_UNLOCK_METHOD_TO_SET = "unlockMethodToSet";
881
882             public static FactoryResetProtectionWarningDialog newInstance(
883                     int titleRes, int messageRes, String unlockMethodToSet) {
884                 FactoryResetProtectionWarningDialog frag =
885                         new FactoryResetProtectionWarningDialog();
886                 Bundle args = new Bundle();
887                 args.putInt(ARG_TITLE_RES, titleRes);
888                 args.putInt(ARG_MESSAGE_RES, messageRes);
889                 args.putString(ARG_UNLOCK_METHOD_TO_SET, unlockMethodToSet);
890                 frag.setArguments(args);
891                 return frag;
892             }
893
894             @Override
895             public void show(FragmentManager manager, String tag) {
896                 if (manager.findFragmentByTag(tag) == null) {
897                     // Prevent opening multiple dialogs if tapped on button quickly
898                     super.show(manager, tag);
899                 }
900             }
901
902             @Override
903             public Dialog onCreateDialog(Bundle savedInstanceState) {
904                 final Bundle args = getArguments();
905
906                 return new AlertDialog.Builder(getActivity())
907                         .setTitle(args.getInt(ARG_TITLE_RES))
908                         .setMessage(args.getInt(ARG_MESSAGE_RES))
909                         .setPositiveButton(R.string.unlock_disable_frp_warning_ok,
910                                 new DialogInterface.OnClickListener() {
911                                     @Override
912                                     public void onClick(DialogInterface dialog, int whichButton) {
913                                         ((ChooseLockGenericFragment) getParentFragment())
914                                                 .setUnlockMethod(
915                                                         args.getString(ARG_UNLOCK_METHOD_TO_SET));
916                                     }
917                                 }
918                         )
919                         .setNegativeButton(R.string.cancel,
920                                 new DialogInterface.OnClickListener() {
921                                     @Override
922                                     public void onClick(DialogInterface dialog, int whichButton) {
923                                         dismiss();
924                                     }
925                                 }
926                         )
927                         .create();
928             }
929         }
930     }
931 }