Avoid Magic numbers

This commit is contained in:
Benoit Marty 2021-01-12 12:06:52 +01:00 committed by Benoit Marty
parent aac3f379a7
commit 609ceb7fa4
2 changed files with 17 additions and 6 deletions

View file

@ -50,8 +50,9 @@ abstract class SyncService : Service() {
private var sessionId: String? = null
private var mIsSelfDestroyed: Boolean = false
private var syncTimeoutSeconds: Int = 6
private var syncDelaySeconds: Int = 60
private var syncTimeoutSeconds: Int = getDefaultSyncTimeoutSeconds()
private var syncDelaySeconds: Int = getDefaultSyncDelaySeconds()
private var periodic: Boolean = false
private var preventReschedule: Boolean = false
@ -190,8 +191,8 @@ abstract class SyncService : Service() {
}
val matrix = Matrix.getInstance(applicationContext)
val safeSessionId = intent.getStringExtra(EXTRA_SESSION_ID) ?: return false
syncTimeoutSeconds = intent.getIntExtra(EXTRA_TIMEOUT_SECONDS, 6)
syncDelaySeconds = intent.getIntExtra(EXTRA_DELAY_SECONDS, 60)
syncTimeoutSeconds = intent.getIntExtra(EXTRA_TIMEOUT_SECONDS, getDefaultSyncTimeoutSeconds())
syncDelaySeconds = intent.getIntExtra(EXTRA_DELAY_SECONDS, getDefaultSyncDelaySeconds())
try {
val sessionComponent = matrix.sessionManager.getSessionComponent(safeSessionId)
?: throw IllegalStateException("## Sync: You should have a session to make it work")
@ -210,6 +211,10 @@ abstract class SyncService : Service() {
}
}
abstract fun getDefaultSyncTimeoutSeconds(): Int
abstract fun getDefaultSyncDelaySeconds(): Int
abstract fun onStart(isInitialSync: Boolean)
abstract fun onRescheduleAsked(sessionId: String, isInitialSync: Boolean, timeout: Int, delay: Int)

View file

@ -33,6 +33,7 @@ import androidx.work.WorkerParameters
import im.vector.app.R
import im.vector.app.core.extensions.vectorComponent
import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.settings.BackgroundSyncMode
import org.matrix.android.sdk.internal.session.sync.job.SyncService
import timber.log.Timber
@ -78,6 +79,10 @@ class VectorSyncService : SyncService() {
notificationUtils = vectorComponent().notificationUtils()
}
override fun getDefaultSyncDelaySeconds() = BackgroundSyncMode.DEFAULT_SYNC_DELAY_SECONDS
override fun getDefaultSyncTimeoutSeconds() = BackgroundSyncMode.DEFAULT_SYNC_TIMEOUT_SECONDS
override fun onStart(isInitialSync: Boolean) {
val notificationSubtitleRes = if (isInitialSync) {
R.string.notification_initial_sync
@ -125,8 +130,8 @@ class VectorSyncService : SyncService() {
override fun doWork(): Result {
Timber.d("## Sync: RestartWhenNetworkOn.doWork()")
val sessionId = inputData.getString(KEY_SESSION_ID) ?: return Result.failure()
val timeout = inputData.getInt(KEY_TIMEOUT, 6)
val delay = inputData.getInt(KEY_DELAY, 60)
val timeout = inputData.getInt(KEY_TIMEOUT, BackgroundSyncMode.DEFAULT_SYNC_TIMEOUT_SECONDS)
val delay = inputData.getInt(KEY_DELAY, BackgroundSyncMode.DEFAULT_SYNC_DELAY_SECONDS)
applicationContext.rescheduleSyncService(sessionId, timeout, delay, true)
// Indicate whether the work finished successfully with the Result
return Result.success()
@ -152,6 +157,7 @@ private fun Context.rescheduleSyncService(sessionId: String,
timeout: Int,
delay: Int,
isNetworkBack: Boolean) {
Timber.d("## Sync: rescheduleSyncService")
val periodicIntent = VectorSyncService.newPeriodicIntent(this, sessionId, timeout, delay, isNetworkBack)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(this, 0, periodicIntent, 0)