Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ internal class BackgroundJobManagerImpl(
.putLong(AutoUploadWorker.SYNCED_FOLDER_ID, syncedFolderID)
.build()

val constraints = SupportedNetworkTransports.getConstraints(requiresCharging = syncedFolder.isChargingOnly)
val constraints = SupportedNetworkTransports.getConstraints()

val requestBuilder = oneTimeRequestBuilder(
jobClass = AutoUploadWorker::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.cancellation.CancellationException
import kotlin.time.Duration.Companion.milliseconds

Expand All @@ -77,6 +78,8 @@ class AutoUploadWorker(
const val OVERRIDE_POWER_SAVING = "overridePowerSaving"
const val SYNCED_FOLDER_ID = "syncedFolderId"
const val NOTIFICATION_ID = 266
private const val MIN_EXECUTION_INTERVAL_MS = 10_000L
private val lastExecutionTimestampByFolder = ConcurrentHashMap<Long, Long>()
}

private val syncFolderHelper = SyncFolderHelper(context)
Expand All @@ -90,10 +93,21 @@ class AutoUploadWorker(
get() = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)

override suspend fun doWork(): Result {
val syncFolderId = inputData.getLong(SYNCED_FOLDER_ID, -1)
val now = System.currentTimeMillis()
val lastExecution = lastExecutionTimestampByFolder[syncFolderId] ?: 0L

if (now - lastExecution < MIN_EXECUTION_INTERVAL_MS && !overridePowerSaving) {
Log_OC.w(
TAG,
"Throttling: AutoUploadWorker executed ${now - lastExecution}ms ago for folder $syncFolderId. Postponing."
)
return Result.success()
}
lastExecutionTimestampByFolder[syncFolderId] = now

return try {
trySetForeground()

val syncFolderId = inputData.getLong(SYNCED_FOLDER_ID, -1)
syncedFolder = syncedFolderProvider.getSyncedFolderByID(syncFolderId)
?.takeIf { it.isEnabled } ?: return Result.failure()

Expand All @@ -115,6 +129,7 @@ class AutoUploadWorker(
return Result.success()
}

trySetForeground()
uploadFiles(syncedFolder)

// only update last scan time after uploading files
Expand Down Expand Up @@ -208,6 +223,11 @@ class AutoUploadWorker(
return true
}

if (syncedFolder.isChargingOnly && !overridePowerSaving && !powerManagementService.battery.isCharging) {
Log_OC.d(TAG, "⚠️ Skipping: charging required, but device is not connected to power")
return true
}

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object SupportedNetworkTransports {
}
}

fun getConstraints(requiresCharging: Boolean = false): Constraints {
fun getConstraints(): Constraints {
val networkRequest = NetworkRequest.Builder()
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
Expand All @@ -38,7 +38,6 @@ object SupportedNetworkTransports {

return Constraints.Builder()
.setRequiredNetworkRequest(networkRequest, NetworkType.CONNECTED)
.setRequiresCharging(requiresCharging)
.build()
}

Expand Down