2020-05-02 11:46:59 +00:00
|
|
|
import 'dart:async';
|
2021-10-22 17:42:35 +00:00
|
|
|
import 'dart:io';
|
2021-10-18 12:30:39 +00:00
|
|
|
import 'dart:isolate';
|
2020-05-02 11:46:59 +00:00
|
|
|
|
2021-01-13 17:39:45 +00:00
|
|
|
import 'package:background_fetch/background_fetch.dart';
|
2021-10-13 05:24:49 +00:00
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
2021-02-09 11:35:45 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-03-24 19:59:36 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-01-08 17:11:32 +00:00
|
|
|
import 'package:in_app_purchase/in_app_purchase.dart';
|
2021-07-21 20:47:43 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-05-02 16:28:54 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2021-11-24 07:26:07 +00:00
|
|
|
import 'package:photos/app.dart';
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2021-07-21 20:47:43 +00:00
|
|
|
import 'package:photos/core/constants.dart';
|
2021-04-04 17:19:46 +00:00
|
|
|
import 'package:photos/core/network.dart';
|
2021-05-07 17:26:51 +00:00
|
|
|
import 'package:photos/db/upload_locks_db.dart';
|
2021-11-15 14:29:31 +00:00
|
|
|
import 'package:photos/services/app_lifecycle_service.dart';
|
2021-01-08 06:38:45 +00:00
|
|
|
import 'package:photos/services/billing_service.dart';
|
2020-10-26 09:57:31 +00:00
|
|
|
import 'package:photos/services/collections_service.dart';
|
2021-08-24 06:51:02 +00:00
|
|
|
import 'package:photos/services/feature_flag_service.dart';
|
2021-06-14 14:58:36 +00:00
|
|
|
import 'package:photos/services/local_sync_service.dart';
|
2020-10-03 17:56:18 +00:00
|
|
|
import 'package:photos/services/memories_service.dart';
|
2021-05-20 23:23:29 +00:00
|
|
|
import 'package:photos/services/notification_service.dart';
|
2021-10-12 09:12:28 +00:00
|
|
|
import 'package:photos/services/push_service.dart';
|
2021-06-14 16:24:15 +00:00
|
|
|
import 'package:photos/services/remote_sync_service.dart';
|
2020-10-03 17:58:26 +00:00
|
|
|
import 'package:photos/services/sync_service.dart';
|
2021-10-12 19:27:11 +00:00
|
|
|
import 'package:photos/services/trash_sync_service.dart';
|
2021-05-22 18:29:09 +00:00
|
|
|
import 'package:photos/services/update_service.dart';
|
2021-03-21 08:32:10 +00:00
|
|
|
import 'package:photos/ui/app_lock.dart';
|
|
|
|
import 'package:photos/ui/lock_screen.dart';
|
2021-01-26 11:46:14 +00:00
|
|
|
import 'package:photos/utils/crypto_util.dart';
|
2021-03-03 16:07:02 +00:00
|
|
|
import 'package:photos/utils/file_uploader.dart';
|
2021-09-08 20:06:37 +00:00
|
|
|
import 'package:photos/utils/local_settings.dart';
|
2021-05-07 17:26:51 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2020-05-03 20:27:06 +00:00
|
|
|
import 'package:super_logging/super_logging.dart';
|
2021-08-15 19:35:16 +00:00
|
|
|
|
2020-08-21 23:28:52 +00:00
|
|
|
final _logger = Logger("main");
|
2021-03-01 22:09:15 +00:00
|
|
|
|
|
|
|
Completer<void> _initializationStatus;
|
2021-05-07 17:26:51 +00:00
|
|
|
const kLastBGTaskHeartBeatTime = "bg_task_hb_time";
|
|
|
|
const kLastFGTaskHeartBeatTime = "fg_task_hb_time";
|
|
|
|
const kHeartBeatFrequency = Duration(seconds: 1);
|
2021-05-08 17:53:17 +00:00
|
|
|
const kFGSyncFrequency = Duration(minutes: 5);
|
2021-10-22 17:42:35 +00:00
|
|
|
const kBGTaskTimeout = Duration(seconds: 25);
|
|
|
|
const kBGPushTimeout = Duration(seconds: 28);
|
2021-05-07 17:26:51 +00:00
|
|
|
const kFGTaskDeathTimeoutInMicroseconds = 5000000;
|
2020-03-24 19:59:36 +00:00
|
|
|
|
|
|
|
void main() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2021-03-01 23:36:58 +00:00
|
|
|
await _runInForeground();
|
2021-03-03 16:07:15 +00:00
|
|
|
BackgroundFetch.registerHeadlessTask(_headlessTaskHandler);
|
2020-05-02 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 23:36:58 +00:00
|
|
|
Future<void> _runInForeground() async {
|
|
|
|
return await _runWithLogs(() async {
|
2021-03-01 22:09:15 +00:00
|
|
|
_logger.info("Starting app in foreground");
|
2021-03-03 16:03:03 +00:00
|
|
|
await _init(false);
|
2021-05-08 17:53:17 +00:00
|
|
|
_scheduleFGSync();
|
2021-03-21 08:32:10 +00:00
|
|
|
runApp(AppLock(
|
2021-11-25 10:22:12 +00:00
|
|
|
builder: (args) => EnteApp(_runBackgroundTask, _killBGTask),
|
2021-03-21 08:32:10 +00:00
|
|
|
lockScreen: LockScreen(),
|
|
|
|
enabled: Configuration.instance.shouldShowLockScreen(),
|
|
|
|
themeData: themeData,
|
|
|
|
));
|
2021-03-01 22:09:15 +00:00
|
|
|
});
|
|
|
|
}
|
2020-05-02 11:46:59 +00:00
|
|
|
|
2021-11-27 15:34:31 +00:00
|
|
|
Future _runBackgroundTask(String taskId) async {
|
2021-11-27 15:42:52 +00:00
|
|
|
_runWithLogs(() async {
|
2021-11-27 15:34:31 +00:00
|
|
|
_runInBackground(taskId);
|
2021-11-27 15:42:52 +00:00
|
|
|
}, prefix: "[bg]");
|
2021-05-07 17:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 15:34:31 +00:00
|
|
|
Future<void> _runInBackground(String taskId) async {
|
2021-05-07 17:26:51 +00:00
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
if (await _isRunningInForeground()) {
|
|
|
|
_logger.info("FG task running, skipping BG task");
|
2021-03-01 22:09:15 +00:00
|
|
|
BackgroundFetch.finish(taskId);
|
2021-05-07 17:26:51 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_logger.info("FG task is not running");
|
2021-03-01 22:09:15 +00:00
|
|
|
}
|
2021-05-07 17:26:51 +00:00
|
|
|
_logger.info("[BackgroundFetch] Event received: $taskId");
|
|
|
|
_scheduleBGTaskKill(taskId);
|
2021-10-22 17:42:35 +00:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
_scheduleSuicide(kBGTaskTimeout); // To prevent OS from punishing us
|
|
|
|
}
|
2021-05-07 17:26:51 +00:00
|
|
|
await _init(true);
|
2021-05-22 21:26:11 +00:00
|
|
|
UpdateService.instance.showUpdateNotification();
|
2021-11-15 15:35:07 +00:00
|
|
|
await _sync();
|
2021-05-07 17:26:51 +00:00
|
|
|
BackgroundFetch.finish(taskId);
|
2021-03-01 22:09:15 +00:00
|
|
|
}
|
2020-05-02 11:46:59 +00:00
|
|
|
|
2021-03-01 22:09:15 +00:00
|
|
|
void _headlessTaskHandler(HeadlessTask task) {
|
|
|
|
if (task.timeout) {
|
|
|
|
BackgroundFetch.finish(task.taskId);
|
|
|
|
} else {
|
|
|
|
_runInBackground(task.taskId);
|
|
|
|
}
|
2021-01-13 17:39:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 16:03:03 +00:00
|
|
|
Future<void> _init(bool isBackground) async {
|
2021-03-01 22:09:15 +00:00
|
|
|
if (_initializationStatus != null) {
|
|
|
|
return _initializationStatus.future;
|
|
|
|
}
|
|
|
|
_initializationStatus = Completer<void>();
|
2021-03-02 06:08:29 +00:00
|
|
|
_logger.info("Initializing...");
|
2021-11-15 15:35:07 +00:00
|
|
|
_scheduleHeartBeat(isBackground);
|
|
|
|
if (isBackground) {
|
|
|
|
AppLifecycleService.instance.onAppInBackground();
|
|
|
|
} else {
|
|
|
|
AppLifecycleService.instance.onAppInForeground();
|
|
|
|
}
|
2021-01-13 17:39:45 +00:00
|
|
|
InAppPurchaseConnection.enablePendingPurchases();
|
2021-01-26 11:46:14 +00:00
|
|
|
CryptoUtil.init();
|
2021-05-20 23:23:29 +00:00
|
|
|
await NotificationService.instance.init();
|
2021-04-04 17:19:46 +00:00
|
|
|
await Network.instance.init();
|
2021-01-13 17:39:45 +00:00
|
|
|
await Configuration.instance.init();
|
2021-05-22 18:29:09 +00:00
|
|
|
await UpdateService.instance.init();
|
2021-01-13 19:41:32 +00:00
|
|
|
await BillingService.instance.init();
|
2021-01-13 17:39:45 +00:00
|
|
|
await CollectionsService.instance.init();
|
2021-03-03 16:07:02 +00:00
|
|
|
await FileUploader.instance.init(isBackground);
|
2021-11-15 15:35:07 +00:00
|
|
|
await LocalSyncService.instance.init();
|
2021-10-12 19:27:11 +00:00
|
|
|
await TrashSyncService.instance.init();
|
2021-11-15 15:35:07 +00:00
|
|
|
await RemoteSyncService.instance.init();
|
2021-06-14 14:58:36 +00:00
|
|
|
await SyncService.instance.init();
|
2021-01-13 17:39:45 +00:00
|
|
|
await MemoriesService.instance.init();
|
2021-09-09 05:53:51 +00:00
|
|
|
await LocalSettings.instance.init();
|
2021-11-25 10:35:38 +00:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
PushService.instance.init().then((_) {
|
|
|
|
FirebaseMessaging.onBackgroundMessage(
|
|
|
|
_firebaseMessagingBackgroundHandler);
|
|
|
|
});
|
|
|
|
}
|
2021-08-24 07:03:27 +00:00
|
|
|
FeatureFlagService.instance.init();
|
2021-02-06 16:11:27 +00:00
|
|
|
_logger.info("Initialization done");
|
2021-03-01 22:09:15 +00:00
|
|
|
_initializationStatus.complete();
|
2020-05-02 11:46:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:35:07 +00:00
|
|
|
Future<void> _sync() async {
|
|
|
|
if (!AppLifecycleService.instance.isForeground) {
|
2021-02-26 14:09:45 +00:00
|
|
|
_logger.info("Syncing in background");
|
|
|
|
}
|
2021-01-13 17:39:45 +00:00
|
|
|
try {
|
2021-03-03 16:03:03 +00:00
|
|
|
await SyncService.instance.sync();
|
2021-01-13 17:39:45 +00:00
|
|
|
} catch (e, s) {
|
2020-10-12 20:34:34 +00:00
|
|
|
_logger.severe("Sync error", e, s);
|
2021-01-13 17:39:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 16:03:03 +00:00
|
|
|
Future _runWithLogs(Function() function, {String prefix = ""}) async {
|
2021-01-13 17:39:45 +00:00
|
|
|
await SuperLogging.main(LogConfig(
|
|
|
|
body: function,
|
|
|
|
logDirPath: (await getTemporaryDirectory()).path + "/logs",
|
|
|
|
maxLogFiles: 5,
|
2021-07-21 20:47:43 +00:00
|
|
|
sentryDsn: kDebugMode ? kSentryDebugDSN : kSentryDSN,
|
2021-03-01 22:09:15 +00:00
|
|
|
enableInDebugMode: true,
|
2021-03-03 16:03:03 +00:00
|
|
|
prefix: prefix,
|
2021-01-13 17:39:45 +00:00
|
|
|
));
|
2020-06-15 18:38:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 17:26:51 +00:00
|
|
|
Future<void> _scheduleHeartBeat(bool isBackground) async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setInt(
|
|
|
|
isBackground ? kLastBGTaskHeartBeatTime : kLastFGTaskHeartBeatTime,
|
|
|
|
DateTime.now().microsecondsSinceEpoch);
|
|
|
|
Future.delayed(kHeartBeatFrequency, () async {
|
2021-05-08 17:53:17 +00:00
|
|
|
_scheduleHeartBeat(isBackground);
|
2021-05-07 17:26:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-08 17:53:17 +00:00
|
|
|
Future<void> _scheduleFGSync() async {
|
|
|
|
await _sync();
|
|
|
|
Future.delayed(kFGSyncFrequency, () async {
|
|
|
|
_scheduleFGSync();
|
|
|
|
});
|
2021-05-07 17:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _scheduleBGTaskKill(String taskId) async {
|
|
|
|
if (await _isRunningInForeground()) {
|
|
|
|
_logger.info("Found app in FG, committing seppuku.");
|
2021-05-07 17:44:19 +00:00
|
|
|
await _killBGTask(taskId);
|
2021-05-07 17:26:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Future.delayed(kHeartBeatFrequency, () async {
|
|
|
|
_scheduleBGTaskKill(taskId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-08 17:53:17 +00:00
|
|
|
Future<bool> _isRunningInForeground() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.reload();
|
|
|
|
final currentTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
|
return (prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0) >
|
|
|
|
(currentTime - kFGTaskDeathTimeoutInMicroseconds);
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:42:35 +00:00
|
|
|
Future<void> _killBGTask([String taskId]) async {
|
2021-05-07 17:44:19 +00:00
|
|
|
await UploadLocksDB.instance.releaseLocksAcquiredByOwnerBefore(
|
2021-05-08 14:17:11 +00:00
|
|
|
ProcessType.background.toString(), DateTime.now().microsecondsSinceEpoch);
|
2021-05-07 17:44:19 +00:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.remove(kLastBGTaskHeartBeatTime);
|
2021-10-22 17:42:35 +00:00
|
|
|
if (taskId != null) {
|
|
|
|
BackgroundFetch.finish(taskId);
|
|
|
|
}
|
2021-10-18 12:30:39 +00:00
|
|
|
Isolate.current.kill(priority: Isolate.immediate);
|
2021-05-07 17:44:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 05:24:49 +00:00
|
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
|
|
if (_initializationStatus == null) {
|
|
|
|
// App is dead
|
|
|
|
_runWithLogs(() async {
|
|
|
|
_logger.info("Background push received");
|
2021-10-22 17:42:35 +00:00
|
|
|
if (Platform.isIOS) {
|
|
|
|
_scheduleSuicide(kBGPushTimeout); // To prevent OS from punishing us
|
|
|
|
}
|
2021-10-13 05:24:49 +00:00
|
|
|
await _init(true);
|
2021-11-15 14:21:43 +00:00
|
|
|
if (PushService.shouldSync(message)) {
|
2021-11-15 15:35:07 +00:00
|
|
|
await _sync();
|
2021-11-15 14:21:43 +00:00
|
|
|
}
|
2021-10-13 05:24:49 +00:00
|
|
|
}, prefix: "[bg]");
|
|
|
|
} else {
|
2021-11-10 19:14:25 +00:00
|
|
|
_logger.info("Background push received when app is alive");
|
2021-11-15 14:21:43 +00:00
|
|
|
if (PushService.shouldSync(message)) {
|
2021-11-15 15:35:07 +00:00
|
|
|
await _sync();
|
2021-11-15 14:21:43 +00:00
|
|
|
}
|
2021-10-13 05:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-22 17:42:35 +00:00
|
|
|
|
|
|
|
void _scheduleSuicide(Duration duration, [String taskID]) {
|
|
|
|
Future.delayed(duration, () {
|
2021-11-25 10:29:29 +00:00
|
|
|
_logger.warning("TLE, committing seppuku");
|
2021-10-22 17:42:35 +00:00
|
|
|
_killBGTask(taskID);
|
|
|
|
});
|
|
|
|
}
|