From 9c80167aa8844dc7155d6384056a3727dd9cb976 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 6 Jul 2023 16:52:13 +0600 Subject: [PATCH 1/6] Implement network quite time preference For now, network quite time settings doesn't affect any behavior. --- app/core/src/main/java/com/fsck/k9/K9.kt | 24 ++++++++++++++++++ .../general/GeneralSettingsDataStore.kt | 6 +++++ app/ui/legacy/src/main/res/values/strings.xml | 2 ++ .../src/main/res/xml/general_settings.xml | 25 +++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/app/core/src/main/java/com/fsck/k9/K9.kt b/app/core/src/main/java/com/fsck/k9/K9.kt index 9d2b1f07be..5f2636093b 100644 --- a/app/core/src/main/java/com/fsck/k9/K9.kt +++ b/app/core/src/main/java/com/fsck/k9/K9.kt @@ -126,6 +126,10 @@ object K9 : EarlyInit { @JvmStatic var backgroundOps = BACKGROUND_OPS.ALWAYS + var isNetworkQuietTimeEnabled = true + var networkQuietTimeStarts: String? = null + var networkQuietTimeEnds: String? = null + @JvmStatic var isShowAnimations = true @@ -291,6 +295,17 @@ object K9 : EarlyInit { return quietTimeChecker.isQuietTime } + val isNetworkQuietTime: Boolean + get() { + if (!isNetworkQuietTimeEnabled) { + return false + } + + val clock = DI.get() + val quietTimeChecker = QuietTimeChecker(clock, networkQuietTimeStarts, networkQuietTimeEnds) + return quietTimeChecker.isQuietTime + } + @Synchronized @JvmStatic fun isSortAscending(sortType: SortType): Boolean { @@ -340,6 +355,11 @@ object K9 : EarlyInit { quietTimeStarts = storage.getString("quietTimeStarts", "21:00") quietTimeEnds = storage.getString("quietTimeEnds", "7:00") + isNetworkQuietTimeEnabled = storage.getBoolean("networkQuietTimeEnabled", true) + networkQuietTimeStarts = storage.getString("networkQuietTimeStarts", "21:00") + networkQuietTimeEnds = storage.getString("networkQuietTimeEnds", "7:00") + + isSentSoundEnabled = storage.getBoolean("isSentSoundEnabled", true) messageListDensity = storage.getEnum("messageListDensity", UiDensity.Default) @@ -418,6 +438,10 @@ object K9 : EarlyInit { editor.putString("quietTimeStarts", quietTimeStarts) editor.putString("quietTimeEnds", quietTimeEnds) + editor.putBoolean("networkQuietTimeEnabled", isNetworkQuietTimeEnabled) + editor.putString("networkQuietTimeStarts", networkQuietTimeStarts) + editor.putString("networkQuietTimeEnds", networkQuietTimeEnds) + editor.putEnum("messageListDensity", messageListDensity) editor.putBoolean("messageListSenderAboveSubject", isMessageListSenderAboveSubject) editor.putBoolean("showUnifiedInbox", isShowUnifiedInbox) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt index 1fd9d4814d..27efc9d579 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt @@ -40,6 +40,7 @@ class GeneralSettingsDataStore( "messageview_show_next" -> K9.isMessageViewShowNext "quiet_time_enabled" -> K9.isQuietTimeEnabled "disable_notifications_during_quiet_time" -> !K9.isNotificationDuringQuietTimeEnabled + "network_quiet_time_enabled" -> K9.isNetworkQuietTimeEnabled "sent_sound_enabled" -> K9.isSentSoundEnabled "privacy_hide_useragent" -> K9.isHideUserAgent "privacy_hide_timezone" -> K9.isHideTimeZone @@ -71,6 +72,7 @@ class GeneralSettingsDataStore( "messageview_return_to_list" -> K9.isMessageViewReturnToList = value "messageview_show_next" -> K9.isMessageViewShowNext = value "quiet_time_enabled" -> K9.isQuietTimeEnabled = value + "network_quiet_time_enabled" -> K9.isNetworkQuietTimeEnabled = value "disable_notifications_during_quiet_time" -> K9.isNotificationDuringQuietTimeEnabled = !value "sent_sound_enabled" -> K9.isSentSoundEnabled = value "privacy_hide_useragent" -> K9.isHideUserAgent = value @@ -113,6 +115,8 @@ class GeneralSettingsDataStore( "notification_quick_delete" -> K9.notificationQuickDeleteBehaviour.name "lock_screen_notification_visibility" -> K9.lockScreenNotificationVisibility.name "background_ops" -> K9.backgroundOps.name + "network_quiet_time_starts" -> K9.networkQuietTimeStarts + "network_quiet_time_ends" -> K9.networkQuietTimeEnds "quiet_time_starts" -> K9.quietTimeStarts "quiet_time_ends" -> K9.quietTimeEnds "message_list_subject_font" -> K9.fontSizes.messageListSubject.toString() @@ -149,6 +153,8 @@ class GeneralSettingsDataStore( K9.lockScreenNotificationVisibility = K9.LockScreenNotificationVisibility.valueOf(value) } "background_ops" -> setBackgroundOps(value) + "network_quiet_time_starts" -> K9.networkQuietTimeStarts = value + "network_quiet_time_ends" -> K9.networkQuietTimeEnds = value "quiet_time_starts" -> K9.quietTimeStarts = value "quiet_time_ends" -> K9.quietTimeEnds = value "message_list_subject_font" -> K9.fontSizes.messageListSubject = value.toInt() diff --git a/app/ui/legacy/src/main/res/values/strings.xml b/app/ui/legacy/src/main/res/values/strings.xml index 9a4cb3ac71..776089b817 100644 --- a/app/ui/legacy/src/main/res/values/strings.xml +++ b/app/ui/legacy/src/main/res/values/strings.xml @@ -1138,4 +1138,6 @@ You can keep this message and use it as a backup for your secret key. If you wan Email address Name and email address + + Disable automatic sync at specific time diff --git a/app/ui/legacy/src/main/res/xml/general_settings.xml b/app/ui/legacy/src/main/res/xml/general_settings.xml index 10e4c28fb2..0d0553eaef 100644 --- a/app/ui/legacy/src/main/res/xml/general_settings.xml +++ b/app/ui/legacy/src/main/res/xml/general_settings.xml @@ -484,6 +484,31 @@ app:useSimpleSummaryProvider="true" android:title="@string/background_ops_label" /> + + + + + + Date: Thu, 6 Jul 2023 17:01:37 +0600 Subject: [PATCH 2/6] Block inbox sync on network quite period If network quite preference is enabled & in the middle of the period, we want to disable inbox folder sync. Same inbox folder sync method can be called, when user opens the app for the first time, then we want to ignore the network quite preference & sync inbox. --- .../java/com/fsck/k9/controller/MessagingController.java | 7 ++++++- .../com/fsck/k9/controller/push/AccountPushController.kt | 8 ++++---- .../java/com/fsck/k9/backend/api/BackendPusherCallback.kt | 2 +- .../java/com/fsck/k9/backend/imap/ImapBackendPusher.kt | 4 ++-- .../java/com/fsck/k9/backend/imap/ImapFolderPusher.kt | 2 +- .../java/com/fsck/k9/backend/imap/ImapPusherCallback.kt | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index 356e9abb49..c7d598f4cd 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -531,9 +531,14 @@ public class MessagingController { ); } - public void synchronizeMailboxBlocking(Account account, String folderServerId) { + public void synchronizeMailboxBlocking(Account account, String folderServerId, Boolean initialSync) { long folderId = getFolderId(account, folderServerId); + if (!initialSync && K9.INSTANCE.isNetworkQuietTime() && folderId == account.getInboxFolderId()) { + Timber.i("Network quite time enabled. Skipping sync inbox"); + return; + } + final CountDownLatch latch = new CountDownLatch(1); putBackground("synchronizeMailbox", null, () -> { try { diff --git a/app/core/src/main/java/com/fsck/k9/controller/push/AccountPushController.kt b/app/core/src/main/java/com/fsck/k9/controller/push/AccountPushController.kt index 2b7a284cba..fba03a2722 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/push/AccountPushController.kt +++ b/app/core/src/main/java/com/fsck/k9/controller/push/AccountPushController.kt @@ -29,8 +29,8 @@ internal class AccountPushController( private var backendPusher: BackendPusher? = null private val backendPusherCallback = object : BackendPusherCallback { - override fun onPushEvent(folderServerId: String) { - syncFolders(folderServerId) + override fun onPushEvent(folderServerId: String, initialSync: Boolean) { + syncFolders(folderServerId, initialSync) } override fun onPushError(exception: Exception) { @@ -91,8 +91,8 @@ internal class AccountPushController( backendPusher?.updateFolders(folderServerIds) } - private fun syncFolders(folderServerId: String) { - messagingController.synchronizeMailboxBlocking(account, folderServerId) + private fun syncFolders(folderServerId: String, initialSync: Boolean) { + messagingController.synchronizeMailboxBlocking(account, folderServerId, initialSync) } private fun disablePush() { diff --git a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendPusherCallback.kt b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendPusherCallback.kt index d503bb7018..587c9863da 100644 --- a/backend/api/src/main/java/com/fsck/k9/backend/api/BackendPusherCallback.kt +++ b/backend/api/src/main/java/com/fsck/k9/backend/api/BackendPusherCallback.kt @@ -1,7 +1,7 @@ package com.fsck.k9.backend.api interface BackendPusherCallback { - fun onPushEvent(folderServerId: String) + fun onPushEvent(folderServerId: String, initialSync: Boolean = false) fun onPushError(exception: Exception) fun onPushNotSupported() } diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackendPusher.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackendPusher.kt index f9f31ba38a..bab23923fe 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackendPusher.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackendPusher.kt @@ -182,8 +182,8 @@ internal class ImapBackendPusher( ) } - override fun onPushEvent(folderServerId: String) { - callback.onPushEvent(folderServerId) + override fun onPushEvent(folderServerId: String, initialSync: Boolean) { + callback.onPushEvent(folderServerId, initialSync) idleRefreshManager.resetTimers() } diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapFolderPusher.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapFolderPusher.kt index c189e98e56..f5053632e9 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapFolderPusher.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapFolderPusher.kt @@ -96,6 +96,6 @@ class ImapFolderPusher( } private fun performInitialSync() { - callback.onPushEvent(folderServerId) + callback.onPushEvent(folderServerId, true) } } diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapPusherCallback.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapPusherCallback.kt index 642668e62c..7552a9b6ee 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapPusherCallback.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapPusherCallback.kt @@ -1,7 +1,7 @@ package com.fsck.k9.backend.imap interface ImapPusherCallback { - fun onPushEvent(folderServerId: String) + fun onPushEvent(folderServerId: String, initialSync: Boolean = false) fun onPushError(folderServerId: String, exception: Exception) fun onPushNotSupported() } -- GitLab From 8caad6087e5f53239e42fb2cf6ba5d6c9809a55a Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 10 Jul 2023 20:37:06 +0600 Subject: [PATCH 3/6] Fix preference text color not updating issue - Fix pref text color not updating on state change issue - Update quite time English text --- .../main/res/color/preference_text_color.xml | 23 +++++++++++++++++++ .../src/main/res/layout/custom_preference.xml | 4 ++-- app/ui/legacy/src/main/res/values/strings.xml | 6 ++--- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 app/ui/legacy/src/main/res/color/preference_text_color.xml diff --git a/app/ui/legacy/src/main/res/color/preference_text_color.xml b/app/ui/legacy/src/main/res/color/preference_text_color.xml new file mode 100644 index 0000000000..676bf6dc84 --- /dev/null +++ b/app/ui/legacy/src/main/res/color/preference_text_color.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/app/ui/legacy/src/main/res/layout/custom_preference.xml b/app/ui/legacy/src/main/res/layout/custom_preference.xml index 096701e5de..1cf9eed862 100644 --- a/app/ui/legacy/src/main/res/layout/custom_preference.xml +++ b/app/ui/legacy/src/main/res/layout/custom_preference.xml @@ -48,7 +48,7 @@ android:singleLine="true" android:textStyle="bold" android:textAppearance="?android:attr/textAppearanceLarge" - android:textColor="@color/color_default_secondary_text" + android:textColor="@color/preference_text_color" android:textSize="14sp" android:layout_marginBottom="2dp" android:ellipsize="marquee" @@ -61,7 +61,7 @@ android:layout_alignStart="@android:id/title" android:layout_alignLeft="@android:id/title" android:textAppearance="?android:attr/textAppearanceSmall" - android:textColor="@color/color_default_secondary_text" + android:textColor="@color/preference_text_color" android:textSize="12sp" android:textStyle="normal" android:maxLines="4" /> diff --git a/app/ui/legacy/src/main/res/values/strings.xml b/app/ui/legacy/src/main/res/values/strings.xml index 776089b817..bc4ba7979c 100644 --- a/app/ui/legacy/src/main/res/values/strings.xml +++ b/app/ui/legacy/src/main/res/values/strings.xml @@ -297,8 +297,8 @@ Disable ringing, buzzing and flashing at night Disable notifications Completely disable notifications during Quiet Time - Quiet Time starts - Quiet Time ends + Quiet Time starts at + Quiet Time ends at Email sent sound Play a sound when an email is successfully sent Set up a new account @@ -1139,5 +1139,5 @@ You can keep this message and use it as a backup for your secret key. If you wan Name and email address - Disable automatic sync at specific time + Disable automatic sync at specific time when activated -- GitLab From 2e3ec4aec983831943979700f99804815427c53b Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 11 Jul 2023 18:37:35 +0600 Subject: [PATCH 4/6] Run mail sync after networkQuitePeriod --- app/core/src/main/java/com/fsck/k9/K9.kt | 6 ++-- .../java/com/fsck/k9/QuietTimeChecker.java | 17 ++++++++-- .../k9/controller/MessagingController.java | 2 +- .../main/java/com/fsck/k9/job/K9JobManager.kt | 13 +++++++- .../main/java/com/fsck/k9/job/KoinModule.kt | 3 ++ .../java/com/fsck/k9/job/MailSyncWorker.kt | 5 +++ .../com/fsck/k9/job/MailSyncWorkerManager.kt | 29 ++++++++++++++++ .../fsck/k9/job/SyncAfterQuitePeriodWorker.kt | 33 +++++++++++++++++++ .../GeneralSettingsDescriptions.java | 10 ++++++ .../com/fsck/k9/preferences/Settings.java | 2 +- .../general/GeneralSettingsDataStore.kt | 27 +++++++++++++-- 11 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 app/core/src/main/java/com/fsck/k9/job/SyncAfterQuitePeriodWorker.kt diff --git a/app/core/src/main/java/com/fsck/k9/K9.kt b/app/core/src/main/java/com/fsck/k9/K9.kt index 5f2636093b..8b49d17e15 100644 --- a/app/core/src/main/java/com/fsck/k9/K9.kt +++ b/app/core/src/main/java/com/fsck/k9/K9.kt @@ -127,8 +127,8 @@ object K9 : EarlyInit { var backgroundOps = BACKGROUND_OPS.ALWAYS var isNetworkQuietTimeEnabled = true - var networkQuietTimeStarts: String? = null - var networkQuietTimeEnds: String? = null + var networkQuietTimeStarts: String? = "7:00" + var networkQuietTimeEnds: String? = "22:00" @JvmStatic var isShowAnimations = true @@ -356,7 +356,7 @@ object K9 : EarlyInit { quietTimeEnds = storage.getString("quietTimeEnds", "7:00") isNetworkQuietTimeEnabled = storage.getBoolean("networkQuietTimeEnabled", true) - networkQuietTimeStarts = storage.getString("networkQuietTimeStarts", "21:00") + networkQuietTimeStarts = storage.getString("networkQuietTimeStarts", "22:00") networkQuietTimeEnds = storage.getString("networkQuietTimeEnds", "7:00") diff --git a/app/core/src/main/java/com/fsck/k9/QuietTimeChecker.java b/app/core/src/main/java/com/fsck/k9/QuietTimeChecker.java index 6844b08d5f..b3a0051157 100644 --- a/app/core/src/main/java/com/fsck/k9/QuietTimeChecker.java +++ b/app/core/src/main/java/com/fsck/k9/QuietTimeChecker.java @@ -6,13 +6,13 @@ import java.util.Calendar; import kotlinx.datetime.Clock; -class QuietTimeChecker { +public class QuietTimeChecker { private final Clock clock; private final int quietTimeStart; private final int quietTimeEnd; - QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) { + public QuietTimeChecker(Clock clock, String quietTimeStart, String quietTimeEnd) { this.clock = clock; this.quietTimeStart = parseTime(quietTimeStart); this.quietTimeEnd = parseTime(quietTimeEnd); @@ -43,4 +43,17 @@ class QuietTimeChecker { return minutesSinceMidnight >= quietTimeStart && minutesSinceMidnight <= quietTimeEnd; } } + + public int minuteToNextEndTime() { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(clock.now().toEpochMilliseconds()); + + int nowInMin = (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE); + + if (quietTimeEnd >= nowInMin) { + return quietTimeEnd - nowInMin; + } + + return (24 * 60) - nowInMin + quietTimeEnd; + } } diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index c7d598f4cd..4b9af6eedd 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -531,7 +531,7 @@ public class MessagingController { ); } - public void synchronizeMailboxBlocking(Account account, String folderServerId, Boolean initialSync) { + public void synchronizeMailboxBlocking(Account account, String folderServerId, boolean initialSync) { long folderId = getFolderId(account, folderServerId); if (!initialSync && K9.INSTANCE.isNetworkQuietTime() && folderId == account.getInboxFolderId()) { diff --git a/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt b/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt index 1328476521..40bf4ebdf6 100644 --- a/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt +++ b/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt @@ -2,6 +2,7 @@ package com.fsck.k9.job import androidx.work.WorkManager import com.fsck.k9.Account +import com.fsck.k9.K9 import com.fsck.k9.Preferences import timber.log.Timber @@ -13,6 +14,16 @@ class K9JobManager( fun scheduleAllMailJobs() { Timber.v("scheduling all jobs") scheduleMailSync() + scheduleSyncAfterQuiteTime() + } + + fun scheduleSyncAfterQuiteTime() { + if (!K9.isNetworkQuietTimeEnabled || K9.networkQuietTimeStarts == null || K9.networkQuietTimeEnds == null) { + mailSyncWorkerManager.cancelSyncAfterQuitePeriodJob() + return + } + + mailSyncWorkerManager.scheduleSyncAfterQuitePeriod() } fun scheduleMailSync(account: Account) { @@ -20,7 +31,7 @@ class K9JobManager( mailSyncWorkerManager.scheduleMailSync(account) } - private fun scheduleMailSync() { + fun scheduleMailSync() { cancelAllMailSyncJobs() preferences.accounts.forEach { account -> diff --git a/app/core/src/main/java/com/fsck/k9/job/KoinModule.kt b/app/core/src/main/java/com/fsck/k9/job/KoinModule.kt index 47064aaeb8..4c0798ae17 100644 --- a/app/core/src/main/java/com/fsck/k9/job/KoinModule.kt +++ b/app/core/src/main/java/com/fsck/k9/job/KoinModule.kt @@ -15,4 +15,7 @@ val jobModule = module { factory { (parameters: WorkerParameters) -> MailSyncWorker(messagingController = get(), preferences = get(), context = get(), parameters) } + factory { (parameters: WorkerParameters) -> + SyncAfterQuitePeriodWorker(jobManager = get(), context = get(), parameters = parameters) + } } diff --git a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorker.kt b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorker.kt index 841e7667b8..63a2f56604 100644 --- a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorker.kt +++ b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorker.kt @@ -31,6 +31,11 @@ class MailSyncWorker( return Result.success() } + if (K9.isNetworkQuietTime) { + Timber.d("Running inside network quite time period. Skipping mail sync.") + return Result.success() + } + val account = preferences.getAccount(accountUuid) if (account == null) { Timber.e("Account %s not found. Can't perform mail sync.", accountUuid) diff --git a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt index a373aba339..d550ed5d7a 100644 --- a/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt +++ b/app/core/src/main/java/com/fsck/k9/job/MailSyncWorkerManager.kt @@ -11,6 +11,7 @@ import androidx.work.workDataOf import com.fsck.k9.Account import com.fsck.k9.K9 import com.fsck.k9.OsAccountManagerUtil +import com.fsck.k9.QuietTimeChecker import java.util.concurrent.TimeUnit import kotlinx.datetime.Clock import timber.log.Timber @@ -89,8 +90,36 @@ class MailSyncWorkerManager( return "$MAIL_SYNC_TAG:$accountUuid" } + fun scheduleSyncAfterQuitePeriod() { + if (isNeverSyncInBackground()) return + + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresStorageNotLow(true) + .build() + + val syncIntervalMinutes = (24 * 60).toLong() + + val quietTimeChecker = QuietTimeChecker(clock, K9.networkQuietTimeStarts, K9.networkQuietTimeEnds) + val initialDelay = (quietTimeChecker.minuteToNextEndTime() + 1).toLong() + + val mailSyncRequest = PeriodicWorkRequestBuilder(syncIntervalMinutes, TimeUnit.MINUTES) + .setInitialDelay(initialDelay, TimeUnit.MINUTES) + .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, INITIAL_BACKOFF_DELAY_MINUTES, TimeUnit.MINUTES) + .setConstraints(constraints) + .addTag(SYNC_AFTER_QUITE_PERIOD_TAG) + .build() + + workManager.enqueueUniquePeriodicWork(SYNC_AFTER_QUITE_PERIOD_TAG, ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, mailSyncRequest) + } + + fun cancelSyncAfterQuitePeriodJob() { + workManager.cancelUniqueWork(SYNC_AFTER_QUITE_PERIOD_TAG) + } + companion object { const val MAIL_SYNC_TAG = "MailSync" private const val INITIAL_BACKOFF_DELAY_MINUTES = 5L + private const val SYNC_AFTER_QUITE_PERIOD_TAG = "MailSyncAfterQuitePeriod" } } diff --git a/app/core/src/main/java/com/fsck/k9/job/SyncAfterQuitePeriodWorker.kt b/app/core/src/main/java/com/fsck/k9/job/SyncAfterQuitePeriodWorker.kt new file mode 100644 index 0000000000..2e8ce9f8d4 --- /dev/null +++ b/app/core/src/main/java/com/fsck/k9/job/SyncAfterQuitePeriodWorker.kt @@ -0,0 +1,33 @@ +/* + * Copyright MURENA SAS 2023 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fsck.k9.job + +import android.content.Context +import androidx.work.Worker +import androidx.work.WorkerParameters + +class SyncAfterQuitePeriodWorker( + private val jobManager: K9JobManager, + context: Context, + parameters: WorkerParameters, +) : Worker(context, parameters) { + + override fun doWork(): Result { + jobManager.scheduleMailSync() + return Result.success() + } +} diff --git a/app/core/src/main/java/com/fsck/k9/preferences/GeneralSettingsDescriptions.java b/app/core/src/main/java/com/fsck/k9/preferences/GeneralSettingsDescriptions.java index 1f2fb45007..1a9b9a220a 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/GeneralSettingsDescriptions.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/GeneralSettingsDescriptions.java @@ -285,6 +285,16 @@ public class GeneralSettingsDescriptions { s.put("fontSizeMessageViewAccountName", Settings.versions( new V(87, new FontSizeSetting(FontSizes.FONT_DEFAULT)) )); + s.put("networkQuietTimeEnabled", Settings.versions( + new V(89, new BooleanSetting(true)) + )); + s.put("networkQuietTimeStarts", Settings.versions( + new V(89, new TimeSetting("7:00")) + )); + s.put("networkQuietTimeEnds", Settings.versions( + new V(89, new TimeSetting("22:00")) + )); + SETTINGS = Collections.unmodifiableMap(s); diff --git a/app/core/src/main/java/com/fsck/k9/preferences/Settings.java b/app/core/src/main/java/com/fsck/k9/preferences/Settings.java index 1aa80b3a59..44199380c3 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/Settings.java +++ b/app/core/src/main/java/com/fsck/k9/preferences/Settings.java @@ -36,7 +36,7 @@ public class Settings { * * @see SettingsExporter */ - public static final int VERSION = 88; + public static final int VERSION = 89; static Map validate(int version, Map> settings, Map importedSettings, boolean useDefaultValues) { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt index 27efc9d579..bde1c03c45 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt @@ -72,7 +72,7 @@ class GeneralSettingsDataStore( "messageview_return_to_list" -> K9.isMessageViewReturnToList = value "messageview_show_next" -> K9.isMessageViewShowNext = value "quiet_time_enabled" -> K9.isQuietTimeEnabled = value - "network_quiet_time_enabled" -> K9.isNetworkQuietTimeEnabled = value + "network_quiet_time_enabled" -> setNetworkQuiteTimeEnable(value) "disable_notifications_during_quiet_time" -> K9.isNotificationDuringQuietTimeEnabled = !value "sent_sound_enabled" -> K9.isSentSoundEnabled = value "privacy_hide_useragent" -> K9.isHideUserAgent = value @@ -153,8 +153,8 @@ class GeneralSettingsDataStore( K9.lockScreenNotificationVisibility = K9.LockScreenNotificationVisibility.valueOf(value) } "background_ops" -> setBackgroundOps(value) - "network_quiet_time_starts" -> K9.networkQuietTimeStarts = value - "network_quiet_time_ends" -> K9.networkQuietTimeEnds = value + "network_quiet_time_starts" -> setNetworkQuiteTimeStarts(value) + "network_quiet_time_ends" -> setNetworkQuiteTimeEnds(value) "quiet_time_starts" -> K9.quietTimeStarts = value "quiet_time_ends" -> K9.quietTimeEnds = value "message_list_subject_font" -> K9.fontSizes.messageListSubject = value.toInt() @@ -287,6 +287,27 @@ class GeneralSettingsDataStore( } } + private fun setNetworkQuiteTimeEnable(newValue: Boolean) { + if (newValue != K9.isNetworkQuietTimeEnabled) { + K9.isNetworkQuietTimeEnabled = newValue + jobManager.scheduleSyncAfterQuiteTime() + } + } + + private fun setNetworkQuiteTimeStarts(newValue: String) { + if (newValue != K9.networkQuietTimeStarts) { + K9.networkQuietTimeStarts = newValue + jobManager.scheduleSyncAfterQuiteTime() + } + } + + private fun setNetworkQuiteTimeEnds(newValue: String) { + if (newValue != K9.networkQuietTimeEnds) { + K9.networkQuietTimeEnds = newValue + jobManager.scheduleSyncAfterQuiteTime() + } + } + private fun swipeActionToString(action: SwipeAction) = when (action) { SwipeAction.None -> "none" SwipeAction.ToggleSelection -> "toggle_selection" -- GitLab From 48bc6ee567c10d78bf3761128ab47f4bed54a9ae Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Mon, 17 Jul 2023 05:33:38 +0000 Subject: [PATCH 5/6] Apply 1 suggestion(s) to 1 file(s) --- .../main/java/com/fsck/k9/controller/MessagingController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index 4b9af6eedd..f7abacd2f7 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -532,7 +532,7 @@ public class MessagingController { } public void synchronizeMailboxBlocking(Account account, String folderServerId, boolean initialSync) { - long folderId = getFolderId(account, folderServerId); + final long folderId = getFolderId(account, folderServerId); if (!initialSync && K9.INSTANCE.isNetworkQuietTime() && folderId == account.getInboxFolderId()) { Timber.i("Network quite time enabled. Skipping sync inbox"); -- GitLab From 86c46a5292cf18679d7077f315706365d0e0dc51 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 17 Jul 2023 11:35:15 +0600 Subject: [PATCH 6/6] update manageSyncAfterQuiteTime method name according to review --- app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt | 4 ++-- .../fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt b/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt index 40bf4ebdf6..27c17f47e3 100644 --- a/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt +++ b/app/core/src/main/java/com/fsck/k9/job/K9JobManager.kt @@ -14,10 +14,10 @@ class K9JobManager( fun scheduleAllMailJobs() { Timber.v("scheduling all jobs") scheduleMailSync() - scheduleSyncAfterQuiteTime() + manageSyncAfterQuiteTime() } - fun scheduleSyncAfterQuiteTime() { + fun manageSyncAfterQuiteTime() { if (!K9.isNetworkQuietTimeEnabled || K9.networkQuietTimeStarts == null || K9.networkQuietTimeEnds == null) { mailSyncWorkerManager.cancelSyncAfterQuitePeriodJob() return diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt index bde1c03c45..b208b4f2a0 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt @@ -290,21 +290,21 @@ class GeneralSettingsDataStore( private fun setNetworkQuiteTimeEnable(newValue: Boolean) { if (newValue != K9.isNetworkQuietTimeEnabled) { K9.isNetworkQuietTimeEnabled = newValue - jobManager.scheduleSyncAfterQuiteTime() + jobManager.manageSyncAfterQuiteTime() } } private fun setNetworkQuiteTimeStarts(newValue: String) { if (newValue != K9.networkQuietTimeStarts) { K9.networkQuietTimeStarts = newValue - jobManager.scheduleSyncAfterQuiteTime() + jobManager.manageSyncAfterQuiteTime() } } private fun setNetworkQuiteTimeEnds(newValue: String) { if (newValue != K9.networkQuietTimeEnds) { K9.networkQuietTimeEnds = newValue - jobManager.scheduleSyncAfterQuiteTime() + jobManager.manageSyncAfterQuiteTime() } } -- GitLab