33 private TextView tvTotal;
34 private TextView tvStatus;
35 private Button btnCancel;
36 private ImageView backButton;
37 private Executor executor;
38 private BiometricPrompt biometricPrompt;
39 private BiometricPrompt.PromptInfo promptInfo;
40 private double totalCompra = 0;
49 Bundle args =
new Bundle();
50 args.putDouble(
"totalCompra", total);
51 fragment.setArguments(args);
56 public void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 if (getArguments() !=
null) {
59 totalCompra = getArguments().getDouble(
"totalCompra", 0);
64 public View
onCreateView(LayoutInflater inflater, ViewGroup container,
65 Bundle savedInstanceState) {
67 View view = inflater.inflate(R.layout.fragment_fingerprint_auth, container,
false);
70 tvTotal = view.findViewById(R.id.tvTotal);
71 tvStatus = view.findViewById(R.id.tvStatus);
72 btnCancel = view.findViewById(R.id.btnCancel);
73 backButton = view.findViewById(R.id.backButton);
76 NumberFormat formatoMoneda = NumberFormat.getCurrencyInstance(
new Locale(
"es",
"CL"));
77 tvTotal.setText(
"Monto de compra: " + formatoMoneda.format(totalCompra));
80 btnCancel.setOnClickListener(v -> {
82 requireActivity().getSupportFragmentManager().popBackStack();
85 backButton.setOnClickListener(v -> {
87 requireActivity().getSupportFragmentManager().popBackStack();
91 setupBiometricAuthentication();
96 private void setupBiometricAuthentication() {
98 BiometricManager biometricManager = BiometricManager.from(requireContext());
99 switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)) {
100 case BiometricManager.BIOMETRIC_SUCCESS:
103 case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
104 tvStatus.setText(
"Este dispositivo no tiene sensor de huella digital");
105 new Handler().postDelayed(() -> navigateToPayment(), 2000);
107 case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
108 tvStatus.setText(
"Sensor biométrico no disponible");
109 new Handler().postDelayed(() -> navigateToPayment(), 2000);
111 case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
112 tvStatus.setText(
"No hay huellas registradas");
113 new Handler().postDelayed(() -> navigateToPayment(), 2000);
118 executor = ContextCompat.getMainExecutor(requireContext());
121 biometricPrompt =
new BiometricPrompt(
this, executor,
122 new BiometricPrompt.AuthenticationCallback() {
124 public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
125 super.onAuthenticationError(errorCode, errString);
126 tvStatus.setText(
"Error de autenticación: " + errString);
130 public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
131 super.onAuthenticationSucceeded(result);
132 tvStatus.setText(
"¡Autenticación exitosa!");
134 new Handler().postDelayed(() -> navigateToPayment(), 1000);
138 public void onAuthenticationFailed() {
139 super.onAuthenticationFailed();
140 tvStatus.setText(
"La autenticación falló, inténtalo de nuevo");
145 promptInfo =
new BiometricPrompt.PromptInfo.Builder()
146 .setTitle(
"Verificación de identidad")
147 .setSubtitle(
"Autentícate con tu huella digital")
148 .setDescription(
"Se requiere verificación para compras superiores a $50.000")
149 .setNegativeButtonText(
"Cancelar")
153 new Handler().postDelayed(() -> biometricPrompt.authenticate(promptInfo), 500);
156 private void navigateToPayment() {
158 FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
159 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
160 fragmentTransaction.replace(R.id.fragment_container_view,
new PaymentFragment());
161 fragmentTransaction.addToBackStack(
null);
162 fragmentTransaction.commit();