-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from joeloewi7178/development
Development
- Loading branch information
Showing
7 changed files
with
317 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/com/joeloewi/croissant/util/WorkExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.joeloewi.croissant.util | ||
|
||
import android.net.ConnectivityManager | ||
import android.os.Build | ||
import androidx.core.content.getSystemService | ||
import androidx.work.CoroutineWorker | ||
|
||
suspend fun <T> CoroutineWorker.withBoundNetwork(block: suspend () -> T): T { | ||
val connectivityManager = applicationContext.getSystemService<ConnectivityManager>() | ||
|
||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
if (network != null) { | ||
val shouldBeUnbound = connectivityManager?.bindProcessToNetwork(network) | ||
|
||
try { | ||
block() | ||
} finally { | ||
if (shouldBeUnbound == true) { | ||
connectivityManager.bindProcessToNetwork(null) | ||
} | ||
} | ||
} else { | ||
block() | ||
} | ||
} else { | ||
block() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.