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 @@ -483,7 +483,6 @@ internal class BackgroundJobManagerImpl(
.addContentUriTrigger(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true)
.addContentUriTrigger(MediaStore.Video.Media.INTERNAL_CONTENT_URI, true)
.addContentUriTrigger(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true)
.addContentUriTrigger(MediaStore.Files.getContentUri("external"), true)
.setTriggerContentUpdateDelay(Duration.ofSeconds(5))
.setTriggerContentMaxDelay(Duration.ofSeconds(10))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.nextcloud.client.jobs.autoUpload

import android.app.Notification
import android.content.Context
import android.os.BatteryManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
Expand Down Expand Up @@ -199,8 +200,12 @@ class AutoUploadWorker(
return false
}

private fun isWaitingForCharging(): Boolean =
syncedFolder.isChargingOnly && !powerManagementService.battery.isCharging
/**
* Uses the same signal as WorkManager's charging constraint. A different signal lets JobScheduler start the
* work while WorkManager stops it again, and the two reschedule each other in a loop.
*/
private fun isWaitingForCharging(): Boolean = syncedFolder.isChargingOnly &&
context.getSystemService(BatteryManager::class.java)?.isCharging != true

private suspend fun hasNothingToDo(): Boolean {
if (repository.hasPendingFiles(syncedFolder)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import android.provider.MediaStore
import com.nextcloud.client.database.dao.FileSystemDao
import com.nextcloud.client.database.entity.FilesystemEntity
import com.nextcloud.client.database.entity.UploadEntity
import com.nextcloud.utils.extensions.isNonRetryable
import com.nextcloud.utils.extensions.shouldSkipFile
import com.owncloud.android.datamodel.AutoUploadFolder
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus
import com.owncloud.android.db.OCUpload
import com.owncloud.android.db.UploadResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.utils.AutoUploadFolderUtils
import java.io.File
Expand Down Expand Up @@ -246,16 +248,24 @@ class FileSystemRepository(
}
}

private fun wasUploadFinished(
internal fun wasUploadFinished(
entity: FilesystemEntity,
syncedFolder: AutoUploadFolder,
localPath: String,
file: File
): Boolean {
val upload = getUpload(entity, syncedFolder, localPath, file) ?: return true

return upload.status == UploadStatus.UPLOAD_SUCCEEDED.value ||
if (upload.status == UploadStatus.UPLOAD_SUCCEEDED.value ||
upload.status == UploadStatus.UPLOAD_CANCELLED.value
) {
return true
}

// A non-retryable result cannot succeed by scanning again. Keeping the file handled stops a conflict from
// being re-queued on every scan; resolving it in the upload list starts a new upload through FileUploadWorker.
val lastResult = upload.lastResult?.let { UploadResult.fromValue(it) }
return lastResult?.isNonRetryable() == true
}

private fun deleteUnfinishedUpload(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.client.jobs.autoUpload

import android.content.Context
import com.nextcloud.client.database.dao.FileSystemDao
import com.nextcloud.client.database.dao.UploadDao
import com.nextcloud.client.database.entity.FilesystemEntity
import com.nextcloud.client.database.entity.UploadEntity
import com.nextcloud.client.preferences.SubFolderRule
import com.owncloud.android.datamodel.AutoUploadFolder
import com.owncloud.android.datamodel.MediaFolderType
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus
import com.owncloud.android.db.UploadResult
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

class FileSystemRepositoryTest {

private val uploadDao: UploadDao = mockk()
private val uploadsStorageManager: UploadsStorageManager = mockk()
private val repository = FileSystemRepository(
mockk<FileSystemDao>(relaxed = true),
uploadsStorageManager,
mockk<Context>(relaxed = true)
)

private val file = File(LOCAL_PATH)
private val folder = AutoUploadFolder(
id = 1L,
localPath = "/tmp",
remotePath = "/Photos",
isWifiOnly = false,
isChargingOnly = false,
alsoUploadExistingFiles = true,
isSubfolderByDate = false,
account = ACCOUNT,
uploadAction = 0,
nameCollisionPolicyInt = 0,
enabled = true,
timestampMs = 0L,
type = MediaFolderType.IMAGE,
isHidden = false,
subfolderRule = SubFolderRule.YEAR_MONTH,
isExcludeHidden = false,
lastScanTimestampMs = 0L
)
private val entity = FilesystemEntity(
id = 1,
localPath = LOCAL_PATH,
remotePath = REMOTE_PATH,
fileIsFolder = 0,
fileFoundRecently = 0L,
fileSentForUpload = 1,
syncedFolderId = "1",
crc32 = null,
fileModified = 0L
)

@Test
fun succeeded_upload_is_finished() {
stubUpload(UploadStatus.UPLOAD_SUCCEEDED, UploadResult.UPLOADED)

assertTrue(repository.wasUploadFinished(entity, folder, LOCAL_PATH, file))
}

@Test
fun missing_upload_is_finished() {
every { uploadsStorageManager.uploadDao } returns uploadDao
every { uploadDao.getUploadByAccountAndPaths(ACCOUNT, LOCAL_PATH, REMOTE_PATH) } returns null

assertTrue(repository.wasUploadFinished(entity, folder, LOCAL_PATH, file))
}

@Test
fun failed_upload_with_retryable_result_is_not_finished() {
stubUpload(UploadStatus.UPLOAD_FAILED, UploadResult.NETWORK_CONNECTION)

assertFalse(repository.wasUploadFinished(entity, folder, LOCAL_PATH, file))
}

@Test
fun failed_upload_with_conflict_is_finished() {
stubUpload(UploadStatus.UPLOAD_FAILED, UploadResult.SYNC_CONFLICT)

assertTrue(repository.wasUploadFinished(entity, folder, LOCAL_PATH, file))
}

private fun stubUpload(status: UploadStatus, lastResult: UploadResult) {
every { uploadsStorageManager.uploadDao } returns uploadDao
every {
uploadDao.getUploadByAccountAndPaths(ACCOUNT, LOCAL_PATH, REMOTE_PATH)
} returns UploadEntity(
id = 1,
localPath = LOCAL_PATH,
remotePath = REMOTE_PATH,
accountName = ACCOUNT,
fileSize = 1L,
status = status.value,
localBehaviour = 0,
uploadTime = 0,
nameCollisionPolicy = 0,
isCreateRemoteFolder = 0,
uploadEndTimestamp = 0,
uploadEndTimestampLong = 0L,
lastResult = lastResult.value,
isWhileChargingOnly = 0,
isWifiOnly = 0,
createdBy = 0,
folderUnlockToken = null
)
}

companion object {
private const val ACCOUNT = "account"
private const val LOCAL_PATH = "/tmp/photo.jpg"
private const val REMOTE_PATH = "/Photos/photo.jpg"
}
}