Skip to content

Commit

Permalink
feat(listeners): Add optional Behavior Listener (#33)
Browse files Browse the repository at this point in the history
* Add Behavior Listener for provider changes
* Update gradle and build.gradle
  • Loading branch information
quentin7b authored Sep 22, 2020
1 parent 2450c6c commit a66bdc8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.4.10'
repositories {
google()
jcenter()
Expand Down
4 changes: 2 additions & 2 deletions slt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 30
versionCode 7
versionName "4.0"
versionCode 8
versionName "4.1"
}

lintOptions {
Expand Down
54 changes: 39 additions & 15 deletions slt/src/main/java/fr/quentinklein/slt/LocationTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class LocationTracker constructor(
// Android LocationManager
private lateinit var locationManager: LocationManager

// TAG for Logs
private val TAG = "LocationTracker"

// Last known location
private var lastKnownLocation: Location? = null

Expand All @@ -47,24 +44,25 @@ class LocationTracker constructor(
}

override fun onProviderDisabled(provider: String) {
Log.i(TAG, "Provider `$provider` has been disabled")
behaviorListener.forEach { l -> l.onProviderDisabled(provider) }
}

override fun onProviderEnabled(provider: String) {
// By default do nothing but log
Log.i(TAG, "Provider `$provider` has been enabled")
behaviorListener.forEach { l -> l.onProviderEnabled(provider) }
}

override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
// By default do nothing but log
Log.i(TAG, "Provider `$provider` status has changed, new status is `$status`")
behaviorListener.forEach { l -> l.onStatusChanged(provider, status, extras) }
}

}

// List used to register the listeners to notify
private val listeners: MutableSet<Listener> = mutableSetOf()

// List used to register the behavior listeners to notify
private val behaviorListener: MutableSet<BehaviorListener> = mutableSetOf()

/**
* Indicates if Tracker is listening for updates or not
*/
Expand All @@ -86,11 +84,26 @@ class LocationTracker constructor(

/**
* Remove a listener from the stack
* @param listener the listener to add to the list.
* @param listener the listener to remove from the list.
* @return true if the listener has been removed, false otherwise
*/
fun removeListener(listener: Listener): Boolean = listeners.remove(listener)

/**
* Add a behavior listener to the stack so it will be notified when a provider is updated
* @param listener the listener to add to the list.
* @return true if the listener has been added, false otherwise
*/
fun addBehaviorListener(listener: BehaviorListener): Boolean = behaviorListener.add(listener)

/**
* Remove a behavior listener from the stack
* @param listener the listener to remove from the list.
* @return true if the listener has been removed, false otherwise
*/
fun removeBehaviorListener(listener: BehaviorListener): Boolean = behaviorListener.remove(listener)


/**
* Make the tracker listening for location updates
* @param context a valid android.content.Context
Expand All @@ -112,9 +125,6 @@ class LocationTracker constructor(
registerForLocationUpdates(LocationManager.PASSIVE_PROVIDER)
}
isListening = true
Log.i(TAG, "Now listening for location updates")
} else {
Log.i(TAG, "Relax, already listening for location updates")
}
}

Expand All @@ -129,9 +139,6 @@ class LocationTracker constructor(
if (clearListeners) {
listeners.clear()
}
Log.i(TAG, "Stop listening for location updates")
} else {
Log.i(TAG, "Not listening at this time")
}
}

Expand Down Expand Up @@ -191,4 +198,21 @@ class LocationTracker constructor(
fun onProviderError(providerError: ProviderError)
}

interface BehaviorListener {
/**
* See android.location.LocationListener#onProviderDisabled
*/
fun onProviderDisabled(provider: String)

/**
* See android.location.LocationListener#onProviderEnabled
*/
fun onProviderEnabled(provider: String)

/**
* See android.location.LocationListener#onStatusChanged
*/
fun onStatusChanged(provider: String, status: Int, extras: Bundle)
}

}

0 comments on commit a66bdc8

Please sign in to comment.