瀏覽代碼

Merge remote-tracking branch 'origin/master' into theme

Neeraj Gupta 3 年之前
父節點
當前提交
150c8177b1

+ 6 - 3
lib/app.dart

@@ -133,6 +133,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
 
   @override
   void initState() {
+    _logger.info('init App');
     super.initState();
     WidgetsBinding.instance.addObserver(this);
     _configureBackgroundFetch();
@@ -170,11 +171,13 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
 
   @override
   void didChangeAppLifecycleState(AppLifecycleState state) {
+    final String stateChangeReason = 'app -> $state';
     if (state == AppLifecycleState.resumed) {
-      AppLifecycleService.instance.onAppInForeground();
+      AppLifecycleService.instance
+          .onAppInForeground(stateChangeReason + ': sync now');
       SyncService.instance.sync();
     } else {
-      AppLifecycleService.instance.onAppInBackground();
+      AppLifecycleService.instance.onAppInBackground(stateChangeReason);
     }
   }
 
@@ -194,7 +197,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
         ), (String taskId) async {
       await widget.runBackgroundTask(taskId);
     }, (taskId) {
-      _logger.info("BG task timeout");
+      _logger.info("BG task timeout taskID: $taskId");
       widget.killBackgroundTask(taskId);
     }).then((int status) {
       _logger.info('[BackgroundFetch] configure success: $status');

+ 43 - 25
lib/main.dart

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

+ 4 - 4
lib/services/app_lifecycle_service.dart

@@ -10,13 +10,13 @@ class AppLifecycleService {
 
   AppLifecycleService._privateConstructor();
 
-  void onAppInForeground() {
-    _logger.info("App in foreground");
+  void onAppInForeground(String reason) {
+    _logger.info("App in foreground via $reason");
     isForeground = true;
   }
 
-  void onAppInBackground() {
-    _logger.info("App in background");
+  void onAppInBackground(String reason) {
+    _logger.info("App in background $reason");
     isForeground = false;
   }
 }

+ 1 - 0
lib/ui/share_collection_widget.dart

@@ -138,6 +138,7 @@ class _SharingDialogState extends State<SharingDialog> {
             ],
           ),
         ),
+        Padding(padding: EdgeInsets.all(8)),
       ]);
       if (widget.collection.publicURLs?.isNotEmpty ?? false) {
         children.add(Padding(

+ 1 - 1
pubspec.lock

@@ -397,7 +397,7 @@ packages:
       name: flutter_email_sender
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "5.1.0"
+    version: "5.0.2"
   flutter_image_compress:
     dependency: "direct main"
     description:

+ 2 - 2
pubspec.yaml

@@ -11,7 +11,7 @@ description: ente photos application
 # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
 # Read more about iOS versioning at
 # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
-version: 0.5.6+285
+version: 0.5.10+290
 
 environment:
   sdk: ">=2.10.0 <3.0.0"
@@ -49,7 +49,7 @@ dependencies:
   flutter_cache_manager: ^3.3.0
   flutter_datetime_picker: ^1.5.1
   flutter_easyloading: ^3.0.0
-  flutter_email_sender: ^5.1.0
+  flutter_email_sender: 5.0.2
   flutter_image_compress: ^1.1.0
   flutter_inappwebview: ^5.3.2
   flutter_local_notifications: ^5.0.0+4

+ 1 - 1
thirdparty/super_logging/lib/super_logging.dart

@@ -162,7 +162,7 @@ class SuperLogging {
       $.info("detected debug mode; sentry & file logging disabled.");
     }
     if (fileIsEnabled) {
-      $.info("using this log file for today: $logFile");
+      $.info("log file for today: $logFile with prefix ${config.prefix}");
     }
     if (sentryIsEnabled) {
       $.info("sentry uploader started");