14
14
15
15
package com .firebase .ui .auth .ui .phone ;
16
16
17
+ import android .arch .lifecycle .Observer ;
17
18
import android .arch .lifecycle .ViewModelProviders ;
19
+ import android .content .ClipData ;
20
+ import android .content .ClipboardManager ;
18
21
import android .content .Context ;
19
22
import android .os .Bundle ;
20
23
import android .os .Handler ;
21
24
import android .support .annotation .NonNull ;
22
25
import android .support .annotation .Nullable ;
23
26
import android .support .annotation .RestrictTo ;
27
+ import android .support .v4 .content .ContextCompat ;
24
28
import android .view .LayoutInflater ;
25
29
import android .view .View ;
26
30
import android .view .ViewGroup ;
27
31
import android .view .inputmethod .InputMethodManager ;
28
- import android .widget .Button ;
29
32
import android .widget .ProgressBar ;
30
33
import android .widget .TextView ;
31
34
35
+ import com .firebase .ui .auth .IdpResponse ;
32
36
import com .firebase .ui .auth .R ;
37
+ import com .firebase .ui .auth .data .model .Resource ;
38
+ import com .firebase .ui .auth .data .model .State ;
33
39
import com .firebase .ui .auth .ui .FragmentBase ;
34
40
import com .firebase .ui .auth .util .ExtraConstants ;
35
41
import com .firebase .ui .auth .util .data .PrivacyDisclosureUtils ;
36
42
import com .firebase .ui .auth .util .ui .BucketedTextChangeListener ;
37
- import com .firebase .ui .auth .util . ui . ImeHelper ;
43
+ import com .firebase .ui .auth .viewmodel . phone . PhoneProviderResponseHandler ;
38
44
39
45
import java .util .concurrent .TimeUnit ;
40
46
41
47
/**
42
- * Display confirmation code to verify phone numbers input in {{ @link CheckPhoneNumberFragment} }
48
+ * Display confirmation code to verify phone numbers input in {@link CheckPhoneNumberFragment}
43
49
*/
44
50
@ RestrictTo (RestrictTo .Scope .LIBRARY_GROUP )
45
51
public class SubmitConfirmationCodeFragment extends FragmentBase {
46
52
47
53
public static final String TAG = "SubmitConfirmationCodeFragment" ;
48
54
55
+ private static final int VERIFICATION_CODE_LENGTH = 6 ;
49
56
private static final long RESEND_WAIT_MILLIS = 15000 ;
50
57
private static final long TICK_INTERVAL_MILLIS = 500 ;
51
58
private static final String EXTRA_MILLIS_UNTIL_FINISHED = "millis_until_finished" ;
@@ -66,9 +73,10 @@ public void run() {
66
73
private TextView mResendCodeTextView ;
67
74
private TextView mCountDownTextView ;
68
75
private SpacedEditText mConfirmationCodeEditText ;
69
- private Button mSubmitConfirmationButton ;
70
76
private long mMillisUntilFinished = RESEND_WAIT_MILLIS ;
71
77
78
+ private boolean mHasResumed ;
79
+
72
80
public static SubmitConfirmationCodeFragment newInstance (String phoneNumber ) {
73
81
SubmitConfirmationCodeFragment fragment = new SubmitConfirmationCodeFragment ();
74
82
Bundle args = new Bundle ();
@@ -103,11 +111,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
103
111
mCountDownTextView = view .findViewById (R .id .ticker );
104
112
mResendCodeTextView = view .findViewById (R .id .resend_code );
105
113
mConfirmationCodeEditText = view .findViewById (R .id .confirmation_code );
106
- mSubmitConfirmationButton = view .findViewById (R .id .submit_confirmation_code );
107
114
108
115
requireActivity ().setTitle (getString (R .string .fui_verify_your_phone_title ));
109
116
processCountdownTick ();
110
- setupSubmitConfirmationButton ();
111
117
setupConfirmationCodeEditText ();
112
118
setupEditPhoneNumberTextView ();
113
119
setupResendConfirmationCodeTextView ();
@@ -117,6 +123,22 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
117
123
view .<TextView >findViewById (R .id .email_footer_tos_and_pp_text ));
118
124
}
119
125
126
+ @ Override
127
+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
128
+ super .onActivityCreated (savedInstanceState );
129
+ ViewModelProviders .of (requireActivity ())
130
+ .get (PhoneProviderResponseHandler .class )
131
+ .getOperation ()
132
+ .observe (this , new Observer <Resource <IdpResponse >>() {
133
+ @ Override
134
+ public void onChanged (@ Nullable Resource <IdpResponse > resource ) {
135
+ if (resource .getState () == State .FAILURE ) {
136
+ mConfirmationCodeEditText .setText ("" );
137
+ }
138
+ }
139
+ });
140
+ }
141
+
120
142
@ Override
121
143
public void onStart () {
122
144
super .onStart ();
@@ -125,6 +147,32 @@ public void onStart() {
125
147
.showSoftInput (mConfirmationCodeEditText , 0 );
126
148
}
127
149
150
+ @ Override
151
+ public void onResume () {
152
+ super .onResume ();
153
+ if (!mHasResumed ) {
154
+ // Don't check for codes before we've even had the chance to send one.
155
+ mHasResumed = true ;
156
+ return ;
157
+ }
158
+
159
+ ClipData clip = ContextCompat .getSystemService (requireContext (), ClipboardManager .class )
160
+ .getPrimaryClip ();
161
+ if (clip != null && clip .getItemCount () == 1 ) {
162
+ CharSequence candidate = clip .getItemAt (0 ).getText ();
163
+ if (candidate != null && candidate .length () == VERIFICATION_CODE_LENGTH ) {
164
+ try {
165
+ Integer .parseInt (candidate .toString ());
166
+
167
+ // We have a number! Try to submit it.
168
+ mConfirmationCodeEditText .setText (candidate );
169
+ } catch (NumberFormatException ignored ) {
170
+ // Turns out it wasn't a number
171
+ }
172
+ }
173
+ }
174
+ }
175
+
128
176
@ Override
129
177
public void onSaveInstanceState (@ NonNull Bundle outState ) {
130
178
mLooper .removeCallbacks (mCountdown );
@@ -139,41 +187,19 @@ public void onDestroy() {
139
187
mLooper .removeCallbacks (mCountdown );
140
188
}
141
189
142
- private void setupSubmitConfirmationButton () {
143
- mSubmitConfirmationButton .setEnabled (false );
144
- mSubmitConfirmationButton .setOnClickListener (new View .OnClickListener () {
145
- @ Override
146
- public void onClick (View v ) {
147
- submitCode ();
148
- }
149
- });
150
- }
151
-
152
190
private void setupConfirmationCodeEditText () {
153
191
mConfirmationCodeEditText .setText ("------" );
154
192
mConfirmationCodeEditText .addTextChangedListener (new BucketedTextChangeListener (
155
- mConfirmationCodeEditText , 6 , "-" ,
193
+ mConfirmationCodeEditText , VERIFICATION_CODE_LENGTH , "-" ,
156
194
new BucketedTextChangeListener .ContentChangeCallback () {
157
195
@ Override
158
- public void whileComplete () {
159
- mSubmitConfirmationButton . setEnabled ( true );
196
+ public void whenComplete () {
197
+ submitCode ( );
160
198
}
161
199
162
200
@ Override
163
- public void whileIncomplete () {
164
- mSubmitConfirmationButton .setEnabled (false );
165
- }
201
+ public void whileIncomplete () {}
166
202
}));
167
-
168
- ImeHelper .setImeOnDoneListener (mConfirmationCodeEditText ,
169
- new ImeHelper .DonePressedListener () {
170
- @ Override
171
- public void onDonePressed () {
172
- if (mSubmitConfirmationButton .isEnabled ()) {
173
- submitCode ();
174
- }
175
- }
176
- });
177
203
}
178
204
179
205
private void setupEditPhoneNumberTextView () {
@@ -222,13 +248,11 @@ private void submitCode() {
222
248
223
249
@ Override
224
250
public void showProgress (int message ) {
225
- mSubmitConfirmationButton .setEnabled (false );
226
251
mProgressBar .setVisibility (View .VISIBLE );
227
252
}
228
253
229
254
@ Override
230
255
public void hideProgress () {
231
- mSubmitConfirmationButton .setEnabled (true );
232
256
mProgressBar .setVisibility (View .INVISIBLE );
233
257
}
234
258
}
0 commit comments