Last active
April 29, 2019 06:48
-
-
Save Tenkei/024777abb8f480136796f1fb84328fb2 to your computer and use it in GitHub Desktop.
Life cycle aware location provider
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
package com.esbati.keivan.livelocation; | |
import android.Manifest; | |
import android.arch.lifecycle.LiveData; | |
import android.content.Context; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.SystemClock; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.annotation.RequiresPermission; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
public class LiveLocation extends LiveData<Location> implements | |
GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, | |
LocationListener{ | |
public static final String TAG = "LiveLocation"; | |
GoogleApiClient googleApiClient; | |
@RequiresPermission(Manifest.permission.ACCESS_COARSE_LOCATION) | |
public LiveLocation(Context context) { | |
googleApiClient = new GoogleApiClient.Builder(context, this, this) | |
.addApi(LocationServices.API) | |
.build(); | |
googleApiClient.connect(); | |
} | |
@SuppressWarnings("MissingPermission") | |
@Override | |
protected void onActive() { | |
super.onActive(); | |
if(googleApiClient.isConnected()) { | |
//Set Listener to Get Updated Location if client is Connected! | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
googleApiClient, | |
new LocationRequest(), | |
this | |
); | |
} else if(googleApiClient.isConnecting()) { | |
//Wait for Client to Connect! | |
} else { | |
//If Client is Not Connected/Connecting, Connect! | |
googleApiClient.connect(); | |
} | |
} | |
@Override | |
protected void onInactive() { | |
super.onInactive(); | |
//If Client is connected remove the Listener | |
if(googleApiClient.isConnected()) | |
LocationServices.FusedLocationApi.removeLocationUpdates( | |
googleApiClient, this); | |
} | |
@SuppressWarnings("MissingPermission") | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
// Try to immediately find a location | |
Location lastLocation = LocationServices.FusedLocationApi | |
.getLastLocation(googleApiClient); | |
if (lastLocation != null){ | |
setValue(lastLocation); | |
Log.d(TAG, "onConnected: " + lastLocation.toString()); | |
} | |
// Request updates if there’s someone observing | |
if(hasActiveObservers()) | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
googleApiClient, | |
new LocationRequest(), | |
this | |
); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.d(TAG, "onConnectionSuspended: " + i); | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
Log.d(TAG, "onConnectionFailed: " + connectionResult.getErrorMessage()); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
setValue(location); | |
Log.d(TAG, "onLocationChanged: " + location.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment