Skip to content
Open
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 @@ -380,7 +380,7 @@ class UploadFileFromContentUriWorker(
}

if (totalSize <= 0) return
val percent: Int = (100.0 * offset.toDouble() / totalSize.toDouble()).toInt()
val percent: Int = UploadProgressCalculator.percent(offset, totalSize)
if (percent == lastPercent) return

CoroutineScope(Dispatchers.IO).launch {
Expand Down Expand Up @@ -479,7 +479,7 @@ class UploadFileFromContentUriWorker(
}
}

val percent: Int = (100.0 * totalTransferredSoFar.toDouble() / totalToTransfer.toDouble()).toInt()
val percent: Int = UploadProgressCalculator.percent(totalTransferredSoFar, totalToTransfer)
if (percent == lastPercent) return

// Set current progress. Observers will listen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class UploadFileFromFileSystemWorker(
}

if (totalSize <= 0) return
val percent: Int = (100.0 * offset.toDouble() / totalSize.toDouble()).toInt()
val percent: Int = UploadProgressCalculator.percent(offset, totalSize)
if (percent == lastPercent) return

CoroutineScope(Dispatchers.IO).launch {
Expand Down Expand Up @@ -480,7 +480,7 @@ class UploadFileFromFileSystemWorker(
}
}

val percent: Int = (100.0 * totalTransferredSoFar.toDouble() / totalToTransfer.toDouble()).toInt()
val percent: Int = UploadProgressCalculator.percent(totalTransferredSoFar, totalToTransfer)
if (percent == lastPercent) return

// Set current progress. Observers will listen.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* openCloud Android client application
*
* Copyright (C) 2026 OpenCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.opencloud.android.workers

internal object UploadProgressCalculator {
fun percent(transferred: Long, total: Long): Int =
if (total <= 0L || transferred <= 0L) {
0
} else {
(100.0 * transferred.toDouble() / total.toDouble()).toInt().coerceIn(0, 100)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* openCloud Android client application
*
* Copyright (C) 2026 OpenCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.opencloud.android.workers

import org.junit.Assert.assertEquals
import org.junit.Test

class UploadProgressCalculatorTest {
@Test
fun `percent returns zero when total is unknown or zero`() {
assertEquals(0, UploadProgressCalculator.percent(transferred = 25, total = -1))
assertEquals(0, UploadProgressCalculator.percent(transferred = 25, total = 0))
}

@Test
fun `percent returns zero when transferred is zero or negative`() {
assertEquals(0, UploadProgressCalculator.percent(transferred = 0, total = 100))
assertEquals(0, UploadProgressCalculator.percent(transferred = -1, total = 100))
}

@Test
fun `percent calculates normal progress`() {
assertEquals(25, UploadProgressCalculator.percent(transferred = 25, total = 100))
}

@Test
fun `percent clamps over complete progress`() {
assertEquals(100, UploadProgressCalculator.percent(transferred = 125, total = 100))
}
}
Loading