From 9eb32ae55a287405d24f15545127a1f852346d37 Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Tue, 22 Aug 2023 17:13:15 -0400 Subject: [PATCH] Don't reschedule bg checks if app is restarted --- lib/main.dart | 37 ++++++++++++++++++---------- lib/providers/settings_provider.dart | 12 +++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 25ad603..165d6b1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -97,10 +97,15 @@ Future bgUpdateCheck(int taskId, Map? params) async { NotificationsProvider notificationsProvider = NotificationsProvider(); AppsProvider appsProvider = AppsProvider(); await appsProvider.loadApps(); + var settingsProvider = SettingsProvider(); + await settingsProvider.initializeSettings(); int maxAttempts = 5; params ??= {}; + if (params['toCheck'] == null) { + settingsProvider.lastBGCheckTime = DateTime.now(); + } params['attemptCount'] = (params['attemptCount'] ?? 0) + 1; params['toCheck'] = params['toCheck'] ?? appsProvider.getAppsSortedByUpdateCheckTime(); @@ -251,22 +256,28 @@ class _ObtainiumState extends State { settingsProvider.resetLocaleSafe(context); } // Register the background update task according to the user's setting - if (existingUpdateInterval != settingsProvider.updateInterval) { - if (existingUpdateInterval != -1) { - logs.add( - 'Setting update interval to ${settingsProvider.updateInterval.toString()}'); - } - existingUpdateInterval = settingsProvider.updateInterval; - if (existingUpdateInterval == 0) { + var actualUpdateInterval = settingsProvider.updateInterval; + if (existingUpdateInterval != actualUpdateInterval) { + if (actualUpdateInterval == 0) { AndroidAlarmManager.cancel(bgUpdateCheckAlarmId); } else { - AndroidAlarmManager.periodic( - Duration(minutes: existingUpdateInterval), - bgUpdateCheckAlarmId, - bgUpdateCheck, - rescheduleOnReboot: true, - wakeup: true); + var settingChanged = existingUpdateInterval != -1; + var lastCheckWasTooLongAgo = actualUpdateInterval != 0 && + settingsProvider.lastBGCheckTime + .add(Duration(seconds: actualUpdateInterval + 60)) + .isBefore(DateTime.now()); + if (settingChanged || lastCheckWasTooLongAgo) { + logs.add( + 'Update interval was set to ${actualUpdateInterval.toString()} (reason: ${settingChanged ? 'setting changed' : 'last check was too long ago or never'}).'); + AndroidAlarmManager.periodic( + Duration(minutes: actualUpdateInterval), + bgUpdateCheckAlarmId, + bgUpdateCheck, + rescheduleOnReboot: true, + wakeup: true); + } } + existingUpdateInterval = actualUpdateInterval; } } diff --git a/lib/providers/settings_provider.dart b/lib/providers/settings_provider.dart index f5aba32..386b267 100644 --- a/lib/providers/settings_provider.dart +++ b/lib/providers/settings_provider.dart @@ -318,4 +318,16 @@ class SettingsProvider with ChangeNotifier { prefs?.setBool('enableBackgroundUpdates', val); notifyListeners(); } + + DateTime get lastBGCheckTime { + int? temp = prefs?.getInt('lastBGCheckTime'); + return temp != null + ? DateTime.fromMillisecondsSinceEpoch(temp) + : DateTime.fromMillisecondsSinceEpoch(0); + } + + set lastBGCheckTime(DateTime val) { + prefs?.setInt('lastBGCheckTime', val.millisecondsSinceEpoch); + notifyListeners(); + } }