diff --git a/lib/app.dart b/lib/app.dart index 02babda4e..978b7f804 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,7 +1,9 @@ +import 'package:background_fetch/background_fetch.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:logging/logging.dart'; import 'package:photos/core/network.dart'; import 'package:photos/l10n/l10n.dart'; import 'package:photos/services/app_lifecycle_service.dart'; @@ -42,17 +44,27 @@ final themeData = ThemeData( class EnteApp extends StatefulWidget { static const _homeWidget = HomeWidget(); - EnteApp({Key key}) : super(key: key); + final Future Function(String) runBackgroundTask; + final Future Function(String) killBackgroundTask; + + EnteApp( + this.runBackgroundTask, + this.killBackgroundTask, { + Key key, + }) : super(key: key); @override _EnteAppState createState() => _EnteAppState(); } class _EnteAppState extends State with WidgetsBindingObserver { + final _logger = Logger("EnteApp"); + @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); + _configureBackgroundFetch(); } @override @@ -83,4 +95,29 @@ class _EnteAppState extends State with WidgetsBindingObserver { AppLifecycleService.instance.onAppInBackground(); } } + + void _configureBackgroundFetch() { + BackgroundFetch.configure( + BackgroundFetchConfig( + minimumFetchInterval: 15, + forceAlarmManager: false, + stopOnTerminate: false, + startOnBoot: true, + enableHeadless: true, + requiresBatteryNotLow: false, + requiresCharging: false, + requiresStorageNotLow: false, + requiresDeviceIdle: false, + requiredNetworkType: NetworkType.NONE, + ), (String taskId) async { + await widget.runBackgroundTask(taskId); + }, (taskId) { + _logger.info("BG task timeout"); + widget.killBackgroundTask(taskId); + }).then((int status) { + _logger.info('[BackgroundFetch] configure success: $status'); + }).catchError((e) { + _logger.info('[BackgroundFetch] configure ERROR: $e'); + }); + } } diff --git a/lib/main.dart b/lib/main.dart index ffb96a8dd..6b8f3deb5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -56,9 +56,8 @@ Future _runInForeground() async { _logger.info("Starting app in foreground"); await _init(false); _scheduleFGSync(); - _configureBackgroundFetch(); runApp(AppLock( - builder: (args) => EnteApp(), + builder: (args) => EnteApp(_runBackgroundTask, _killBGTask), lockScreen: LockScreen(), enabled: Configuration.instance.shouldShowLockScreen(), themeData: themeData, @@ -69,14 +68,14 @@ Future _runInForeground() async { Future _runInBackground(String taskId) async { if (_initializationStatus == null) { _runWithLogs(() async { - _backgroundTask(taskId); + _runBackgroundTask(taskId); }, prefix: "[bg]"); } else { - _backgroundTask(taskId); + _runBackgroundTask(taskId); } } -void _backgroundTask(String taskId) async { +Future _runBackgroundTask(String taskId) async { await Future.delayed(Duration(seconds: 3)); if (await _isRunningInForeground()) { _logger.info("FG task running, skipping BG task"); @@ -239,28 +238,3 @@ void _scheduleSuicide(Duration duration, [String taskID]) { _killBGTask(taskID); }); } - -void _configureBackgroundFetch() { - BackgroundFetch.configure( - BackgroundFetchConfig( - minimumFetchInterval: 15, - forceAlarmManager: false, - stopOnTerminate: false, - startOnBoot: true, - enableHeadless: true, - requiresBatteryNotLow: false, - requiresCharging: false, - requiresStorageNotLow: false, - requiresDeviceIdle: false, - requiredNetworkType: NetworkType.NONE, - ), (String taskId) async { - await _runInBackground(taskId); - }, (taskId) { - _logger.info("BG task timeout"); - _killBGTask(taskId); - }).then((int status) { - _logger.info('[BackgroundFetch] configure success: $status'); - }).catchError((e) { - _logger.info('[BackgroundFetch] configure ERROR: $e'); - }); -}