From 2c6e95f9027e11f56ff310252e584bbc34219f66 Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Wed, 2 Jul 2025 16:00:29 -0400 Subject: [PATCH] Updates --- assets/translations/en.json | 2 ++ lib/main.dart | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index 7dde0a2..7bccf94 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -330,6 +330,8 @@ "documentationLinksNote": "The Obtainium GitHub page linked below contains links to videos, articles, discussions and other resources that will help you understand how to use the app.", "batteryOptimizationNote": "Note that background downloads may work more reliably if you disable OS battery optimizations for Obtainium.", "fileDeletionError": "Failed to delete file (try deleting it manually then try again): \"{}\"", + "foregroundService": "Obtainium foreground service", + "fgServiceNotice": "This notification is required for background update checking (it can be hidden in the OS settings)", "removeAppQuestion": { "one": "Remove app?", "other": "Remove apps?" diff --git a/lib/main.dart b/lib/main.dart index dff79a8..2fdcd99 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -92,26 +92,22 @@ void startCallback() { class MyTaskHandler extends TaskHandler { static const String incrementCountCommand = 'incrementCount'; - // Called when the task is started. @override Future onStart(DateTime timestamp, TaskStarter starter) async { print('onStart(starter: ${starter.name})'); bgUpdateCheck('bg_check', null); } - // Called based on the eventAction set in ForegroundTaskOptions. @override void onRepeatEvent(DateTime timestamp) { - bgUpdateCheck('Foreground service bg_check', null); + bgUpdateCheck('bg_check', null); } - // Called when the task is destroyed. @override Future onDestroy(DateTime timestamp, bool isTimeout) async { print('Foreground service onDestroy(isTimeout: $isTimeout)'); } - // Called when data is sent using `FlutterForegroundTask.sendDataToTask`. @override void onReceiveData(Object data) {} } @@ -192,8 +188,8 @@ class _ObtainiumState extends State { FlutterForegroundTask.init( androidNotificationOptions: AndroidNotificationOptions( channelId: 'bg_update', - channelName: tr('placeholder'), - channelDescription: tr('placeholder'), + channelName: tr('foregroundService'), + channelDescription: tr('foregroundService'), onlyAlertOnce: true, ), iosNotificationOptions: const IOSNotificationOptions( @@ -210,21 +206,30 @@ class _ObtainiumState extends State { ); } - Future startForegroundService() async { + Future startForegroundService(bool restart) async { if (await FlutterForegroundTask.isRunningService) { - return FlutterForegroundTask.restartService(); + if (restart) { + return FlutterForegroundTask.restartService(); + } } else { return FlutterForegroundTask.startService( serviceTypes: [ForegroundServiceTypes.specialUse], serviceId: 666, - notificationTitle: tr('placeholder'), - notificationText: tr('placeholder'), + notificationTitle: tr('foregroundService'), + notificationText: tr('fgServiceNotice'), notificationIcon: NotificationIcon( metaDataName: 'dev.imranr.obtainium.service.NOTIFICATION_ICON', ), callback: startCallback, ); } + return null; + } + + stopForegroundService() async { + if (await FlutterForegroundTask.isRunningService) { + return FlutterForegroundTask.stopService(); + } } // void onReceiveForegroundServiceData(Object data) { @@ -244,7 +249,11 @@ class _ObtainiumState extends State { AppsProvider appsProvider = context.read(); LogsProvider logs = context.read(); NotificationsProvider notifs = context.read(); - startForegroundService(); + if (settingsProvider.updateInterval == 0) { + stopForegroundService(); + } else { + startForegroundService(false); + } if (settingsProvider.prefs == null) { settingsProvider.initializeSettings(); } else {