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 @@ -12,6 +12,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.autoUpload.AutoUploadHelper
import com.nextcloud.client.jobs.upload.UploadDiagnostics
import com.owncloud.android.datamodel.SyncedFolderProvider
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.utils.FilesSyncHelper
Expand Down Expand Up @@ -41,6 +42,10 @@ class ContentObserverWork(
val workerName = BackgroundJobManagerImpl.formatClassTag(this@ContentObserverWork::class)
backgroundJobManager.logStartOfWorker(workerName)
Log_OC.d(TAG, "started")
UploadDiagnostics.log(
"content change, uris: ${params.triggeredContentUris}, " +
"authorities: ${params.triggeredContentAuthorities}"
)

try {
if (params.triggeredContentUris.isNotEmpty()) {
Expand Down Expand Up @@ -73,6 +78,7 @@ class ContentObserverWork(
private suspend fun checkAndTriggerAutoUpload() = withContext(Dispatchers.IO) {
if (powerManagementService.blocksAutoUpload) {
Log_OC.w(TAG, "⚡ Power saving mode active — skipping file sync.")
UploadDiagnostics.log("content change ignored, power saving blocks auto upload")
return@withContext
}

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.Build
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
Expand All @@ -22,6 +23,7 @@ import com.nextcloud.client.jobs.upload.FileUploadEventBroadcaster
import com.nextcloud.client.jobs.upload.FileUploadHelper
import com.nextcloud.client.jobs.upload.FileUploadWorker
import com.nextcloud.client.jobs.upload.UploadDelayPolicy
import com.nextcloud.client.jobs.upload.UploadDiagnostics
import com.nextcloud.client.jobs.utils.UploadErrorNotificationManager
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.preferences.AppPreferences
Expand Down Expand Up @@ -56,6 +58,7 @@ import kotlinx.coroutines.withContext
import java.io.File
import kotlin.coroutines.cancellation.CancellationException
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeSource

class AutoUploadWorker(
private val context: Context,
Expand Down Expand Up @@ -85,6 +88,7 @@ class AutoUploadWorker(
private val notificationManager = AutoUploadNotificationManager(context, viewThemeUtils, NOTIFICATION_ID)
private val fileUploadHelper = FileUploadHelper.instance()
private val retryPolicy = UploadDelayPolicy()
private val startMark = TimeSource.Monotonic.markNow()

private val overridePowerSaving: Boolean
get() = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
Expand All @@ -96,8 +100,10 @@ class AutoUploadWorker(
?.takeIf { it.isEnabled } ?: return Result.failure()

Log_OC.d(TAG, syncedFolder.getLog())
logStartState()

if (isBlocked(syncFolderId)) {
UploadDiagnostics.log("auto upload blocked, folderId: $syncFolderId")
return Result.success()
}

Expand All @@ -110,6 +116,7 @@ class AutoUploadWorker(
autoUploadHelper.insertEntries(syncedFolder)

if (hasNothingToDo()) {
UploadDiagnostics.log("auto upload skipped, no pending files and scan interval not reached")
return Result.success()
}

Expand All @@ -119,10 +126,17 @@ class AutoUploadWorker(
syncedFolder.lastScanTimestampMs = System.currentTimeMillis()
syncedFolderProvider.updateSyncFolder(syncedFolder)

UploadDiagnostics.log(
"auto upload finished, folderId: ${syncedFolder.id}, runtime: ${startMark.elapsedNow()}, " +
"pending files left: ${repository.hasPendingFiles(syncedFolder)}"
)
Log_OC.d(TAG, "✅ ${syncedFolder.remotePath} completed")
Result.success()
} catch (e: CancellationException) {
Log_OC.w(TAG, "⚠️ Job cancelled")
UploadDiagnostics.log(
"auto upload stopped, runtime: ${startMark.elapsedNow()}, stopReason: ${getStopReasonOrNull()}"
)
throw e
} catch (e: Exception) {
Log_OC.e(TAG, "❌ failed: ${e.message}")
Expand All @@ -146,9 +160,30 @@ class AutoUploadWorker(
setForeground(notificationManager.getForegroundInfo(createNotification(title)))
} catch (e: Exception) {
Log_OC.w(TAG, "⚠️ Could not set foreground service: ${e.message}")
UploadDiagnostics.log("auto upload foreground service not granted, system may stop the worker: $e")
}
}

private fun logStartState() {
UploadDiagnostics.log(
"auto upload started, folderId: ${syncedFolder.id}, type: ${syncedFolder.type}, " +
"overridePowerSaving: $overridePowerSaving, " +
"blocksAutoUpload: ${powerManagementService.blocksAutoUpload}, " +
"paused: ${preferences.isGlobalUploadPaused}, chargingOnly: ${syncedFolder.isChargingOnly}, " +
"charging: ${powerManagementService.battery.isCharging}, wifiOnly: ${syncedFolder.isWifiOnly}, " +
"collisionPolicy: ${syncedFolder.nameCollisionPolicy}, " +
"uploadExisting: ${syncedFolder.alsoUploadExistingFiles()}, " +
"lastScan: ${syncedFolder.lastScanTimestampMs}, connectivity: ${connectivityService.connectivity}, " +
"workerNetwork: ${UploadDiagnostics.describeNetwork(context, network)}"
)
}

private fun getStopReasonOrNull(): Int? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
stopReason
} else {
null
}

private fun createNotification(title: String): Notification = notificationManager.notificationBuilder
.setContentTitle(title)
.setSmallIcon(R.drawable.uploads)
Expand Down Expand Up @@ -294,13 +329,15 @@ class AutoUploadWorker(
// A camera app can register the file with MediaStore before it finished writing it.
if (file.isFile && file.length() == 0L) {
Log_OC.w(TAG, "skipping empty file, still being written: $localPath")
UploadDiagnostics.log("skipped empty file: $localPath")
return@forEachIndexed
}

val entityResult = getEntityResult(user, localPath, remotePath, capability)
if (entityResult !is AutoUploadEntityResult.Success) {
repository.markFileAsHandled(localPath, syncedFolder)
Log_OC.d(TAG, "marked file as handled: $localPath")
UploadDiagnostics.log("marked as handled without upload: $localPath, reason: $entityResult")
return@forEachIndexed
}

Expand All @@ -324,12 +361,23 @@ class AutoUploadWorker(
val operation = createUploadFileOperation(upload, user)
Log_OC.d(TAG, "🕒 uploading: $localPath, id: $generatedId")

val sizeBefore = file.length()
val modifiedBefore = file.lastModified()
UploadDiagnostics.log(
"upload start: $localPath, size: $sizeBefore, modified: $modifiedBefore"
)

FileUploadWorker.registerActiveUpload(operation)
val result = try {
operation.execute(client)
} finally {
FileUploadWorker.unregisterActiveUpload(operation.ocUploadId)
}

UploadDiagnostics.log(
"upload end: $localPath, result: ${result.code}, size: $sizeBefore -> ${file.length()}, " +
"modified: $modifiedBefore -> ${file.lastModified()}"
)
fileUploadEventBroadcaster.sendUploadStarted(operation, context)

UploadErrorNotificationManager.handleResult(
Expand Down Expand Up @@ -383,6 +431,7 @@ class AutoUploadWorker(
"Exception during upload file, localPath: $localPath, remotePath: $remotePath," +
" exception: $e"
)
UploadDiagnostics.log("upload exception: $localPath, $e")

if (path.isEmpty() || !file.exists()) {
Log_OC.w(TAG, "detected non-existing local file, removing entity")
Expand Down Expand Up @@ -440,6 +489,7 @@ class AutoUploadWorker(
TAG,
"last upload failed with ${lastUploadResult.value}, skipping auto-upload: $localPath"
)
UploadDiagnostics.log("non retryable last result: $lastUploadResult, $localPath")
return AutoUploadEntityResult.NonRetryable
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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.client.jobs.upload.UploadDiagnostics
import com.nextcloud.utils.extensions.shouldSkipFile
import com.owncloud.android.datamodel.SyncedFolder
import com.owncloud.android.datamodel.UploadsStorageManager
Expand Down Expand Up @@ -116,7 +117,8 @@ class FileSystemRepository(
val projection = arrayOf(
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.DATE_MODIFIED,
MediaStore.MediaColumns.DATE_ADDED
MediaStore.MediaColumns.DATE_ADDED,
MediaStore.MediaColumns.SIZE
)

var syncedPath = syncedFolder.localPath
Expand Down Expand Up @@ -150,6 +152,7 @@ class FileSystemRepository(
val idxData = cursor.getColumnIndex(MediaStore.MediaColumns.DATA)
val idxModified = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED)
val idxAdded = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_ADDED)
val idxSize = cursor.getColumnIndex(MediaStore.MediaColumns.SIZE)

if (idxData == -1) {
Log_OC.e(TAG, "MediaStore column DATA missing — cannot process URI: $uri")
Expand All @@ -176,6 +179,10 @@ class FileSystemRepository(
"Found file: $filePath (created=$creationTimeMs, modified=$lastModifiedMs)"
)

if (idxSize != -1) {
logSizeMismatch(filePath, cursor.getLong(idxSize))
}

insertOrReplace(filePath, lastModifiedMs, creationTimeMs, syncedFolder, checkFileType)
}
}
Expand Down Expand Up @@ -213,13 +220,20 @@ class FileSystemRepository(
}

Log_OC.w(TAG, "marked as sent but upload never finished, queueing again: $localPath")
UploadDiagnostics.log("upload never finished, queueing again: $localPath")
deleteUnfinishedUpload(entity, syncedFolder, localPath, file)

// the upload never finished, so the checks below must not skip it as an already handled old file
fileSentForUpload = false
}

if (syncedFolder.shouldSkipFile(file, fileModified, creationTime, fileSentForUpload)) {
if (entity != null && !hasNotChanged && fileSentForUpload) {
UploadDiagnostics.log(
"changed after it was handled but skipped: $localPath, modified: ${entity.fileModified} " +
"-> $fileModified, size: ${file.length()}, lastScan: ${syncedFolder.lastScanTimestampMs}"
)
}
return
}

Expand All @@ -237,13 +251,27 @@ class FileSystemRepository(
)

Log_OC.d(TAG, "inserting new file system entity: $newEntity")
UploadDiagnostics.log(
"queued: $localPath, size: ${file.length()}, modified: $fileModified, " +
"previouslyHandled: $fileSentForUpload, folderId: ${syncedFolder.id}"
)

dao.insertOrReplace(newEntity)
} catch (e: Exception) {
Log_OC.e(TAG, "Failed to insert/update file: $localPath", e)
}
}

private fun logSizeMismatch(filePath: String?, mediaStoreSize: Long) {
val file = filePath?.let { File(it) }?.takeIf { it.exists() } ?: return
val fileSize = file.length()
if (fileSize == mediaStoreSize) {
return
}

UploadDiagnostics.log("MediaStore size $mediaStoreSize differs from file size $fileSize: $filePath")
}

private fun wasUploadFinished(
entity: FilesystemEntity,
syncedFolder: SyncedFolder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ class FileUploadHelper {

private fun checkConnectivity(connectivityService: ConnectivityService): Boolean {
// check that connection isn't walled off and that the server is reachable
return connectivityService.connectivity.isConnected && !connectivityService.isInternetWalled()
val isOnline = connectivityService.connectivity.isConnected && !connectivityService.isInternetWalled()
UploadDiagnostics.log("retry uploads, online: $isOnline, connectivity: ${connectivityService.connectivity}")

return isOnline
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ class FileUploadWorker(
trySetForeground()

Log_OC.d(TAG, "FileUploadWorker started")
UploadDiagnostics.log(
"file upload worker started, connectivity: ${connectivityService.connectivity}, " +
"workerNetwork: ${UploadDiagnostics.describeNetwork(context, network)}"
)
val workerName = BackgroundJobManagerImpl.formatClassTag(this::class)
backgroundJobManager.logStartOfWorker(workerName)

Expand Down Expand Up @@ -193,6 +197,7 @@ class FileUploadWorker(
} catch (e: Exception) {
// Continue without foreground service - uploads will still work
Log_OC.w(TAG, "Could not set foreground service: ${e.message}")
UploadDiagnostics.log("file upload foreground service not granted, system may stop the worker: $e")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.client.jobs.upload

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import com.owncloud.android.lib.common.utils.Log_OC

object UploadDiagnostics {
const val TAG = "UploadDiagnostics"

@JvmStatic
fun log(message: String) {
Log_OC.d(TAG, message)
}

fun describeNetwork(context: Context, network: Network?): String {
if (network == null) {
return "none"
}

val capabilities = context.getSystemService(ConnectivityManager::class.java)?.getNetworkCapabilities(network)

return "$network $capabilities"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.app.NotificationCompat
import com.nextcloud.client.jobs.notification.WorkerNotificationManager
import com.nextcloud.client.jobs.upload.FileUploadHelper
import com.nextcloud.client.jobs.upload.UploadBroadcastAction
import com.nextcloud.client.jobs.upload.UploadDiagnostics
import com.nextcloud.client.notifications.AppWideNotificationManager
import com.nextcloud.utils.extensions.isConflict
import com.nextcloud.utils.extensions.isFileSpecificError
Expand Down Expand Up @@ -55,6 +56,7 @@ object UploadErrorNotificationManager {
onLocked: () -> Unit = {}
) {
Log_OC.d(TAG, "handle upload result with result code: " + result.code)
UploadDiagnostics.log("upload result: ${operation.storagePath}, code: ${result.code}")

if (result.isSuccess || operation.isMissingPermissionThrown) {
Log_OC.d(TAG, "operation is successful, cancelled or lack of storage permission, notification skipped")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.os.SystemClock
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.jobs.upload.UploadDiagnostics
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.common.utils.Log_OC
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -134,6 +135,7 @@ class ConnectivityServiceImpl(
walledCheckCache.setValue(currentKey, isWalled)

Log_OC.d(TAG, "server check, isWalled: $isWalled")
UploadDiagnostics.log("server check, isWalled: $isWalled, capabilities: $resolvedCapabilities")
return isWalled
}

Expand Down Expand Up @@ -173,6 +175,7 @@ class ConnectivityServiceImpl(

if (previous != newConnectivity) {
Log_OC.d(TAG, "network capability changed - notifying listeners")
UploadDiagnostics.log("connectivity changed: $previous -> $newConnectivity, capabilities: $capabilities")
currentConnectivity = newConnectivity
walledCheckCache.putConnectivityValue(currentKey, newConnectivity)

Expand Down Expand Up @@ -236,6 +239,7 @@ class ConnectivityServiceImpl(
return connectivityManager.allNetworks
.mapNotNull { connectivityManager.getNetworkCapabilities(it) }
.firstOrNull { SupportedNetworkTransports.isSupportedTransport(it) }
.also { UploadDiagnostics.log("no active network, fallback capabilities: $it") }
}

private fun getWalledValueFromException(e: Exception): Boolean = when (e) {
Expand Down
Loading
Loading