2019-10-24 18:29:41 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2020-07-17 00:08:04 +02:00
|
|
|
package org.mozilla.fenix.settings.logins.fragment
|
2019-10-24 18:29:41 +02:00
|
|
|
|
2019-10-30 01:14:12 +01:00
|
|
|
import android.app.Activity.RESULT_OK
|
|
|
|
import android.app.KeyguardManager
|
2020-07-20 22:25:24 +02:00
|
|
|
import android.content.Context
|
2019-10-30 01:14:12 +01:00
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.content.Intent
|
2020-07-20 22:25:24 +02:00
|
|
|
import android.os.Build.VERSION.SDK_INT
|
2019-10-30 01:14:12 +01:00
|
|
|
import android.os.Build.VERSION_CODES.M
|
2019-10-24 18:29:41 +02:00
|
|
|
import android.os.Bundle
|
2020-07-20 22:25:24 +02:00
|
|
|
import android.provider.Settings.ACTION_SECURITY_SETTINGS
|
2020-02-14 18:38:06 +01:00
|
|
|
import android.util.Log
|
2019-10-30 01:14:12 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.biometric.BiometricManager
|
|
|
|
import androidx.biometric.BiometricPrompt
|
2020-07-07 21:58:11 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2020-07-09 19:50:51 +02:00
|
|
|
import androidx.core.content.getSystemService
|
2019-10-30 01:14:12 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-10-24 18:29:41 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2020-01-14 01:18:41 +01:00
|
|
|
import androidx.preference.SwitchPreference
|
2019-10-30 01:14:12 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2020-02-14 18:38:06 +01:00
|
|
|
import kotlinx.coroutines.delay
|
2019-10-30 01:14:12 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2019-10-24 18:29:41 +02:00
|
|
|
import org.mozilla.fenix.R
|
2020-07-07 21:58:11 +02:00
|
|
|
import org.mozilla.fenix.addons.runIfFragmentIsAttached
|
2019-11-13 00:55:36 +01:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
|
|
|
import org.mozilla.fenix.ext.components
|
2019-10-24 18:29:41 +02:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2020-04-23 09:12:41 +02:00
|
|
|
import org.mozilla.fenix.ext.secure
|
2019-10-30 01:14:12 +01:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2019-11-25 21:36:47 +01:00
|
|
|
import org.mozilla.fenix.ext.showToolbar
|
2020-05-13 00:32:01 +02:00
|
|
|
import org.mozilla.fenix.settings.SharedPreferenceUpdater
|
2020-07-20 22:25:24 +02:00
|
|
|
import org.mozilla.fenix.settings.logins.SyncLoginsPreferenceView
|
2020-06-15 20:24:14 +02:00
|
|
|
import org.mozilla.fenix.settings.requirePreference
|
2019-10-24 18:29:41 +02:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
@Suppress("TooManyFunctions")
|
|
|
|
class SavedLoginsAuthFragment : PreferenceFragmentCompat() {
|
2019-11-07 22:17:37 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
private lateinit var biometricPrompt: BiometricPrompt
|
|
|
|
private lateinit var promptInfo: BiometricPrompt.PromptInfo
|
2019-11-07 22:17:37 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
private val biometricPromptCallback = object : BiometricPrompt.AuthenticationCallback() {
|
2019-11-07 22:17:37 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
|
|
|
Log.e(LOG_TAG, "onAuthenticationError $errString")
|
|
|
|
togglePrefsEnabledWhileAuthenticating(enabled = true)
|
|
|
|
}
|
2019-11-07 22:17:37 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
|
|
|
Log.d(LOG_TAG, "onAuthenticationSucceeded")
|
|
|
|
navigateToSavedLogins()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onAuthenticationFailed() {
|
|
|
|
Log.e(LOG_TAG, "onAuthenticationFailed")
|
|
|
|
togglePrefsEnabledWhileAuthenticating(enabled = true)
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 22:17:37 +01:00
|
|
|
|
2019-10-24 18:29:41 +02:00
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
setPreferencesFromResource(R.xml.logins_preferences, rootKey)
|
|
|
|
}
|
|
|
|
|
2020-07-07 21:58:11 +02:00
|
|
|
/**
|
|
|
|
* There is a bug where while the biometric prompt is showing, you were able to quickly navigate
|
|
|
|
* so we are disabling the settings that navigate while authenticating.
|
|
|
|
* https://github.com/mozilla-mobile/fenix/issues/12312
|
|
|
|
*/
|
|
|
|
private fun togglePrefsEnabledWhileAuthenticating(enabled: Boolean) {
|
|
|
|
requirePreference<Preference>(R.string.pref_key_password_sync_logins).isEnabled = enabled
|
|
|
|
requirePreference<Preference>(R.string.pref_key_save_logins_settings).isEnabled = enabled
|
|
|
|
requirePreference<Preference>(R.string.pref_key_saved_logins).isEnabled = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun navigateToSavedLogins() {
|
|
|
|
runIfFragmentIsAttached {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch(Main) {
|
|
|
|
// Workaround for likely biometric library bug
|
|
|
|
// https://github.com/mozilla-mobile/fenix/issues/8438
|
|
|
|
delay(SHORT_DELAY_MS)
|
|
|
|
navigateToSavedLoginsFragment()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 22:17:37 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
val executor = ContextCompat.getMainExecutor(requireContext())
|
2020-07-07 21:58:11 +02:00
|
|
|
|
2019-11-07 22:17:37 +01:00
|
|
|
biometricPrompt = BiometricPrompt(this, executor, biometricPromptCallback)
|
|
|
|
|
|
|
|
promptInfo = BiometricPrompt.PromptInfo.Builder()
|
|
|
|
.setTitle(getString(R.string.logins_biometric_prompt_message))
|
|
|
|
.setDeviceCredentialAllowed(true)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:29:41 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-11-25 21:36:47 +01:00
|
|
|
showToolbar(getString(R.string.preferences_passwords_logins_and_passwords))
|
2019-10-24 18:29:41 +02:00
|
|
|
|
2020-06-15 20:24:14 +02:00
|
|
|
requirePreference<Preference>(R.string.pref_key_save_logins_settings).apply {
|
2020-01-14 01:18:41 +01:00
|
|
|
summary = getString(
|
|
|
|
if (context.settings().shouldPromptToSaveLogins)
|
|
|
|
R.string.preferences_passwords_save_logins_ask_to_save else
|
|
|
|
R.string.preferences_passwords_save_logins_never_save
|
|
|
|
)
|
|
|
|
setOnPreferenceClickListener {
|
|
|
|
navigateToSaveLoginSettingFragment()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:37:03 +02:00
|
|
|
requirePreference<Preference>(R.string.pref_key_login_exceptions).apply {
|
|
|
|
setOnPreferenceClickListener {
|
|
|
|
navigateToLoginExceptionFragment()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:24:14 +02:00
|
|
|
requirePreference<SwitchPreference>(R.string.pref_key_autofill_logins).apply {
|
2020-06-06 03:06:30 +02:00
|
|
|
isChecked = context.settings().shouldAutofillLogins
|
|
|
|
onPreferenceChangeListener = object : SharedPreferenceUpdater() {
|
|
|
|
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
|
2020-07-20 22:25:24 +02:00
|
|
|
context.components.core.engine.settings.loginAutofillEnabled =
|
2020-06-06 03:06:30 +02:00
|
|
|
newValue as Boolean
|
|
|
|
return super.onPreferenceChange(preference, newValue)
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 01:18:41 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 20:24:14 +02:00
|
|
|
requirePreference<Preference>(R.string.pref_key_saved_logins).setOnPreferenceClickListener {
|
2020-07-20 22:25:24 +02:00
|
|
|
verifyCredentialsOrShowSetupWarning(it.context)
|
2019-10-24 18:29:41 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
SyncLoginsPreferenceView(
|
|
|
|
requirePreference(R.string.pref_key_password_sync_logins),
|
|
|
|
lifecycleOwner = viewLifecycleOwner,
|
|
|
|
accountManager = requireComponents.backgroundServices.accountManager,
|
|
|
|
navController = findNavController()
|
|
|
|
)
|
2020-07-17 14:19:50 +02:00
|
|
|
|
|
|
|
togglePrefsEnabledWhileAuthenticating(enabled = true)
|
2019-10-24 18:29:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
private fun canUseBiometricPrompt(context: Context): Boolean {
|
|
|
|
return if (SDK_INT >= M) {
|
|
|
|
val manager = BiometricManager.from(context)
|
|
|
|
val canAuthenticate = manager.canAuthenticate()
|
2019-10-24 18:29:41 +02:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
val hardwareUnavailable = canAuthenticate == BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ||
|
|
|
|
canAuthenticate == BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE
|
|
|
|
val biometricsEnrolled = canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS
|
2019-10-24 18:29:41 +02:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
!hardwareUnavailable && biometricsEnrolled
|
2020-03-02 22:36:05 +01:00
|
|
|
} else {
|
|
|
|
false
|
2019-10-30 01:14:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
private fun verifyCredentialsOrShowSetupWarning(context: Context) {
|
|
|
|
// Use the BiometricPrompt first
|
|
|
|
if (canUseBiometricPrompt(context)) {
|
|
|
|
biometricPrompt.authenticate(promptInfo)
|
|
|
|
return
|
2019-10-30 01:14:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
// Fallback to prompting for password with the KeyguardManager
|
|
|
|
val manager = context.getSystemService<KeyguardManager>()
|
|
|
|
if (manager?.isKeyguardSecure == true) {
|
|
|
|
showPinVerification(manager)
|
2019-10-30 01:14:12 +01:00
|
|
|
} else {
|
2020-07-20 22:25:24 +02:00
|
|
|
// Warn that the device has not been secured
|
|
|
|
if (context.settings().shouldShowSecurityPinWarning) {
|
|
|
|
showPinDialogWarning(context)
|
2019-10-30 01:14:12 +01:00
|
|
|
} else {
|
|
|
|
navigateToSavedLoginsFragment()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
private fun showPinDialogWarning(context: Context) {
|
|
|
|
AlertDialog.Builder(context).apply {
|
|
|
|
setTitle(getString(R.string.logins_warning_dialog_title))
|
|
|
|
setMessage(
|
|
|
|
getString(R.string.logins_warning_dialog_message)
|
|
|
|
)
|
2019-10-30 01:14:12 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
setNegativeButton(getString(R.string.logins_warning_dialog_later)) { _: DialogInterface, _ ->
|
|
|
|
navigateToSavedLoginsFragment()
|
|
|
|
}
|
2019-10-30 01:14:12 +01:00
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
setPositiveButton(getString(R.string.logins_warning_dialog_set_up_now)) { it: DialogInterface, _ ->
|
|
|
|
it.dismiss()
|
|
|
|
val intent = Intent(ACTION_SECURITY_SETTINGS)
|
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}.show().secure(activity)
|
|
|
|
context.settings().incrementShowLoginsSecureWarningCount()
|
2019-10-30 01:14:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
@Suppress("Deprecation") // This is only used when BiometricPrompt is unavailable
|
|
|
|
private fun showPinVerification(manager: KeyguardManager) {
|
2019-10-30 01:14:12 +01:00
|
|
|
val intent = manager.createConfirmDeviceCredentialIntent(
|
|
|
|
getString(R.string.logins_biometric_prompt_message_pin),
|
|
|
|
getString(R.string.logins_biometric_prompt_message)
|
|
|
|
)
|
2020-07-20 22:25:24 +02:00
|
|
|
startActivityForResult(intent, PIN_REQUEST)
|
2019-10-30 01:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
if (requestCode == PIN_REQUEST && resultCode == RESULT_OK) {
|
|
|
|
navigateToSavedLoginsFragment()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 22:25:24 +02:00
|
|
|
/**
|
|
|
|
* Called when authentication succeeds.
|
|
|
|
*/
|
2019-10-30 01:14:12 +01:00
|
|
|
private fun navigateToSavedLoginsFragment() {
|
2020-01-14 01:18:41 +01:00
|
|
|
context?.components?.analytics?.metrics?.track(Event.OpenLogins)
|
2020-07-07 21:58:11 +02:00
|
|
|
val directions =
|
|
|
|
SavedLoginsAuthFragmentDirections.actionSavedLoginsAuthFragmentToLoginsListFragment()
|
2020-01-14 01:18:41 +01:00
|
|
|
findNavController().navigate(directions)
|
2019-10-24 18:29:41 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 01:18:41 +01:00
|
|
|
private fun navigateToSaveLoginSettingFragment() {
|
|
|
|
val directions =
|
2020-05-13 00:32:01 +02:00
|
|
|
SavedLoginsAuthFragmentDirections.actionSavedLoginsAuthFragmentToSavedLoginsSettingFragment()
|
2020-01-14 01:18:41 +01:00
|
|
|
findNavController().navigate(directions)
|
2019-10-24 18:29:41 +02:00
|
|
|
}
|
2019-10-30 01:14:12 +01:00
|
|
|
|
2020-07-02 04:37:03 +02:00
|
|
|
private fun navigateToLoginExceptionFragment() {
|
|
|
|
val directions =
|
|
|
|
SavedLoginsAuthFragmentDirections.actionSavedLoginsAuthFragmentToLoginExceptionsFragment()
|
|
|
|
findNavController().navigate(directions)
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:14:12 +01:00
|
|
|
companion object {
|
2020-02-14 18:38:06 +01:00
|
|
|
const val SHORT_DELAY_MS = 100L
|
|
|
|
private const val LOG_TAG = "LoginsFragment"
|
2019-10-30 01:14:12 +01:00
|
|
|
const val PIN_REQUEST = 303
|
|
|
|
}
|
2019-10-24 18:29:41 +02:00
|
|
|
}
|