From 733e2eaf0011a5593f8a624269e68e1c712adb3e Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Fri, 20 Nov 2026 12:00:04 +0100 Subject: [PATCH] add logs for detect issues Signed-off-by: alperozturk96 --- .../client/jobs/ContentObserverWork.kt | 6 +++ .../jobs/autoUpload/AutoUploadWorker.kt | 50 +++++++++++++++++++ .../jobs/autoUpload/FileSystemRepository.kt | 30 ++++++++++- .../client/jobs/upload/FileUploadHelper.kt | 5 +- .../client/jobs/upload/FileUploadWorker.kt | 5 ++ .../client/jobs/upload/UploadDiagnostics.kt | 31 ++++++++++++ .../utils/UploadErrorNotificationManager.kt | 2 + .../client/network/ConnectivityServiceImpl.kt | 4 ++ .../operations/UploadFileOperation.java | 7 ++- .../owncloud/android/utils/ReceiversHelper.kt | 5 +- 10 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/client/jobs/upload/UploadDiagnostics.kt diff --git a/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt b/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt index 8d6f52e0e0a7..8050fe9ba60a 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt @@ -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 @@ -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()) { @@ -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 } diff --git a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt index 004ed470144b..322c211e4d96 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt @@ -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 @@ -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 @@ -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, @@ -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) @@ -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() } @@ -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() } @@ -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}") @@ -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) @@ -294,6 +329,7 @@ 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 } @@ -301,6 +337,7 @@ class AutoUploadWorker( 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 } @@ -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( @@ -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") @@ -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 } diff --git a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt index eb79de7687a1..9e7eedda2bec 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt @@ -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 @@ -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 @@ -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") @@ -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) } } @@ -213,6 +220,7 @@ 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 @@ -220,6 +228,12 @@ class FileSystemRepository( } 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 } @@ -237,6 +251,10 @@ 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) { @@ -244,6 +262,16 @@ class FileSystemRepository( } } + 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, diff --git a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt index 70b691f0bce9..f34565bc3b84 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt @@ -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 } /** diff --git a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt index 3d7872b7f4cb..5feb2b9ea84d 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt @@ -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) @@ -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") } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/upload/UploadDiagnostics.kt b/app/src/main/java/com/nextcloud/client/jobs/upload/UploadDiagnostics.kt new file mode 100644 index 000000000000..087ab3a4181e --- /dev/null +++ b/app/src/main/java/com/nextcloud/client/jobs/upload/UploadDiagnostics.kt @@ -0,0 +1,31 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2026 Alper Ozturk + * 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" + } +} diff --git a/app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt index c6622a537b67..f1350f76a72b 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt @@ -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 @@ -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") diff --git a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt index a272a030aa0a..77bf177b1420 100644 --- a/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt +++ b/app/src/main/java/com/nextcloud/client/network/ConnectivityServiceImpl.kt @@ -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 @@ -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 } @@ -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) @@ -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) { diff --git a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java index 4270b3e4bdb2..19fbca17dca9 100644 --- a/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java @@ -22,6 +22,7 @@ import com.nextcloud.client.device.PowerManagementService; import com.nextcloud.client.jobs.upload.FileUploadHelper; import com.nextcloud.client.jobs.upload.FileUploadWorker; +import com.nextcloud.client.jobs.upload.UploadDiagnostics; import com.nextcloud.client.network.Connectivity; import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.utils.AppConstants; @@ -1021,7 +1022,11 @@ private RemoteOperationResult checkConditions(File originalFile) { } // check that internet is not behind walled garden - if (!connectivityService.getConnectivity().isConnected() || connectivityService.isInternetWalled()) { + final Connectivity currentConnectivity = connectivityService.getConnectivity(); + final boolean isOffline = !currentConnectivity.isConnected() || connectivityService.isInternetWalled(); + UploadDiagnostics.log("upload conditions: " + mOriginalStoragePath + ", offline: " + isOffline + + ", connectivity: " + currentConnectivity); + if (isOffline) { remoteOperationResult = new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION); } diff --git a/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.kt b/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.kt index 6ea6d5e1ecd4..4e745433ad0f 100644 --- a/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.kt +++ b/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.kt @@ -16,6 +16,7 @@ import android.os.PowerManager import com.nextcloud.client.account.UserAccountManager import com.nextcloud.client.device.PowerManagementService import com.nextcloud.client.di.ApplicationScope +import com.nextcloud.client.jobs.upload.UploadDiagnostics import com.nextcloud.client.network.ConnectivityKey import com.nextcloud.client.network.ConnectivityService import com.nextcloud.client.network.WalledCheckCache @@ -45,7 +46,8 @@ class ReceiversHelper @Inject constructor( } private fun registerNetworkChangeReceiver() { - registerReceiver(CONNECTIVITY_CHANGE_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) { + registerReceiver(CONNECTIVITY_CHANGE_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION) { intent -> + UploadDiagnostics.log("${intent.action}, connectivity: ${connectivityService.connectivity}") appScope.launch { clearNetworkCaches() } if (!connectivityService.connectivity.isConnected) return@registerReceiver @@ -58,6 +60,7 @@ class ReceiversHelper @Inject constructor( registerReceiver(Intent.ACTION_POWER_CONNECTED, Intent.ACTION_POWER_DISCONNECTED) { intent -> if (intent.action != Intent.ACTION_POWER_CONNECTED) return@registerReceiver + UploadDiagnostics.log("power connected, connectivity: ${connectivityService.connectivity}") restartUploadsIfNeeded() } }