Skip to content

Commit

Permalink
Convert TrackerSettings to Kotlin and remove timeout field
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin7b committed Jun 22, 2020
1 parent 4a43216 commit c240ba1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 193 deletions.
29 changes: 9 additions & 20 deletions slt/src/main/java/fr/quentinklein/slt/LocationTracker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.quentinklein.slt;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
Expand Down Expand Up @@ -59,7 +60,7 @@ public abstract class LocationTracker implements LocationListener {
*/
@RequiresPermission(anyOf = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION})
public LocationTracker(@NonNull Context context) {
this(context, TrackerSettings.DEFAULT);
this(context, new TrackerSettings());
}

/**
Expand All @@ -76,13 +77,13 @@ public LocationTracker(@NonNull Context context, @NonNull TrackerSettings tracke
// Get the location manager
this.mLocationManagerService = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// default
if (sLocation == null && trackerSettings.shouldUseGPS()) {
if (sLocation == null && trackerSettings.getShouldUseGPS()) {
LocationTracker.sLocation = mLocationManagerService.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (sLocation == null && trackerSettings.shouldUseNetwork()) {
if (sLocation == null && trackerSettings.getShouldUseNetwork()) {
LocationTracker.sLocation = mLocationManagerService.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (sLocation == null && trackerSettings.shouldUsePassive()) {
if (sLocation == null && trackerSettings.getShouldUsePassive()) {
LocationTracker.sLocation = mLocationManagerService.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
}
Expand All @@ -93,6 +94,7 @@ public LocationTracker(@NonNull Context context, @NonNull TrackerSettings tracke
*
* @see #startListening()
*/
@SuppressLint("MissingPermission")
@Deprecated
public final void startListen() {
startListening();
Expand All @@ -106,23 +108,23 @@ public final void startListening() {
if (!mIsListening) {
Log.i(TAG, "LocationTracked is now listening for location updates");
// Listen for GPS Updates
if (mTrackerSettings.shouldUseGPS()) {
if (mTrackerSettings.getShouldUseGPS()) {
if (LocationUtils.isGpsProviderEnabled(mContext)) {
mLocationManagerService.requestLocationUpdates(LocationManager.GPS_PROVIDER, mTrackerSettings.getTimeBetweenUpdates(), mTrackerSettings.getMetersBetweenUpdates(), this);
} else {
onProviderError(new ProviderError(LocationManager.GPS_PROVIDER, "Provider is not enabled"));
}
}
// Listen for Network Updates
if (mTrackerSettings.shouldUseNetwork()) {
if (mTrackerSettings.getShouldUseNetwork()) {
if (LocationUtils.isNetworkProviderEnabled(mContext)) {
mLocationManagerService.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, mTrackerSettings.getTimeBetweenUpdates(), mTrackerSettings.getMetersBetweenUpdates(), this);
} else {
onProviderError(new ProviderError(LocationManager.NETWORK_PROVIDER, "Provider is not enabled"));
}
}
// Listen for Passive Updates
if (mTrackerSettings.shouldUsePassive()) {
if (mTrackerSettings.getShouldUseNetwork()) {
if (LocationUtils.isPassiveProviderEnabled(mContext)) {
mLocationManagerService.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, mTrackerSettings.getTimeBetweenUpdates(), this.mTrackerSettings.getMetersBetweenUpdates(), this);
} else {
Expand All @@ -131,19 +133,6 @@ public final void startListening() {
}
mIsListening = true;

// If user has set a timeout
if (mTrackerSettings.getTimeout() != -1) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!mIsLocationFound && mIsListening) {
Log.i(TAG, "No location found in the meantime");
stopListening();
onTimeout();
}
}
}, mTrackerSettings.getTimeout());
}
} else {
Log.i(TAG, "Relax, LocationTracked is already listening for location updates");
}
Expand Down
186 changes: 13 additions & 173 deletions slt/src/main/java/fr/quentinklein/slt/TrackerSettings.kt
Original file line number Diff line number Diff line change
@@ -1,177 +1,17 @@
package fr.quentinklein.slt

import android.support.annotation.FloatRange
import android.support.annotation.IntRange

/**
* @author Quentin Klein <klein.quentin></klein.quentin>@gmail.com>, Yasir.Ali <ali.yasir0></ali.yasir0>@gmail.com>
*
*
* Helps the LocationTracker to set the attributes:
*
* * useGPS * true if GPS usage is wanted * false otherwise
* * useNetwork * true if Network usage is wanted * false otherwise
* * usePassive * true if Passive usage is wanted * false otherwise
* * minTimeBetweenUpdates the minimum time interval between location updates in milliseconds
* * minMetersBetweenUpdates the minimum distance between location updates, in meters
* * timeout the minimum time delay before the tracker stops scanning for location in milliseconds
*
*
* Build the settings for the tracker
* @param timeBetweenUpdates The minimum time interval between location updates, in milliseconds by default its value is 5 minutes
* @param metersBetweenUpdates The minimum distance between location updates in meters, by default its value is 100m
* @param shouldUseGPS Specifies if tracker should use the GPS (default is true)
* @param shouldUseNetwork Specifies if tracker should use the Network (default is true)
* @param shouldUsePassive Specifies if tracker should use the Passive provider (default is true)
*/
class TrackerSettings {
/**
* The minimum time interval between location updates, in milliseconds by default its value is [.DEFAULT_MIN_TIME_BETWEEN_UPDATES]
*/
private var mTimeBetweenUpdates: Long = -1

/**
* The minimum distance between location updates in meters, by default its value is [.DEFAULT_MIN_METERS_BETWEEN_UPDATES]
*/
private var mMetersBetweenUpdates = -1f

/**
* The value of mTimeout to stop the listener after a specified time in case the listener is unable to get the location for a specified time
*/
private var mTimeout = -1

/**
* Specifies if tracker should use the GPS (default is true)
*/
private var mUseGPS = true

/**
* Specifies if tracker should use the Network (default is true)
*/
private var mUseNetwork = true

/**
* Specifies if tracker should use the Passive provider (default is true)
*/
private var mUsePassive = true

/**
* Set the delay between updates of the location
*
* @param timeBetweenUpdates the delay between the updates
* @return the instance of TrackerSettings
*/
fun setTimeBetweenUpdates(@FloatRange(from = 1) timeBetweenUpdates: Long): TrackerSettings {
if (timeBetweenUpdates > 0) {
mTimeBetweenUpdates = timeBetweenUpdates
}
return this
}

val timeBetweenUpdates: Long
get() = if (mTimeBetweenUpdates <= 0) DEFAULT_MIN_TIME_BETWEEN_UPDATES else mTimeBetweenUpdates

/**
* Set the distance between updates of the location
*
* @param metersBetweenUpdates the distance between the updates
* @return the instance of TrackerSettings
*/
fun setMetersBetweenUpdates(@FloatRange(from = 1) metersBetweenUpdates: Float): TrackerSettings {
if (metersBetweenUpdates > 0) {
mMetersBetweenUpdates = metersBetweenUpdates
}
return this
}

val metersBetweenUpdates: Float
get() = if (mMetersBetweenUpdates <= 0) DEFAULT_MIN_METERS_BETWEEN_UPDATES else mMetersBetweenUpdates

/**
* Set the timeout before giving up if no updates
*
* @param timeout the timeout before giving up
* @return the instance of TrackerSettings
*/
fun setTimeout(@IntRange(from = 1) timeout: Int): TrackerSettings {
if (timeout > 0) {
mTimeout = timeout
}
return this
}

val timeout: Int
get() = if (mTimeout <= -1) DEFAULT_TIMEOUT else mTimeout

/**
* Set the usage of the GPS for the tracking
*
* @param useGPS true if the tracker should use the GPS, false if not
* @return the instance of TrackerSettings
*/
fun setUseGPS(useGPS: Boolean): TrackerSettings {
mUseGPS = useGPS
return this
}

fun shouldUseGPS(): Boolean {
return mUseGPS
}

/**
* Set the usage of network for the tracking
*
* @param useNetwork true if the tracker should use the network, false if not
* @return the instance of TrackerSettings
*/
fun setUseNetwork(useNetwork: Boolean): TrackerSettings {
mUseNetwork = useNetwork
return this
}

fun shouldUseNetwork(): Boolean {
return mUseNetwork
}

/**
* Set the usage of the passive sensor for the tracking
*
* @param usePassive true if the tracker should listen to passive updates, false if not
* @return the instance of TrackerSettings
*/
fun setUsePassive(usePassive: Boolean): TrackerSettings {
mUsePassive = usePassive
return this
}

fun shouldUsePassive(): Boolean {
return mUsePassive
}

companion object {
/**
* Basic tracker settings, with all the default parameters
*
* * 5min between updates
* * 100m between updates
* * 100m between updates
* * 1m timeout
* * Uses Network
* * Uses Passive
*
*/
val DEFAULT = TrackerSettings()

/**
* The default time interval between location updates
* Its value is 5 minutes
*/
const val DEFAULT_MIN_TIME_BETWEEN_UPDATES = 5 * 60 * 1000.toLong()

/**
* The default distance between location updates
* Its value is 100m
*/
const val DEFAULT_MIN_METERS_BETWEEN_UPDATES = 100f

/**
* The default value of timeout that helps to stop the listener if the listener is taking too much time
* Its value is 1 minutes
*/
const val DEFAULT_TIMEOUT = 60 * 1000
}
}
class TrackerSettings(
val timeBetweenUpdates: Long = 5 * 60 * 1000.toLong(),
val metersBetweenUpdates: Float = 100f,
val shouldUseGPS: Boolean = true,
val shouldUseNetwork: Boolean = true,
val shouldUsePassive: Boolean = true
)

0 comments on commit c240ba1

Please sign in to comment.