Make sure we unbind services instantly when required to not leak service

This commit is contained in:
Marvin W 2022-01-23 20:44:05 +01:00
parent daa8c33eae
commit 0b41056020
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
5 changed files with 26 additions and 12 deletions

View file

@ -54,6 +54,12 @@ abstract class AbstractBackendHelper(private val TAG: String, private val contex
Log.w(TAG, e) Log.w(TAG, e)
} }
} }
unbindNow()
}
}
fun unbindNow() {
if (bound) {
try { try {
Log.d(TAG, "Unbinding from: $serviceIntent") Log.d(TAG, "Unbinding from: $serviceIntent")
context.unbindService(this) context.unbindService(this)

View file

@ -52,8 +52,14 @@ class GeocodeFuser(private val context: Context, private val lifecycle: Lifecycl
} }
} }
suspend fun destroy() { private fun unbindNow() {
unbind() for (backendHelper in backendHelpers) {
backendHelper.unbindNow()
}
}
fun destroy() {
unbindNow()
backendHelpers.clear() backendHelpers.clear()
} }

View file

@ -41,9 +41,7 @@ class GeocodeService : LifecycleService() {
} }
override fun onDestroy() { override fun onDestroy() {
lifecycleScope.launch {
service.destroy() service.destroy()
}
super.onDestroy() super.onDestroy()
Log.d(TAG, "Destroyed") Log.d(TAG, "Destroyed")
} }
@ -187,7 +185,7 @@ class GeocodeServiceImpl(private val context: Context, private val lifecycle: Li
fuser.dump(writer) fuser.dump(writer)
} }
suspend fun destroy() { fun destroy() {
fuser.destroy() fuser.destroy()
} }

View file

@ -57,6 +57,12 @@ class LocationFuser(private val context: Context, private val lifecycle: Lifecyc
} }
} }
private fun unbindNow() {
for (handler in backendHelpers) {
handler.unbindNow()
}
}
fun bind() { fun bind() {
fusing = false fusing = false
for (handler in backendHelpers) { for (handler in backendHelpers) {
@ -64,8 +70,8 @@ class LocationFuser(private val context: Context, private val lifecycle: Lifecyc
} }
} }
suspend fun destroy() { fun destroy() {
unbind() unbindNow()
backendHelpers.clear() backendHelpers.clear()
} }

View file

@ -46,9 +46,7 @@ class LocationService : LifecycleService() {
} }
override fun onDestroy() { override fun onDestroy() {
lifecycleScope.launch {
service.destroy() service.destroy()
}
super.onDestroy() super.onDestroy()
Log.d(TAG, "Destroyed") Log.d(TAG, "Destroyed")
} }
@ -340,7 +338,7 @@ class LocationServiceImpl(private val context: Context, private val lifecycle: L
fuser.dump(writer) fuser.dump(writer)
} }
suspend fun destroy() { fun destroy() {
fuser.destroy() fuser.destroy()
} }