Configure background fetch during app widget init

This commit is contained in:
vishnukvmd 2021-11-25 15:52:12 +05:30
parent 3f9137481f
commit 01f9b673aa
2 changed files with 42 additions and 31 deletions

View file

@ -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<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
EnteApp(
this.runBackgroundTask,
this.killBackgroundTask, {
Key key,
}) : super(key: key);
@override
_EnteAppState createState() => _EnteAppState();
}
class _EnteAppState extends State<EnteApp> 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<EnteApp> 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');
});
}
}

View file

@ -56,9 +56,8 @@ Future<void> _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<void> _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<void> _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');
});
}