1+ package com .assistant .android .bolchal ;
2+
3+ import androidx .annotation .NonNull ;
4+ import androidx .appcompat .app .AppCompatActivity ;
5+
6+ import android .content .Intent ;
7+ import android .os .Bundle ;
8+ import android .os .CountDownTimer ;
9+ import android .view .View ;
10+ import android .widget .Button ;
11+ import android .widget .EditText ;
12+ import android .widget .TextView ;
13+ import android .widget .Toast ;
14+
15+ import com .assistant .android .bolchal .ui .OptEditText ;
16+ import com .google .android .gms .tasks .OnCompleteListener ;
17+ import com .google .android .gms .tasks .Task ;
18+ import com .google .firebase .FirebaseException ;
19+ import com .google .firebase .auth .AuthResult ;
20+ import com .google .firebase .auth .FirebaseAuth ;
21+ import com .google .firebase .auth .FirebaseAuthInvalidCredentialsException ;
22+ import com .google .firebase .auth .FirebaseUser ;
23+ import com .google .firebase .auth .PhoneAuthCredential ;
24+ import com .google .firebase .auth .PhoneAuthOptions ;
25+ import com .google .firebase .auth .PhoneAuthProvider ;
26+ import com .hbb20 .CountryCodePicker ;
27+
28+ import java .util .Objects ;
29+ import java .util .concurrent .TimeUnit ;
30+
31+ public class Otp_verification extends AppCompatActivity {
32+
33+ private static final String TAG = "LogIn_page" ;
34+ private FirebaseAuth mAuth ;
35+ private PhoneAuthProvider .OnVerificationStateChangedCallbacks mCallbacks ;
36+ private String SmsCode ;
37+ private TextView timer ;
38+ private TextView resendTxtView ;
39+ private TextView verify ;
40+ private TextView xxPhone_num ;
41+
42+ @ Override
43+ protected void onCreate (Bundle savedInstanceState ) {
44+ super .onCreate (savedInstanceState );
45+ setContentView (R .layout .layout_otp_verification );
46+
47+ //Hide Top ActionBar
48+ Objects .requireNonNull (getSupportActionBar ()).hide ();
49+
50+ OptEditText code = findViewById (R .id .codeTextView );
51+ verify = findViewById (R .id .verify );
52+ timer = findViewById (R .id .timer );
53+ resendTxtView = findViewById (R .id .resend );
54+ xxPhone_num = findViewById (R .id .xxx_Ph );
55+
56+
57+ mAuth = FirebaseAuth .getInstance ();
58+
59+ /**
60+ * get Phone Num as extra from Intent of Phone Number Sent
61+ */
62+ String phone_num = getIntent ().getStringExtra ("phoneNum" );
63+
64+ //Set coded phoneNUm to the screen
65+ String coded = "+" + phone_num .charAt (1 ) + phone_num .charAt (2 ) + "XXXXXXX" + phone_num .charAt (phone_num .length ()-3 ) +
66+ phone_num .charAt (phone_num .length ()-2 ) + phone_num .charAt (phone_num .length ()-1 );
67+ xxPhone_num .setText (phone_num );
68+
69+
70+ /**
71+ * Verification Check up
72+ */
73+ mCallbacks = new PhoneAuthProvider .OnVerificationStateChangedCallbacks () {
74+ /**
75+ * ON different device Manually input code
76+ *
77+ * @param s
78+ * @param forceResendingToken
79+ */
80+ @ Override
81+ public void onCodeSent (@ NonNull String s , @ NonNull PhoneAuthProvider .ForceResendingToken forceResendingToken ) {
82+ SmsCode = s ;
83+ }
84+
85+ @ Override //Automatic code verify in case of same number in the device
86+ public void onVerificationCompleted (@ NonNull PhoneAuthCredential phoneAuthCredential ) {
87+ signInWithPhoneAuthCredential (phoneAuthCredential );
88+ }
89+
90+ @ Override
91+ public void onVerificationFailed (@ NonNull FirebaseException e ) {
92+ Toast .makeText (Otp_verification .this , e + " exception" , Toast .LENGTH_SHORT ).show ();
93+ }
94+ };
95+
96+
97+ /**
98+ * This Method Sends Sms for verification
99+ */
100+ sendMsg (phone_num );
101+
102+ verify .setOnClickListener (new View .OnClickListener () {
103+ @ Override
104+ public void onClick (View view ) {
105+ if (code .getText ().toString ().length () == 6 ) {
106+ PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider .getCredential (SmsCode , code .getText ().toString ());
107+ signInWithPhoneAuthCredential (phoneAuthCredential );
108+ }
109+ }
110+ });
111+
112+ //SetTimer
113+ startTimer ();
114+
115+ resendTxtView .setOnClickListener (view -> {
116+ resendTxtView .setVisibility (View .GONE );
117+ timer .setVisibility (View .VISIBLE );
118+ sendMsg (phone_num );
119+ startTimer ();
120+ });
121+
122+ }
123+
124+ //Send messages
125+ private void sendMsg (String phone_num ){
126+ PhoneAuthOptions options =
127+ PhoneAuthOptions .newBuilder (mAuth )
128+ .setPhoneNumber (phone_num ) // Phone number to verify
129+ .setTimeout (30L , TimeUnit .SECONDS ) // Timeout and unit
130+ .setActivity (Otp_verification .this ) // Activity (for callback binding)
131+ .setCallbacks (mCallbacks ) // OnVerificationStateChangedCallbacks
132+ .build ();
133+ PhoneAuthProvider .verifyPhoneNumber (options );
134+ }
135+
136+ //Make CountDown Timer
137+ private void startTimer () {
138+ new CountDownTimer (30000 ,1000 ){
139+
140+ @ Override
141+ public void onTick (long l ) {
142+ long sec = l /1000 ;
143+ timer .setText ("Resend OTP in: 00:" +sec );
144+ }
145+
146+ @ Override
147+ public void onFinish () {
148+ timer .setVisibility (View .GONE );
149+ resendTxtView .setVisibility (View .VISIBLE );
150+ }
151+ }.start ();
152+ }
153+
154+ private void signInWithPhoneAuthCredential (PhoneAuthCredential credential ) {
155+ mAuth .signInWithCredential (credential )
156+ .addOnCompleteListener (this , new OnCompleteListener <AuthResult >() {
157+ @ Override
158+ public void onComplete (@ NonNull Task <AuthResult > task ) {
159+ if (task .isSuccessful ()) {
160+ // Sign in success, update UI with the signed-in user's information
161+ Intent intent = new Intent (Otp_verification .this ,details .class );
162+
163+ FirebaseUser user = task .getResult ().getUser ();
164+
165+ intent .putExtra ("userName" ,user );
166+ startActivity (intent );
167+ finish ();
168+ // Update UI
169+ } else {
170+ // Sign in failed, display a message and update the UI
171+ Toast .makeText (Otp_verification .this , "signInWithCredential:failure" + task .getException (),Toast .LENGTH_LONG ).show ();
172+ if (task .getException () instanceof FirebaseAuthInvalidCredentialsException ) {
173+ // The verification code entered was invalid
174+ }
175+ }
176+ }
177+ });
178+ }
179+
180+ }
0 commit comments