Update usage of work:work-runtime

Co-authored-by: cuong-tran <cuongtran.tm@gmail.com>

Update dependency androidx.work:work-runtime to v2.10.0

(cherry picked from commit 57e6e198b8101aa4ea60da89aea371f827b5f7e4)
This commit is contained in:
AntsyLich 2024-11-07 19:49:30 +06:00 committed by Cuong-Tran
parent c38178b789
commit a3ef057e15
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
3 changed files with 72 additions and 34 deletions

View file

@ -2,6 +2,8 @@ package eu.kanade.tachiyomi.data.library
import android.content.Context
import android.content.pm.ServiceInfo
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import androidx.work.BackoffPolicy
import androidx.work.Constraints
@ -145,10 +147,12 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
override suspend fun doWork(): Result {
if (tags.contains(WORK_NAME_AUTO)) {
val preferences = Injekt.get<LibraryPreferences>()
val restrictions = preferences.autoUpdateDeviceRestrictions().get()
if ((DEVICE_ONLY_ON_WIFI in restrictions) && !context.isConnectedToWifi()) {
return Result.retry()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
val preferences = Injekt.get<LibraryPreferences>()
val restrictions = preferences.autoUpdateDeviceRestrictions().get()
if ((DEVICE_ONLY_ON_WIFI in restrictions) && !context.isConnectedToWifi()) {
return Result.retry()
}
}
// Find a running manual worker. If exists, try again later
@ -794,15 +798,24 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
val interval = prefInterval ?: preferences.autoUpdateInterval().get()
if (interval > 0) {
val restrictions = preferences.autoUpdateDeviceRestrictions().get()
val constraints = Constraints(
requiredNetworkType = if (DEVICE_NETWORK_NOT_METERED in restrictions) {
NetworkType.UNMETERED
} else {
NetworkType.CONNECTED
},
requiresCharging = DEVICE_CHARGING in restrictions,
requiresBatteryNotLow = true,
)
val networkType = if (DEVICE_NETWORK_NOT_METERED in restrictions) {
NetworkType.UNMETERED
} else {
NetworkType.CONNECTED
}
val networkRequestBuilder = NetworkRequest.Builder()
if (DEVICE_ONLY_ON_WIFI in restrictions) {
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
}
if (DEVICE_NETWORK_NOT_METERED in restrictions) {
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
}
val constraints = Constraints.Builder()
// 'networkRequest' only applies to Android 9+, otherwise 'networkType' is used
.setRequiredNetworkRequest(networkRequestBuilder.build(), networkType)
.setRequiresCharging(DEVICE_CHARGING in restrictions)
.setRequiresBatteryNotLow(true)
.build()
val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(
interval.toLong(),

View file

@ -5,6 +5,8 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.content.pm.ServiceInfo
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.work.BackoffPolicy
@ -67,13 +69,15 @@ class AppUpdateDownloadJob(private val context: Context, workerParams: WorkerPar
if (!context.packageManager.canRequestPackageInstalls()) {
return Result.failure()
}
val restrictions = preferences.appShouldAutoUpdate().get()
if ((AppUpdatePolicy.DEVICE_ONLY_ON_WIFI in restrictions) &&
!context.isConnectedToWifi() ||
(AppUpdatePolicy.DEVICE_NETWORK_NOT_METERED in restrictions) &&
context.connectivityManager.isActiveNetworkMetered
) {
return Result.retry()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
val restrictions = preferences.appShouldAutoUpdate().get()
if ((AppUpdatePolicy.DEVICE_ONLY_ON_WIFI in restrictions) &&
!context.isConnectedToWifi() ||
(AppUpdatePolicy.DEVICE_NETWORK_NOT_METERED in restrictions) &&
context.connectivityManager.isActiveNetworkMetered
) {
return Result.retry()
}
}
}
// KMK <--
@ -284,15 +288,25 @@ class AppUpdateDownloadJob(private val context: Context, workerParams: WorkerPar
if (scheduled) {
data.putBoolean(SCHEDULED_RUN, true)
val restrictions = Injekt.get<UnsortedPreferences>().appShouldAutoUpdate().get()
val constraints = Constraints(
requiredNetworkType = if (AppUpdatePolicy.DEVICE_NETWORK_NOT_METERED in restrictions) {
NetworkType.UNMETERED
} else {
NetworkType.CONNECTED
},
requiresCharging = AppUpdatePolicy.DEVICE_CHARGING in restrictions,
requiresBatteryNotLow = true,
)
val networkType = if (AppUpdatePolicy.DEVICE_NETWORK_NOT_METERED in restrictions) {
NetworkType.UNMETERED
} else {
NetworkType.CONNECTED
}
val networkRequestBuilder = NetworkRequest.Builder()
if (AppUpdatePolicy.DEVICE_ONLY_ON_WIFI in restrictions) {
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
}
if (AppUpdatePolicy.DEVICE_NETWORK_NOT_METERED in restrictions) {
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
}
val constraints = Constraints.Builder()
// 'networkRequest' only applies to Android 9+, otherwise 'networkType' is used
.setRequiredNetworkRequest(networkRequestBuilder.build(), networkType)
.setRequiresCharging(AppUpdatePolicy.DEVICE_CHARGING in restrictions)
.setRequiresBatteryNotLow(true)
.build()
setConstraints(constraints)
setInitialDelay(10, TimeUnit.MINUTES)
setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)

View file

@ -2,6 +2,8 @@ package exh.eh
import android.content.Context
import android.content.pm.ServiceInfo
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import androidx.work.Constraints
import androidx.work.CoroutineWorker
@ -70,8 +72,11 @@ class EHentaiUpdateWorker(private val context: Context, workerParams: WorkerPara
override suspend fun doWork(): Result {
return try {
if (requiresWifiConnection(preferences) && !context.isConnectedToWifi()) {
Result.success() // retry again later
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P &&
requiresWifiConnection(preferences) &&
!context.isConnectedToWifi()
) {
Result.retry() // retry again later
} else {
setForegroundSafely()
startUpdating()
@ -79,7 +84,7 @@ class EHentaiUpdateWorker(private val context: Context, workerParams: WorkerPara
Result.success()
}
} catch (e: Exception) {
Result.success() // retry again later
Result.retry() // retry again later
} finally {
updateNotifier.cancelProgressNotification()
}
@ -285,8 +290,14 @@ class EHentaiUpdateWorker(private val context: Context, workerParams: WorkerPara
val restrictions = prefRestrictions ?: preferences.exhAutoUpdateRequirements().get()
val acRestriction = DEVICE_CHARGING in restrictions
val networkRequestBuilder = NetworkRequest.Builder()
if (DEVICE_ONLY_ON_WIFI in restrictions) {
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
}
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
// 'networkRequest' only applies to Android 9+, otherwise 'networkType' is used
.setRequiredNetworkRequest(networkRequestBuilder.build(), NetworkType.CONNECTED)
.setRequiresCharging(acRestriction)
.build()
@ -312,7 +323,7 @@ class EHentaiUpdateWorker(private val context: Context, workerParams: WorkerPara
}
}
fun requiresWifiConnection(preferences: UnsortedPreferences): Boolean {
private fun requiresWifiConnection(preferences: UnsortedPreferences): Boolean {
val restrictions = preferences.exhAutoUpdateRequirements().get()
return DEVICE_ONLY_ON_WIFI in restrictions
}