Skip to content

Commit

Permalink
Debug info: show periodicity and next run of sync workers (bitfireAT/…
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Oct 24, 2023
1 parent 8263b5f commit e262ee9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import android.accounts.Account
import android.content.Context
import android.provider.CalendarContract
import androidx.hilt.work.HiltWorker
import androidx.work.*
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.Operation
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.AccountSettings
import dagger.assisted.Assisted
Expand Down Expand Up @@ -95,23 +103,6 @@ class PeriodicSyncWorker @AssistedInject constructor(
WorkManager.getInstance(context)
.cancelUniqueWork(workerName(account, authority))

/**
* Finds out whether the [PeriodicSyncWorker] is currently enqueued or running
*
* @param account account to check
* @param authority authority to check (for instance: [CalendarContract.AUTHORITY]])
* @return boolean whether the [PeriodicSyncWorker] is running or enqueued
*/
fun isEnabled(context: Context, account: Account, authority: String): Boolean =
WorkManager.getInstance(context)
.getWorkInfos(
WorkQuery.Builder
.fromTags(listOf(workerName(account, authority)))
.addStates(listOf(WorkInfo.State.ENQUEUED, WorkInfo.State.RUNNING))
.build()
).get()
.isNotEmpty()

}

override fun doWork(): Result {
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/DebugInfoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.net.Uri
import android.os.*
import android.provider.CalendarContract
import android.provider.ContactsContract
import android.text.format.DateUtils
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -624,7 +625,7 @@ class DebugInfoActivity : AppCompatActivity() {
}

private fun dumpAccount(account: Account, infos: Iterable<AccountDumpInfo>): String {
val table = TextTable("Authority", "getIsSyncable", "getSyncAutomatically", "PeriodicSyncWorker", "Interval", "Entries")
val table = TextTable("Authority", "isSyncable", "syncAutomatically", "Interval", "Entries")
for (info in infos) {
var nrEntries = ""
var client: ContentProviderClient? = null
Expand All @@ -647,7 +648,6 @@ class DebugInfoActivity : AppCompatActivity() {
info.authority,
ContentResolver.getIsSyncable(account, info.authority),
ContentResolver.getSyncAutomatically(account, info.authority), // content-triggered sync
PeriodicSyncWorker.isEnabled(context, account, info.authority), // should always be false for address book accounts
accountSettings.getSyncInterval(info.authority)?.let {"${it/60} min"},
nrEntries
)
Expand All @@ -661,7 +661,7 @@ class DebugInfoActivity : AppCompatActivity() {
* whether they exist one by one
*/
private fun dumpSyncWorkersInfo(account: Account): String {
val table = TextTable("Tags", "Authority", "State", "Retries", "Generation", "ID")
val table = TextTable("Tags", "Authority", "State", "Next run", "Retries", "Generation", "Periodicity")
listOf(
context.getString(R.string.address_books_authority),
CalendarContract.AUTHORITY,
Expand All @@ -677,12 +677,20 @@ class DebugInfoActivity : AppCompatActivity() {
WorkQuery.Builder.fromUniqueWorkNames(listOf(workerName)).build()
).get().forEach { workInfo ->
table.addLine(
workInfo.tags.map { StringUtils.removeStartIgnoreCase(it, SyncWorker::class.java.getPackage()!!.name + ".") },
workInfo.tags.map { it.replace("\\bat\\.bitfire\\.davdroid\\.".toRegex(), ".") },
authority,
"${workInfo.state} (${workInfo.stopReason})",
workInfo.nextScheduleTimeMillis.let { nextRun ->
when (nextRun) {
Long.MAX_VALUE -> ""
else -> DateUtils.getRelativeTimeSpanString(nextRun)
}
},
workInfo.runAttemptCount,
workInfo.generation,
workInfo.id
workInfo.periodicityInfo?.let { periodicity ->
"every ${periodicity.repeatIntervalMillis/60000} min"
} ?: "not periodic"
)
}
}
Expand Down

0 comments on commit e262ee9

Please sign in to comment.