Search in sources :

Example 11 with ResolvableApiException

use of com.google.android.gms.common.api.ResolvableApiException in project coursera-android by aporter.

the class LocationGetLocationActivity method continueAcquiringLocations.

private void continueAcquiringLocations() {
    // Start location services
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(POLLING_FREQ);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    // Used if needed to turn on settings related to location services
    SettingsClient client = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {

        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize location requests here.
            if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                mLocationCallback = getLocationCallback();
                mLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
                // Schedule a runnable to stop location updates after a period of time
                Executors.newScheduledThreadPool(1).schedule(new Runnable() {

                    @Override
                    public void run() {
                        mLocationClient.removeLocationUpdates(mLocationCallback);
                    }
                }, MEASURE_TIME, TimeUnit.MILLISECONDS);
            }
        }
    });
    task.addOnFailureListener(this, new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            int statusCode = ((ApiException) e).getStatusCode();
            switch(statusCode) {
                case CommonStatusCodes.RESOLUTION_REQUIRED:
                    // 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(LocationGetLocationActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // to fix the settings so we won't show the dialog.
                    break;
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException) SettingsClient(com.google.android.gms.location.SettingsClient) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 12 with ResolvableApiException

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

the class SignInKickstarter method start.

public void start() {
    if (!TextUtils.isEmpty(getArguments().emailLink)) {
        setResult(Resource.forFailure(new IntentRequiredException(EmailLinkCatcherActivity.createIntent(getApplication(), getArguments()), RequestCodes.EMAIL_FLOW)));
        return;
    }
    // Signing in with Generic IDP puts the app in the background - it can be reclaimed by the
    // OS during the sign in flow.
    Task<AuthResult> pendingResultTask = getAuth().getPendingAuthResult();
    if (pendingResultTask != null) {
        pendingResultTask.addOnSuccessListener(authResult -> {
            final IdpResponse response = new IdpResponse.Builder(new User.Builder(authResult.getCredential().getProvider(), authResult.getUser().getEmail()).build()).build();
            handleSuccess(response, authResult);
        }).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
        return;
    }
    // Only support password credentials if email auth is enabled
    boolean supportPasswords = ProviderUtils.getConfigFromIdps(getArguments().providers, EmailAuthProvider.PROVIDER_ID) != null;
    List<String> accountTypes = getCredentialAccountTypes();
    // If the request will be empty, avoid the step entirely
    boolean willRequestCredentials = supportPasswords || accountTypes.size() > 0;
    if (getArguments().enableCredentials && willRequestCredentials) {
        setResult(Resource.forLoading());
        GoogleApiUtils.getCredentialsClient(getApplication()).request(new CredentialRequest.Builder().setPasswordLoginSupported(supportPasswords).setAccountTypes(accountTypes.toArray(new String[accountTypes.size()])).build()).addOnCompleteListener(task -> {
            try {
                handleCredential(task.getResult(ApiException.class).getCredential());
            } catch (ResolvableApiException e) {
                if (e.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) {
                    setResult(Resource.forFailure(new PendingIntentRequiredException(e.getResolution(), RequestCodes.CRED_HINT)));
                } else {
                    startAuthMethodChoice();
                }
            } catch (ApiException e) {
                startAuthMethodChoice();
            }
        });
    } else {
        startAuthMethodChoice();
    }
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) Bundle(android.os.Bundle) EmailLinkCatcherActivity(com.firebase.ui.auth.ui.email.EmailLinkCatcherActivity) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) PhoneActivity(com.firebase.ui.auth.ui.phone.PhoneActivity) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) GoogleApiUtils(com.firebase.ui.auth.util.GoogleApiUtils) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) Task(com.google.android.gms.tasks.Task) ArrayList(java.util.ArrayList) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) AuthMethodPickerActivity(com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity) SingleSignInActivity(com.firebase.ui.auth.ui.idp.SingleSignInActivity) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) AuthUI(com.firebase.ui.auth.AuthUI) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) EmailActivity(com.firebase.ui.auth.ui.email.EmailActivity) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) TextUtils(android.text.TextUtils) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) CredentialRequestResponse(com.google.android.gms.auth.api.credentials.CredentialRequestResponse) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) CommonStatusCodes(com.google.android.gms.common.api.CommonStatusCodes) List(java.util.List) Credential(com.google.android.gms.auth.api.credentials.Credential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) Activity(android.app.Activity) ApiException(com.google.android.gms.common.api.ApiException) IdpResponse(com.firebase.ui.auth.IdpResponse) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) AuthResult(com.google.firebase.auth.AuthResult) IntentRequiredException(com.firebase.ui.auth.data.model.IntentRequiredException) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) IdpResponse(com.firebase.ui.auth.IdpResponse) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 13 with ResolvableApiException

use of com.google.android.gms.common.api.ResolvableApiException 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

ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)13 ApiException (com.google.android.gms.common.api.ApiException)10 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)10 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)6 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)6 LocationRequest (com.google.android.gms.location.LocationRequest)5 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)5 IntentSender (android.content.IntentSender)4 PendingIntentRequiredException (com.firebase.ui.auth.data.model.PendingIntentRequiredException)3 SettingsClient (com.google.android.gms.location.SettingsClient)3 Resource (com.firebase.ui.auth.data.model.Resource)2 Credential (com.google.android.gms.auth.api.credentials.Credential)2 Activity (android.app.Activity)1 Application (android.app.Application)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 TextUtils (android.text.TextUtils)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AuthUI (com.firebase.ui.auth.AuthUI)1