Fix SyncService and Alarm
This commit is contained in:
parent
f3db43f317
commit
e5e62dc4a7
|
@ -143,9 +143,6 @@ abstract class SyncService : Service() {
|
||||||
backgroundDetectionObserver = matrix.backgroundDetectionObserver
|
backgroundDetectionObserver = matrix.backgroundDetectionObserver
|
||||||
return true
|
return true
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
if (BuildConfig.DEBUG) {
|
|
||||||
throw exception
|
|
||||||
}
|
|
||||||
Timber.e(exception, "An exception occurred during initialisation")
|
Timber.e(exception, "An exception occurred during initialisation")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,12 +25,22 @@ import android.os.Build
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import im.vector.matrix.android.internal.session.sync.job.SyncService
|
import im.vector.matrix.android.internal.session.sync.job.SyncService
|
||||||
|
import im.vector.riotx.core.di.HasVectorInjector
|
||||||
import im.vector.riotx.core.services.VectorSyncService
|
import im.vector.riotx.core.services.VectorSyncService
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
|
class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
val appContext = context.applicationContext
|
||||||
|
if (appContext is HasVectorInjector) {
|
||||||
|
val activeSession = appContext.injector().activeSessionHolder().getSafeActiveSession()
|
||||||
|
if (activeSession == null) {
|
||||||
|
Timber.v("No active session don't launch sync service.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Acquire a lock to give enough time for the sync :/
|
// Acquire a lock to give enough time for the sync :/
|
||||||
(context.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
(context.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "riotx:fdroidSynclock").apply {
|
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "riotx:fdroidSynclock").apply {
|
||||||
|
@ -51,7 +61,6 @@ class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduleAlarm(context, sessionId, 30_000L)
|
scheduleAlarm(context, sessionId, 30_000L)
|
||||||
|
|
||||||
Timber.i("Alarm scheduled to restart service")
|
Timber.i("Alarm scheduled to restart service")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,15 @@
|
||||||
*/
|
*/
|
||||||
package im.vector.riotx.core.services
|
package im.vector.riotx.core.services
|
||||||
|
|
||||||
|
import android.app.AlarmManager
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
import im.vector.matrix.android.internal.session.sync.job.SyncService
|
import im.vector.matrix.android.internal.session.sync.job.SyncService
|
||||||
import im.vector.riotx.R
|
import im.vector.riotx.R
|
||||||
import im.vector.riotx.core.extensions.vectorComponent
|
import im.vector.riotx.core.extensions.vectorComponent
|
||||||
import im.vector.riotx.fdroid.receiver.AlarmSyncBroadcastReceiver
|
|
||||||
import im.vector.riotx.features.notifications.NotificationUtils
|
import im.vector.riotx.features.notifications.NotificationUtils
|
||||||
|
|
||||||
class VectorSyncService : SyncService() {
|
class VectorSyncService : SyncService() {
|
||||||
|
@ -67,7 +69,17 @@ class VectorSyncService : SyncService() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reschedule(sessionId: String, delay: Long) {
|
private fun reschedule(sessionId: String, delay: Long) {
|
||||||
AlarmSyncBroadcastReceiver.cancelAlarm(applicationContext)
|
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
AlarmSyncBroadcastReceiver.scheduleAlarm(applicationContext, sessionId, delay)
|
PendingIntent.getForegroundService(this, 0, newIntent(this, sessionId), 0)
|
||||||
|
} else {
|
||||||
|
PendingIntent.getService(this, 0, newIntent(this, sessionId), 0)
|
||||||
|
}
|
||||||
|
val firstMillis = System.currentTimeMillis() + delay
|
||||||
|
val alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
|
||||||
|
} else {
|
||||||
|
alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue