1 /* 2 * Copyright (C) 2016 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.password; 18 19 import static com.android.internal.util.Preconditions.checkNotNull; 20 21 import android.annotation.Nullable; 22 import android.app.admin.DevicePolicyManager; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.hardware.fingerprint.FingerprintManager; 27 import android.os.Bundle; 28 import android.os.UserHandle; 29 30 import com.android.internal.annotations.VisibleForTesting; 31 import com.android.settings.ChooseLockGeneric; 32 import com.android.settings.ChooseLockSettingsHelper; 33 34 /** 35 * Business logic for {@link SetNewPasswordActivity}. 36 * 37 * <p>On devices that supports fingerprint, this controller directs the user to configure 38 * fingerprint + a backup password if the device admin allows fingerprint for keyguard and 39 * the user has never configured a fingerprint before. 40 */ 41 final class SetNewPasswordController { 42 43 interface Ui { 44 /** Starts the {@link ChooseLockGeneric} activity with the given extras. */ 45 void launchChooseLock(@Nullable Bundle chooseLockFingerprintExtras); 46 } 47 48 private final int mCurrentUserId; 49 private final PackageManager mPackageManager; 50 @Nullable private final FingerprintManager mFingerprintManager; 51 private final DevicePolicyManager mDevicePolicyManager; 52 private final Ui mUi; 53 54 public SetNewPasswordController(Context context, Ui ui) { 55 this(context.getUserId(), 56 context.getPackageManager(), 57 (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE), 58 (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE), 59 ui); 60 } 61 62 @VisibleForTesting 63 SetNewPasswordController( 64 int currentUserId, 65 PackageManager packageManager, 66 FingerprintManager fingerprintManager, 67 DevicePolicyManager devicePolicyManager, 68 Ui ui) { 69 mCurrentUserId = currentUserId; 70 mPackageManager = checkNotNull(packageManager); 71 mFingerprintManager = fingerprintManager; 72 mDevicePolicyManager = checkNotNull(devicePolicyManager); 73 mUi = checkNotNull(ui); 74 } 75 76 /** 77 * Dispatches the set new password intent to the correct activity that handles it. 78 */ 79 public void dispatchSetNewPasswordIntent() { 80 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT) 81 && mFingerprintManager != null 82 && mFingerprintManager.isHardwareDetected() 83 && !mFingerprintManager.hasEnrolledFingerprints() 84 && !isFingerprintDisabledByAdmin()) { 85 mUi.launchChooseLock(getFingerprintChooseLockExtras()); 86 } else { 87 mUi.launchChooseLock(null); 88 } 89 } 90 91 private Bundle getFingerprintChooseLockExtras() { 92 Bundle chooseLockExtras = new Bundle(); 93 if (mFingerprintManager != null) { 94 long challenge = mFingerprintManager.preEnroll(); 95 chooseLockExtras.putInt(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY, 96 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); 97 chooseLockExtras.putBoolean( 98 ChooseLockGeneric.ChooseLockGenericFragment.HIDE_DISABLED_PREFS, true); 99 chooseLockExtras.putBoolean(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true); 100 chooseLockExtras.putLong(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge); 101 chooseLockExtras.putBoolean(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, true); 102 if (mCurrentUserId != UserHandle.USER_NULL) { 103 chooseLockExtras.putInt(Intent.EXTRA_USER_ID, mCurrentUserId); 104 } 105 } 106 return chooseLockExtras; 107 } 108 109 private boolean isFingerprintDisabledByAdmin() { 110 int disabledFeatures = mDevicePolicyManager.getKeyguardDisabledFeatures( 111 null, mCurrentUserId); 112 return (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0; 113 } 114 } 115