Skip to content

Instantly share code, notes, and snippets.

@fida1989
Created March 31, 2019 08:25
Show Gist options
  • Save fida1989/98804ccc6b53e5c353a475c88bf8933d to your computer and use it in GitHub Desktop.
Save fida1989/98804ccc6b53e5c353a475c88bf8933d to your computer and use it in GitHub Desktop.
FusedLocationSingleton
import android.annotation.SuppressLint;
import android.content.Intent;
import android.location.Location;
import android.os.Looper;
import android.support.v4.content.LocalBroadcastManager;
import com.easytransport.shuttle.driver.MyCustomApplication;
import com.easytransport.shuttle.driver.Utils.Constants;
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.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnSuccessListener;
public class FusedLocationSingleton {
/***********************************************************************************************
* properties
**********************************************************************************************/
/**
* The desired interval for location updates. Inexact. Updates may be more or less frequent.
*/
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
/**
* The fastest rate for active location updates. Exact. Updates will never be more frequent
* than this value.
*/
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
// Keys for storing activity state in the Bundle.
private final static String KEY_REQUESTING_LOCATION_UPDATES = "requesting-location-updates";
private final static String KEY_LOCATION = "location";
private final static String KEY_LAST_UPDATED_TIME_STRING = "last-updated-time-string";
//
private static FusedLocationSingleton mInstance = null;
/**
* Provides access to the Fused Location Provider API.
*/
private FusedLocationProviderClient mFusedLocationClient;
/**
* Provides access to the Location Settings API.
*/
private SettingsClient mSettingsClient;
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
private LocationRequest mLocationRequest;
/**
* Stores the types of location services the client is interested in using. Used for checking
* settings to determine if the device has optimal location settings.
*/
private LocationSettingsRequest mLocationSettingsRequest;
/**
* Callback for Location events.
*/
private LocationCallback mLocationCallback;
/* protected GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public final static int FAST_LOCATION_FREQUENCY = 5 * 1000;
public final static int LOCATION_FREQUENCY = 5 * 1000;*/
private Location loc = null;
/***********************************************************************************************
* methods
**********************************************************************************************/
/**
* constructor
*/
private FusedLocationSingleton() {
buildLocationClient();
}
public static FusedLocationSingleton getInstance() {
if (null == mInstance) {
mInstance = new FusedLocationSingleton();
}
return mInstance;
}
/**
* destructor
*
* @throws Throwable
*/
@Override
protected void finalize() throws Throwable {
super.finalize();
stopLocationUpdates();
}
///////////// 1
/**
* builds a LocationApiClient
*/
private synchronized void buildLocationClient() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MyCustomApplication.Companion.applicationContext());
mLocationRequest = new LocationRequest();
// Sets the desired interval for active location updates. This interval is
// inexact. You may not receive updates at all if no location sources are available, or
// you may receive them slower than requested. You may also receive updates faster than
// requested if other applications are requesting location at a faster interval.
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
// Sets the fastest rate for active location updates. This interval is exact, and your
// application will never receive updates faster than this value.
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
createLocationCallback();
}
/**
* Creates a callback for receiving location events.
*/
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
// send location in broadcast
Intent intent = new Intent(Constants.INSTANCE.getINTENT_FILTER_LOCATION_UPDATE());
intent.putExtra(Constants.INSTANCE.getLBM_EVENT_LOCATION_UPDATE(), locationResult.getLocations().get(0));
LocalBroadcastManager.getInstance(MyCustomApplication.Companion.applicationContext()).sendBroadcast(intent);
}
};
}
///////////// 3
/**
* request location updates
*/
@SuppressLint("MissingPermission")
private void requestLocationUpdates() {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
}
/**
* start location updates
*/
public void startLocationUpdates() {
// connect and force the updates
requestLocationUpdates();
}
/**
* removes location updates from the FusedLocationApi
*/
public void stopLocationUpdates() {
// It is a good practice to remove location requests when the activity is in a paused or
// stopped state. Doing so helps battery performance and is especially
// recommended in applications that request frequent location updates.
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
}
@SuppressLint("MissingPermission")
public Location getLastLocation() {
if (mFusedLocationClient != null) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
loc = location;
} else {
loc = null;
}
}
});
return loc;
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment