Rev 9 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
package com.cesams.twogetskills.inconcert.fragment;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.cesams.twogetskills.inconcert.Constants;
import com.cesams.twogetskills.inconcert.R;
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;
import java.util.concurrent.Executor;
/**
* A simple {@link Fragment} subclass.
* Use the {@link BiometricOptions#newInstance} factory method to
* create an instance of this fragment.
*/
public class BiometricOptions extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private Executor executor;
private BiometricPrompt biometricPrompt;
private BiometricPrompt.PromptInfo promptInfo;
private ITwoGetSkills iTwoGetSkills;
private static final int REQUEST_CODE = 8;
private TextView username;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public BiometricOptions() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BiometricOptions.
*/
// TODO: Rename and change types and number of parameters
public static BiometricOptions newInstance(String param1, String param2) {
BiometricOptions fragment = new BiometricOptions();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_biometric_options, container, false);
iTwoGetSkills = (ITwoGetSkills) getActivity();
username = view.findViewById(R.id.textView21);
username.setText("Hola "+(iTwoGetSkills.getPreference().getFirstName()));
BiometricManager biometricManager = BiometricManager.from(getActivity().getApplicationContext());
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)) {
case BiometricManager.BIOMETRIC_SUCCESS:
Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
getActivity().runOnUiThread(() -> {
iTwoGetSkills.getPreference().setBiometriclogin(false);
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SIGNIN);
Toast.makeText(getActivity().getApplicationContext(), "Este dispositivo no cuenta con sensor de huellas, inicia con tu correo", Toast.LENGTH_SHORT).show();
});
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
// Prompts the user to create credentials that your app accepts.
final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL);
enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
BiometricManager.Authenticators.BIOMETRIC_STRONG);
startActivityForResult(enrollIntent, REQUEST_CODE);
break;
}
executor = ContextCompat.getMainExecutor(getActivity().getApplicationContext());
biometricPrompt = new BiometricPrompt(getActivity(),
executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
if(errorCode==11)
{
Toast.makeText(requireActivity().getApplicationContext(), "Necesitas registrar una huella antes de usar esta función", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(requireActivity().getApplicationContext(), "No puedes usar Fingerprint OnRoom en este momento, ingresa con tu correo", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
/* Toast.makeText(requireActivity().getApplicationContext(),
"Authentication succeeded!", Toast.LENGTH_SHORT).show(); */
getActivity().runOnUiThread(() -> {
iTwoGetSkills.getPreference().setBiometriclogin(true);
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_MY_CAPSULES);
iTwoGetSkills.showNavigationAndToolbar();
});
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(requireActivity().getApplicationContext(), "No se pudo comprobar tu identidad",
Toast.LENGTH_SHORT)
.show();
}
});
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Iniciar sesión en OnRoom")
.setSubtitle("Podés usar Fingerprint para iniciar sesión en OnRoom")
.setNegativeButtonText("Usar contraseña")
.build();
// Prompt appears when user clicks "Log in".
// Consider integrating with the keystore to unlock cryptographic operations,
// if needed by your app.
Button biometricLoginButton = view.findViewById(R.id.huellasignin);
biometricLoginButton.setOnClickListener(viewp -> {
biometricPrompt.authenticate(promptInfo);
});
Button omitbiometrics = view.findViewById(R.id.signin_button_sign_up2);
omitbiometrics.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().runOnUiThread(() -> {
iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SIGNIN);
// iTwoGetSkills.showNavigationAndToolbar();
});
}
});
return view;
}
}