You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iceraven-browser/app/src/main/java/org/mozilla/fenix/settings/creditcards/CreditCardsSettingFragment.kt

250 lines
9.5 KiB
Kotlin

/* 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/. */
package org.mozilla.fenix.settings.creditcards
import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.service.fxa.SyncEngine
import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.R
import org.mozilla.fenix.components.StoreProvider
16900 make navgraph inflation asynchronous (#18889) * For #16900: implement async navgraph inflation For #16900: removed nav graph from xml For #16900: inflate navGraph programatically For #16900: Made NavGraph inflation asynchronous For #16900: Changed to block with runBlocking For #16900: Refactored blocking call into a function For 16900: NavGraph inflation is now async We now attach the nav graph (or check if its attached) on every nav call ( an extension function for NavController). This is done by checking the value of the job stored in PerfNavController.map which keeps track of the job with the NavController as a Key. If the job hasn't been completed, it will block the main thread until the job is done. The job itself is responsible for attaching the navgraph to the navcontroller (and the inflation of the latter too) For 16900: rebased upstream master For 16900: Rebase on master For #16900: Fixed Async Navgraph navigation per review comments. 1)The Asynchronous method is now found in NavGraphProvider.kt. It creates a job on the IO dispatcher 2)The Job is tracked through a WeakHashMap from Controller --> NavGraph 3)The Coroutine scope doesn't use MainScope() anymore 4)The Coroutine is cancelled if the Activity is destroyed 5)The tests mockk the blockForNavGraphInflation method through the FenixReoboelectricTestApplication instead of calling the mock every setup() For #16900: inflateNavGraphAsync now takes navController For #16900: Pass lifecycleScope to NavGraphProvider For #16900: removed unused mock For #16900: Added linter rules for navigate calls We need linting rules to make sure no one calls the NavController.navigate() methods For #16900: Added TestRule to help abstract the mocks in the code For 16900: Fix linting problems For #16900: Cleaned duplicated code in tests For #16900: cleaned up NavGraphTestRule for finished test For #16900: had to revert an accidentally edited file For #16900: rebased master * For #16900: Review nits for async navgraph This is composed of squash commits, the original messages can be found below: -> DisableNavGraphProviderAssertionRule + kdoc. Use test rule in RobolectricApplication. Fix failing CrashReporterControllerTest Fix blame by -> navigate in tests. This commit was generated by the following commands only: ``` find app/src/test -type f -exec sed -i '' "/import org.mozilla.fenix.ext.navigateBlockingForAsyncNavGraph/d" {} \; find app/src/test -type f -exec sed -i "" "s/navigateBlockingForAsyncNavGraph/navigate/g" {} \; git checkout app/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker ``` Fix various blame This is expected to be squashed into the first commit so, if so, it'd fix the blame. Move test rule to helpers pkg. add missing license header Add import change I missed fix unused imports Replace robolectricTestrunner with test rule. Improve navGraphProvider docs Remove unnecessary rule as defined by robolectric. add clarifying comment to robolectric remove unnecessary space * For #16900: nit fixes for MozillaNavigateCheck and lint fixes 3 squash commits: *Changed violation message and fixed the lint rule for MozillaNavigateCheck *Added suppression to NavController.kt *Fixed detekt violations * For 16900: Fixed failing tests Co-authored-by: Michael Comella <michael.l.comella@gmail.com>
3 years ago
import org.mozilla.fenix.ext.navigateBlockingForAsyncNavGraph
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.runIfFragmentIsAttached
import org.mozilla.fenix.ext.secure
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.SharedPreferenceUpdater
import org.mozilla.fenix.settings.SyncPreferenceView
import org.mozilla.fenix.settings.biometric.BiometricPromptFeature
import org.mozilla.fenix.settings.biometric.BiometricPromptPreferenceFragment
import org.mozilla.fenix.settings.requirePreference
/**
* "Credit cards" settings fragment displays a list of settings related to autofilling, adding and
* syncing credit cards. Authentication for saved credit cards uses [BiometricPromptFeature]
* or [KeyguardManager].
*/
@SuppressWarnings("TooManyFunctions")
class CreditCardsSettingFragment : BiometricPromptPreferenceFragment() {
private lateinit var creditCardsStore: CreditCardsFragmentStore
private var isCreditCardsListLoaded: Boolean = false
/**
* List of preferences to be enabled or disabled during authentication.
*/
private val creditCardPreferences: List<Int> = listOf(
R.string.pref_key_credit_cards_save_and_autofill_cards,
R.string.pref_key_credit_cards_sync_cards_across_devices,
R.string.pref_key_credit_cards_manage_cards
)
override fun unlockMessage() = getString(R.string.credit_cards_biometric_prompt_message)
override fun navigateOnSuccess() {
runIfFragmentIsAttached {
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
// Workaround for likely biometric library bug
// https://github.com/mozilla-mobile/fenix/issues/8438
delay(SHORT_DELAY_MS)
navigateToCreditCardManagementFragment()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
creditCardsStore = StoreProvider.get(this) {
CreditCardsFragmentStore(CreditCardsListState(creditCards = emptyList()))
}
loadCreditCards()
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.credit_cards_preferences, rootKey)
requirePreference<SwitchPreference>(R.string.pref_key_credit_cards_save_and_autofill_cards).apply {
isChecked = context.settings().shouldAutofillCreditCardDetails
onPreferenceChangeListener = SharedPreferenceUpdater()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
loadCreditCards()
return super.onCreateView(inflater, container, savedInstanceState)
}
@ExperimentalCoroutinesApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
consumeFrom(creditCardsStore) { state ->
updateCardManagementPreference(state.creditCards.isNotEmpty(), findNavController())
}
setBiometricPrompt(view, creditCardPreferences)
}
override fun onPause() {
super.onPause()
isCreditCardsListLoaded = false
}
override fun onResume() {
super.onResume()
showToolbar(getString(R.string.preferences_credit_cards))
SyncPreferenceView(
syncPreference = requirePreference(R.string.pref_key_credit_cards_sync_cards_across_devices),
lifecycleOwner = viewLifecycleOwner,
accountManager = requireComponents.backgroundServices.accountManager,
syncEngine = SyncEngine.CreditCards,
loggedOffTitle = requireContext()
.getString(R.string.preferences_credit_cards_sync_cards_across_devices),
loggedInTitle = requireContext()
.getString(R.string.preferences_credit_cards_sync_cards),
onSignInToSyncClicked = {
findNavController().navigateBlockingForAsyncNavGraph(
NavGraphDirections.actionGlobalTurnOnSync()
)
},
onReconnectClicked = {
findNavController().navigateBlockingForAsyncNavGraph(
CreditCardsSettingFragmentDirections.actionGlobalAccountProblemFragment()
)
}
)
togglePrefsEnabled(creditCardPreferences, true)
}
/**
* Updates preferences visibility depending on credit cards being already saved or not.
*/
@VisibleForTesting
internal fun updateCardManagementPreference(
hasCreditCards: Boolean,
navController: NavController
) {
val manageSavedCardsPreference =
requirePreference<Preference>(R.string.pref_key_credit_cards_manage_cards)
if (hasCreditCards) {
manageSavedCardsPreference.icon = null
manageSavedCardsPreference.title =
getString(R.string.preferences_credit_cards_manage_saved_cards)
} else {
manageSavedCardsPreference.setIcon(R.drawable.ic_new)
manageSavedCardsPreference.title =
getString(R.string.preferences_credit_cards_add_credit_card)
}
manageSavedCardsPreference.setOnPreferenceClickListener {
if (hasCreditCards) {
verifyCredentialsOrShowSetupWarning(requireContext(), creditCardPreferences)
} else {
navController.navigateBlockingForAsyncNavGraph(
CreditCardsSettingFragmentDirections
.actionCreditCardsSettingFragmentToCreditCardEditorFragment()
)
}
super.onPreferenceTreeClick(it)
}
}
/**
* Fetches all the credit cards from autofillStorage and updates the [CreditCardsListState]
* with the list of credit cards.
*/
private fun loadCreditCards() {
if (isCreditCardsListLoaded) {
return
}
lifecycleScope.launch(Dispatchers.IO) {
val creditCards = requireComponents.core.autofillStorage.getAllCreditCards()
lifecycleScope.launch(Dispatchers.Main) {
creditCardsStore.dispatch(CreditCardsAction.UpdateCreditCards(creditCards))
}
}
isCreditCardsListLoaded = true
}
/**
* Shows a dialog warning to set up a pin/password when the device is not secured. This is
* only used when BiometricPrompt is unavailable on the device.
*/
override fun showPinDialogWarning(context: Context) {
AlertDialog.Builder(context).apply {
setTitle(getString(R.string.credit_cards_warning_dialog_title))
setMessage(getString(R.string.credit_cards_warning_dialog_message))
setNegativeButton(getString(R.string.credit_cards_warning_dialog_later)) { _: DialogInterface, _ ->
navigateToCreditCardManagementFragment()
}
setPositiveButton(getString(R.string.credit_cards_warning_dialog_set_up_now)) { it: DialogInterface, _ ->
it.dismiss()
val intent = Intent(Settings.ACTION_SECURITY_SETTINGS)
startActivity(intent)
}
create()
}.show().secure(activity)
context.settings().incrementSecureWarningCount()
}
/**
* Shows a prompt to verify the device's pin/password and start activity based on the result.
* This is only used when BiometricPrompt is unavailable on the device.
*
* @param manager The device [KeyguardManager]
*/
@Suppress("Deprecation")
override fun showPinVerification(manager: KeyguardManager) {
val intent = manager.createConfirmDeviceCredentialIntent(
getString(R.string.credit_cards_biometric_prompt_message_pin),
getString(R.string.credit_cards_biometric_prompt_message)
)
startActivityForResult(intent, BiometricPromptPreferenceFragment.PIN_REQUEST)
}
private fun navigateToCreditCardManagementFragment() {
val directions =
CreditCardsSettingFragmentDirections
.actionCreditCardsSettingFragmentToCreditCardsManagementFragment()
findNavController().navigateBlockingForAsyncNavGraph(directions)
}
companion object {
const val SHORT_DELAY_MS = 100L
}
}