diff --git a/app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt b/app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt index 2da0f79d4a..9f9a6b9ec9 100644 --- a/app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt +++ b/app/src/main/java/com/nextcloud/client/etm/pages/EtmBackgroundJobsFragment.kt @@ -46,9 +46,10 @@ import javax.inject.Inject class EtmBackgroundJobsFragment : EtmBaseFragment(), Injectable { @Inject - lateinit var preferences : AppPreferences + lateinit var preferences: AppPreferences - class Adapter(private val inflater: LayoutInflater, private val preferences: AppPreferences) : RecyclerView.Adapter(){ + class Adapter(private val inflater: LayoutInflater, private val preferences: AppPreferences) : + RecyclerView.Adapter() { class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val uuid = view.findViewById(R.id.etm_background_job_uuid) @@ -88,7 +89,6 @@ class EtmBackgroundJobsFragment : EtmBaseFragment(), Injectable { View.GONE } } - } private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:MM:ssZ", Locale.getDefault()) @@ -128,31 +128,33 @@ class EtmBackgroundJobsFragment : EtmBaseFragment(), Injectable { vh.progressEnabled = false } - val logs = preferences.readLogEntry() - val logsForThisWorker = logs.filter { BackgroundJobManagerImpl.parseTag(it.workerClass)?.second == info.workerClass } - if(logsForThisWorker.isNotEmpty()) { + val logs = preferences.readLogEntry() + val logsForThisWorker = + logs.filter { BackgroundJobManagerImpl.parseTag(it.workerClass)?.second == info.workerClass } + if (logsForThisWorker.isNotEmpty()) { vh.executionTimesRow.visibility = View.VISIBLE - vh.executionCount.text = logsForThisWorker.filter { it.started != null }.size.toString() + " (${logsForThisWorker.filter { it.finished != null }.size})" + vh.executionCount.text = + "${logsForThisWorker.filter { it.started != null }.size} " + + "(${logsForThisWorker.filter { it.finished != null }.size})" var logText = "Worker Logs\n\n" + - "*** Does NOT differentiate between imitate or periodic kinds of Work! ***\n"+ + "*** Does NOT differentiate between imitate or periodic kinds of Work! ***\n" + "*** Times run in 48h: Times started (Times finished) ***\n" - logsForThisWorker.forEach{ + logsForThisWorker.forEach { logText += "----------------------\n" logText += "Worker ${BackgroundJobManagerImpl.parseTag(it.workerClass)?.second}\n" - logText += if (it.started == null){ + logText += if (it.started == null) { "ENDED at\n${it.finished}\nWith result: ${it.result}\n" - }else{ + } else { "STARTED at\n${it.started}\n" } } vh.executionLog.text = logText - }else{ + } else { vh.executionLog.text = "Worker Logs\n\n" + "No Entries -> Maybe logging is not implemented for Worker or it has not run yet." vh.executionCount.text = "0" vh.executionTimesRow.visibility = View.GONE } - } } @@ -186,22 +188,27 @@ class EtmBackgroundJobsFragment : EtmBaseFragment(), Injectable { vm.cancelAllJobs() true } + R.id.etm_background_jobs_prune -> { vm.pruneJobs() true } + R.id.etm_background_jobs_start_test -> { vm.startTestJob(periodic = false) true } + R.id.etm_background_jobs_schedule_test -> { vm.startTestJob(periodic = true) true } + R.id.etm_background_jobs_cancel_test -> { vm.cancelTestJob() true } + else -> super.onOptionsItemSelected(item) } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobFactory.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobFactory.kt index f126f869e1..cc8c999dca 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobFactory.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobFactory.kt @@ -51,7 +51,7 @@ import javax.inject.Provider * * This class is doing too many things and should be split up into smaller factories. */ -@Suppress("LongParameterList") // satisfied by DI +@Suppress("LongParameterList", "TooManyFunctions") // satisfied by DI class BackgroundJobFactory @Inject constructor( private val logger: Logger, private val preferences: AppPreferences, @@ -143,7 +143,7 @@ class BackgroundJobFactory @Inject constructor( resources, arbitraryDataProvider, contentResolver, - accountManager, + accountManager ) } @@ -152,7 +152,7 @@ class BackgroundJobFactory @Inject constructor( context, params, logger, - contentResolver, + contentResolver ) } @@ -162,7 +162,7 @@ class BackgroundJobFactory @Inject constructor( params, contentResolver, accountManager, - preferences, + preferences ) } @@ -171,7 +171,7 @@ class BackgroundJobFactory @Inject constructor( context, params, logger, - contentResolver, + contentResolver ) } @@ -210,7 +210,7 @@ class BackgroundJobFactory @Inject constructor( preferences, clock, viewThemeUtils.get(), - syncedFolderProvider, + syncedFolderProvider ) } @@ -221,7 +221,7 @@ class BackgroundJobFactory @Inject constructor( notificationManager, accountManager, deckApi, - viewThemeUtils.get(), + viewThemeUtils.get() ) } @@ -249,7 +249,7 @@ class BackgroundJobFactory @Inject constructor( localBroadcastManager.get(), backgroundJobManager.get(), context, - params, + params ) } @@ -261,7 +261,7 @@ class BackgroundJobFactory @Inject constructor( notificationManager = notificationManager, userAccountManager = accountManager, logger = logger, - params = params, + params = params ) } diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index 4fa8a68b2e..750e967cce 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -36,7 +36,7 @@ interface BackgroundJobManager { */ val jobs: LiveData> - fun logStartOfWorker(workerName : String?) + fun logStartOfWorker(workerName: String?) fun logEndOfWorker(workerName: String?, result: ListenableWorker.Result) diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index 53a787928f..68bf01dcda 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -66,7 +66,6 @@ internal class BackgroundJobManagerImpl( private val preferences: AppPreferences ) : BackgroundJobManager, Injectable { - companion object { const val TAG_ALL = "*" // This tag allows us to retrieve list of all jobs run by Nextcloud client @@ -105,7 +104,7 @@ internal class BackgroundJobManagerImpl( const val DEFAULT_PERIODIC_JOB_INTERVAL_MINUTES = 15L const val DEFAULT_IMMEDIATE_JOB_DELAY_SEC = 3L - private const val KEEP_LOG_MILLIS = 1000 * 60 * 60 * 24 *3L + private const val KEEP_LOG_MILLIS = 1000 * 60 * 60 * 24 * 3L fun formatNameTag(name: String, user: User? = null): String { return if (user == null) { @@ -130,11 +129,11 @@ internal class BackgroundJobManagerImpl( } fun parseTimestamp(timestamp: String): Date { - try { + return try { val ms = timestamp.toLong() - return Date(ms) + Date(ms) } catch (ex: NumberFormatException) { - return Date(0) + Date(0) } } @@ -161,40 +160,38 @@ internal class BackgroundJobManagerImpl( } } - fun deleteOldLogs(logEntries: MutableList) : MutableList{ - + fun deleteOldLogs(logEntries: MutableList): MutableList { logEntries.removeIf { - return@removeIf (it.started != null && - Date(Date().time - KEEP_LOG_MILLIS).after(it.started)) || - (it.finished != null && - Date(Date().time - KEEP_LOG_MILLIS).after(it.finished)) - + return@removeIf ( + it.started != null && + Date(Date().time - KEEP_LOG_MILLIS).after(it.started) + ) || + ( + it.finished != null && + Date(Date().time - KEEP_LOG_MILLIS).after(it.finished) + ) } return logEntries - } - - } - override fun logStartOfWorker(workerName : String?) { + override fun logStartOfWorker(workerName: String?) { val logs = deleteOldLogs(preferences.readLogEntry().toMutableList()) if (workerName == null) { logs.add(LogEntry(Date(), null, null, NOT_SET_VALUE)) - }else{ + } else { logs.add(LogEntry(Date(), null, null, workerName)) } preferences.saveLogEntry(logs) } - override fun logEndOfWorker(workerName: String?, result: ListenableWorker.Result){ - + override fun logEndOfWorker(workerName: String?, result: ListenableWorker.Result) { val logs = deleteOldLogs(preferences.readLogEntry().toMutableList()) if (workerName == null) { - logs.add(LogEntry(null,Date(),result.toString(), NOT_SET_VALUE)) - }else{ - logs.add(LogEntry(null,Date(),result.toString(),workerName)) + logs.add(LogEntry(null, Date(), result.toString(), NOT_SET_VALUE)) + } else { + logs.add(LogEntry(null, Date(), result.toString(), workerName)) } preferences.saveLogEntry(logs) } 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 99117678a8..4e9840ccef 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/ContentObserverWork.kt @@ -50,7 +50,7 @@ class ContentObserverWork( recheduleSelf() val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result } diff --git a/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt b/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt index 1246b0a333..9d98b0404b 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt @@ -81,7 +81,7 @@ class FilesSyncWork( // If we are in power save mode, better to postpone upload if (powerManagementService.isPowerSavingEnabled && !overridePowerSaving) { val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result } val resources = context.resources @@ -113,7 +113,7 @@ class FilesSyncWork( } } val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result } diff --git a/app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt index 2563b94366..ce3516740a 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt @@ -88,7 +88,7 @@ class FilesUploadWorker( Log_OC.w(TAG, "User was null for file upload worker") val result = Result.failure() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result // user account is needed } @@ -107,7 +107,7 @@ class FilesUploadWorker( Log_OC.d(TAG, "No more pending uploads for account $accountName, stopping work") val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result // user account is needed } diff --git a/app/src/main/java/com/nextcloud/client/jobs/HealthStatusWork.kt b/app/src/main/java/com/nextcloud/client/jobs/HealthStatusWork.kt index 2512f42e70..bd59f07fe6 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/HealthStatusWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/HealthStatusWork.kt @@ -96,7 +96,7 @@ class HealthStatusWork( } val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result } diff --git a/app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt b/app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt index d6f644fa3f..95379fcbf0 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/JobInfo.kt @@ -19,8 +19,6 @@ */ package com.nextcloud.client.jobs -import androidx.work.ListenableWorker -import com.google.common.util.concurrent.ListenableFuture import java.util.Date import java.util.UUID @@ -31,11 +29,10 @@ data class JobInfo( val user: String = "", val workerClass: String = "", val started: Date = Date(0), - val progress: Int = 0, + val progress: Int = 0 ) - -data class LogEntry ( +data class LogEntry( val started: Date? = null, val finished: Date? = null, val result: String? = null, diff --git a/app/src/main/java/com/nextcloud/client/jobs/JobsModule.kt b/app/src/main/java/com/nextcloud/client/jobs/JobsModule.kt index ca7fdb7128..79f2f2386c 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/JobsModule.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/JobsModule.kt @@ -51,7 +51,11 @@ class JobsModule { @Provides @Singleton - fun backgroundJobManager(workManager: WorkManager, clock: Clock, preferences: AppPreferences): BackgroundJobManager { + fun backgroundJobManager( + workManager: WorkManager, + clock: Clock, + preferences: AppPreferences + ): BackgroundJobManager { return BackgroundJobManagerImpl(workManager, clock, preferences) } } diff --git a/app/src/main/java/com/nextcloud/client/jobs/TestJob.kt b/app/src/main/java/com/nextcloud/client/jobs/TestJob.kt index e74b652770..cfd9e996c0 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/TestJob.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/TestJob.kt @@ -48,7 +48,7 @@ class TestJob( } val result = Result.success() - backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class),result) + backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result) return result } }