BG task: notif, retry, log tweaks

This commit is contained in:
Imran Remtulla
2023-08-24 10:46:11 -04:00
parent 57d44c972f
commit 5e41d5762b
2 changed files with 30 additions and 16 deletions

View File

@@ -118,7 +118,7 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
var settingsProvider = SettingsProvider(); var settingsProvider = SettingsProvider();
await settingsProvider.initializeSettings(); await settingsProvider.initializeSettings();
int maxAttempts = 5; int maxAttempts = 4;
params ??= {}; params ??= {};
if (params['toCheck'] == null) { if (params['toCheck'] == null) {
@@ -137,13 +137,14 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
if (!installMode) { if (!installMode) {
var didCompleteChecking = false; var didCompleteChecking = false;
CheckingUpdatesNotification? notif;
for (int i = 0; i < toCheck.length; i++) { for (int i = 0; i < toCheck.length; i++) {
var appId = toCheck[i]; var appId = toCheck[i];
AppInMemory? app = appsProvider.apps[appId]; AppInMemory? app = appsProvider.apps[appId];
if (app?.app.installedVersion != null) { if (app?.app.installedVersion != null) {
try { try {
logs.add('BG update task $taskId: Checking for updates for $appId.'); notificationsProvider.notify(
notificationsProvider.notify(checkingUpdatesNotification, notif = CheckingUpdatesNotification(app?.name ?? appId),
cancelExisting: true); cancelExisting: true);
App? newApp = await appsProvider.checkUpdate(appId); App? newApp = await appsProvider.checkUpdate(appId);
if (newApp != null) { if (newApp != null) {
@@ -166,7 +167,7 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
? (e.remainingMinutes * 60) ? (e.remainingMinutes * 60)
: e is ClientException : e is ClientException
? (15 * 60) ? (15 * 60)
: 1; : (attemptCount ^ 2);
logs.add( logs.add(
'BG update task $taskId: Will continue in $remainingSeconds seconds (with $appId moved to the end of the line).'); 'BG update task $taskId: Will continue in $remainingSeconds seconds (with $appId moved to the end of the line).');
var remainingToCheck = moveStrToEnd(toCheck.sublist(i), appId); var remainingToCheck = moveStrToEnd(toCheck.sublist(i), appId);
@@ -183,18 +184,23 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
.notify(ErrorCheckingUpdatesNotification(e.toString())); .notify(ErrorCheckingUpdatesNotification(e.toString()));
} }
} finally { } finally {
notificationsProvider.cancel(checkingUpdatesNotification.id); if (notif != null) {
notificationsProvider.cancel(notif.id);
}
} }
} }
} }
if (didCompleteChecking && toInstall.isNotEmpty) { if (didCompleteChecking && toInstall.isNotEmpty) {
logs.add( logs.add(
'BG update task $taskId: Scheduling install task to run immediately.'); 'BG update task $taskId: Done. Scheduling install task to run immediately.');
AndroidAlarmManager.oneShot( AndroidAlarmManager.oneShot(
const Duration(minutes: 0), taskId + 1, bgUpdateCheck, const Duration(minutes: 0), taskId + 1, bgUpdateCheck,
params: {'toCheck': [], 'toInstall': toInstall}); params: {'toCheck': [], 'toInstall': toInstall});
} else if (didCompleteChecking) {
logs.add('BG install task $taskId: Done.');
} }
} else { } else {
var didCompleteInstalling = false;
toInstall = moveStrToEnd(toInstall, obtainiumId); toInstall = moveStrToEnd(toInstall, obtainiumId);
for (var i = 0; i < toInstall.length; i++) { for (var i = 0; i < toInstall.length; i++) {
String appId = toInstall[i]; String appId = toInstall[i];
@@ -207,11 +213,14 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
await Future.delayed(const Duration( await Future.delayed(const Duration(
seconds: seconds:
5)); // Just in case task ending causes install fail (not clear) 5)); // Just in case task ending causes install fail (not clear)
if (i == (toCheck.length - 1)) {
didCompleteInstalling = true;
}
} catch (e) { } catch (e) {
logs.add( logs.add(
'BG install task $taskId: Got error on updating $appId \'${e.toString()}\'.'); 'BG install task $taskId: Got error on updating $appId \'${e.toString()}\'.');
if (attemptCount < maxAttempts) { if (attemptCount < maxAttempts) {
var remainingSeconds = 1; var remainingSeconds = attemptCount;
logs.add( logs.add(
'BG install task $taskId: Will continue in $remainingSeconds seconds (with $appId moved to the end of the line).'); 'BG install task $taskId: Will continue in $remainingSeconds seconds (with $appId moved to the end of the line).');
var remainingToInstall = moveStrToEnd(toInstall.sublist(i), appId); var remainingToInstall = moveStrToEnd(toInstall.sublist(i), appId);
@@ -228,9 +237,11 @@ Future<void> bgUpdateCheck(int taskId, Map<String, dynamic>? params) async {
.notify(ErrorCheckingUpdatesNotification(e.toString())); .notify(ErrorCheckingUpdatesNotification(e.toString()));
} }
} }
if (didCompleteInstalling) {
logs.add('BG install task $taskId: Done.');
}
} }
} }
logs.add('BG ${installMode ? 'install' : 'update'} task $taskId: Done.');
} }
void main() async { void main() async {

View File

@@ -117,14 +117,17 @@ final completeInstallationNotification = ObtainiumNotification(
tr('completeAppInstallationNotifDescription'), tr('completeAppInstallationNotifDescription'),
Importance.max); Importance.max);
final checkingUpdatesNotification = ObtainiumNotification( class CheckingUpdatesNotification extends ObtainiumNotification {
4, CheckingUpdatesNotification(String appName)
tr('checkingForUpdates'), : super(
'', 4,
'BG_UPDATE_CHECK', tr('checkingForUpdates'),
tr('checkingForUpdates'), appName,
tr('checkingForUpdatesNotifDescription'), 'BG_UPDATE_CHECK',
Importance.min); tr('checkingForUpdates'),
tr('checkingForUpdatesNotifDescription'),
Importance.min);
}
class NotificationsProvider { class NotificationsProvider {
FlutterLocalNotificationsPlugin notifications = FlutterLocalNotificationsPlugin notifications =