Skip to content

Commit

Permalink
fix #126 location handler lifecycle and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed May 7, 2016
1 parent a854119 commit 700bbd3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ class AppService protected constructor(private val handler: Handler, private val
if (alertEnabled && backgroundPeriod > 0) {
logElements += "enable_bg"
locationHandler.enableBackgroundMode()
locationHandler.updateProvider()
disableHandler()
createAlarm()
} else {
logElements += "disable_bg"
alertHandler.unsetAlertListener()
locationHandler.shutdown()
discardAlarm()
}
} else {
Expand All @@ -255,6 +255,7 @@ class AppService protected constructor(private val handler: Handler, private val
dataHandler.updateData()
}
}
locationHandler.start()
locationHandler.disableBackgroundMode()
}
Log.v(Main.LOG_TAG, "AppService.configureServiceMode() ${logElements.joinToString(", ")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ open class LocationHandler(
: SharedPreferences.OnSharedPreferenceChangeListener {

private var backgroundMode = true
set(value) {
val shouldUpdateProvider = field != value
field = value
if (shouldUpdateProvider) {
updateProvider()
}
}

private var provider: LocationProvider? = null
private val consumerContainer = object : ConsumerContainer<LocationEvent>() {
override fun addedFirstConsumer() {
provider?.run {
if(!isRunning) {
if (!isRunning) {
start()
Log.d(Main.LOG_TAG, "LocationHandler: enable provider")
}
Expand All @@ -53,7 +61,7 @@ open class LocationHandler(

override fun removedLastConsumer() {
provider?.run {
if(isRunning) {
if (isRunning) {
Log.d(Main.LOG_TAG, "LocationHandler: disable provider")
shutdown()
}
Expand Down Expand Up @@ -133,15 +141,31 @@ open class LocationHandler(
backgroundMode = false
}

fun updateProvider() {
private fun updateProvider() {
shutdown()

provider?.run {
if(isRunning)
shutdown()
if (this is ManagerLocationProvider) {
backgroundMode = this@LocationHandler.backgroundMode
}
}

start()
}

if (this is ManagerLocationProvider)
this.backgroundMode = backgroundMode
fun shutdown() {
provider?.run {
if (isRunning) {
shutdown()
}
}
}

start()
fun start() {
provider?.run {
if (!isRunning) {
start()
}
}
}

Expand All @@ -166,4 +190,5 @@ open class LocationHandler(
companion object {
val MANUAL_PROVIDER = "manual"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ abstract class LocationProvider(protected val locationUpdate: (Location?) -> Uni

open fun start() {
isRunning = true
Log.v(Main.LOG_TAG, "Provider $type started" )
Log.v(Main.LOG_TAG, "LocationProvider.start() type: $type" )
}

open fun shutdown() {
isRunning = false

//Send the consumers a NULL location, so they know we don't have a valid location at the moment
sendLocationUpdate(null)
Log.v(Main.LOG_TAG, "Provider $type stopped" )
Log.v(Main.LOG_TAG, "LocationProvider.shutdown() type: $type" )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,49 @@ import android.util.Log
import org.blitzortung.android.app.Main
import org.jetbrains.anko.locationManager

abstract class ManagerLocationProvider(protected val context: Context,
var backgroundMode: Boolean = true,
locationUpdate: (Location?) -> Unit,
override val type: String)
: LocationProvider(locationUpdate), LocationListener {
abstract class ManagerLocationProvider(
protected val context: Context,
var backgroundMode: Boolean = true,
locationUpdate: (Location?) -> Unit,
override val type: String
) : LocationProvider(locationUpdate), LocationListener {

protected val locationManager = context.locationManager

protected open val minTime: Long
get() = if(backgroundMode) 12000 else 20000
get() = if (backgroundMode) 120000 else 20000

protected open val minDistance: Float
get() = if(backgroundMode) 200f else 50f
get() = if (backgroundMode) 200f else 50f

override fun start() {
//Don't start the LocationProvider if we dont have any permissions
if(!this.isPermissionGranted) {
if (!this.isPermissionGranted) {
Log.d(Main.LOG_TAG, "Tried to start provider '$type' without permission granted")
return
}

super.start()

Log.v(Main.LOG_TAG, "ManagerLocationProvider.start() background: $backgroundMode, type: $type, minTime: $minTime, minDistance: $minDistance")
locationManager.requestLocationUpdates(type, minTime, minDistance, this)
}

override fun onLocationChanged(location: Location?) {
//Don't send NULL locations to the listeners
if(location is Location) {
if (location is Location) {
sendLocationUpdate(location)
}
}

override fun onProviderDisabled(provider: String) { }
override fun onProviderDisabled(provider: String) {
}

override fun onProviderEnabled(provider: String) { }
override fun onProviderEnabled(provider: String) {
}

override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { }
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}

override val isEnabled: Boolean
get() = locationManager.isProviderEnabled(type)
Expand Down

0 comments on commit 700bbd3

Please sign in to comment.