Don't reschedule bg checks if app is restarted

This commit is contained in:
Imran Remtulla
2023-08-22 17:13:15 -04:00
parent 82e08150ab
commit 9eb32ae55a
2 changed files with 36 additions and 13 deletions

View File

@@ -97,10 +97,15 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
NotificationsProvider notificationsProvider = NotificationsProvider(); NotificationsProvider notificationsProvider = NotificationsProvider();
AppsProvider appsProvider = AppsProvider(); AppsProvider appsProvider = AppsProvider();
await appsProvider.loadApps(); await appsProvider.loadApps();
var settingsProvider = SettingsProvider();
await settingsProvider.initializeSettings();
int maxAttempts = 5; int maxAttempts = 5;
params ??= {}; params ??= {};
if (params['toCheck'] == null) {
settingsProvider.lastBGCheckTime = DateTime.now();
}
params['attemptCount'] = (params['attemptCount'] ?? 0) + 1; params['attemptCount'] = (params['attemptCount'] ?? 0) + 1;
params['toCheck'] = params['toCheck'] =
params['toCheck'] ?? appsProvider.getAppsSortedByUpdateCheckTime(); params['toCheck'] ?? appsProvider.getAppsSortedByUpdateCheckTime();
@@ -251,22 +256,28 @@ class _ObtainiumState extends State<Obtainium> {
settingsProvider.resetLocaleSafe(context); settingsProvider.resetLocaleSafe(context);
} }
// Register the background update task according to the user's setting // Register the background update task according to the user's setting
if (existingUpdateInterval != settingsProvider.updateInterval) { var actualUpdateInterval = settingsProvider.updateInterval;
if (existingUpdateInterval != -1) { if (existingUpdateInterval != actualUpdateInterval) {
logs.add( if (actualUpdateInterval == 0) {
'Setting update interval to ${settingsProvider.updateInterval.toString()}');
}
existingUpdateInterval = settingsProvider.updateInterval;
if (existingUpdateInterval == 0) {
AndroidAlarmManager.cancel(bgUpdateCheckAlarmId); AndroidAlarmManager.cancel(bgUpdateCheckAlarmId);
} else { } else {
AndroidAlarmManager.periodic( var settingChanged = existingUpdateInterval != -1;
Duration(minutes: existingUpdateInterval), var lastCheckWasTooLongAgo = actualUpdateInterval != 0 &&
bgUpdateCheckAlarmId, settingsProvider.lastBGCheckTime
bgUpdateCheck, .add(Duration(seconds: actualUpdateInterval + 60))
rescheduleOnReboot: true, .isBefore(DateTime.now());
wakeup: true); 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;
} }
} }

View File

@@ -318,4 +318,16 @@ class SettingsProvider with ChangeNotifier {
prefs?.setBool('enableBackgroundUpdates', val); prefs?.setBool('enableBackgroundUpdates', val);
notifyListeners(); 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();
}
} }