Search in sources :

Example 1 with ResolvableApiException

use of com.google.android.gms.common.api.ResolvableApiException in project Instafood by Gear61.

the class LocationManager method checkLocationServicesAndFetchLocationIfOn.

private void checkLocationServicesAndFetchLocationIfOn() {
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    SettingsClient client = LocationServices.getSettingsClient(activity);
    client.checkLocationSettings(builder.build()).addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {

        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            fetchAutomaticLocation();
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception exception) {
            if (exception instanceof ResolvableApiException) {
                locationServicesManager.askForLocationServices(LOCATION_SERVICES_CODE);
            } else {
                onLocationFetchFail();
            }
        }
    });
}
Also used : SettingsClient(com.google.android.gms.location.SettingsClient) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) 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)

Example 2 with ResolvableApiException

use of com.google.android.gms.common.api.ResolvableApiException in project Instafood by Gear61.

the class LocationServicesManager method askForLocationServices.

void askForLocationServices(final int requestCode) {
    Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(activity).checkLocationSettings(locationBuilder.build());
    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {

        @Override
        public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
            try {
                task.getResult(ApiException.class);
            } catch (ApiException exception) {
                switch(exception.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            // Show dialog to turn on location services
                            resolvable.startResolutionForResult(activity, requestCode);
                        } catch (IntentSender.SendIntentException | ClassCastException ignored) {
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        openLocationSettings();
                        break;
                }
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 3 with ResolvableApiException

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

the class SettingsActivity method startLocationUpdates.

public 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(SettingsActivity.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(context, "Location Services Unavailable", Toast.LENGTH_LONG).show();
                    useGPS = false;
                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putBoolean("use_device_location", false);
                    edit.apply();
                    settingsFragment.useDeviceLocation.setChecked(false);
                    settingsFragment.setLocSummary();
                    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)

Example 4 with ResolvableApiException

use of com.google.android.gms.common.api.ResolvableApiException in project routerkeygenAndroid by routerkeygen.

the class NetworksListActivity method settingsRequest.

public static void settingsRequest(final Activity activity, OnSuccessListener cb) {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(100000);
    mLocationRequest.setFastestInterval(50000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    SettingsClient client = LocationServices.getSettingsClient(activity);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(activity, cb);
    task.addOnFailureListener(activity, 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(activity, 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 : SettingsClient(com.google.android.gms.location.SettingsClient) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) IntentSender(android.content.IntentSender) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 5 with ResolvableApiException

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

the class SmartLockHandlerTest method testSaveCredentials_resolution.

@Test
public void testSaveCredentials_resolution() {
    mHandler.getOperation().observeForever(mResultObserver);
    // Mock credentials to throw an RAE
    ResolvableApiException mockRae = mock(ResolvableApiException.class);
    when(mMockCredentials.save(any(Credential.class))).thenReturn(AutoCompleteTask.forFailure(mockRae));
    // Kick off save
    mHandler.saveCredentials(TestHelper.getMockFirebaseUser(), TestConstants.PASSWORD, null);
    InOrder inOrder = inOrder(mResultObserver);
    inOrder.verify(mResultObserver).onChanged(argThat(ResourceMatchers.isLoading()));
    // Make sure we get a resolution
    ArgumentCaptor<Resource<IdpResponse>> resolveCaptor = ArgumentCaptor.forClass(Resource.class);
    inOrder.verify(mResultObserver).onChanged(resolveCaptor.capture());
    // Call activity result
    PendingIntentRequiredException e = ((PendingIntentRequiredException) resolveCaptor.getValue().getException());
    mHandler.onActivityResult(e.getRequestCode(), Activity.RESULT_OK);
    // Make sure we get success
    inOrder.verify(mResultObserver).onChanged(argThat(ResourceMatchers.isSuccess()));
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) Credential(com.google.android.gms.auth.api.credentials.Credential) InOrder(org.mockito.InOrder) Resource(com.firebase.ui.auth.data.model.Resource) PendingIntentRequiredException(com.firebase.ui.auth.data.model.PendingIntentRequiredException) Test(org.junit.Test)

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