Tesis 1.0.0
Loading...
Searching...
No Matches
FingerprintAuthFragment.java
Go to the documentation of this file.
1package com.example.food_front;
2
3import android.content.Context;
4import android.content.pm.PackageManager;
5import android.hardware.fingerprint.FingerprintManager;
6import android.os.Build;
7import android.os.Bundle;
8import android.os.CancellationSignal;
9import android.os.Handler;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.view.ViewGroup;
13import android.widget.Button;
14import android.widget.ImageView;
15import android.widget.TextView;
16import android.widget.Toast;
17
18import androidx.annotation.NonNull;
19import androidx.annotation.RequiresApi;
20import androidx.biometric.BiometricManager;
21import androidx.biometric.BiometricPrompt;
22import androidx.core.content.ContextCompat;
23import androidx.fragment.app.Fragment;
24import androidx.fragment.app.FragmentManager;
25import androidx.fragment.app.FragmentTransaction;
26
27import java.text.NumberFormat;
28import java.util.Locale;
29import java.util.concurrent.Executor;
30
31public class FingerprintAuthFragment extends Fragment {
32
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;
41
43 // Constructor vacío requerido
44 }
45
46 // Método estático para crear una nueva instancia con el total de la compra
47 public static FingerprintAuthFragment newInstance(double total) {
49 Bundle args = new Bundle();
50 args.putDouble("totalCompra", total);
51 fragment.setArguments(args);
52 return fragment;
53 }
54
55 @Override
56 public void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 if (getArguments() != null) {
59 totalCompra = getArguments().getDouble("totalCompra", 0);
60 }
61 }
62
63 @Override
64 public View onCreateView(LayoutInflater inflater, ViewGroup container,
65 Bundle savedInstanceState) {
66 // Inflar el layout para este fragmento
67 View view = inflater.inflate(R.layout.fragment_fingerprint_auth, container, false);
68
69 // Inicializar vistas
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);
74
75 // Formatear y mostrar el total de la compra
76 NumberFormat formatoMoneda = NumberFormat.getCurrencyInstance(new Locale("es", "CL"));
77 tvTotal.setText("Monto de compra: " + formatoMoneda.format(totalCompra));
78
79 // Configurar botones
80 btnCancel.setOnClickListener(v -> {
81 // Volver al fragmento anterior
82 requireActivity().getSupportFragmentManager().popBackStack();
83 });
84
85 backButton.setOnClickListener(v -> {
86 // Volver al fragmento anterior
87 requireActivity().getSupportFragmentManager().popBackStack();
88 });
89
90 // Configurar autenticación biométrica
91 setupBiometricAuthentication();
92
93 return view;
94 }
95
96 private void setupBiometricAuthentication() {
97 // Verificar si el dispositivo soporta autenticación biométrica
98 BiometricManager biometricManager = BiometricManager.from(requireContext());
99 switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)) {
100 case BiometricManager.BIOMETRIC_SUCCESS:
101 // El dispositivo puede usar biometría
102 break;
103 case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
104 tvStatus.setText("Este dispositivo no tiene sensor de huella digital");
105 new Handler().postDelayed(() -> navigateToPayment(), 2000);
106 return;
107 case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
108 tvStatus.setText("Sensor biométrico no disponible");
109 new Handler().postDelayed(() -> navigateToPayment(), 2000);
110 return;
111 case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
112 tvStatus.setText("No hay huellas registradas");
113 new Handler().postDelayed(() -> navigateToPayment(), 2000);
114 return;
115 }
116
117 // Configurar executor para la autenticación biométrica
118 executor = ContextCompat.getMainExecutor(requireContext());
119
120 // Configurar BiometricPrompt
121 biometricPrompt = new BiometricPrompt(this, executor,
122 new BiometricPrompt.AuthenticationCallback() {
123 @Override
124 public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
125 super.onAuthenticationError(errorCode, errString);
126 tvStatus.setText("Error de autenticación: " + errString);
127 }
128
129 @Override
130 public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
131 super.onAuthenticationSucceeded(result);
132 tvStatus.setText("¡Autenticación exitosa!");
133 // Esperar un momento y navegar a la pantalla de pago
134 new Handler().postDelayed(() -> navigateToPayment(), 1000);
135 }
136
137 @Override
138 public void onAuthenticationFailed() {
139 super.onAuthenticationFailed();
140 tvStatus.setText("La autenticación falló, inténtalo de nuevo");
141 }
142 });
143
144 // Configurar el diálogo de autenticación
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")
150 .build();
151
152 // Mostrar diálogo de autenticación biométrica cuando el fragmento se haya cargado
153 new Handler().postDelayed(() -> biometricPrompt.authenticate(promptInfo), 500);
154 }
155
156 private void navigateToPayment() {
157 // Navegar al fragmento de pago
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();
163 }
164}
static FingerprintAuthFragment newInstance(double total)
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)