Skip to content

Commit 2ca60dd

Browse files
SUPERCILEXsamtstern
authored andcommitted
Automatically submit phone verification codes when possible (#1558)
1 parent b7f3579 commit 2ca60dd

File tree

92 files changed

+80
-135
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+80
-135
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cache:
1515
- $HOME/.android/build-cache
1616
before_install:
1717
- mkdir "$ANDROID_HOME/licenses" || true
18-
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55\nd56f5187479451eabf01fb78af6dfcb131a6481e" > "$ANDROID_HOME/licenses/android-sdk-license"
18+
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55\n24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"
1919
- echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd\n504667f4c0de7af1a06de9f4b1727b84351f2910" > "$ANDROID_HOME/licenses/android-sdk-preview-license"
2020
android:
2121
components:

auth/src/main/java/com/firebase/ui/auth/ui/phone/CheckPhoneNumberFragment.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
6262
super.onCreate(savedInstanceState);
6363
mVerificationHandler = ViewModelProviders.of(requireActivity())
6464
.get(PhoneNumberVerificationHandler.class);
65-
mCheckPhoneHandler = ViewModelProviders.of(requireActivity())
65+
mCheckPhoneHandler = ViewModelProviders.of(this)
6666
.get(CheckPhoneHandler.class);
6767
}
6868

@@ -124,6 +124,9 @@ protected void onFailure(@NonNull Exception e) {
124124
// Fragment back stacks are the stuff of nightmares (what's new?): the fragment isn't
125125
// destroyed so its state isn't saved and we have to rely on an instance field. Sigh.
126126
mCalled = true;
127+
128+
// DON'T REMOVE
129+
setDefaultCountryForSpinner();
127130
}
128131

129132
@Override
@@ -202,8 +205,6 @@ private void setupCountrySpinner() {
202205
Bundle params = getArguments().getBundle(ExtraConstants.PARAMS);
203206
mCountryListSpinner.init(params);
204207

205-
setDefaultCountryForSpinner();
206-
207208
// Clear error when spinner is clicked on
208209
mCountryListSpinner.setOnClickListener(new View.OnClickListener() {
209210
@Override

auth/src/main/java/com/firebase/ui/auth/ui/phone/PhoneActivity.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.support.annotation.Nullable;
2323
import android.support.annotation.RestrictTo;
2424
import android.support.design.widget.TextInputLayout;
25+
import android.support.v4.app.FragmentManager;
2526
import android.widget.Toast;
2627

2728
import com.firebase.ui.auth.ErrorCodes;
@@ -83,7 +84,18 @@ protected void onFailure(@NonNull Exception e) {
8384
protected void onSuccess(@NonNull PhoneVerification verification) {
8485
if (verification.isAutoVerified()) {
8586
Toast.makeText(
86-
PhoneActivity.this, R.string.fui_auto_verified, Toast.LENGTH_LONG).show();
87+
PhoneActivity.this,
88+
R.string.fui_auto_verified,
89+
Toast.LENGTH_LONG
90+
).show();
91+
92+
FragmentManager manager = getSupportFragmentManager();
93+
if (manager.findFragmentByTag(SubmitConfirmationCodeFragment.TAG) != null) {
94+
// Ensure the submit code screen isn't visible if there's no code to submit.
95+
// It's possible to get into this state when an SMS is sent, but then
96+
// automatically retrieved.
97+
manager.popBackStack();
98+
}
8799
}
88100

89101
handler.startSignIn(verification.getCredential(), new IdpResponse.Builder(

auth/src/main/java/com/firebase/ui/auth/ui/phone/SubmitConfirmationCodeFragment.java

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,45 @@
1414

1515
package com.firebase.ui.auth.ui.phone;
1616

17+
import android.arch.lifecycle.Observer;
1718
import android.arch.lifecycle.ViewModelProviders;
19+
import android.content.ClipData;
20+
import android.content.ClipboardManager;
1821
import android.content.Context;
1922
import android.os.Bundle;
2023
import android.os.Handler;
2124
import android.support.annotation.NonNull;
2225
import android.support.annotation.Nullable;
2326
import android.support.annotation.RestrictTo;
27+
import android.support.v4.content.ContextCompat;
2428
import android.view.LayoutInflater;
2529
import android.view.View;
2630
import android.view.ViewGroup;
2731
import android.view.inputmethod.InputMethodManager;
28-
import android.widget.Button;
2932
import android.widget.ProgressBar;
3033
import android.widget.TextView;
3134

35+
import com.firebase.ui.auth.IdpResponse;
3236
import com.firebase.ui.auth.R;
37+
import com.firebase.ui.auth.data.model.Resource;
38+
import com.firebase.ui.auth.data.model.State;
3339
import com.firebase.ui.auth.ui.FragmentBase;
3440
import com.firebase.ui.auth.util.ExtraConstants;
3541
import com.firebase.ui.auth.util.data.PrivacyDisclosureUtils;
3642
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;
3844

3945
import 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)
4551
public 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
}

auth/src/main/java/com/firebase/ui/auth/util/ui/BucketedTextChangeListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface ContentChangeCallback {
4646
* Idempotent function invoked by the listener when the edit text changes and is of expected
4747
* length
4848
*/
49-
void whileComplete();
49+
void whenComplete();
5050

5151
/**
5252
* Idempotent function invoked by the listener when the edit text changes and is not of
@@ -114,7 +114,7 @@ public void onTextChanged(
114114

115115
// 4) Callback listeners waiting on content to be of expected length
116116
if (enteredContentLength == mExpectedContentLength && mCallback != null) {
117-
mCallback.whileComplete();
117+
mCallback.whenComplete();
118118
} else if (mCallback != null) {
119119
mCallback.whileIncomplete();
120120
}

auth/src/main/res/layout/fui_confirmation_code_layout.xml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@
6161

6262
</android.support.design.widget.TextInputLayout>
6363

64-
<Button
65-
android:id="@+id/submit_confirmation_code"
66-
style="@style/FirebaseUI.VerifyPhoneButton"
67-
android:layout_gravity="end"
68-
android:text="@string/fui_continue_phone_login"
69-
app:layout_constraintTop_toBottomOf="@+id/confirmation_code_layout" />
70-
7164
<TextView
7265
android:id="@+id/ticker"
7366
style="@style/FirebaseUI.Text.T08"
@@ -80,7 +73,7 @@
8073
android:textIsSelectable="false"
8174
app:layout_constraintStart_toStartOf="parent"
8275
app:layout_constraintEnd_toEndOf="parent"
83-
app:layout_constraintTop_toBottomOf="@+id/submit_confirmation_code"
76+
app:layout_constraintTop_toBottomOf="@+id/confirmation_code_layout"
8477
tools:text="Resend in 0:01" />
8578

8679
<TextView

auth/src/main/res/values-ar/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">تمّ التحقّق تلقائيًا من رقم الهاتف.</string>
8484
<string name="fui_resend_code">إعادة إرسال الرمز</string>
8585
<string name="fui_verify_phone_number">تأكيد ملكية رقم الهاتف</string>
86-
<string name="fui_continue_phone_login">متابعة</string>
8786
<string name="fui_sms_terms_of_service">عند النقر على “%1$s”، قد يتمّ إرسال رسالة قصيرة SMS وقد يتمّ تطبيق رسوم الرسائل والبيانات.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">يشير النقر على “%1$s” إلى موافقتك على %2$s و%3$s. وقد يتمّ إرسال رسالة قصيرة كما قد تنطبق رسوم الرسائل والبيانات.</string>
8988
</resources>

auth/src/main/res/values-b+es+419/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Se verificó automáticamente el número de teléfono</string>
8484
<string name="fui_resend_code">Reenviar código</string>
8585
<string name="fui_verify_phone_number">Verificar número de teléfono</string>
86-
<string name="fui_continue_phone_login">Continuar</string>
8786
<string name="fui_sms_terms_of_service">Si presionas “%1$s”, se enviará un SMS. Se aplicarán las tarifas de mensajes y datos.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Si presionas “%1$s”, indicas que aceptas nuestras %2$s y %3$s. Es posible que se te envíe un SMS. Podrían aplicarse las tarifas de mensajes y datos.</string>
8988
</resources>

auth/src/main/res/values-bg/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Телефонният номер е потвърден автоматично</string>
8484
<string name="fui_resend_code">Повторно изпращане на кода</string>
8585
<string name="fui_verify_phone_number">Потвърждаване на телефонния номер</string>
86-
<string name="fui_continue_phone_login">Напред</string>
8786
<string name="fui_sms_terms_of_service">Докосвайки „%1$s“, може да получите SMS съобщение. То може да се таксува по тарифите за данни и SMS.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Докосвайки „%1$s“, приемате нашите %2$s и %3$s. Възможно е да получите SMS съобщение. То може да се таксува по тарифите за данни и SMS.</string>
8988
</resources>

auth/src/main/res/values-bn/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">ফোন নম্বরটি নিজে থেকে যাচাই করা হয়েছে</string>
8484
<string name="fui_resend_code">কোডটি আবার পাঠান</string>
8585
<string name="fui_verify_phone_number">ফোন নম্বর যাচাই করুন</string>
86-
<string name="fui_continue_phone_login">চালিয়ে যান</string>
8786
<string name="fui_sms_terms_of_service">%1$s এ ট্যাপ করলে আপনি একটি এসএমএস পাঠাতে পারেন। মেসেজ ও ডেটার চার্জ প্রযোজ্য।</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">“%1$s” বোতামে ট্যাপ করার অর্থ, আপনি আমাদের %2$s এবং %3$s-এর সাথে সম্মত। একটি এসএমএস পাঠানো হতে পারে। মেসেজ এবং ডেটার উপরে প্রযোজ্য চার্জ লাগতে পারে।</string>
8988
</resources>

auth/src/main/res/values-ca/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">El número de telèfon s\'ha verificat automàticament</string>
8484
<string name="fui_resend_code">Torna a enviar el codi</string>
8585
<string name="fui_verify_phone_number">Verifica el número de telèfon</string>
86-
<string name="fui_continue_phone_login">Continua</string>
8786
<string name="fui_sms_terms_of_service">En tocar %1$s, és possible que s\'enviï un SMS. Es poden aplicar tarifes de dades i missatges.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">En tocar %1$s, acceptes les nostres %2$s i la nostra %3$s. És possible que s\'enviï un SMS. Es poden aplicar tarifes de dades i missatges.</string>
8988
</resources>

auth/src/main/res/values-cs/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Telefonní číslo bylo automaticky ověřeno</string>
8484
<string name="fui_resend_code">Znovu poslat kód</string>
8585
<string name="fui_verify_phone_number">Ověřit telefonní číslo</string>
86-
<string name="fui_continue_phone_login">Pokračovat</string>
8786
<string name="fui_sms_terms_of_service">Po klepnutí na možnost %1$s může být odeslána SMS. Mohou být účtovány poplatky za zprávy a data.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Klepnutím na tlačítko %1$s vyjadřujete svůj souhlas s dokumenty %2$s a %3$s. Může být odeslána SMS a mohou být účtovány poplatky za zprávy a data.</string>
8988
</resources>

auth/src/main/res/values-da/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Telefonnummeret blev bekræftet automatisk</string>
8484
<string name="fui_resend_code">Send koden igen</string>
8585
<string name="fui_verify_phone_number">Bekræft telefonnummer</string>
86-
<string name="fui_continue_phone_login">Fortsæt</string>
8786
<string name="fui_sms_terms_of_service">Når du trykker på “%1$s”, sendes der måske en sms. Der opkræves muligvis gebyrer for beskeder og data.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Når du trykker på “%1$s”, indikerer du, at du accepterer vores %2$s og %3$s. Der sendes måske en sms. Der opkræves muligvis gebyrer for beskeder og data.</string>
8988
</resources>

auth/src/main/res/values-de-rAT/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Telefonnummer wurde automatisch bestätigt</string>
8484
<string name="fui_resend_code">Code erneut senden</string>
8585
<string name="fui_verify_phone_number">Telefonnummer bestätigen</string>
86-
<string name="fui_continue_phone_login">Weiter</string>
8786
<string name="fui_sms_terms_of_service">Wenn Sie auf “%1$s” tippen, erhalten Sie möglicherweise eine SMS. Es können Gebühren für SMS und Datenübertragung anfallen.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Indem Sie auf “%1$s” tippen, stimmen Sie unseren %2$s und unserer %3$s zu. Sie erhalten möglicherweise eine SMS und es können Gebühren für die Nachricht und die Datenübertragung anfallen.</string>
8988
</resources>

auth/src/main/res/values-de-rCH/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<string name="fui_auto_verified">Telefonnummer wurde automatisch bestätigt</string>
8484
<string name="fui_resend_code">Code erneut senden</string>
8585
<string name="fui_verify_phone_number">Telefonnummer bestätigen</string>
86-
<string name="fui_continue_phone_login">Weiter</string>
8786
<string name="fui_sms_terms_of_service">Wenn Sie auf “%1$s” tippen, erhalten Sie möglicherweise eine SMS. Es können Gebühren für SMS und Datenübertragung anfallen.</string>
8887
<string name="fui_sms_terms_of_service_and_privacy_policy_extended">Indem Sie auf “%1$s” tippen, stimmen Sie unseren %2$s und unserer %3$s zu. Sie erhalten möglicherweise eine SMS und es können Gebühren für die Nachricht und die Datenübertragung anfallen.</string>
8988
</resources>

0 commit comments

Comments
 (0)