1414
1515package com .firebase .ui .auth .ui .phone ;
1616
17+ import android .arch .lifecycle .Observer ;
1718import android .arch .lifecycle .ViewModelProviders ;
19+ import android .content .ClipData ;
20+ import android .content .ClipboardManager ;
1821import android .content .Context ;
1922import android .os .Bundle ;
2023import android .os .Handler ;
2124import android .support .annotation .NonNull ;
2225import android .support .annotation .Nullable ;
2326import android .support .annotation .RestrictTo ;
27+ import android .support .v4 .content .ContextCompat ;
2428import android .view .LayoutInflater ;
2529import android .view .View ;
2630import android .view .ViewGroup ;
2731import android .view .inputmethod .InputMethodManager ;
28- import android .widget .Button ;
2932import android .widget .ProgressBar ;
3033import android .widget .TextView ;
3134
35+ import com .firebase .ui .auth .IdpResponse ;
3236import com .firebase .ui .auth .R ;
37+ import com .firebase .ui .auth .data .model .Resource ;
38+ import com .firebase .ui .auth .data .model .State ;
3339import com .firebase .ui .auth .ui .FragmentBase ;
3440import com .firebase .ui .auth .util .ExtraConstants ;
3541import com .firebase .ui .auth .util .data .PrivacyDisclosureUtils ;
3642import 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 ;
3844
3945import java .util .concurrent .TimeUnit ;
4046
4147/**
42- * Display confirmation code to verify phone numbers input in {{ @link CheckPhoneNumberFragment} }
48+ * Display confirmation code to verify phone numbers input in {@link CheckPhoneNumberFragment}
4349 */
4450@ RestrictTo (RestrictTo .Scope .LIBRARY_GROUP )
4551public class SubmitConfirmationCodeFragment extends FragmentBase {
4652
4753 public static final String TAG = "SubmitConfirmationCodeFragment" ;
4854
55+ private static final int VERIFICATION_CODE_LENGTH = 6 ;
4956 private static final long RESEND_WAIT_MILLIS = 15000 ;
5057 private static final long TICK_INTERVAL_MILLIS = 500 ;
5158 private static final String EXTRA_MILLIS_UNTIL_FINISHED = "millis_until_finished" ;
@@ -66,9 +73,10 @@ public void run() {
6673 private TextView mResendCodeTextView ;
6774 private TextView mCountDownTextView ;
6875 private SpacedEditText mConfirmationCodeEditText ;
69- private Button mSubmitConfirmationButton ;
7076 private long mMillisUntilFinished = RESEND_WAIT_MILLIS ;
7177
78+ private boolean mHasResumed ;
79+
7280 public static SubmitConfirmationCodeFragment newInstance (String phoneNumber ) {
7381 SubmitConfirmationCodeFragment fragment = new SubmitConfirmationCodeFragment ();
7482 Bundle args = new Bundle ();
@@ -103,11 +111,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
103111 mCountDownTextView = view .findViewById (R .id .ticker );
104112 mResendCodeTextView = view .findViewById (R .id .resend_code );
105113 mConfirmationCodeEditText = view .findViewById (R .id .confirmation_code );
106- mSubmitConfirmationButton = view .findViewById (R .id .submit_confirmation_code );
107114
108115 requireActivity ().setTitle (getString (R .string .fui_verify_your_phone_title ));
109116 processCountdownTick ();
110- setupSubmitConfirmationButton ();
111117 setupConfirmationCodeEditText ();
112118 setupEditPhoneNumberTextView ();
113119 setupResendConfirmationCodeTextView ();
@@ -117,6 +123,22 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
117123 view .<TextView >findViewById (R .id .email_footer_tos_and_pp_text ));
118124 }
119125
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+
120142 @ Override
121143 public void onStart () {
122144 super .onStart ();
@@ -125,6 +147,32 @@ public void onStart() {
125147 .showSoftInput (mConfirmationCodeEditText , 0 );
126148 }
127149
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+
128176 @ Override
129177 public void onSaveInstanceState (@ NonNull Bundle outState ) {
130178 mLooper .removeCallbacks (mCountdown );
@@ -139,41 +187,19 @@ public void onDestroy() {
139187 mLooper .removeCallbacks (mCountdown );
140188 }
141189
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-
152190 private void setupConfirmationCodeEditText () {
153191 mConfirmationCodeEditText .setText ("------" );
154192 mConfirmationCodeEditText .addTextChangedListener (new BucketedTextChangeListener (
155- mConfirmationCodeEditText , 6 , "-" ,
193+ mConfirmationCodeEditText , VERIFICATION_CODE_LENGTH , "-" ,
156194 new BucketedTextChangeListener .ContentChangeCallback () {
157195 @ Override
158- public void whileComplete () {
159- mSubmitConfirmationButton . setEnabled ( true );
196+ public void whenComplete () {
197+ submitCode ( );
160198 }
161199
162200 @ Override
163- public void whileIncomplete () {
164- mSubmitConfirmationButton .setEnabled (false );
165- }
201+ public void whileIncomplete () {}
166202 }));
167-
168- ImeHelper .setImeOnDoneListener (mConfirmationCodeEditText ,
169- new ImeHelper .DonePressedListener () {
170- @ Override
171- public void onDonePressed () {
172- if (mSubmitConfirmationButton .isEnabled ()) {
173- submitCode ();
174- }
175- }
176- });
177203 }
178204
179205 private void setupEditPhoneNumberTextView () {
@@ -222,13 +248,11 @@ private void submitCode() {
222248
223249 @ Override
224250 public void showProgress (int message ) {
225- mSubmitConfirmationButton .setEnabled (false );
226251 mProgressBar .setVisibility (View .VISIBLE );
227252 }
228253
229254 @ Override
230255 public void hideProgress () {
231- mSubmitConfirmationButton .setEnabled (true );
232256 mProgressBar .setVisibility (View .INVISIBLE );
233257 }
234258}
0 commit comments