2026-02-22 06:54:27 -08:00
|
|
|
import '../utils/platform_info.dart';
|
2025-12-27 15:32:32 -07:00
|
|
|
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
|
|
|
|
|
|
|
|
|
class BackgroundService {
|
|
|
|
|
bool _initialized = false;
|
|
|
|
|
|
|
|
|
|
Future<void> initialize() async {
|
2026-02-22 06:54:27 -08:00
|
|
|
if (!PlatformInfo.isAndroid || _initialized) return;
|
2025-12-27 15:32:32 -07:00
|
|
|
FlutterForegroundTask.init(
|
|
|
|
|
androidNotificationOptions: AndroidNotificationOptions(
|
|
|
|
|
channelId: 'meshcore_background',
|
|
|
|
|
channelName: 'MeshCore Background',
|
|
|
|
|
channelDescription: 'Keeps MeshCore running in the background.',
|
|
|
|
|
channelImportance: NotificationChannelImportance.LOW,
|
|
|
|
|
priority: NotificationPriority.LOW,
|
|
|
|
|
),
|
|
|
|
|
iosNotificationOptions: const IOSNotificationOptions(
|
|
|
|
|
showNotification: false,
|
|
|
|
|
playSound: false,
|
|
|
|
|
),
|
2026-02-14 02:22:45 -07:00
|
|
|
foregroundTaskOptions: ForegroundTaskOptions(
|
|
|
|
|
eventAction: ForegroundTaskEventAction.repeat(5000),
|
2025-12-27 15:32:32 -07:00
|
|
|
autoRunOnBoot: false,
|
|
|
|
|
allowWifiLock: false,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
_initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> start() async {
|
2026-02-22 06:54:27 -08:00
|
|
|
if (!PlatformInfo.isAndroid) return;
|
2025-12-27 15:32:32 -07:00
|
|
|
if (!_initialized) {
|
|
|
|
|
await initialize();
|
|
|
|
|
}
|
|
|
|
|
final running = await FlutterForegroundTask.isRunningService;
|
|
|
|
|
if (running) return;
|
|
|
|
|
await FlutterForegroundTask.startService(
|
|
|
|
|
notificationTitle: 'MeshCore running',
|
|
|
|
|
notificationText: 'Keeping BLE connected',
|
|
|
|
|
callback: startCallback,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> stop() async {
|
2026-02-22 06:54:27 -08:00
|
|
|
if (!PlatformInfo.isAndroid) return;
|
2025-12-27 15:32:32 -07:00
|
|
|
final running = await FlutterForegroundTask.isRunningService;
|
|
|
|
|
if (!running) return;
|
|
|
|
|
await FlutterForegroundTask.stopService();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@pragma('vm:entry-point')
|
|
|
|
|
void startCallback() {
|
|
|
|
|
FlutterForegroundTask.setTaskHandler(_MeshCoreTaskHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _MeshCoreTaskHandler extends TaskHandler {
|
|
|
|
|
@override
|
2026-02-14 02:22:45 -07:00
|
|
|
Future<void> onStart(DateTime timestamp, TaskStarter starter) async {}
|
2025-12-27 15:32:32 -07:00
|
|
|
|
|
|
|
|
@override
|
2026-02-14 02:22:45 -07:00
|
|
|
void onRepeatEvent(DateTime timestamp) {}
|
2025-12-27 15:32:32 -07:00
|
|
|
|
|
|
|
|
@override
|
2026-02-14 02:22:45 -07:00
|
|
|
Future<void> onDestroy(DateTime timestamp, bool isTimeout) async {}
|
2025-12-27 15:32:32 -07:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onNotificationButtonPressed(String id) {}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onNotificationPressed() {
|
|
|
|
|
FlutterForegroundTask.launchApp('/');
|
|
|
|
|
}
|
|
|
|
|
}
|