Search in sources :

Example 26 with ApiException

use of com.google.android.gms.common.api.ApiException in project FirebaseUI-Android by firebase.

the class AuthUI method delete.

/**
 * Delete the use from FirebaseAuth and delete any associated credentials from the Credentials
 * API. Returns a {@link Task} that succeeds if the Firebase Auth user deletion succeeds and
 * fails if the Firebase Auth deletion fails. Credentials deletion failures are handled
 * silently.
 *
 * @param context the calling {@link Context}.
 */
@NonNull
public Task<Void> delete(@NonNull final Context context) {
    final FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser == null) {
        return Tasks.forException(new FirebaseAuthInvalidUserException(String.valueOf(CommonStatusCodes.SIGN_IN_REQUIRED), "No currently signed in user."));
    }
    final List<Credential> credentials = getCredentialsFromFirebaseUser(currentUser);
    // Ensure the order in which tasks are executed properly destructures the user.
    return signOutIdps(context).continueWithTask(task -> {
        // Propagate exception if there was one
        task.getResult();
        if (!GoogleApiUtils.isPlayServicesAvailable(context)) {
            Log.w(TAG, "Google Play services not available during delete");
            return Tasks.forResult((Void) null);
        }
        final CredentialsClient client = GoogleApiUtils.getCredentialsClient(context);
        List<Task<?>> credentialTasks = new ArrayList<>();
        for (Credential credential : credentials) {
            credentialTasks.add(client.delete(credential));
        }
        return Tasks.whenAll(credentialTasks).continueWith(task1 -> {
            Exception e = task1.getException();
            Throwable t = e == null ? null : e.getCause();
            if (!(t instanceof ApiException) || ((ApiException) t).getStatusCode() != CommonStatusCodes.CANCELED) {
                // doesn't mean fully deleting the user failed.
                return task1.getResult();
            }
            return null;
        });
    }).continueWithTask(task -> {
        // Propagate exception if there was one
        task.getResult();
        return currentUser.delete();
    });
}
Also used : Scope(com.google.android.gms.common.api.Scope) Arrays(java.util.Arrays) Bundle(android.os.Bundle) UserInfo(com.google.firebase.auth.UserInfo) NonNull(androidx.annotation.NonNull) GoogleApiUtils(com.firebase.ui.auth.util.GoogleApiUtils) DrawableRes(androidx.annotation.DrawableRes) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) GoogleSignIn(com.google.android.gms.auth.api.signin.GoogleSignIn) RestrictTo(androidx.annotation.RestrictTo) Locale(java.util.Locale) Map(java.util.Map) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) ProviderAvailability(com.firebase.ui.auth.util.data.ProviderAvailability) Log(android.util.Log) ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) IdentityHashMap(java.util.IdentityHashMap) FirebaseAuthProvider(com.google.firebase.auth.FirebaseAuthProvider) FirebaseUser(com.google.firebase.auth.FirebaseUser) Set(java.util.Set) FacebookAuthProvider(com.google.firebase.auth.FacebookAuthProvider) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) StyleRes(androidx.annotation.StyleRes) LoginManager(com.facebook.login.LoginManager) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) Preconditions(com.firebase.ui.auth.util.Preconditions) List(java.util.List) Tasks(com.google.android.gms.tasks.Tasks) Nullable(androidx.annotation.Nullable) AuthResult(com.google.firebase.auth.AuthResult) PhoneNumberUtils(com.firebase.ui.auth.util.data.PhoneNumberUtils) ApiException(com.google.android.gms.common.api.ApiException) Context(android.content.Context) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) GithubAuthProvider(com.google.firebase.auth.GithubAuthProvider) Intent(android.content.Intent) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) TwitterAuthProvider(com.google.firebase.auth.TwitterAuthProvider) HashMap(java.util.HashMap) Parcel(android.os.Parcel) Task(com.google.android.gms.tasks.Task) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AuthMethodPickerActivity(com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity) CredentialUtils(com.firebase.ui.auth.util.CredentialUtils) CredentialsClient(com.google.android.gms.auth.api.credentials.CredentialsClient) StringDef(androidx.annotation.StringDef) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) Parcelable(android.os.Parcelable) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) TextUtils(android.text.TextUtils) CallSuper(androidx.annotation.CallSuper) CommonStatusCodes(com.google.android.gms.common.api.CommonStatusCodes) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Collections(java.util.Collections) GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) FirebaseApp(com.google.firebase.FirebaseApp) RetentionPolicy(java.lang.annotation.RetentionPolicy) CredentialsClient(com.google.android.gms.auth.api.credentials.CredentialsClient) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) List(java.util.List) ArrayList(java.util.ArrayList) FirebaseUser(com.google.firebase.auth.FirebaseUser) ApiException(com.google.android.gms.common.api.ApiException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) ApiException(com.google.android.gms.common.api.ApiException) NonNull(androidx.annotation.NonNull)

Example 27 with ApiException

use of com.google.android.gms.common.api.ApiException in project FirebaseUI-Android by firebase.

the class GoogleSignInHandler method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode != RequestCodes.GOOGLE_PROVIDER) {
        return;
    }
    try {
        GoogleSignInAccount account = GoogleSignIn.getSignedInAccountFromIntent(data).getResult(ApiException.class);
        setResult(Resource.forSuccess(createIdpResponse(account)));
    } catch (ApiException e) {
        if (e.getStatusCode() == CommonStatusCodes.INVALID_ACCOUNT) {
            // If we get INVALID_ACCOUNT, it means the pre-set account was not available on the
            // device so set the email to null and launch the sign-in picker.
            mEmail = null;
            start();
        } else if (e.getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS) {
            // Hack for https://github.com/googlesamples/google-services/issues/345
            // Google remembers the account so the picker doesn't appear twice for the user.
            start();
        } else if (e.getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {
            setResult(Resource.forFailure(new UserCancellationException()));
        } else {
            if (e.getStatusCode() == CommonStatusCodes.DEVELOPER_ERROR) {
                Log.w(TAG, "Developer error: this application is misconfigured. " + "Check your SHA1 and package name in the Firebase console.");
            }
            setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.PROVIDER_ERROR, "Code: " + e.getStatusCode() + ", message: " + e.getMessage())));
        }
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 28 with ApiException

use of com.google.android.gms.common.api.ApiException in project NightSkyGuide by MTBehnke.

the class MainActivity method startLocationUpdates.

private void startLocationUpdates() {
    // if settings are satisfied initialize location requests
    mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {

        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            locUpdates = true;
            // All location settings are satisfied.
            // noinspection MissingPermission - this comment needs to stay here to stop inspection on next line
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
        }
    }).addOnFailureListener(this, new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            int statusCode = ((ApiException) e).getStatusCode();
            switch(statusCode) {
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // location settings are not satisfied, but this can be fixed by showing the user a dialog.
                    try {
                        // show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // location settings are not satisfied, however no way to fix the settings so don't show dialog.
                    Toast.makeText(MainActivity.this, "Location Services Unavailable", Toast.LENGTH_LONG).show();
                    useGPS = false;
                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putBoolean("use_device_location", false);
                    edit.apply();
                    break;
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

ApiException (com.google.android.gms.common.api.ApiException)28 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)15 ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)10 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)9 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)7 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)6 LocationRequest (com.google.android.gms.location.LocationRequest)5 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)5 IntentSender (android.content.IntentSender)4 Intent (android.content.Intent)3 NonNull (androidx.annotation.NonNull)3 FirebaseAuthInvalidUserException (com.google.firebase.auth.FirebaseAuthInvalidUserException)3 Bundle (android.os.Bundle)2 TextUtils (android.text.TextUtils)2 Nullable (androidx.annotation.Nullable)2 UserCancellationException (com.firebase.ui.auth.data.model.UserCancellationException)2 AuthMethodPickerActivity (com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity)2 ExtraConstants (com.firebase.ui.auth.util.ExtraConstants)2 GoogleApiUtils (com.firebase.ui.auth.util.GoogleApiUtils)2 ProviderUtils (com.firebase.ui.auth.util.data.ProviderUtils)2