1
+ package my .foodOn .app ;
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 .google .android .gms .tasks .OnCompleteListener ;
16
+ import com .google .android .gms .tasks .Task ;
17
+ import com .google .android .gms .tasks .TaskExecutors ;
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 .PhoneAuthCredential ;
22
+ import com .google .firebase .auth .PhoneAuthProvider ;
23
+
24
+ import java .util .concurrent .TimeUnit ;
25
+
26
+ public class Chefsendotp extends AppCompatActivity {
27
+
28
+ String verificationId ;
29
+ FirebaseAuth FAuth ;
30
+ Button verify , Resend ;
31
+ TextView txt ;
32
+ EditText entercode ;
33
+ String phoneno ;
34
+
35
+ @ Override
36
+ protected void onCreate (Bundle savedInstanceState ) {
37
+ super .onCreate (savedInstanceState );
38
+ setContentView (R .layout .activity_chefsendotp );
39
+
40
+ phoneno = getIntent ().getStringExtra ("Phonenumber" ).trim ();
41
+
42
+ entercode = (EditText ) findViewById (R .id .code );
43
+ txt = (TextView ) findViewById (R .id .text );
44
+ Resend = (Button )findViewById (R .id .Resendotp );
45
+ verify = (Button ) findViewById (R .id .Verify );
46
+ FAuth = FirebaseAuth .getInstance ();
47
+
48
+ Resend .setVisibility (View .INVISIBLE );
49
+ txt .setVisibility (View .INVISIBLE );
50
+
51
+ sendverificationcode (phoneno );
52
+
53
+ verify .setOnClickListener (new View .OnClickListener () {
54
+ @ Override
55
+ public void onClick (View v ) {
56
+
57
+ String code = entercode .getText ().toString ().trim ();
58
+ Resend .setVisibility (View .INVISIBLE );
59
+
60
+ if (code .isEmpty () && code .length ()<6 ){
61
+ entercode .setError ("Enter code" );
62
+ entercode .requestFocus ();
63
+ return ;
64
+ }
65
+ verifyCode (code );
66
+ }
67
+ });
68
+
69
+ new CountDownTimer (60000 ,1000 ){
70
+
71
+ @ Override
72
+ public void onTick (long millisUntilFinished ) {
73
+
74
+ txt .setVisibility (View .VISIBLE );
75
+ txt .setText ("Resend Code Within " +millisUntilFinished /1000 +" Seconds" );
76
+
77
+ }
78
+
79
+ /**
80
+ * Callback fired when the time is up.
81
+ */
82
+ @ Override
83
+ public void onFinish () {
84
+ Resend .setVisibility (View .VISIBLE );
85
+ txt .setVisibility (View .INVISIBLE );
86
+
87
+ }
88
+ }.start ();
89
+
90
+ Resend .setOnClickListener (new View .OnClickListener () {
91
+ @ Override
92
+ public void onClick (View v ) {
93
+
94
+ Resend .setVisibility (View .INVISIBLE );
95
+ Resendotp (phoneno );
96
+
97
+ new CountDownTimer (60000 ,1000 ){
98
+
99
+ @ Override
100
+ public void onTick (long millisUntilFinished ) {
101
+
102
+ txt .setVisibility (View .VISIBLE );
103
+ txt .setText ("Resend Code Within " +millisUntilFinished /1000 +" Seconds" );
104
+
105
+ }
106
+
107
+ /**
108
+ * Callback fired when the time is up.
109
+ */
110
+ @ Override
111
+ public void onFinish () {
112
+ Resend .setVisibility (View .VISIBLE );
113
+ txt .setVisibility (View .INVISIBLE );
114
+
115
+ }
116
+ }.start ();
117
+ }
118
+ });
119
+
120
+ }
121
+
122
+ private void Resendotp (String phonenum ) {
123
+
124
+ sendverificationcode (phonenum );
125
+ }
126
+
127
+
128
+ private void sendverificationcode (String number ) {
129
+
130
+ PhoneAuthProvider .getInstance ().verifyPhoneNumber (
131
+
132
+ number ,
133
+ 60 ,
134
+ TimeUnit .SECONDS ,
135
+ TaskExecutors .MAIN_THREAD ,
136
+ mcallBack
137
+ );
138
+ }
139
+
140
+ private PhoneAuthProvider .OnVerificationStateChangedCallbacks mcallBack =new PhoneAuthProvider .OnVerificationStateChangedCallbacks (){
141
+ @ Override
142
+ public void onVerificationCompleted (@ NonNull PhoneAuthCredential phoneAuthCredential ) {
143
+
144
+ String code = phoneAuthCredential .getSmsCode ();
145
+ if (code != null ){
146
+ entercode .setText (code ); // Auto Verification
147
+ verifyCode (code );
148
+ }
149
+ }
150
+
151
+ @ Override
152
+ public void onVerificationFailed (@ NonNull FirebaseException e ) {
153
+ Toast .makeText (Chefsendotp .this , e .getMessage (),Toast .LENGTH_LONG ).show ();
154
+
155
+ }
156
+
157
+ @ Override
158
+ public void onCodeSent (String s , PhoneAuthProvider .ForceResendingToken forceResendingToken ){
159
+ super .onCodeSent (s ,forceResendingToken );
160
+
161
+ verificationId = s ;
162
+
163
+ }
164
+ };
165
+
166
+ private void verifyCode (String code ) {
167
+
168
+ PhoneAuthCredential credential = PhoneAuthProvider .getCredential (verificationId , code );
169
+ signInWithPhone (credential );
170
+ }
171
+
172
+ private void signInWithPhone (PhoneAuthCredential credential ) {
173
+
174
+ FAuth .signInWithCredential (credential )
175
+ .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
176
+ @ Override
177
+ public void onComplete (@ NonNull Task <AuthResult > task ) {
178
+
179
+ if (task .isSuccessful ()){
180
+ startActivity (new Intent (Chefsendotp .this ,ChefFoodPanel_BottomNavigation .class ));
181
+ finish ();
182
+
183
+ }else {
184
+ ReusableCodeForAll .ShowAlert (Chefsendotp .this ,"Error" ,task .getException ().getMessage ());
185
+ }
186
+
187
+ }
188
+ });
189
+
190
+ }
191
+
192
+ }
0 commit comments