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 sessionId: String? = null
private var mIsSelfDestroyed: Boolean = false private var mIsSelfDestroyed: Boolean = false
private var syncTimeoutSeconds: Int = 6 private var syncTimeoutSeconds: Int = getDefaultSyncTimeoutSeconds()
private var syncDelaySeconds: Int = 60 private var syncDelaySeconds: Int = getDefaultSyncDelaySeconds()
private var periodic: Boolean = false private var periodic: Boolean = false
private var preventReschedule: Boolean = false private var preventReschedule: Boolean = false
@ -190,8 +191,8 @@ abstract class SyncService : Service() {
} }
val matrix = Matrix.getInstance(applicationContext) val matrix = Matrix.getInstance(applicationContext)
val safeSessionId = intent.getStringExtra(EXTRA_SESSION_ID) ?: return false val safeSessionId = intent.getStringExtra(EXTRA_SESSION_ID) ?: return false
syncTimeoutSeconds = intent.getIntExtra(EXTRA_TIMEOUT_SECONDS, 6) syncTimeoutSeconds = intent.getIntExtra(EXTRA_TIMEOUT_SECONDS, getDefaultSyncTimeoutSeconds())
syncDelaySeconds = intent.getIntExtra(EXTRA_DELAY_SECONDS, 60) syncDelaySeconds = intent.getIntExtra(EXTRA_DELAY_SECONDS, getDefaultSyncDelaySeconds())
try { try {
val sessionComponent = matrix.sessionManager.getSessionComponent(safeSessionId) val sessionComponent = matrix.sessionManager.getSessionComponent(safeSessionId)
?: throw IllegalStateException("## Sync: You should have a session to make it work") ?: 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 onStart(isInitialSync: Boolean)
abstract fun onRescheduleAsked(sessionId: String, isInitialSync: Boolean, timeout: Int, delay: Int) 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.R
import im.vector.app.core.extensions.vectorComponent import im.vector.app.core.extensions.vectorComponent
import im.vector.app.features.notifications.NotificationUtils 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 org.matrix.android.sdk.internal.session.sync.job.SyncService
import timber.log.Timber import timber.log.Timber
@ -78,6 +79,10 @@ class VectorSyncService : SyncService() {
notificationUtils = vectorComponent().notificationUtils() notificationUtils = vectorComponent().notificationUtils()
} }
override fun getDefaultSyncDelaySeconds() = BackgroundSyncMode.DEFAULT_SYNC_DELAY_SECONDS
override fun getDefaultSyncTimeoutSeconds() = BackgroundSyncMode.DEFAULT_SYNC_TIMEOUT_SECONDS
override fun onStart(isInitialSync: Boolean) { override fun onStart(isInitialSync: Boolean) {
val notificationSubtitleRes = if (isInitialSync) { val notificationSubtitleRes = if (isInitialSync) {
R.string.notification_initial_sync R.string.notification_initial_sync
@ -125,8 +130,8 @@ class VectorSyncService : SyncService() {
override fun doWork(): Result { override fun doWork(): Result {
Timber.d("## Sync: RestartWhenNetworkOn.doWork()") Timber.d("## Sync: RestartWhenNetworkOn.doWork()")
val sessionId = inputData.getString(KEY_SESSION_ID) ?: return Result.failure() val sessionId = inputData.getString(KEY_SESSION_ID) ?: return Result.failure()
val timeout = inputData.getInt(KEY_TIMEOUT, 6) val timeout = inputData.getInt(KEY_TIMEOUT, BackgroundSyncMode.DEFAULT_SYNC_TIMEOUT_SECONDS)
val delay = inputData.getInt(KEY_DELAY, 60) val delay = inputData.getInt(KEY_DELAY, BackgroundSyncMode.DEFAULT_SYNC_DELAY_SECONDS)
applicationContext.rescheduleSyncService(sessionId, timeout, delay, true) applicationContext.rescheduleSyncService(sessionId, timeout, delay, true)
// Indicate whether the work finished successfully with the Result // Indicate whether the work finished successfully with the Result
return Result.success() return Result.success()
@ -152,6 +157,7 @@ private fun Context.rescheduleSyncService(sessionId: String,
timeout: Int, timeout: Int,
delay: Int, delay: Int,
isNetworkBack: Boolean) { isNetworkBack: Boolean) {
Timber.d("## Sync: rescheduleSyncService")
val periodicIntent = VectorSyncService.newPeriodicIntent(this, sessionId, timeout, delay, isNetworkBack) val periodicIntent = VectorSyncService.newPeriodicIntent(this, sessionId, timeout, delay, isNetworkBack)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(this, 0, periodicIntent, 0) PendingIntent.getForegroundService(this, 0, periodicIntent, 0)