8 |
gabriel |
1 |
package com.cesams.twogetskills.inconcert.fragment;
|
|
|
2 |
|
|
|
3 |
import android.content.Intent;
|
|
|
4 |
import android.os.Bundle;
|
|
|
5 |
|
|
|
6 |
import androidx.annotation.NonNull;
|
|
|
7 |
import androidx.biometric.BiometricManager;
|
|
|
8 |
import androidx.biometric.BiometricPrompt;
|
|
|
9 |
import androidx.core.content.ContextCompat;
|
|
|
10 |
import androidx.fragment.app.Fragment;
|
|
|
11 |
|
|
|
12 |
import android.provider.Settings;
|
|
|
13 |
import android.util.Log;
|
|
|
14 |
import android.view.LayoutInflater;
|
|
|
15 |
import android.view.View;
|
|
|
16 |
import android.view.ViewGroup;
|
|
|
17 |
import android.widget.Button;
|
|
|
18 |
import android.widget.TextView;
|
|
|
19 |
import android.widget.Toast;
|
|
|
20 |
|
|
|
21 |
import com.cesams.twogetskills.inconcert.Constants;
|
|
|
22 |
import com.cesams.twogetskills.inconcert.R;
|
|
|
23 |
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;
|
|
|
24 |
|
|
|
25 |
import java.util.concurrent.Executor;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* A simple {@link Fragment} subclass.
|
|
|
29 |
* Use the {@link BiometricOptions#newInstance} factory method to
|
|
|
30 |
* create an instance of this fragment.
|
|
|
31 |
*/
|
|
|
32 |
public class BiometricOptions extends Fragment {
|
|
|
33 |
|
|
|
34 |
// TODO: Rename parameter arguments, choose names that match
|
|
|
35 |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
|
36 |
private static final String ARG_PARAM1 = "param1";
|
|
|
37 |
private static final String ARG_PARAM2 = "param2";
|
|
|
38 |
private Executor executor;
|
|
|
39 |
private BiometricPrompt biometricPrompt;
|
|
|
40 |
private BiometricPrompt.PromptInfo promptInfo;
|
|
|
41 |
private ITwoGetSkills iTwoGetSkills;
|
|
|
42 |
private static final int REQUEST_CODE = 8;
|
|
|
43 |
private TextView username;
|
|
|
44 |
|
|
|
45 |
// TODO: Rename and change types of parameters
|
|
|
46 |
private String mParam1;
|
|
|
47 |
private String mParam2;
|
|
|
48 |
|
|
|
49 |
public BiometricOptions() {
|
|
|
50 |
// Required empty public constructor
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Use this factory method to create a new instance of
|
|
|
55 |
* this fragment using the provided parameters.
|
|
|
56 |
*
|
|
|
57 |
* @param param1 Parameter 1.
|
|
|
58 |
* @param param2 Parameter 2.
|
|
|
59 |
* @return A new instance of fragment BiometricOptions.
|
|
|
60 |
*/
|
|
|
61 |
// TODO: Rename and change types and number of parameters
|
|
|
62 |
public static BiometricOptions newInstance(String param1, String param2) {
|
|
|
63 |
BiometricOptions fragment = new BiometricOptions();
|
|
|
64 |
Bundle args = new Bundle();
|
|
|
65 |
args.putString(ARG_PARAM1, param1);
|
|
|
66 |
args.putString(ARG_PARAM2, param2);
|
|
|
67 |
fragment.setArguments(args);
|
|
|
68 |
return fragment;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
@Override
|
|
|
72 |
public void onCreate(Bundle savedInstanceState) {
|
|
|
73 |
super.onCreate(savedInstanceState);
|
|
|
74 |
if (getArguments() != null) {
|
|
|
75 |
mParam1 = getArguments().getString(ARG_PARAM1);
|
|
|
76 |
mParam2 = getArguments().getString(ARG_PARAM2);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@Override
|
|
|
81 |
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
82 |
Bundle savedInstanceState) {
|
|
|
83 |
// Inflate the layout for this fragment
|
|
|
84 |
View view= inflater.inflate(R.layout.fragment_biometric_options, container, false);
|
|
|
85 |
iTwoGetSkills = (ITwoGetSkills) getActivity();
|
|
|
86 |
username = view.findViewById(R.id.textView21);
|
|
|
87 |
username.setText("Hola "+(iTwoGetSkills.getPreference().getFirstName()));
|
|
|
88 |
|
9 |
gabriel |
89 |
BiometricManager biometricManager = BiometricManager.from(getActivity().getApplicationContext());
|
|
|
90 |
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)) {
|
|
|
91 |
case BiometricManager.BIOMETRIC_SUCCESS:
|
|
|
92 |
Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
|
|
|
93 |
break;
|
|
|
94 |
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
|
|
|
95 |
getActivity().runOnUiThread(() -> {
|
|
|
96 |
iTwoGetSkills.getPreference().setBiometriclogin(false);
|
|
|
97 |
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SIGNIN);
|
|
|
98 |
Toast.makeText(getActivity().getApplicationContext(), "Este dispositivo no cuenta con sensor de huellas, inicia con tu correo", Toast.LENGTH_SHORT).show();
|
|
|
99 |
});
|
|
|
100 |
break;
|
|
|
101 |
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
|
|
|
102 |
Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
|
|
|
103 |
break;
|
|
|
104 |
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
|
|
|
105 |
// Prompts the user to create credentials that your app accepts.
|
|
|
106 |
final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL);
|
|
|
107 |
enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
|
|
|
108 |
BiometricManager.Authenticators.BIOMETRIC_STRONG);
|
|
|
109 |
startActivityForResult(enrollIntent, REQUEST_CODE);
|
|
|
110 |
break;
|
|
|
111 |
}
|
|
|
112 |
|
8 |
gabriel |
113 |
executor = ContextCompat.getMainExecutor(getActivity().getApplicationContext());
|
|
|
114 |
biometricPrompt = new BiometricPrompt(getActivity(),
|
|
|
115 |
executor, new BiometricPrompt.AuthenticationCallback() {
|
|
|
116 |
@Override
|
|
|
117 |
public void onAuthenticationError(int errorCode,
|
|
|
118 |
@NonNull CharSequence errString) {
|
|
|
119 |
super.onAuthenticationError(errorCode, errString);
|
9 |
gabriel |
120 |
|
|
|
121 |
if(errorCode==11)
|
|
|
122 |
{
|
|
|
123 |
Toast.makeText(requireActivity().getApplicationContext(), "Necesitas registrar una huella antes de usar esta función", Toast.LENGTH_SHORT).show();
|
|
|
124 |
}
|
|
|
125 |
else
|
|
|
126 |
{
|
|
|
127 |
Toast.makeText(requireActivity().getApplicationContext(), "No puedes usar Fingerprint OnRoom en este momento, ingresa con tu correo", Toast.LENGTH_SHORT).show();
|
|
|
128 |
}
|
8 |
gabriel |
129 |
}
|
|
|
130 |
|
|
|
131 |
@Override
|
|
|
132 |
public void onAuthenticationSucceeded(
|
|
|
133 |
@NonNull BiometricPrompt.AuthenticationResult result) {
|
|
|
134 |
super.onAuthenticationSucceeded(result);
|
|
|
135 |
/* Toast.makeText(requireActivity().getApplicationContext(),
|
|
|
136 |
"Authentication succeeded!", Toast.LENGTH_SHORT).show(); */
|
|
|
137 |
|
|
|
138 |
getActivity().runOnUiThread(() -> {
|
|
|
139 |
|
|
|
140 |
iTwoGetSkills.getPreference().setBiometriclogin(true);
|
18 |
efrain |
141 |
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
|
8 |
gabriel |
142 |
iTwoGetSkills.showNavigationAndToolbar();
|
|
|
143 |
});
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
@Override
|
|
|
147 |
public void onAuthenticationFailed() {
|
|
|
148 |
super.onAuthenticationFailed();
|
|
|
149 |
Toast.makeText(requireActivity().getApplicationContext(), "No se pudo comprobar tu identidad",
|
|
|
150 |
Toast.LENGTH_SHORT)
|
|
|
151 |
.show();
|
|
|
152 |
}
|
|
|
153 |
});
|
|
|
154 |
|
|
|
155 |
promptInfo = new BiometricPrompt.PromptInfo.Builder()
|
|
|
156 |
.setTitle("Iniciar sesión en OnRoom")
|
|
|
157 |
.setSubtitle("Podés usar Fingerprint para iniciar sesión en OnRoom")
|
|
|
158 |
.setNegativeButtonText("Usar contraseña")
|
|
|
159 |
.build();
|
|
|
160 |
|
|
|
161 |
// Prompt appears when user clicks "Log in".
|
|
|
162 |
// Consider integrating with the keystore to unlock cryptographic operations,
|
|
|
163 |
// if needed by your app.
|
|
|
164 |
Button biometricLoginButton = view.findViewById(R.id.huellasignin);
|
|
|
165 |
biometricLoginButton.setOnClickListener(viewp -> {
|
|
|
166 |
biometricPrompt.authenticate(promptInfo);
|
|
|
167 |
});
|
|
|
168 |
|
|
|
169 |
Button omitbiometrics = view.findViewById(R.id.signin_button_sign_up2);
|
|
|
170 |
omitbiometrics.setOnClickListener(new View.OnClickListener() {
|
|
|
171 |
@Override
|
|
|
172 |
public void onClick(View view) {
|
|
|
173 |
getActivity().runOnUiThread(() -> {
|
|
|
174 |
|
|
|
175 |
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SIGNIN);
|
|
|
176 |
// iTwoGetSkills.showNavigationAndToolbar();
|
|
|
177 |
});
|
|
|
178 |
}
|
|
|
179 |
});
|
|
|
180 |
|
9 |
gabriel |
181 |
|
8 |
gabriel |
182 |
return view;
|
|
|
183 |
}
|
|
|
184 |
}
|