|
@@ -1,6 +1,5 @@
|
|
import 'dart:async';
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:io';
|
|
-import 'dart:isolate';
|
|
|
|
|
|
|
|
import 'package:adaptive_theme/adaptive_theme.dart';
|
|
import 'package:adaptive_theme/adaptive_theme.dart';
|
|
import 'package:background_fetch/background_fetch.dart';
|
|
import 'package:background_fetch/background_fetch.dart';
|
|
@@ -56,8 +55,8 @@ void main() async {
|
|
Future<void> _runInForeground() async {
|
|
Future<void> _runInForeground() async {
|
|
return await _runWithLogs(() async {
|
|
return await _runWithLogs(() async {
|
|
_logger.info("Starting app in foreground");
|
|
_logger.info("Starting app in foreground");
|
|
- await _init(false);
|
|
|
|
- _scheduleFGSync();
|
|
|
|
|
|
+ await _init(false, via: 'mainMethod');
|
|
|
|
+ _scheduleFGSync('appStart in FG');
|
|
runApp(AppLock(
|
|
runApp(AppLock(
|
|
builder: (args) => EnteApp(_runBackgroundTask, _killBGTask),
|
|
builder: (args) => EnteApp(_runBackgroundTask, _killBGTask),
|
|
lockScreen: LockScreen(),
|
|
lockScreen: LockScreen(),
|
|
@@ -70,10 +69,11 @@ Future<void> _runInForeground() async {
|
|
Future<void> _runBackgroundTask(String taskId) async {
|
|
Future<void> _runBackgroundTask(String taskId) async {
|
|
if (Platform.isIOS && _isProcessRunning) {
|
|
if (Platform.isIOS && _isProcessRunning) {
|
|
_logger.info("Background task triggered when process was already running");
|
|
_logger.info("Background task triggered when process was already running");
|
|
- await _sync();
|
|
|
|
|
|
+ await _sync('bgTaskActiveProcess');
|
|
BackgroundFetch.finish(taskId);
|
|
BackgroundFetch.finish(taskId);
|
|
} else {
|
|
} else {
|
|
_runWithLogs(() async {
|
|
_runWithLogs(() async {
|
|
|
|
+ _logger.info("run background task");
|
|
_runInBackground(taskId);
|
|
_runInBackground(taskId);
|
|
}, prefix: "[bg]");
|
|
}, prefix: "[bg]");
|
|
}
|
|
}
|
|
@@ -82,7 +82,7 @@ Future<void> _runBackgroundTask(String taskId) async {
|
|
Future<void> _runInBackground(String taskId) async {
|
|
Future<void> _runInBackground(String taskId) async {
|
|
await Future.delayed(Duration(seconds: 3));
|
|
await Future.delayed(Duration(seconds: 3));
|
|
if (await _isRunningInForeground()) {
|
|
if (await _isRunningInForeground()) {
|
|
- _logger.info("FG task running, skipping BG task");
|
|
|
|
|
|
+ _logger.info("FG task running, skipping BG taskID: $taskId");
|
|
BackgroundFetch.finish(taskId);
|
|
BackgroundFetch.finish(taskId);
|
|
return;
|
|
return;
|
|
} else {
|
|
} else {
|
|
@@ -91,11 +91,11 @@ Future<void> _runInBackground(String taskId) async {
|
|
_logger.info("[BackgroundFetch] Event received: $taskId");
|
|
_logger.info("[BackgroundFetch] Event received: $taskId");
|
|
_scheduleBGTaskKill(taskId);
|
|
_scheduleBGTaskKill(taskId);
|
|
if (Platform.isIOS) {
|
|
if (Platform.isIOS) {
|
|
- _scheduleSuicide(kBGTaskTimeout); // To prevent OS from punishing us
|
|
|
|
|
|
+ _scheduleSuicide(kBGTaskTimeout, taskId); // To prevent OS from punishing us
|
|
}
|
|
}
|
|
- await _init(true);
|
|
|
|
|
|
+ await _init(true, via: 'runViaBackgroundTask');
|
|
UpdateService.instance.showUpdateNotification();
|
|
UpdateService.instance.showUpdateNotification();
|
|
- await _sync();
|
|
|
|
|
|
+ await _sync('bgSync');
|
|
BackgroundFetch.finish(taskId);
|
|
BackgroundFetch.finish(taskId);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -107,15 +107,16 @@ void _headlessTaskHandler(HeadlessTask task) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-Future<void> _init(bool isBackground) async {
|
|
|
|
|
|
+Future<void> _init(bool isBackground, {String via = ''}) async {
|
|
_isProcessRunning = true;
|
|
_isProcessRunning = true;
|
|
- _logger.info("Initializing...");
|
|
|
|
|
|
+ _logger.info("Initializing... inBG =$isBackground via: $via");
|
|
|
|
+ await _logFGHeartBeatInfo();
|
|
_savedThemeMode = await AdaptiveTheme.getThemeMode();
|
|
_savedThemeMode = await AdaptiveTheme.getThemeMode();
|
|
_scheduleHeartBeat(isBackground);
|
|
_scheduleHeartBeat(isBackground);
|
|
if (isBackground) {
|
|
if (isBackground) {
|
|
- AppLifecycleService.instance.onAppInBackground();
|
|
|
|
|
|
+ AppLifecycleService.instance.onAppInBackground('init via: $via');
|
|
} else {
|
|
} else {
|
|
- AppLifecycleService.instance.onAppInForeground();
|
|
|
|
|
|
+ AppLifecycleService.instance.onAppInForeground('init via: $via');
|
|
}
|
|
}
|
|
InAppPurchaseConnection.enablePendingPurchases();
|
|
InAppPurchaseConnection.enablePendingPurchases();
|
|
CryptoUtil.init();
|
|
CryptoUtil.init();
|
|
@@ -142,9 +143,11 @@ Future<void> _init(bool isBackground) async {
|
|
_logger.info("Initialization done");
|
|
_logger.info("Initialization done");
|
|
}
|
|
}
|
|
|
|
|
|
-Future<void> _sync() async {
|
|
|
|
|
|
+Future<void> _sync(String caller) async {
|
|
if (!AppLifecycleService.instance.isForeground) {
|
|
if (!AppLifecycleService.instance.isForeground) {
|
|
- _logger.info("Syncing in background");
|
|
|
|
|
|
+ _logger.info("Syncing in background caller $caller");
|
|
|
|
+ } else {
|
|
|
|
+ _logger.info("Syncing in foreground caller $caller");
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
await SyncService.instance.sync();
|
|
await SyncService.instance.sync();
|
|
@@ -174,16 +177,16 @@ Future<void> _scheduleHeartBeat(bool isBackground) async {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
-Future<void> _scheduleFGSync() async {
|
|
|
|
- await _sync();
|
|
|
|
|
|
+Future<void> _scheduleFGSync(String caller) async {
|
|
|
|
+ await _sync(caller);
|
|
Future.delayed(kFGSyncFrequency, () async {
|
|
Future.delayed(kFGSyncFrequency, () async {
|
|
- _scheduleFGSync();
|
|
|
|
|
|
+ _scheduleFGSync('fgSyncCron');
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
void _scheduleBGTaskKill(String taskId) async {
|
|
void _scheduleBGTaskKill(String taskId) async {
|
|
if (await _isRunningInForeground()) {
|
|
if (await _isRunningInForeground()) {
|
|
- _logger.info("Found app in FG, committing seppuku.");
|
|
|
|
|
|
+ _logger.info("Found app in FG, committing seppuku. $taskId");
|
|
await _killBGTask(taskId);
|
|
await _killBGTask(taskId);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -208,14 +211,16 @@ Future<void> _killBGTask([String taskId]) async {
|
|
if (taskId != null) {
|
|
if (taskId != null) {
|
|
BackgroundFetch.finish(taskId);
|
|
BackgroundFetch.finish(taskId);
|
|
}
|
|
}
|
|
- Isolate.current.kill(priority: Isolate.immediate);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
|
|
+ bool isRunningInFG = await _isRunningInForeground(); // hb
|
|
|
|
+ bool isInForeground = AppLifecycleService.instance.isForeground;
|
|
if (_isProcessRunning) {
|
|
if (_isProcessRunning) {
|
|
- _logger.info("Background push received when app is alive");
|
|
|
|
|
|
+ _logger.info(
|
|
|
|
+ "Background push received when app is alive and runningInFS: $isRunningInFG inForeground: $isInForeground");
|
|
if (PushService.shouldSync(message)) {
|
|
if (PushService.shouldSync(message)) {
|
|
- await _sync();
|
|
|
|
|
|
+ await _sync('firebaseBgSyncActiveProcess');
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
// App is dead
|
|
// App is dead
|
|
@@ -224,17 +229,30 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
if (Platform.isIOS) {
|
|
if (Platform.isIOS) {
|
|
_scheduleSuicide(kBGPushTimeout); // To prevent OS from punishing us
|
|
_scheduleSuicide(kBGPushTimeout); // To prevent OS from punishing us
|
|
}
|
|
}
|
|
- await _init(true);
|
|
|
|
|
|
+ await _init(true, via: 'firebasePush');
|
|
if (PushService.shouldSync(message)) {
|
|
if (PushService.shouldSync(message)) {
|
|
- await _sync();
|
|
|
|
|
|
+ await _sync('firebaseBgSyncNoActiveProcess');
|
|
}
|
|
}
|
|
- }, prefix: "[bg]");
|
|
|
|
|
|
+ }, prefix: "[fbg]");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+Future<void> _logFGHeartBeatInfo() async {
|
|
|
|
+ bool isRunningInFG = await _isRunningInForeground();
|
|
|
|
+ final prefs = await SharedPreferences.getInstance();
|
|
|
|
+ await prefs.reload();
|
|
|
|
+ var lastFGTaskHeartBeatTime = prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0;
|
|
|
|
+ String lastRun = lastFGTaskHeartBeatTime == 0
|
|
|
|
+ ? 'never'
|
|
|
|
+ : DateTime.fromMicrosecondsSinceEpoch(lastFGTaskHeartBeatTime).toString();
|
|
|
|
+ _logger.info('isAlreaduunningFG: $isRunningInFG, last Beat: $lastRun');
|
|
|
|
+}
|
|
|
|
+
|
|
void _scheduleSuicide(Duration duration, [String taskID]) {
|
|
void _scheduleSuicide(Duration duration, [String taskID]) {
|
|
|
|
+ var taskIDVal = taskID ?? 'no taskID';
|
|
|
|
+ _logger.warning("Schedule seppuku taskID: $taskIDVal");
|
|
Future.delayed(duration, () {
|
|
Future.delayed(duration, () {
|
|
- _logger.warning("TLE, committing seppuku");
|
|
|
|
|
|
+ _logger.warning("TLE, committing seppuku for taskID: $taskIDVal");
|
|
_killBGTask(taskID);
|
|
_killBGTask(taskID);
|
|
});
|
|
});
|
|
}
|
|
}
|