Skip to content

Instantly share code, notes, and snippets.

@praslnx8
Created July 18, 2017 10:14
Show Gist options
  • Save praslnx8/31001f757faee135b1157386d73a68b7 to your computer and use it in GitHub Desktop.
Save praslnx8/31001f757faee135b1157386d73a68b7 to your computer and use it in GitHub Desktop.
package com.prasilabs.ffloc.services.location;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
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 com.prasilabs.ffloc.debug.ConsoleLog;
import com.prasilabs.ffloc.enums.BikeEvent;
import com.prasilabs.ffloc.internalPojos.FLocation;
import com.prasilabs.ffloc.modelEngines.TrackerModelEngine;
import com.prasilabs.ffloc.services.clientNotification.FFNotifManager;
import com.prasilabs.ffloc.services.orientation.FSensorTracker;
/**
* Created by prasi on 25/11/16.
*/
@Deprecated
public class GLocationFetchService extends Service implements LocationListener, FSensorTracker.FSensorCallBack, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
private static final String TAG = GLocationFetchService.class.getSimpleName();
private static final String FROM_BTN_STR = "from_btn";
private static GLocationFetchService self;
private LocationManager lm;
private GoogleApiClient mGoogleApiClient;
private boolean isFromBtn;
public static GLocationFetchService getSelf()
{
return self;
}
public static void startLocationService(Context context, boolean fromBtn)
{
Intent intent = new Intent(context, GLocationFetchService.class);
intent.putExtra(FROM_BTN_STR, fromBtn);
intent.setFlags(START_REDELIVER_INTENT);
context.startService(intent);
}
@Deprecated
private static void stopService() {
try {
if (self != null) {
self.stopSelf();
}
} catch (Exception e) {
ConsoleLog.e(e);
}
}
@Override
public void onCreate()
{
super.onCreate();
if(lm == null)
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
self = this;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if(intent != null && intent.getExtras() != null)
{
isFromBtn = intent.getBooleanExtra(FROM_BTN_STR, false);
}
init();
FSensorTracker.getInstance(this).register(this);
return START_STICKY;
}
private void init()
{
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
if (checkLocationPermission(this))
{
ConsoleLog.i(TAG, "location registered");
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mGoogleApiClient.connect();
FFNotifManager.showTrackingNotification(this);
TrackerModelEngine.getInstance().startLocation();
}
else
{
stopService();
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onLocationChanged(Location location)
{
if(location != null)
{
TrackerModelEngine.getInstance().updateCurrentLocation(FLocation.getLocation(location));
}
}
private boolean checkLocationPermission(Context context)
{
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
@Deprecated
public boolean stopTracking()
{
boolean status = false;
/*try {
TrackerModelEngine.getInstance().stopTracking(null,null);
} catch (UnauthorizedAccessException e) {
e.printStackTrace();
}*/
removeLocationUpdates();
FFNotifManager.dismissTrackingNotification(this);
stopService();
return status;
}
@Override
public void onDestroy()
{
super.onDestroy();
FSensorTracker.getInstance(this).unRegister();
FFNotifManager.dismissTrackingNotification(this);
removeLocationUpdates();
}
private void removeLocationUpdates()
{
if(mGoogleApiClient.isConnected())
{
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void addEvent(BikeEvent bikeEvent)
{
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
if(checkLocationPermission(this))
{
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(2);
locationRequest.setFastestInterval(1);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, locationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i)
{
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
ConsoleLog.w(TAG, "connection failed");
}
public boolean isFromBtn() {
return isFromBtn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment