Search in sources :

Example 1 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project RSAndroidApp by RailwayStations.

the class NearbyNotificationService method startLocationUpdates.

private void startLocationUpdates() {
    LocationRequest locationRequest = new LocationRequest().setInterval(2 * FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS).setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    AsyncTask<PendingResult<LocationSettingsResult>, Void, Boolean> task = new AsyncTask<PendingResult<LocationSettingsResult>, Void, Boolean>() {

        @Override
        @SafeVarargs
        protected final Boolean doInBackground(PendingResult<LocationSettingsResult>... pendingResults) {
            com.google.android.gms.common.api.Status status = pendingResults[0].await().getStatus();
            int statusCode = status.getStatusCode();
            return statusCode == LocationSettingsStatusCodes.SUCCESS || statusCode == LocationSettingsStatusCodes.SUCCESS_CACHE;
        }

        @Override
        protected final void onPostExecute(Boolean success) {
            super.onPostExecute(success);
            if (!success) {
                Log.e(TAG, "Device settings unsuitable for location");
                Toast.makeText(NearbyNotificationService.this, R.string.no_location_enabled, Toast.LENGTH_LONG).show();
                stopSelf();
            }
        }
    };
    //noinspection unchecked
    task.execute(result);
    try {
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    } catch (SecurityException se) {
        Log.e(TAG, "Still no permission for location services");
        Toast.makeText(this, "Bitte einmal \"in der Nähe\" aufrufen", Toast.LENGTH_LONG).show();
        stopSelf();
    }
}
Also used : LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) AsyncTask(android.os.AsyncTask) PendingResult(com.google.android.gms.common.api.PendingResult) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest)

Example 2 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project android_packages_apps_GmsCore by microg.

the class GoogleLocationManagerServiceImpl method requestLocationSettingsDialog.

@Override
public void requestLocationSettingsDialog(LocationSettingsRequest settingsRequest, ISettingsCallbacks callback, String packageName) throws RemoteException {
    Log.d(TAG, "requestLocationSettingsDialog: " + settingsRequest);
    callback.onLocationSettingsResult(new LocationSettingsResult(new LocationSettingsStates(true, true, false, true, true, false), Status.CANCELED));
}
Also used : LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) LocationSettingsStates(com.google.android.gms.location.LocationSettingsStates)

Example 3 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project iNaturalistAndroid by inaturalist.

the class INaturalistApp method isLocationEnabled.

/**
 * Checks if place services are enabled
 */
public boolean isLocationEnabled(final OnLocationStatus locationCallback) {
    // First, check if GPS is disabled
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
    if (!gpsEnabled)
        return false;
    // Next, see if specifically the user has revoked place access to our app
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
    }
    if (locationCallback != null) {
        final LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(5000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

            @Override
            public void onResult(LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                switch(status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All place settings are satisfied. The client can initialize place
                        // requests here.
                        locationCallback.onLocationStatus(true);
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied
                        locationCallback.onLocationStatus(false);
                        break;
                }
            }
        });
    }
    return gpsEnabled;
}
Also used : LocationManager(android.location.LocationManager) Status(com.google.android.gms.common.api.Status) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest)

Example 4 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project Android-ReactiveLocation by mcharmas.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
    updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
    addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
    currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
    locationProvider = new ReactiveLocationProvider(getApplicationContext(), ReactiveLocationProviderConfiguration.builder().setRetryOnConnectionSuspended(true).build());
    lastKnownLocationObservable = locationProvider.getLastKnownLocation().observeOn(AndroidSchedulers.mainThread());
    final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
    locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(// Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
    true).build()).doOnNext(new Consumer<LocationSettingsResult>() {

        @Override
        public void accept(LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException th) {
                    Log.e("MainActivity", "Error opening settings activity.", th);
                }
            }
        }
    }).flatMap(new Function<LocationSettingsResult, Observable<Location>>() {

        @Override
        public Observable<Location> apply(LocationSettingsResult locationSettingsResult) {
            return locationProvider.getUpdatedLocation(locationRequest);
        }
    }).observeOn(AndroidSchedulers.mainThread());
    addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Function<Location, Observable<List<Address>>>() {

        @Override
        public Observable<List<Address>> apply(Location location) {
            return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
        }
    }).map(new Function<List<Address>, Address>() {

        @Override
        public Address apply(List<Address> addresses) {
            return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
        }
    }).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    activityObservable = locationProvider.getDetectedActivity(50).observeOn(AndroidSchedulers.mainThread());
}
Also used : Status(com.google.android.gms.common.api.Status) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Address(android.location.Address) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) Consumer(io.reactivex.functions.Consumer) List(java.util.List) ReactiveLocationProvider(pl.charmas.android.reactivelocation2.ReactiveLocationProvider) IntentSender(android.content.IntentSender) AddressToStringFunc(pl.charmas.android.reactivelocation2.sample.utils.AddressToStringFunc) Location(android.location.Location)

Example 5 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project Android-ReactiveLocation by mcharmas.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
    updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
    addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
    currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
    locationProvider = new ReactiveLocationProvider(getApplicationContext());
    lastKnownLocationObservable = locationProvider.getLastKnownLocation();
    final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
    locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(//Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
    true).build()).doOnNext(new Action1<LocationSettingsResult>() {

        @Override
        public void call(LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException th) {
                    Log.e("MainActivity", "Error opening settings activity.", th);
                }
            }
        }
    }).flatMap(new Func1<LocationSettingsResult, Observable<Location>>() {

        @Override
        public Observable<Location> call(LocationSettingsResult locationSettingsResult) {
            return locationProvider.getUpdatedLocation(locationRequest);
        }
    });
    addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Func1<Location, Observable<List<Address>>>() {

        @Override
        public Observable<List<Address>> call(Location location) {
            return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
        }
    }).map(new Func1<List<Address>, Address>() {

        @Override
        public Address call(List<Address> addresses) {
            return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
        }
    }).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    activityObservable = locationProvider.getDetectedActivity(50);
}
Also used : Status(com.google.android.gms.common.api.Status) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Address(android.location.Address) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) List(java.util.List) ReactiveLocationProvider(pl.charmas.android.reactivelocation.ReactiveLocationProvider) Func1(rx.functions.Func1) AddressToStringFunc(pl.charmas.android.reactivelocation.sample.utils.AddressToStringFunc) Location(android.location.Location)

Aggregations

LocationSettingsResult (com.google.android.gms.location.LocationSettingsResult)7 LocationRequest (com.google.android.gms.location.LocationRequest)6 Status (com.google.android.gms.common.api.Status)5 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)4 IntentSender (android.content.IntentSender)2 Address (android.location.Address)2 Location (android.location.Location)2 LocationManager (android.location.LocationManager)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 PendingResult (com.google.android.gms.common.api.PendingResult)2 LocationSettingsStates (com.google.android.gms.location.LocationSettingsStates)2 List (java.util.List)2 AsyncTask (android.os.AsyncTask)1 Bundle (android.os.Bundle)1 View (android.view.View)1 ConnectionResult (com.google.android.gms.common.ConnectionResult)1 ResultCallback (com.google.android.gms.common.api.ResultCallback)1 CameraUpdate (com.google.android.gms.maps.CameraUpdate)1 LatLng (com.google.android.gms.maps.model.LatLng)1 Observable (io.reactivex.Observable)1