Search in sources :

Example 11 with ViewModelProvider

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);
}
Also used : RecoverPasswordHandler(com.firebase.ui.auth.viewmodel.email.RecoverPasswordHandler) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) EmailFieldValidator(com.firebase.ui.auth.util.ui.fieldvalidators.EmailFieldValidator) TextView(android.widget.TextView) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 12 with ViewModelProvider

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.");
    }
}
Also used : Credential(com.google.android.gms.auth.api.credentials.Credential) SmartLockHandler(com.firebase.ui.auth.viewmodel.smartlock.SmartLockHandler) IdpResponse(com.firebase.ui.auth.IdpResponse) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 13 with ViewModelProvider

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());
    });
}
Also used : Intent(android.content.Intent) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 14 with ViewModelProvider

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);
    }
}
Also used : IdpConfig(com.firebase.ui.auth.AuthUI.IdpConfig) LayoutRes(androidx.annotation.LayoutRes) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 15 with ViewModelProvider

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);
    }
}
Also used : User(com.firebase.ui.auth.data.model.User) Intent(android.content.Intent) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) SocialProviderResponseHandler(com.firebase.ui.auth.viewmodel.idp.SocialProviderResponseHandler) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Aggregations

ViewModelProvider (androidx.lifecycle.ViewModelProvider)56 TextView (android.widget.TextView)13 Intent (android.content.Intent)12 View (android.view.View)11 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)8 IdpResponse (com.firebase.ui.auth.IdpResponse)8 Bundle (android.os.Bundle)7 Toolbar (androidx.appcompat.widget.Toolbar)7 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)7 Handler (android.os.Handler)6 HandlerThread (android.os.HandlerThread)5 SpannableStringBuilder (android.text.SpannableStringBuilder)5 Nullable (androidx.annotation.Nullable)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)4 Animator (android.animation.Animator)3 Bitmap (android.graphics.Bitmap)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)3 Uri (android.net.Uri)3 FragmentManager (androidx.fragment.app.FragmentManager)3