Skip to content

Commit 9f03d5c

Browse files
committed
Location: Fix potential dead-lock
1 parent c808647 commit 9f03d5c

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

play-services-location/core/src/main/java/org/microg/gms/location/UnifiedLocationProvider.kt

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,18 @@ class UnifiedLocationProvider(private val context: Context, private val changeLi
8282
}
8383
}
8484
requests.add(request)
85-
updateConnection()
85+
lifecycleScope.launchWhenStarted {
86+
updateConnection()
87+
}
8688
}
8789

8890
fun removeRequest(request: LocationRequestHelper) {
8991
Log.d(TAG, "unified network: removeRequest $request")
9092
requests.remove(request)
91-
updateConnection()
93+
94+
lifecycleScope.launchWhenStarted {
95+
updateConnection()
96+
}
9297
}
9398

9499
fun getLastLocation(): Location? {
@@ -98,28 +103,25 @@ class UnifiedLocationProvider(private val context: Context, private val changeLi
98103
return lastLocation
99104
}
100105

101-
@Synchronized
102-
private fun updateConnection() {
103-
lifecycleScope.launchWhenStarted {
104-
activeRequestMutex.withLock {
105-
if (activeRequestIds.isNotEmpty() && requests.isEmpty()) {
106-
Log.d(TAG, "unified network: no longer requesting location update")
107-
for (id in activeRequestIds) {
108-
client.cancelLocationRequestById(id)
109-
}
110-
activeRequestIds.clear()
111-
} else if (requests.isNotEmpty()) {
112-
val requests = ArrayList(requests).filter { it.isActive }
113-
for (id in activeRequestIds.filter { id -> requests.none { it.id == id } }) {
114-
client.cancelLocationRequestById(id)
115-
}
116-
for (request in requests.filter { it.id !in activeRequestIds }) {
117-
client.updateLocationRequest(LocationRequest(listener, request.locationRequest.intervalMillis, request.locationRequest.maxUpdates, request.id), Bundle().apply {
118-
putString("packageName", request.packageName)
119-
putString("source", "GoogleLocationManager")
120-
})
121-
activeRequestIds.add(request.id)
122-
}
106+
private suspend fun updateConnection() {
107+
activeRequestMutex.withLock {
108+
if (activeRequestIds.isNotEmpty() && requests.isEmpty()) {
109+
Log.d(TAG, "unified network: no longer requesting location update")
110+
for (id in activeRequestIds) {
111+
client.cancelLocationRequestById(id)
112+
}
113+
activeRequestIds.clear()
114+
} else if (requests.isNotEmpty()) {
115+
val requests = ArrayList(requests).filter { it.isActive }
116+
for (id in activeRequestIds.filter { id -> requests.none { it.id == id } }) {
117+
client.cancelLocationRequestById(id)
118+
}
119+
for (request in requests.filter { it.id !in activeRequestIds }) {
120+
client.updateLocationRequest(LocationRequest(listener, request.locationRequest.intervalMillis, request.locationRequest.maxUpdates, request.id), Bundle().apply {
121+
putString("packageName", request.packageName)
122+
putString("source", "GoogleLocationManager")
123+
})
124+
activeRequestIds.add(request.id)
123125
}
124126
}
125127
}

0 commit comments

Comments
 (0)