in auth/app/src/main/java/com/google/firebase/quickstart/auth/java/PhoneAuthFragment.java [61:164]
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore instance state
if (savedInstanceState != null) {
onViewStateRestored(savedInstanceState);
}
// Assign click listeners
mBinding.buttonStartVerification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validatePhoneNumber()) {
return;
}
startPhoneNumberVerification(mBinding.fieldPhoneNumber.getText().toString());
}
});
mBinding.buttonVerifyPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = mBinding.fieldVerificationCode.getText().toString();
if (TextUtils.isEmpty(code)) {
mBinding.fieldVerificationCode.setError("Cannot be empty.");
return;
}
verifyPhoneNumberWithCode(mVerificationId, code);
}
});
mBinding.buttonResend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validatePhoneNumber()) {
return;
}
resendVerificationCode(mBinding.fieldPhoneNumber.getText().toString(), mResendToken);
}
});
mBinding.signOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Initialize phone auth callbacks
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verification without
// user action.
Log.d(TAG, "onVerificationCompleted:" + credential);
mVerificationInProgress = false;
// Update the UI and attempt sign in with the phone credential
updateUI(STATE_VERIFY_SUCCESS, credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w(TAG, "onVerificationFailed", e);
mVerificationInProgress = false;
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
mBinding.fieldPhoneNumber.setError("Invalid phone number.");
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
Snackbar.make(view, "Quota exceeded.",
Snackbar.LENGTH_SHORT).show();
}
// Show a message and update the UI
updateUI(STATE_VERIFY_FAILED);
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d(TAG, "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
// Update UI
updateUI(STATE_CODE_SENT);
}
};
}