use of androidx.lifecycle.ViewModelProvider in project FirebaseUI-Android by firebase.
the class RecoverPasswordActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fui_forgot_password_layout);
mHandler = new ViewModelProvider(this).get(RecoverPasswordHandler.class);
mHandler.init(getFlowParams());
mHandler.getOperation().observe(this, new ResourceObserver<String>(this, R.string.fui_progress_dialog_sending) {
@Override
protected void onSuccess(@NonNull String email) {
mEmailInputLayout.setError(null);
showEmailSentDialog(email);
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthInvalidUserException || e instanceof FirebaseAuthInvalidCredentialsException) {
// No FirebaseUser exists with this email address, show error.
mEmailInputLayout.setError(getString(R.string.fui_error_email_does_not_exist));
} else {
// Unknown error
mEmailInputLayout.setError(getString(R.string.fui_error_unknown));
}
}
});
mProgressBar = findViewById(R.id.top_progress_bar);
mSubmitButton = findViewById(R.id.button_done);
mEmailInputLayout = findViewById(R.id.email_layout);
mEmailEditText = findViewById(R.id.email);
mEmailFieldValidator = new EmailFieldValidator(mEmailInputLayout);
String email = getIntent().getStringExtra(ExtraConstants.EMAIL);
if (email != null) {
mEmailEditText.setText(email);
}
ImeHelper.setImeOnDoneListener(mEmailEditText, this);
mSubmitButton.setOnClickListener(this);
TextView footerText = findViewById(R.id.email_footer_tos_and_pp_text);
PrivacyDisclosureUtils.setupTermsOfServiceFooter(this, getFlowParams(), footerText);
}
use of androidx.lifecycle.ViewModelProvider in project FirebaseUI-Android by firebase.
the class CredentialSaveActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final IdpResponse response = getIntent().getParcelableExtra(ExtraConstants.IDP_RESPONSE);
Credential credential = getIntent().getParcelableExtra(ExtraConstants.CREDENTIAL);
mHandler = new ViewModelProvider(this).get(SmartLockHandler.class);
mHandler.init(getFlowParams());
mHandler.setResponse(response);
mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
finish(RESULT_OK, response.toIntent());
}
@Override
protected void onFailure(@NonNull Exception e) {
// RESULT_OK since we don't want to halt sign-in just because of a credential save
// error.
finish(RESULT_OK, response.toIntent());
}
});
// Avoid double-saving
Resource<IdpResponse> currentOp = mHandler.getOperation().getValue();
if (currentOp == null) {
Log.d(TAG, "Launching save operation.");
mHandler.saveCredentials(credential);
} else {
Log.d(TAG, "Save operation in progress, doing nothing.");
}
}
use of androidx.lifecycle.ViewModelProvider in project FirebaseUI-Android by firebase.
the class AuthMethodPickerActivity method handleSignInOperation.
private void handleSignInOperation(final IdpConfig idpConfig, View view) {
ViewModelProvider supplier = new ViewModelProvider(this);
final String providerId = idpConfig.getProviderId();
final ProviderSignInBase<?> provider;
AuthUI authUI = getAuthUI();
switch(providerId) {
case EMAIL_LINK_PROVIDER:
case EmailAuthProvider.PROVIDER_ID:
provider = supplier.get(EmailSignInHandler.class).initWith(null);
break;
case PhoneAuthProvider.PROVIDER_ID:
provider = supplier.get(PhoneSignInHandler.class).initWith(idpConfig);
break;
case AuthUI.ANONYMOUS_PROVIDER:
provider = supplier.get(AnonymousSignInHandler.class).initWith(getFlowParams());
break;
case GoogleAuthProvider.PROVIDER_ID:
if (authUI.isUseEmulator()) {
provider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericGoogleConfig());
} else {
provider = supplier.get(GoogleSignInHandler.class).initWith(new GoogleSignInHandler.Params(idpConfig));
}
break;
case FacebookAuthProvider.PROVIDER_ID:
if (authUI.isUseEmulator()) {
provider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericFacebookConfig());
} else {
provider = supplier.get(FacebookSignInHandler.class).initWith(idpConfig);
}
break;
default:
if (!TextUtils.isEmpty(idpConfig.getParams().getString(GENERIC_OAUTH_PROVIDER_ID))) {
provider = supplier.get(GenericIdpSignInHandler.class).initWith(idpConfig);
break;
}
throw new IllegalStateException("Unknown provider: " + providerId);
}
mProviders.add(provider);
provider.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
handleResponse(response);
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, IdpResponse.from(e)));
return;
}
handleResponse(IdpResponse.from(e));
}
private void handleResponse(@NonNull IdpResponse response) {
// If we're using the emulator then the social flows actually use Generic IDP
// instead which means we shouldn't use the social response handler.
boolean isSocialResponse = AuthUI.SOCIAL_PROVIDERS.contains(providerId) && !getAuthUI().isUseEmulator();
if (!response.isSuccessful()) {
// We have no idea what provider this error stemmed from so just forward
// this along to the handler.
mHandler.startSignIn(response);
} else if (isSocialResponse) {
// Don't use the response's provider since it can be different than the one
// that launched the sign-in attempt. Ex: the email flow is started, but
// ends up turning into a Google sign-in because that account already
// existed. In the previous example, an extra sign-in would incorrectly
// started.
mHandler.startSignIn(response);
} else {
// Email, phone, or generic: the credentials should have already been saved so
// simply move along.
// Anononymous sign in also does not require any other operations.
finish(response.isSuccessful() ? RESULT_OK : RESULT_CANCELED, response.toIntent());
}
}
});
view.setOnClickListener(view1 -> {
if (isOffline()) {
Snackbar.make(findViewById(android.R.id.content), getString(R.string.fui_no_internet), Snackbar.LENGTH_SHORT).show();
return;
}
provider.startSignIn(getAuth(), AuthMethodPickerActivity.this, idpConfig.getProviderId());
});
}
use of androidx.lifecycle.ViewModelProvider in project FirebaseUI-Android by firebase.
the class AuthMethodPickerActivity method populateIdpList.
private void populateIdpList(List<IdpConfig> providerConfigs) {
ViewModelProvider supplier = new ViewModelProvider(this);
mProviders = new ArrayList<>();
for (IdpConfig idpConfig : providerConfigs) {
@LayoutRes int buttonLayout;
final String providerId = idpConfig.getProviderId();
switch(providerId) {
case GoogleAuthProvider.PROVIDER_ID:
buttonLayout = R.layout.fui_idp_button_google;
break;
case FacebookAuthProvider.PROVIDER_ID:
buttonLayout = R.layout.fui_idp_button_facebook;
break;
case EMAIL_LINK_PROVIDER:
case EmailAuthProvider.PROVIDER_ID:
buttonLayout = R.layout.fui_provider_button_email;
break;
case PhoneAuthProvider.PROVIDER_ID:
buttonLayout = R.layout.fui_provider_button_phone;
break;
case AuthUI.ANONYMOUS_PROVIDER:
buttonLayout = R.layout.fui_provider_button_anonymous;
break;
default:
if (!TextUtils.isEmpty(idpConfig.getParams().getString(GENERIC_OAUTH_PROVIDER_ID))) {
buttonLayout = idpConfig.getParams().getInt(GENERIC_OAUTH_BUTTON_ID);
break;
}
throw new IllegalStateException("Unknown provider: " + providerId);
}
View loginButton = getLayoutInflater().inflate(buttonLayout, mProviderHolder, false);
handleSignInOperation(idpConfig, loginButton);
mProviderHolder.addView(loginButton);
}
}
use of androidx.lifecycle.ViewModelProvider in project FirebaseUI-Android by firebase.
the class SingleSignInActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User user = User.getUser(getIntent());
final String provider = user.getProviderId();
AuthUI.IdpConfig providerConfig = ProviderUtils.getConfigFromIdps(getFlowParams().providers, provider);
if (providerConfig == null) {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "Provider not enabled: " + provider)));
return;
}
ViewModelProvider supplier = new ViewModelProvider(this);
mHandler = supplier.get(SocialProviderResponseHandler.class);
mHandler.init(getFlowParams());
boolean useEmulator = getAuthUI().isUseEmulator();
switch(provider) {
case GoogleAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericGoogleConfig());
} else {
mProvider = supplier.get(GoogleSignInHandler.class).initWith(new GoogleSignInHandler.Params(providerConfig, user.getEmail()));
}
break;
case FacebookAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericFacebookConfig());
} else {
mProvider = supplier.get(FacebookSignInHandler.class).initWith(providerConfig);
}
break;
default:
if (!TextUtils.isEmpty(providerConfig.getParams().getString(GENERIC_OAUTH_PROVIDER_ID))) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(providerConfig);
break;
}
throw new IllegalStateException("Invalid provider id: " + provider);
}
mProvider.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
boolean useSocialHandler = AuthUI.SOCIAL_PROVIDERS.contains(provider) && !getAuthUI().isUseEmulator();
if (useSocialHandler || !response.isSuccessful()) {
mHandler.startSignIn(response);
return;
}
finish(response.isSuccessful() ? RESULT_OK : RESULT_CANCELED, response.toIntent());
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, IdpResponse.from(e)));
return;
}
mHandler.startSignIn(IdpResponse.from(e));
}
});
mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
startSaveCredentials(mHandler.getCurrentUser(), response, null);
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
IdpResponse res = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, res));
} else {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(e));
}
}
});
if (mHandler.getOperation().getValue() == null) {
mProvider.startSignIn(getAuth(), this, provider);
}
}
Aggregations