17
17
import android .content .Context ;
18
18
import android .content .Intent ;
19
19
import android .graphics .Typeface ;
20
- import android .net .Uri ;
21
20
import android .os .Bundle ;
22
21
import android .support .annotation .NonNull ;
23
22
import android .support .design .widget .TextInputLayout ;
24
23
import android .text .Spannable ;
25
24
import android .text .SpannableStringBuilder ;
26
25
import android .text .style .StyleSpan ;
27
26
import android .view .View ;
28
- import android .widget .Button ;
29
27
import android .widget .EditText ;
30
- import android .widget .ImageView ;
31
28
import android .widget .TextView ;
32
29
33
30
import com .firebase .ui .auth .R ;
41
38
import com .firebase .ui .auth .ui .email .PasswordToggler ;
42
39
import com .firebase .ui .auth .ui .email .RecoverPasswordActivity ;
43
40
import com .firebase .ui .auth .util .SmartlockUtil ;
44
- import com .google .android .gms .tasks .OnCompleteListener ;
41
+ import com .google .android .gms .tasks .OnFailureListener ;
45
42
import com .google .android .gms .tasks .OnSuccessListener ;
46
- import com .google .android .gms .tasks .Task ;
47
43
import com .google .firebase .auth .AuthCredential ;
48
44
import com .google .firebase .auth .AuthResult ;
49
45
import com .google .firebase .auth .FirebaseAuth ;
50
- import com .google .firebase .auth .FirebaseUser ;
51
46
47
+ /**
48
+ * Activity to link a pre-existing email/password account to a new IDP sign-in by confirming
49
+ * the password before initiating a link.
50
+ */
52
51
public class WelcomeBackPasswordPrompt extends AppCompatBase implements View .OnClickListener {
52
+
53
53
private static final int RC_CREDENTIAL_SAVE = 3 ;
54
54
private static final String TAG = "WelcomeBackPassword" ;
55
- final StyleSpan bold = new StyleSpan (Typeface .BOLD );
55
+ private static final StyleSpan BOLD = new StyleSpan (Typeface .BOLD );
56
+
56
57
private String mEmail ;
57
58
private TextInputLayout mPasswordLayout ;
58
59
private EditText mPasswordField ;
@@ -62,29 +63,34 @@ public class WelcomeBackPasswordPrompt extends AppCompatBase implements View.OnC
62
63
protected void onCreate (Bundle savedInstanceState ) {
63
64
super .onCreate (savedInstanceState );
64
65
setContentView (R .layout .welcome_back_password_prompt_layout );
66
+
65
67
mPasswordLayout = (TextInputLayout ) findViewById (R .id .password_layout );
68
+ mPasswordField = (EditText ) findViewById (R .id .password );
69
+
66
70
mIdpResponse = getIntent ().getParcelableExtra (ExtraConstants .EXTRA_IDP_RESPONSE );
67
71
mEmail = mIdpResponse .getEmail ();
68
- TextView bodyTextView = (TextView ) findViewById (R .id .welcome_back_password_body );
72
+
73
+ // Create welcome back text with email bolded
69
74
String bodyText = getResources ().getString (R .string .welcome_back_password_prompt_body );
70
75
bodyText = String .format (bodyText , mEmail );
71
76
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder (bodyText );
72
77
int emailStart = bodyText .indexOf (mEmail );
73
- spannableStringBuilder .setSpan (bold , emailStart , emailStart + mEmail .length (), Spannable
78
+ spannableStringBuilder .setSpan (BOLD , emailStart , emailStart + mEmail .length (), Spannable
74
79
.SPAN_INCLUSIVE_INCLUSIVE );
80
+
81
+ TextView bodyTextView = ((TextView ) findViewById (R .id .welcome_back_password_body ));
75
82
bodyTextView .setText (spannableStringBuilder );
76
- Button signIn = (Button ) findViewById (R .id .button_done );
77
- signIn .setOnClickListener (this );
78
- mPasswordField = (EditText ) findViewById (R .id .password );
79
- ImageView toggleImage = (ImageView ) findViewById (R .id .toggle_visibility );
80
- toggleImage .setOnClickListener (new PasswordToggler (mPasswordField ));
81
- TextView troubleSigningIn = (TextView ) findViewById (R .id .trouble_signing_in );
82
- troubleSigningIn .setOnClickListener (this );
83
+
84
+ // Click listeners
85
+ findViewById (R .id .button_done ).setOnClickListener (this );
86
+ findViewById (R .id .toggle_visibility ).setOnClickListener (
87
+ new PasswordToggler (mPasswordField ));
88
+ findViewById (R .id .trouble_signing_in ).setOnClickListener (this );
83
89
}
84
90
85
91
@ Override
86
92
public void onClick (View view ) {
87
- int id = view .getId ();
93
+ final int id = view .getId ();
88
94
if (id == R .id .button_done ) {
89
95
mActivityHelper .showLoadingDialog (R .string .progress_dialog_signing_in );
90
96
next (mEmail , mPasswordField .getText ().toString ());
@@ -108,48 +114,48 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
108
114
109
115
private void next (String email , final String password ) {
110
116
final FirebaseAuth firebaseAuth = mActivityHelper .getFirebaseAuth ();
117
+
118
+ // Sign in with known email and the password provided
111
119
firebaseAuth .signInWithEmailAndPassword (email , password )
112
120
.addOnFailureListener (
113
121
new TaskFailureLogger (TAG , "Error signing in with email and password" ))
114
- .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
115
- @ Override
116
- public void onComplete (@ NonNull Task <AuthResult > task ) {
117
- if (task .isSuccessful ()) {
118
- AuthCredential authCredential =
119
- AuthCredentialHelper .getAuthCredential (mIdpResponse );
120
- task .getResult ().getUser ().linkWithCredential (authCredential );
121
- firebaseAuth .signOut ();
122
-
123
- firebaseAuth .signInWithCredential (authCredential )
124
- .addOnFailureListener (
125
- new TaskFailureLogger (TAG , "Error signing in with credential" ))
126
- .addOnSuccessListener (
127
- new OnSuccessListener <AuthResult >() {
128
- @ Override
129
- public void onSuccess (AuthResult authResult ) {
130
- FirebaseUser firebaseUser = authResult .getUser ();
131
- String photoUrl = null ;
132
- Uri photoUri = firebaseUser .getPhotoUrl ();
133
- if (photoUri != null ) {
134
- photoUrl = photoUri .toString ();
135
- }
136
- mActivityHelper .dismissDialog ();
137
-
138
- SmartlockUtil .saveCredentialOrFinish (
139
- WelcomeBackPasswordPrompt .this ,
140
- RC_CREDENTIAL_SAVE ,
141
- mActivityHelper .getFlowParams (),
142
- firebaseUser ,
143
- password ,
144
- null /* provider */ );
145
- }
146
- });
147
- } else {
148
- String error = task .getException ().getLocalizedMessage ();
149
- mPasswordLayout .setError (error );
150
- }
151
- }
152
- });
122
+ .addOnSuccessListener (new OnSuccessListener <AuthResult >() {
123
+ @ Override
124
+ public void onSuccess (AuthResult authResult ) {
125
+ // Get the social AuthCredential from the IDPResponse object, link
126
+ // it to the email/password account.
127
+ AuthCredential authCredential =
128
+ AuthCredentialHelper .getAuthCredential (mIdpResponse );
129
+ authResult .getUser ().linkWithCredential (authCredential );
130
+ firebaseAuth .signOut ();
131
+
132
+ // Sign in with the credential
133
+ firebaseAuth .signInWithCredential (authCredential )
134
+ .addOnFailureListener (
135
+ new TaskFailureLogger (TAG , "Error signing in with credential" ))
136
+ .addOnSuccessListener (
137
+ new OnSuccessListener <AuthResult >() {
138
+ @ Override
139
+ public void onSuccess (AuthResult authResult ) {
140
+ mActivityHelper .dismissDialog ();
141
+ SmartlockUtil .saveCredentialOrFinish (
142
+ WelcomeBackPasswordPrompt .this ,
143
+ RC_CREDENTIAL_SAVE ,
144
+ mActivityHelper .getFlowParams (),
145
+ authResult .getUser (),
146
+ password ,
147
+ null /* provider */ );
148
+ }
149
+ });
150
+ }
151
+ })
152
+ .addOnFailureListener (new OnFailureListener () {
153
+ @ Override
154
+ public void onFailure (@ NonNull Exception e ) {
155
+ String error = e .getLocalizedMessage ();
156
+ mPasswordLayout .setError (error );
157
+ }
158
+ });
153
159
}
154
160
155
161
public static Intent createIntent (
0 commit comments