Created
February 8, 2021 20:18
-
-
Save alifhasnain/d6b470e41bbf53df9d77f46ce3958df5 to your computer and use it in GitHub Desktop.
Lifecycle aware location observer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest; | |
import android.annotation.SuppressLint; | |
import android.app.Activity; | |
import android.content.IntentSender; | |
import android.content.pm.PackageManager; | |
import android.os.Looper; | |
import androidx.core.app.ActivityCompat; | |
import androidx.lifecycle.Lifecycle; | |
import androidx.lifecycle.LifecycleObserver; | |
import androidx.lifecycle.OnLifecycleEvent; | |
import com.google.android.gms.common.api.ResolvableApiException; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.location.LocationSettingsRequest; | |
import com.google.android.gms.location.LocationSettingsStates; | |
import com.google.android.gms.location.SettingsClient; | |
import timber.log.Timber; | |
import static cmyap.mypackage.ui.MainActivity.RESOLVE_LOCATION_PERMISSION_RESOLUTION; | |
class LocationObserver implements LifecycleObserver { | |
private Activity activity; | |
private final FusedLocationProviderClient fusedLocationProviderClient; | |
private final LocationCallback locationCallback; | |
public LocationObserver(Activity activity, LocationCallback locationCallback) { | |
this.activity = activity; | |
this.fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity); | |
this.locationCallback = locationCallback; | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | |
@SuppressLint("BinaryOperationInTimber") | |
private void checkIfLocationSettingsAreOkayOrResolveThem() { | |
LocationRequest locationRequest = getLocationRequest(); | |
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() | |
.addLocationRequest(locationRequest); | |
SettingsClient client = LocationServices.getSettingsClient(activity); | |
client.checkLocationSettings(builder.build()).addOnSuccessListener(locationSettingsResponse -> { | |
LocationSettingsStates states = locationSettingsResponse.getLocationSettingsStates(); | |
Timber.e("GPS Present: " + states.isGpsPresent()); | |
Timber.e("Location Present: " + states.isLocationPresent()); | |
Timber.e("GPS Usable: " + states.isGpsUsable()); | |
Timber.e("Location Usable: " + states.isLocationUsable()); | |
}).addOnFailureListener(e -> { | |
Timber.e(e); | |
if (e instanceof ResolvableApiException) { | |
try { | |
ResolvableApiException resolvable = (ResolvableApiException) e; | |
resolvable.startResolutionForResult(activity, RESOLVE_LOCATION_PERMISSION_RESOLUTION); | |
} catch (IntentSender.SendIntentException sendEx) { | |
Timber.e(sendEx); | |
} catch(NullPointerException ex) { | |
Timber.e(ex); | |
} | |
} | |
}); | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | |
public void connectListener() { | |
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | |
fusedLocationProviderClient.requestLocationUpdates(getLocationRequest(), locationCallback, Looper.getMainLooper()); | |
} | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) | |
public void disconnectListener() { | |
fusedLocationProviderClient.removeLocationUpdates(locationCallback); | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
public void clearContext() { | |
activity = null; | |
} | |
private LocationRequest getLocationRequest() { | |
LocationRequest locationRequest = LocationRequest.create(); | |
locationRequest.setInterval(3000); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
locationRequest.setExpirationDuration(1800000); | |
return locationRequest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment