Skip to content

Instantly share code, notes, and snippets.

@kjromero
Created June 6, 2019 17:17
Show Gist options
  • Select an option

  • Save kjromero/f25d7ea1fcb750058f3847eb687b3ba2 to your computer and use it in GitHub Desktop.

Select an option

Save kjromero/f25d7ea1fcb750058f3847eb687b3ba2 to your computer and use it in GitHub Desktop.
Tracking Device
public class TrackService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener,
android.location.LocationListener {
private static final long LOCATION_INTERVAL = 20000;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private LocationManager locationManager;
private boolean usingLocationServices;
private boolean usingLocationAPI;
private int courierId;
private RealtimeData singleton;
/**
* Diálogo que se está mostrando en este momento
*/
private Dialog dialog;
@Inject
ApiService apiService;
@Inject
TrackApiService trackApiService;
@Override
public void onCreate() {
//Inyecta las dependencias
((MyApplication) getApplication()).getApplicationComponent().inject(this);
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationRequest = new LocationRequest();
locationRequest.setInterval(LOCATION_INTERVAL);
locationRequest.setFastestInterval(LOCATION_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
usingLocationServices = false;
usingLocationAPI = false;
courierId = Functions.getCourierId(this);
singleton = RealtimeData.getInstance();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!usingLocationServices) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int result = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
if (usingLocationAPI)
removeLocationAPIUpdates();
googleApiClient.connect();
} else if (!usingLocationAPI)
requestLocationAPIUpdates();
}
return START_STICKY;
}
@Override
public void onDestroy() {
if (usingLocationServices)
removeLocationServicesUpdates();
if (usingLocationAPI)
removeLocationAPIUpdates();
}
private void requestLocationServicesUpdates() {
usingLocationServices = true;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
private void requestLocationAPIUpdates() {
usingLocationAPI = true;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, 0, this);
}
private void removeLocationServicesUpdates() {
usingLocationServices = false;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
private void removeLocationAPIUpdates() {
usingLocationAPI = false;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
EventBus.getDefault().post(new LocationChangeEvent(location));
singleton.setLocation(location);
List<Task> tasks = singleton.getActiveTasksClone();
SendTrackPayload payload = new SendTrackPayload();
payload.setCourierId(courierId);
payload.setLatitude(location.getLatitude());
payload.setLongitude(location.getLongitude());
Call call = trackApiService.sendTrack(payload);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
//No se hace nada por ahora
}
@Override
public void onFailure(Call call, Throwable t) {
if (!(t instanceof IOException))
Crashlytics.logException(t);
}
});
}
//-------------------------------------------------------------------------------------------
// Métodos de Location Services
// ------------------------------------------------------------------------------------------
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationServicesUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
usingLocationServices = false;
requestLocationAPIUpdates();
}
//-------------------------------------------------------------------------------------------
// Métodos de Location API
// ------------------------------------------------------------------------------------------
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment