Created
November 15, 2016 22:20
-
-
Save jdholdren/ea9e51c10a4afa75aca8973318d3252d to your computer and use it in GitHub Desktop.
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.content.Context; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.content.ContextCompat; | |
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; | |
import rx.Observable; | |
import rx.Subscriber; | |
import rx.functions.Action0; | |
public class LocationGatewayImpl { | |
private Context context; | |
public LocationGatewayImpl(Context context) { | |
this.context = context; | |
} | |
public Observable<Location> listenToLocation() { | |
final LocationConnectionSubscriber locationConnectionSubscriber = new LocationConnectionSubscriber(); | |
final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this.context) | |
.addOnConnectionFailedListener(locationConnectionSubscriber) | |
.addConnectionCallbacks(locationConnectionSubscriber) | |
.addApi(LocationServices.API) | |
.build(); | |
return Observable.create(new Observable.OnSubscribe<Location>() { | |
@Override | |
public void call(final Subscriber<? super Location> subscriber) { | |
// Set up the listener | |
locationConnectionSubscriber.setListener(new LocationConnectionSubscriber.OnConnectionListener() { | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
subscriber.onError(new Exception(connectionResult.getErrorMessage())); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | |
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); | |
if (location != null) { | |
// Emit the initial location | |
subscriber.onNext(location); | |
} | |
// Listen to location updates | |
LocationRequest locationRequest = new LocationRequest() | |
.setInterval(1000) | |
.setFastestInterval(5000) | |
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
googleApiClient, | |
locationRequest, | |
new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
subscriber.onNext(location); | |
} | |
} | |
); | |
} | |
} | |
}); | |
// Finally connect to the api | |
googleApiClient.connect(); | |
} | |
}).doOnUnsubscribe(new Action0() { | |
@Override | |
public void call() { | |
googleApiClient.disconnect(); | |
} | |
}); | |
} | |
/** | |
* Facade for forwarding out events | |
*/ | |
private static class LocationConnectionSubscriber | |
implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks { | |
// The listener for events | |
private OnConnectionListener listener; | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
listener.onConnectionFailed(connectionResult); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
listener.onConnected(bundle); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
// No-op | |
} | |
public void setListener(OnConnectionListener listener) { | |
this.listener = listener; | |
} | |
interface OnConnectionListener { | |
void onConnectionFailed(@NonNull ConnectionResult connectionResult); | |
void onConnected(@Nullable Bundle bundle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment