Search in sources :

Example 1 with LocationSettingsResponse

use of com.google.android.gms.location.LocationSettingsResponse 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 LocationSettingsResponse

use of com.google.android.gms.location.LocationSettingsResponse 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 LocationSettingsResponse

use of com.google.android.gms.location.LocationSettingsResponse 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 LocationSettingsResponse

use of com.google.android.gms.location.LocationSettingsResponse 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 LocationSettingsResponse

use of com.google.android.gms.location.LocationSettingsResponse in project IITB-App by wncc.

the class MapFragment method displayLocationSettingsRequest.

private void displayLocationSettingsRequest(final boolean showWarning) {
    if (getView() == null || getActivity() == null)
        return;
    LocationRequest mLocationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(10 * 1000).setFastestInterval(1 * 1000);
    LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
    settingsBuilder.setAlwaysShow(true);
    Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getActivity()).checkLocationSettings(settingsBuilder.build());
    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {

        @Override
        public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse result = task.getResult(ApiException.class);
                if (result.getLocationSettingsStates().isGpsPresent() && result.getLocationSettingsStates().isGpsUsable() && result.getLocationSettingsStates().isLocationPresent() && result.getLocationSettingsStates().isLocationUsable()) {
                    setupGPS(showWarning);
                }
            } catch (ApiException ex) {
                switch(ex.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException resolvableApiException = (ResolvableApiException) ex;
                            resolvableApiException.startResolutionForResult(getActivity(), 87);
                            setupGPS(showWarning);
                        } catch (IntentSender.SendIntentException e) {
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Toast.makeText(getContext(), "GPS is not enabled!", Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }
    });
}
Also used : 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)

Aggregations

ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)10 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)10 ApiException (com.google.android.gms.common.api.ApiException)9 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)6 LocationRequest (com.google.android.gms.location.LocationRequest)5 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 IntentSender (android.content.IntentSender)4 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)4 SettingsClient (com.google.android.gms.location.SettingsClient)3