Skip to content

Commit d8c82c8

Browse files
authored
Prevent back-button state loss bug with phone verification (#1260)
1 parent 50a4ec7 commit d8c82c8

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

auth/src/main/java/com/firebase/ui/auth/data/model/CountryInfo.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@
1818
*/
1919
package com.firebase.ui.auth.data.model;
2020

21+
import android.os.Parcel;
22+
import android.os.Parcelable;
2123
import android.support.annotation.RestrictTo;
2224

2325
import java.text.Collator;
2426
import java.util.Locale;
2527

2628
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
27-
public final class CountryInfo implements Comparable<CountryInfo> {
29+
public final class CountryInfo implements Comparable<CountryInfo>, Parcelable {
30+
31+
public static final Parcelable.Creator<CountryInfo> CREATOR = new Parcelable.Creator<CountryInfo>() {
32+
@Override
33+
public CountryInfo createFromParcel(Parcel source) {
34+
return new CountryInfo(source);
35+
}
36+
37+
@Override
38+
public CountryInfo[] newArray(int size) {
39+
return new CountryInfo[size];
40+
}
41+
};
42+
2843
private final Collator mCollator;
2944
private final Locale mLocale;
3045
private final int mCountryCode;
@@ -36,6 +51,14 @@ public CountryInfo(Locale locale, int countryCode) {
3651
mCountryCode = countryCode;
3752
}
3853

54+
protected CountryInfo(Parcel in) {
55+
mCollator = Collator.getInstance(Locale.getDefault());
56+
mCollator.setStrength(Collator.PRIMARY);
57+
58+
mLocale = (Locale) in.readSerializable();
59+
mCountryCode = in.readInt();
60+
}
61+
3962
public static String localeToEmoji(Locale locale) {
4063
String countryCode = locale.getCountry();
4164
// 0x41 is Letter A
@@ -85,4 +108,15 @@ public String toString() {
85108
public int compareTo(CountryInfo info) {
86109
return mCollator.compare(mLocale.getDisplayCountry(), info.mLocale.getDisplayCountry());
87110
}
111+
112+
@Override
113+
public int describeContents() {
114+
return 0;
115+
}
116+
117+
@Override
118+
public void writeToParcel(Parcel dest, int flags) {
119+
dest.writeSerializable(mLocale);
120+
dest.writeInt(mCountryCode);
121+
}
88122
}

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.content.Context;
2222
import android.content.DialogInterface;
2323
import android.os.AsyncTask;
24+
import android.os.Bundle;
25+
import android.os.Parcelable;
2426
import android.support.v7.widget.AppCompatEditText;
2527
import android.text.TextUtils;
2628
import android.util.AttributeSet;
@@ -37,11 +39,16 @@
3739

3840
public final class CountryListSpinner extends AppCompatEditText implements
3941
View.OnClickListener, CountryListLoadTask.Listener {
42+
43+
private static final String KEY_SUPER_STATE = "KEY_SUPER_STATE";
44+
private static final String KEY_COUNTRY_INFO = "KEY_COUNTRY_INFO";
45+
4046
private final String mTextFormat;
4147
private final DialogPopup mDialogPopup;
4248
private final CountryListAdapter mCountryListAdapter;
4349
private OnClickListener mListener;
4450
private String mSelectedCountryName;
51+
private CountryInfo mSelectedCountryInfo;
4552

4653
public CountryListSpinner(Context context) {
4754
this(context, null, android.R.attr.spinnerStyle);
@@ -60,7 +67,32 @@ public CountryListSpinner(Context context, AttributeSet attrs, int defStyle) {
6067
mTextFormat = "%1$s +%2$d";
6168
mSelectedCountryName = "";
6269
CountryInfo countryInfo = PhoneNumberUtils.getCurrentCountryInfo(getContext());
63-
setSpinnerText(countryInfo.getCountryCode(), countryInfo.getLocale());
70+
setSelectedForCountry(countryInfo.getCountryCode(), countryInfo.getLocale());
71+
}
72+
73+
@Override
74+
public Parcelable onSaveInstanceState() {
75+
Parcelable superState = super.onSaveInstanceState();
76+
77+
Bundle bundle = new Bundle();
78+
bundle.putParcelable(KEY_SUPER_STATE, superState);
79+
bundle.putParcelable(KEY_COUNTRY_INFO, mSelectedCountryInfo);
80+
81+
return bundle;
82+
}
83+
84+
@Override
85+
public void onRestoreInstanceState(Parcelable state) {
86+
if (!(state instanceof Bundle)) {
87+
super.onRestoreInstanceState(state);
88+
return;
89+
}
90+
91+
Bundle bundle = (Bundle) state;
92+
Parcelable superState = bundle.getParcelable(KEY_SUPER_STATE);
93+
mSelectedCountryInfo = bundle.getParcelable(KEY_COUNTRY_INFO);
94+
95+
super.onRestoreInstanceState(superState);
6496
}
6597

6698
private static void hideKeyboard(Context context, View view) {
@@ -70,19 +102,23 @@ private static void hideKeyboard(Context context, View view) {
70102
}
71103
}
72104

73-
private void setSpinnerText(int countryCode, Locale locale) {
105+
private void setSelectedForCountry(int countryCode, Locale locale) {
74106
setText(String.format(mTextFormat, CountryInfo.localeToEmoji(locale), countryCode));
75-
setTag(new CountryInfo(locale, countryCode));
107+
mSelectedCountryInfo = new CountryInfo(locale, countryCode);
76108
}
77109

78110
public void setSelectedForCountry(final Locale locale, String countryCode) {
79111
final String countryName = locale.getDisplayName();
80112
if (!TextUtils.isEmpty(countryName) && !TextUtils.isEmpty(countryCode)) {
81113
mSelectedCountryName = countryName;
82-
setSpinnerText(Integer.parseInt(countryCode), locale);
114+
setSelectedForCountry(Integer.parseInt(countryCode), locale);
83115
}
84116
}
85117

118+
public CountryInfo getSelectedCountryInfo() {
119+
return mSelectedCountryInfo;
120+
}
121+
86122
@Override
87123
protected void onDetachedFromWindow() {
88124
super.onDetachedFromWindow();
@@ -169,7 +205,7 @@ public void run() {
169205
public void onClick(DialogInterface dialog, int which) {
170206
final CountryInfo countryInfo = listAdapter.getItem(which);
171207
mSelectedCountryName = countryInfo.getLocale().getDisplayCountry();
172-
setSpinnerText(countryInfo.getCountryCode(), countryInfo.getLocale());
208+
setSelectedForCountry(countryInfo.getCountryCode(), countryInfo.getLocale());
173209
dismiss();
174210
}
175211
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,14 @@ private void onNext() {
202202
if (phoneNumber == null) {
203203
mPhoneInputLayout.setError(getString(R.string.fui_invalid_phone_number));
204204
} else {
205+
mPhoneInputLayout.setError(null);
205206
mVerifier.verifyPhoneNumber(phoneNumber, false);
206207
}
207208
}
208209

209210
@Nullable
210211
private String getPseudoValidPhoneNumber() {
211-
final CountryInfo countryInfo = (CountryInfo) mCountryListSpinner.getTag();
212+
final CountryInfo countryInfo = mCountryListSpinner.getSelectedCountryInfo();
212213
final String everythingElse = mPhoneEditText.getText().toString();
213214

214215
if (TextUtils.isEmpty(everythingElse)) {

auth/src/test/java/com/firebase/ui/auth/ui/phone/PhoneActivityTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void testDefaultFullPhoneNumber_prePopulatesPhoneNumberInBundle() {
131131

132132
assertEquals(PHONE_NO_COUNTRY_CODE, mPhoneEditText.getText().toString());
133133
assertEquals(YE_COUNTRY_CODE,
134-
String.valueOf(((CountryInfo) mCountryListSpinner.getTag()).getCountryCode()));
134+
String.valueOf((mCountryListSpinner.getSelectedCountryInfo()).getCountryCode()));
135135
}
136136

137137
@Test
@@ -157,9 +157,9 @@ public void testDefaultCountryCodeAndNationalNumber_prePopulatesPhoneNumberInBun
157157

158158
assertEquals(PHONE_NO_COUNTRY_CODE, mPhoneEditText.getText().toString());
159159
assertEquals(CA_COUNTRY_CODE,
160-
String.valueOf(((CountryInfo) mCountryListSpinner.getTag()).getCountryCode()));
160+
String.valueOf((mCountryListSpinner.getSelectedCountryInfo()).getCountryCode()));
161161
assertEquals(new Locale("", CA_ISO2),
162-
((CountryInfo) mCountryListSpinner.getTag()).getLocale());
162+
((CountryInfo) mCountryListSpinner.getSelectedCountryInfo()).getLocale());
163163
}
164164

165165
@Test

0 commit comments

Comments
 (0)