Code cleanup, fix crash if appContext !is HasVectorInjector

This commit is contained in:
Benoit Marty 2021-01-12 11:13:38 +01:00 committed by Benoit Marty
parent ec0a04e893
commit b69d8ad71a

View file

@ -26,28 +26,20 @@ import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import im.vector.app.core.di.HasVectorInjector import im.vector.app.core.di.HasVectorInjector
import im.vector.app.core.services.VectorSyncService import im.vector.app.core.services.VectorSyncService
import im.vector.app.features.settings.VectorPreferences
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
class AlarmSyncBroadcastReceiver : BroadcastReceiver() { class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
lateinit var vectorPreferences: VectorPreferences
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val appContext = context.applicationContext Timber.d("## Sync: AlarmSyncBroadcastReceiver received intent")
if (appContext is HasVectorInjector) { val vectorPreferences = (context.applicationContext as? HasVectorInjector)
val activeSession = appContext.injector().activeSessionHolder().getSafeActiveSession() ?.injector()
if (activeSession == null) { ?.takeIf { it.activeSessionHolder().getSafeActiveSession() != null }
Timber.v("No active session don't launch sync service.") ?.vectorPreferences()
return ?: return Unit.also { Timber.v("No active session, so don't launch sync service.") }
}
vectorPreferences = appContext.injector().vectorPreferences()
}
val sessionId = intent.getStringExtra(SyncService.EXTRA_SESSION_ID) ?: return val sessionId = intent.getStringExtra(SyncService.EXTRA_SESSION_ID) ?: return
// This method is called when the BroadcastReceiver is receiving an Intent broadcast.
Timber.d("RestartBroadcastReceiver received intent")
VectorSyncService.newPeriodicIntent(context, sessionId, vectorPreferences.backgroundSyncTimeOut(), vectorPreferences.backgroundSyncDelay()).let { VectorSyncService.newPeriodicIntent(context, sessionId, vectorPreferences.backgroundSyncTimeOut(), vectorPreferences.backgroundSyncDelay()).let {
try { try {
ContextCompat.startForegroundService(context, it) ContextCompat.startForegroundService(context, it)