@ -36,10 +36,51 @@ import java.util.concurrent.Executors
@Suppress ( " TooManyFunctions " )
class LoginsFragment : PreferenceFragmentCompat ( ) , AccountObserver {
@TargetApi ( M )
private lateinit var biometricPromptCallback : BiometricPrompt . AuthenticationCallback
@TargetApi ( M )
private val executor = Executors . newSingleThreadExecutor ( )
@TargetApi ( M )
private lateinit var biometricPrompt : BiometricPrompt
@TargetApi ( M )
private lateinit var promptInfo : BiometricPrompt . PromptInfo
override fun onCreatePreferences ( savedInstanceState : Bundle ? , rootKey : String ? ) {
setPreferencesFromResource ( R . xml . logins _preferences , rootKey )
}
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
biometricPromptCallback = object : BiometricPrompt . AuthenticationCallback ( ) {
override fun onAuthenticationError ( errorCode : Int , errString : CharSequence ) {
// Authentication Error
}
override fun onAuthenticationSucceeded ( result : BiometricPrompt . AuthenticationResult ) {
lifecycleScope . launch ( Main ) {
navigateToSavedLoginsFragment ( )
}
}
override fun onAuthenticationFailed ( ) {
// Authenticated Failed
}
}
biometricPrompt = BiometricPrompt ( this , executor , biometricPromptCallback )
promptInfo = BiometricPrompt . PromptInfo . Builder ( )
. setTitle ( getString ( R . string . logins _biometric _prompt _message ) )
. setDeviceCredentialAllowed ( true )
. build ( )
}
override fun onResume ( ) {
super . onResume ( )
activity ?. title = getString ( R . string . preferences _passwords _logins _and _passwords )
@ -48,7 +89,7 @@ class LoginsFragment : PreferenceFragmentCompat(), AccountObserver {
val savedLoginsKey = getPreferenceKey ( R . string . pref _key _saved _logins )
findPreference < Preference > ( savedLoginsKey ) ?. setOnPreferenceClickListener {
if ( Build . VERSION . SDK _INT >= M && isHardwareAvailable && hasBiometricEnrolled ) {
showBiometricPrompt( )
biometricPrompt. authenticate ( promptInfo )
} else {
verifyPinOrShowSetupWarning ( )
}
@ -137,37 +178,6 @@ class LoginsFragment : PreferenceFragmentCompat(), AccountObserver {
}
}
@TargetApi ( M )
private fun showBiometricPrompt ( ) {
val biometricPromptCallback = object : BiometricPrompt . AuthenticationCallback ( ) {
override fun onAuthenticationError ( errorCode : Int , errString : CharSequence ) {
// Authentication Error
}
override fun onAuthenticationSucceeded ( result : BiometricPrompt . AuthenticationResult ) {
lifecycleScope . launch ( Main ) {
navigateToSavedLoginsFragment ( )
}
}
override fun onAuthenticationFailed ( ) {
// Authenticated Failed
}
}
val executor = Executors . newSingleThreadExecutor ( )
val biometricPrompt = BiometricPrompt ( this , executor , biometricPromptCallback )
val promptInfo = BiometricPrompt . PromptInfo . Builder ( )
. setTitle ( getString ( R . string . logins _biometric _prompt _message ) )
. setDeviceCredentialAllowed ( true )
. build ( )
biometricPrompt . authenticate ( promptInfo )
}
private fun verifyPinOrShowSetupWarning ( ) {
val manager = activity ?. getSystemService ( KEYGUARD _SERVICE ) as KeyguardManager
if ( manager . isKeyguardSecure ) {