3333import android .widget .RadioButton ;
3434import android .widget .TextView ;
3535
36+ import com .firebase .ui .auth .AuthMethodPickerLayout ;
3637import com .firebase .ui .auth .AuthUI ;
3738import com .firebase .ui .auth .AuthUI .IdpConfig ;
3839import com .firebase .ui .auth .ErrorCodes ;
3940import com .firebase .ui .auth .IdpResponse ;
41+ import com .firebase .ui .auth .util .ExtraConstants ;
4042import com .firebase .uidemo .R ;
4143import com .firebase .uidemo .util .ConfigurationUtils ;
4244import com .google .android .gms .common .Scopes ;
4345import com .google .android .gms .tasks .OnCompleteListener ;
4446import com .google .android .gms .tasks .Task ;
47+ import com .google .firebase .auth .ActionCodeSettings ;
4548import com .google .firebase .auth .AuthResult ;
4649import com .google .firebase .auth .FirebaseAuth ;
4750
@@ -57,8 +60,10 @@ public class AuthUiActivity extends AppCompatActivity {
5760
5861 private static final String GOOGLE_TOS_URL = "https://www.google.com/policies/terms/" ;
5962 private static final String FIREBASE_TOS_URL = "https://firebase.google.com/terms/" ;
60- private static final String GOOGLE_PRIVACY_POLICY_URL = "https://www.google.com/policies/privacy/" ;
61- private static final String FIREBASE_PRIVACY_POLICY_URL = "https://firebase.google.com/terms/analytics/#7_privacy" ;
63+ private static final String GOOGLE_PRIVACY_POLICY_URL = "https://www.google" +
64+ ".com/policies/privacy/" ;
65+ private static final String FIREBASE_PRIVACY_POLICY_URL = "https://firebase.google" +
66+ ".com/terms/analytics/#7_privacy" ;
6267
6368 private static final int RC_SIGN_IN = 100 ;
6469
@@ -69,9 +74,13 @@ public class AuthUiActivity extends AppCompatActivity {
6974 @ BindView (R .id .twitter_provider ) CheckBox mUseTwitterProvider ;
7075 @ BindView (R .id .github_provider ) CheckBox mUseGitHubProvider ;
7176 @ BindView (R .id .email_provider ) CheckBox mUseEmailProvider ;
77+ @ BindView (R .id .email_link_provider ) CheckBox mUseEmailLinkProvider ;
7278 @ BindView (R .id .phone_provider ) CheckBox mUsePhoneProvider ;
7379 @ BindView (R .id .anonymous_provider ) CheckBox mUseAnonymousProvider ;
7480
81+ @ BindView (R .id .default_layout ) RadioButton mDefaultLayout ;
82+ @ BindView (R .id .custom_layout ) RadioButton mCustomLayout ;
83+
7584 @ BindView (R .id .default_theme ) RadioButton mDefaultTheme ;
7685 @ BindView (R .id .green_theme ) RadioButton mGreenTheme ;
7786 @ BindView (R .id .purple_theme ) RadioButton mPurpleTheme ;
@@ -163,6 +172,41 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
163172 });
164173 }
165174
175+ mUseEmailLinkProvider .setOnCheckedChangeListener (new OnCheckedChangeListener () {
176+ @ Override
177+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
178+ flipPasswordProviderCheckbox (isChecked );
179+ }
180+ });
181+
182+ mUseEmailProvider .setOnCheckedChangeListener (new OnCheckedChangeListener () {
183+ @ Override
184+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
185+ flipEmailLinkProviderCheckbox (isChecked );
186+ }
187+ });
188+
189+ mUseEmailLinkProvider .setChecked (false );
190+ mUseEmailProvider .setChecked (true );
191+
192+ // The custom layout in this app only supports Email and Google providers.
193+ mCustomLayout .setOnCheckedChangeListener (new OnCheckedChangeListener () {
194+ @ Override
195+ public void onCheckedChanged (CompoundButton compoundButton , boolean checked ) {
196+ if (checked ) {
197+ mUseGoogleProvider .setChecked (true );
198+ mUseEmailProvider .setChecked (true );
199+
200+ mUseFacebookProvider .setChecked (false );
201+ mUseTwitterProvider .setChecked (false );
202+ mUseGitHubProvider .setChecked (false );
203+ mUseEmailLinkProvider .setChecked (false );
204+ mUsePhoneProvider .setChecked (false );
205+ mUseAnonymousProvider .setChecked (false );
206+ }
207+ }
208+ });
209+
166210 if (ConfigurationUtils .isGoogleMisconfigured (this )
167211 || ConfigurationUtils .isFacebookMisconfigured (this )
168212 || ConfigurationUtils .isTwitterMisconfigured (this )
@@ -173,24 +217,79 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
173217 if (AppCompatDelegate .getDefaultNightMode () == AppCompatDelegate .MODE_NIGHT_YES ) {
174218 mDarkTheme .setChecked (true );
175219 }
220+
221+ catchEmailLinkSignIn ();
222+ }
223+
224+ public void catchEmailLinkSignIn () {
225+ if (getIntent ().getExtras () == null ) {
226+ return ;
227+ }
228+ String link = getIntent ().getExtras ().getString (ExtraConstants .EMAIL_LINK_SIGN_IN );
229+ if (link != null ) {
230+ signInWithEmailLink (link );
231+ }
232+ }
233+
234+ public void flipPasswordProviderCheckbox (boolean emailLinkProviderIsChecked ) {
235+ if (emailLinkProviderIsChecked ) {
236+ mUseEmailProvider .setChecked (false );
237+ }
238+ }
239+
240+ public void flipEmailLinkProviderCheckbox (boolean passwordProviderIsChecked ) {
241+ if (passwordProviderIsChecked ) {
242+ mUseEmailLinkProvider .setChecked (false );
243+ }
176244 }
177245
178246 @ OnClick (R .id .sign_in )
179247 public void signIn () {
180- AuthUI .SignInIntentBuilder builder = AuthUI .getInstance ().createSignInIntentBuilder ()
248+ startActivityForResult (buildSignInIntent (/*link=*/ null ), RC_SIGN_IN );
249+ }
250+
251+ public void signInWithEmailLink (@ Nullable String link ) {
252+ startActivityForResult (buildSignInIntent (link ), RC_SIGN_IN );
253+ }
254+
255+ @ NonNull
256+ public Intent buildSignInIntent (@ Nullable String link ) {
257+ AuthUI .SignInIntentBuilder builder = AuthUI .getInstance ().createSignInIntentBuilder ()
181258 .setTheme (getSelectedTheme ())
182259 .setLogo (getSelectedLogo ())
183260 .setAvailableProviders (getSelectedProviders ())
184261 .setIsSmartLockEnabled (mEnableCredentialSelector .isChecked (),
185262 mEnableHintSelector .isChecked ());
186263
264+ if (mCustomLayout .isChecked ()) {
265+ AuthMethodPickerLayout customLayout = new AuthMethodPickerLayout
266+ .Builder (R .layout .auth_method_picker_custom_layout )
267+ .setGoogleButtonId (R .id .custom_google_signin_button )
268+ .setEmailButtonId (R .id .custom_email_signin_clickable_text )
269+ .setTosAndPrivacyPolicyId (R .id .custom_tos_pp )
270+ .build ();
271+
272+ builder .setTheme (R .style .CustomTheme );
273+ builder .setAuthMethodPickerLayout (customLayout );
274+ }
275+
187276 if (getSelectedTosUrl () != null && getSelectedPrivacyPolicyUrl () != null ) {
188277 builder .setTosAndPrivacyPolicyUrls (
189278 getSelectedTosUrl (),
190279 getSelectedPrivacyPolicyUrl ());
191280 }
192281
193- startActivityForResult (builder .build (), RC_SIGN_IN );
282+ if (link != null ) {
283+ builder .setEmailLink (link );
284+ }
285+
286+ FirebaseAuth auth = FirebaseAuth .getInstance ();
287+
288+ if (auth .getCurrentUser () != null && auth .getCurrentUser ().isAnonymous ()) {
289+ builder .enableAnonymousUsersAutoUpgrade ();
290+ }
291+
292+ return builder .build ();
194293 }
195294
196295 @ OnClick (R .id .sign_in_silent )
@@ -220,7 +319,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
220319 protected void onResume () {
221320 super .onResume ();
222321 FirebaseAuth auth = FirebaseAuth .getInstance ();
223- if (auth .getCurrentUser () != null ) {
322+ if (auth .getCurrentUser () != null && getIntent (). getExtras () == null ) {
224323 startSignedInActivity (null );
225324 finish ();
226325 }
@@ -246,6 +345,12 @@ private void handleSignInResponse(int resultCode, @Nullable Intent data) {
246345 return ;
247346 }
248347
348+ if (response .getError ().getErrorCode () == ErrorCodes .ANONYMOUS_UPGRADE_MERGE_CONFLICT ) {
349+ Intent intent = new Intent (this , AnonymousUpgradeActivity .class ).putExtra
350+ (ExtraConstants .IDP_RESPONSE , response );
351+ startActivity (intent );
352+ }
353+
249354 showSnackbar (R .string .unknown_error );
250355 Log .e (TAG , "Sign-in error: " , response .getError ());
251356 }
@@ -317,6 +422,20 @@ private List<IdpConfig> getSelectedProviders() {
317422 .build ());
318423 }
319424
425+ if (mUseEmailLinkProvider .isChecked ()) {
426+ ActionCodeSettings actionCodeSettings = ActionCodeSettings .newBuilder ()
427+ .setAndroidPackageName ("com.firebase.uidemo" , true , null )
428+ .setHandleCodeInApp (true )
429+ .setUrl ("https://google.com" )
430+ .build ();
431+
432+ selectedProviders .add (new IdpConfig .EmailBuilder ()
433+ .setAllowNewAccounts (mAllowNewEmailAccounts .isChecked ())
434+ .setActionCodeSettings (actionCodeSettings )
435+ .enableEmailLinkSignIn ()
436+ .build ());
437+ }
438+
320439 if (mUsePhoneProvider .isChecked ()) {
321440 selectedProviders .add (new IdpConfig .PhoneBuilder ().build ());
322441 }
0 commit comments