Skip to content

Commit 298e4dc

Browse files
authored
Merge pull request #6971 from grzesiek2010/COLLECT-6937
Use NetworkRequest transport types for auto-send constraints
2 parents b9684e6 + 703300d commit 298e4dc

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

async/src/main/java/org/odk/collect/async/CoroutineAndWorkManagerScheduler.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.odk.collect.async
22

3+
import android.net.NetworkCapabilities
4+
import android.net.NetworkRequest
35
import androidx.work.Constraints
46
import androidx.work.Data
57
import androidx.work.ExistingPeriodicWorkPolicy
@@ -57,14 +59,25 @@ class CoroutineAndWorkManagerScheduler(
5759
inputData: Map<String, String>,
5860
networkConstraint: Scheduler.NetworkType?
5961
) {
60-
val constraintNetworkType = when (networkConstraint) {
62+
val networkRequest = NetworkRequest.Builder().apply {
63+
addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
64+
65+
when (networkConstraint) {
66+
Scheduler.NetworkType.WIFI -> addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
67+
Scheduler.NetworkType.CELLULAR -> addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
68+
else -> Unit
69+
}
70+
}.build()
71+
72+
val networkType = when (networkConstraint) {
6173
Scheduler.NetworkType.WIFI -> NetworkType.UNMETERED
6274
Scheduler.NetworkType.CELLULAR -> NetworkType.METERED
6375
else -> NetworkType.CONNECTED
6476
}
6577

66-
val constraints = Constraints.Builder()
67-
.setRequiredNetworkType(constraintNetworkType)
78+
val constraints = Constraints
79+
.Builder()
80+
.setRequiredNetworkRequest(networkRequest, networkType)
6881
.build()
6982

7083
val workManagerInputData = Data.Builder()

0 commit comments

Comments
 (0)