From 7efc58cb4256be2d3a36cd141705df00e2c057b3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 11 Sep 2020 11:02:54 +0200 Subject: [PATCH] Avoid duplication of code and fix issue on OnApplicationUpgradeOrRebootReceiver: background starts even if notification are disabled --- .../matrix/android/sdk/api/session/Session.kt | 2 +- .../app/fdroid/BackgroundSyncStarter.kt | 52 +++++++++++++++++++ .../OnApplicationUpgradeOrRebootReceiver.kt | 26 +++------- .../java/im/vector/app/push/fcm/FcmHelper.kt | 27 +--------- 4 files changed, 61 insertions(+), 46 deletions(-) create mode 100644 vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index f5b10a5645..4dfc24ddae 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -110,7 +110,7 @@ interface Session : * This does not work in doze mode :/ * If battery optimization is on it can work in app standby but that's all :/ */ - fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long = 30L) + fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) fun stopAnyBackgroundSync() diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt new file mode 100644 index 0000000000..7221e2b065 --- /dev/null +++ b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.fdroid + +import android.content.Context +import im.vector.app.core.di.ActiveSessionHolder +import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver +import im.vector.app.features.settings.BackgroundSyncMode +import im.vector.app.features.settings.VectorPreferences +import timber.log.Timber + +object BackgroundSyncStarter { + fun start(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) { + if (vectorPreferences.areNotificationEnabledForDevice()) { + val activeSession = activeSessionHolder.getSafeActiveSession() ?: return + + when (vectorPreferences.getFdroidSyncBackgroundMode()) { + BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY -> { + // we rely on periodic worker + Timber.i("## Sync: Work scheduled to periodically sync in ${vectorPreferences.backgroundSyncDelay()}s") + activeSession.startAutomaticBackgroundSync( + vectorPreferences.backgroundSyncTimeOut().toLong(), + vectorPreferences.backgroundSyncDelay().toLong() + ) + } + BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME -> { + // We need to use alarm in this mode + AlarmSyncBroadcastReceiver.scheduleAlarm(context, activeSession.sessionId, vectorPreferences.backgroundSyncDelay()) + Timber.i("## Sync: Alarm scheduled to start syncing") + } + BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED -> { + // we do nothing + Timber.i("## Sync: background sync is disabled") + } + } + } + } +} diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt index 2460e149a5..797b5734a2 100644 --- a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt +++ b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt @@ -22,7 +22,7 @@ import android.content.Context import android.content.Intent import im.vector.app.core.di.HasVectorInjector import im.vector.app.core.extensions.vectorComponent -import im.vector.app.features.settings.BackgroundSyncMode +import im.vector.app.fdroid.BackgroundSyncStarter import timber.log.Timber class OnApplicationUpgradeOrRebootReceiver : BroadcastReceiver() { @@ -31,25 +31,11 @@ class OnApplicationUpgradeOrRebootReceiver : BroadcastReceiver() { Timber.v("## onReceive() ${intent.action}") val appContext = context.applicationContext if (appContext is HasVectorInjector) { - val activeSession = appContext.injector().activeSessionHolder().getSafeActiveSession() - val preferences = appContext.vectorComponent().vectorPreferences() - if (activeSession != null) { - when (preferences.getFdroidSyncBackgroundMode()) { - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY -> { - Timber.i("## Sync: OnBoot Work scheduled to periodically sync") - activeSession.startAutomaticBackgroundSync( - preferences.backgroundSyncTimeOut().toLong(), - preferences.backgroundSyncDelay().toLong() - ) - } - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME -> { - AlarmSyncBroadcastReceiver.scheduleAlarm(context, activeSession.sessionId, preferences.backgroundSyncDelay()) - } - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED -> { - // nop - } - } - } + BackgroundSyncStarter.start( + context, + appContext.vectorComponent().vectorPreferences(), + appContext.injector().activeSessionHolder() + ) } } } diff --git a/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt b/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt index 169fd9ecac..5f0ee396ee 100755 --- a/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt +++ b/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt @@ -22,10 +22,9 @@ import android.app.Activity import android.content.Context import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.PushersManager +import im.vector.app.fdroid.BackgroundSyncStarter import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver -import im.vector.app.features.settings.BackgroundSyncMode import im.vector.app.features.settings.VectorPreferences -import timber.log.Timber /** * This class has an alter ego in the gplay variant. @@ -69,28 +68,6 @@ object FcmHelper { } fun onEnterBackground(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) { - // We need to use alarm in this mode - if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) { - when (vectorPreferences.getFdroidSyncBackgroundMode()) { - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY -> { - // we rely on periodic worker - Timber.i("## Sync: Work scheduled to periodically sync in ${vectorPreferences.backgroundSyncDelay()} sec") - activeSessionHolder - .getSafeActiveSession() - ?.startAutomaticBackgroundSync( - vectorPreferences.backgroundSyncTimeOut().toLong(), - vectorPreferences.backgroundSyncDelay().toLong() - ) - } - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME -> { - val currentSession = activeSessionHolder.getActiveSession() - AlarmSyncBroadcastReceiver.scheduleAlarm(context, currentSession.sessionId, vectorPreferences.backgroundSyncDelay()) - Timber.i("## Sync: Alarm scheduled to start syncing") - } - BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED -> { - // we do nothing - } - } - } + BackgroundSyncStarter.start(context, vectorPreferences, activeSessionHolder) } }