2020-05-02 17:16:59 +05:30
|
|
|
import 'dart:async';
|
2021-10-22 23:12:35 +05:30
|
|
|
import 'dart:io';
|
2020-05-02 17:16:59 +05:30
|
|
|
|
2021-01-13 23:09:45 +05:30
|
|
|
import 'package:background_fetch/background_fetch.dart';
|
2021-10-13 10:54:49 +05:30
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
2021-02-09 17:05:45 +05:30
|
|
|
import 'package:flutter/foundation.dart';
|
2020-03-25 01:29:36 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2023-02-22 12:57:34 +05:30
|
|
|
import "package:flutter/rendering.dart";
|
2021-07-22 02:17:43 +05:30
|
|
|
import 'package:logging/logging.dart';
|
2020-05-02 21:58:54 +05:30
|
|
|
import 'package:path_provider/path_provider.dart';
|
2021-11-24 12:56:07 +05:30
|
|
|
import 'package:photos/app.dart';
|
2020-05-05 01:38:20 +05:30
|
|
|
import 'package:photos/core/configuration.dart';
|
2021-07-22 02:17:43 +05:30
|
|
|
import 'package:photos/core/constants.dart';
|
2022-05-29 03:46:22 +05:30
|
|
|
import 'package:photos/core/error-reporting/super_logging.dart';
|
2023-01-11 13:55:26 +05:30
|
|
|
import 'package:photos/core/errors.dart';
|
2023-02-03 13:09:04 +05:30
|
|
|
import 'package:photos/core/network/network.dart';
|
2021-05-07 22:56:51 +05:30
|
|
|
import 'package:photos/db/upload_locks_db.dart';
|
2022-06-21 20:07:17 +05:30
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2021-11-15 19:59:31 +05:30
|
|
|
import 'package:photos/services/app_lifecycle_service.dart';
|
2021-01-08 12:08:45 +05:30
|
|
|
import 'package:photos/services/billing_service.dart';
|
2020-10-26 15:27:31 +05:30
|
|
|
import 'package:photos/services/collections_service.dart';
|
2022-11-17 11:00:23 +05:30
|
|
|
import 'package:photos/services/favorites_service.dart';
|
2021-08-24 12:21:02 +05:30
|
|
|
import 'package:photos/services/feature_flag_service.dart';
|
2022-08-01 16:35:16 +05:30
|
|
|
import 'package:photos/services/local_file_update_service.dart';
|
2021-06-14 20:28:36 +05:30
|
|
|
import 'package:photos/services/local_sync_service.dart';
|
2020-10-03 23:26:18 +05:30
|
|
|
import 'package:photos/services/memories_service.dart';
|
2021-05-21 04:53:29 +05:30
|
|
|
import 'package:photos/services/notification_service.dart';
|
2023-02-08 19:10:18 +05:30
|
|
|
import "package:photos/services/object_detection/object_detection_service.dart";
|
2021-10-12 14:42:28 +05:30
|
|
|
import 'package:photos/services/push_service.dart';
|
2021-06-14 21:54:15 +05:30
|
|
|
import 'package:photos/services/remote_sync_service.dart';
|
2022-08-06 18:05:29 +05:30
|
|
|
import 'package:photos/services/search_service.dart';
|
2023-02-15 22:17:40 +05:30
|
|
|
import "package:photos/services/storage_bonus_service.dart";
|
2020-10-03 23:28:26 +05:30
|
|
|
import 'package:photos/services/sync_service.dart';
|
2021-10-13 00:57:11 +05:30
|
|
|
import 'package:photos/services/trash_sync_service.dart';
|
2021-05-22 23:59:09 +05:30
|
|
|
import 'package:photos/services/update_service.dart';
|
2022-09-20 11:35:34 +05:30
|
|
|
import 'package:photos/services/user_remote_flag_service.dart';
|
2022-06-23 18:40:35 +05:30
|
|
|
import 'package:photos/services/user_service.dart';
|
2022-07-01 20:09:02 +05:30
|
|
|
import 'package:photos/ui/tools/app_lock.dart';
|
|
|
|
import 'package:photos/ui/tools/lock_screen.dart';
|
2021-01-26 17:16:14 +05:30
|
|
|
import 'package:photos/utils/crypto_util.dart';
|
2021-03-03 21:37:02 +05:30
|
|
|
import 'package:photos/utils/file_uploader.dart';
|
2021-09-09 01:36:37 +05:30
|
|
|
import 'package:photos/utils/local_settings.dart';
|
2021-05-07 22:56:51 +05:30
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-08-16 01:05:16 +05:30
|
|
|
|
2020-08-22 04:58:52 +05:30
|
|
|
final _logger = Logger("main");
|
2021-03-02 03:39:15 +05:30
|
|
|
|
2021-11-27 21:15:56 +05:30
|
|
|
bool _isProcessRunning = false;
|
2021-05-07 22:56:51 +05:30
|
|
|
const kLastBGTaskHeartBeatTime = "bg_task_hb_time";
|
|
|
|
const kLastFGTaskHeartBeatTime = "fg_task_hb_time";
|
|
|
|
const kHeartBeatFrequency = Duration(seconds: 1);
|
2021-05-08 23:23:17 +05:30
|
|
|
const kFGSyncFrequency = Duration(minutes: 5);
|
2021-10-22 23:12:35 +05:30
|
|
|
const kBGTaskTimeout = Duration(seconds: 25);
|
|
|
|
const kBGPushTimeout = Duration(seconds: 28);
|
2021-05-07 22:56:51 +05:30
|
|
|
const kFGTaskDeathTimeoutInMicroseconds = 5000000;
|
2022-12-27 14:26:55 +05:30
|
|
|
const kBackgroundLockLatency = Duration(seconds: 3);
|
2020-03-25 01:29:36 +05:30
|
|
|
|
|
|
|
void main() async {
|
2023-02-22 12:57:34 +05:30
|
|
|
debugRepaintRainbowEnabled = false;
|
2020-03-25 01:29:36 +05:30
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2021-03-02 05:06:58 +05:30
|
|
|
await _runInForeground();
|
2021-03-03 21:37:15 +05:30
|
|
|
BackgroundFetch.registerHeadlessTask(_headlessTaskHandler);
|
2020-05-02 21:58:54 +05:30
|
|
|
}
|
|
|
|
|
2021-03-02 05:06:58 +05:30
|
|
|
Future<void> _runInForeground() async {
|
|
|
|
return await _runWithLogs(() async {
|
2021-03-02 03:39:15 +05:30
|
|
|
_logger.info("Starting app in foreground");
|
2022-03-08 01:48:50 +05:30
|
|
|
await _init(false, via: 'mainMethod');
|
2022-11-06 16:06:33 +05:30
|
|
|
unawaited(_scheduleFGSync('appStart in FG'));
|
2022-06-11 13:53:52 +05:30
|
|
|
runApp(
|
|
|
|
AppLock(
|
2022-07-04 14:13:01 +05:30
|
|
|
builder: (args) => const EnteApp(_runBackgroundTask, _killBGTask),
|
|
|
|
lockScreen: const LockScreen(),
|
2022-06-11 13:53:52 +05:30
|
|
|
enabled: Configuration.instance.shouldShowLockScreen(),
|
|
|
|
lightTheme: lightThemeData,
|
|
|
|
darkTheme: darkThemeData,
|
2022-12-27 14:26:55 +05:30
|
|
|
backgroundLockLatency: kBackgroundLockLatency,
|
2022-06-11 13:53:52 +05:30
|
|
|
),
|
|
|
|
);
|
2021-03-02 03:39:15 +05:30
|
|
|
});
|
|
|
|
}
|
2020-05-02 17:16:59 +05:30
|
|
|
|
2022-10-18 13:38:09 +05:30
|
|
|
Future<void> _runBackgroundTask(String taskId, {String mode = 'normal'}) async {
|
2022-07-05 22:51:06 +05:30
|
|
|
if (_isProcessRunning) {
|
2021-11-28 10:52:14 +05:30
|
|
|
_logger.info("Background task triggered when process was already running");
|
2022-03-08 12:47:19 +05:30
|
|
|
await _sync('bgTaskActiveProcess');
|
2021-11-28 10:52:14 +05:30
|
|
|
BackgroundFetch.finish(taskId);
|
|
|
|
} else {
|
2022-06-11 13:53:52 +05:30
|
|
|
_runWithLogs(
|
|
|
|
() async {
|
2022-10-18 13:38:09 +05:30
|
|
|
_logger.info("Starting background task in $mode mode");
|
2022-06-11 13:53:52 +05:30
|
|
|
_runInBackground(taskId);
|
|
|
|
},
|
|
|
|
prefix: "[bg]",
|
|
|
|
);
|
2021-11-28 10:52:14 +05:30
|
|
|
}
|
2021-05-07 22:56:51 +05:30
|
|
|
}
|
|
|
|
|
2021-11-27 21:04:31 +05:30
|
|
|
Future<void> _runInBackground(String taskId) async {
|
2022-07-04 11:32:17 +05:30
|
|
|
await Future.delayed(const Duration(seconds: 3));
|
2021-05-07 22:56:51 +05:30
|
|
|
if (await _isRunningInForeground()) {
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.info("FG task running, skipping BG taskID: $taskId");
|
2021-03-02 03:39:15 +05:30
|
|
|
BackgroundFetch.finish(taskId);
|
2021-05-07 22:56:51 +05:30
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_logger.info("FG task is not running");
|
2021-03-02 03:39:15 +05:30
|
|
|
}
|
2021-05-07 22:56:51 +05:30
|
|
|
_logger.info("[BackgroundFetch] Event received: $taskId");
|
|
|
|
_scheduleBGTaskKill(taskId);
|
2021-10-22 23:12:35 +05:30
|
|
|
if (Platform.isIOS) {
|
2022-03-08 01:48:50 +05:30
|
|
|
_scheduleSuicide(kBGTaskTimeout, taskId); // To prevent OS from punishing us
|
2021-10-22 23:12:35 +05:30
|
|
|
}
|
2022-03-08 01:48:50 +05:30
|
|
|
await _init(true, via: 'runViaBackgroundTask');
|
2021-05-23 02:56:11 +05:30
|
|
|
UpdateService.instance.showUpdateNotification();
|
2022-03-08 12:47:19 +05:30
|
|
|
await _sync('bgSync');
|
2021-05-07 22:56:51 +05:30
|
|
|
BackgroundFetch.finish(taskId);
|
2021-03-02 03:39:15 +05:30
|
|
|
}
|
2020-05-02 17:16:59 +05:30
|
|
|
|
2022-10-15 12:25:03 +05:30
|
|
|
// https://stackoverflow.com/a/73796478/546896
|
|
|
|
@pragma('vm:entry-point')
|
2021-03-02 03:39:15 +05:30
|
|
|
void _headlessTaskHandler(HeadlessTask task) {
|
2022-10-18 13:30:29 +05:30
|
|
|
print("_headlessTaskHandler");
|
2021-03-02 03:39:15 +05:30
|
|
|
if (task.timeout) {
|
|
|
|
BackgroundFetch.finish(task.taskId);
|
|
|
|
} else {
|
2022-10-18 13:38:09 +05:30
|
|
|
_runBackgroundTask(task.taskId, mode: "headless");
|
2021-03-02 03:39:15 +05:30
|
|
|
}
|
2021-01-13 23:09:45 +05:30
|
|
|
}
|
|
|
|
|
2022-03-08 01:48:50 +05:30
|
|
|
Future<void> _init(bool isBackground, {String via = ''}) async {
|
2021-11-27 21:15:56 +05:30
|
|
|
_isProcessRunning = true;
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.info("Initializing... inBG =$isBackground via: $via");
|
2022-11-06 11:44:46 +05:30
|
|
|
final SharedPreferences preferences = await SharedPreferences.getInstance();
|
2022-03-08 01:48:50 +05:30
|
|
|
await _logFGHeartBeatInfo();
|
2022-11-06 11:44:46 +05:30
|
|
|
_scheduleHeartBeat(preferences, isBackground);
|
2021-11-15 21:05:07 +05:30
|
|
|
if (isBackground) {
|
2022-03-08 01:48:50 +05:30
|
|
|
AppLifecycleService.instance.onAppInBackground('init via: $via');
|
2021-11-15 21:05:07 +05:30
|
|
|
} else {
|
2022-03-08 01:48:50 +05:30
|
|
|
AppLifecycleService.instance.onAppInForeground('init via: $via');
|
2021-11-15 21:05:07 +05:30
|
|
|
}
|
2021-01-26 17:16:14 +05:30
|
|
|
CryptoUtil.init();
|
2021-05-21 04:53:29 +05:30
|
|
|
await NotificationService.instance.init();
|
2023-02-03 13:09:04 +05:30
|
|
|
await NetworkClient.instance.init();
|
2021-01-13 23:09:45 +05:30
|
|
|
await Configuration.instance.init();
|
2022-06-23 18:47:31 +05:30
|
|
|
await UserService.instance.init();
|
2022-09-20 11:35:34 +05:30
|
|
|
await UserRemoteFlagService.instance.init();
|
2021-05-22 23:59:09 +05:30
|
|
|
await UpdateService.instance.init();
|
2022-11-06 11:44:46 +05:30
|
|
|
BillingService.instance.init();
|
|
|
|
await CollectionsService.instance.init(preferences);
|
2022-11-17 11:00:23 +05:30
|
|
|
FavoritesService.instance.initFav().ignore();
|
2022-11-06 11:44:46 +05:30
|
|
|
await FileUploader.instance.init(preferences, isBackground);
|
|
|
|
await LocalSyncService.instance.init(preferences);
|
|
|
|
TrashSyncService.instance.init(preferences);
|
|
|
|
RemoteSyncService.instance.init(preferences);
|
|
|
|
await SyncService.instance.init(preferences);
|
|
|
|
MemoriesService.instance.init();
|
|
|
|
LocalSettings.instance.init(preferences);
|
|
|
|
LocalFileUpdateService.instance.init(preferences);
|
|
|
|
SearchService.instance.init();
|
2023-02-15 22:17:40 +05:30
|
|
|
StorageBonusService.instance.init(preferences);
|
2021-11-25 16:05:38 +05:30
|
|
|
if (Platform.isIOS) {
|
|
|
|
PushService.instance.init().then((_) {
|
|
|
|
FirebaseMessaging.onBackgroundMessage(
|
2022-06-11 13:53:52 +05:30
|
|
|
_firebaseMessagingBackgroundHandler,
|
|
|
|
);
|
2021-11-25 16:05:38 +05:30
|
|
|
});
|
|
|
|
}
|
2021-08-24 12:33:27 +05:30
|
|
|
FeatureFlagService.instance.init();
|
2023-02-18 16:53:02 +05:30
|
|
|
if (FeatureFlagService.instance.isInternalUserOrDebugBuild()) {
|
|
|
|
await ObjectDetectionService.instance.init();
|
|
|
|
}
|
2021-02-06 21:41:27 +05:30
|
|
|
_logger.info("Initialization done");
|
2020-05-02 17:16:59 +05:30
|
|
|
}
|
|
|
|
|
2022-03-08 12:47:19 +05:30
|
|
|
Future<void> _sync(String caller) async {
|
2021-11-15 21:05:07 +05:30
|
|
|
if (!AppLifecycleService.instance.isForeground) {
|
2022-03-08 12:47:19 +05:30
|
|
|
_logger.info("Syncing in background caller $caller");
|
|
|
|
} else {
|
|
|
|
_logger.info("Syncing in foreground caller $caller");
|
2021-02-26 19:39:45 +05:30
|
|
|
}
|
2021-01-13 23:09:45 +05:30
|
|
|
try {
|
2021-03-03 21:33:03 +05:30
|
|
|
await SyncService.instance.sync();
|
2021-01-13 23:09:45 +05:30
|
|
|
} catch (e, s) {
|
2023-01-11 13:55:26 +05:30
|
|
|
if (!isHandledSyncError(e)) {
|
|
|
|
_logger.severe("Sync error", e, s);
|
|
|
|
}
|
2021-01-13 23:09:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:33:03 +05:30
|
|
|
Future _runWithLogs(Function() function, {String prefix = ""}) async {
|
2022-06-11 13:53:52 +05:30
|
|
|
await SuperLogging.main(
|
|
|
|
LogConfig(
|
|
|
|
body: function,
|
2022-06-23 08:48:10 +05:30
|
|
|
logDirPath: (await getApplicationSupportDirectory()).path + "/logs",
|
2022-06-11 13:53:52 +05:30
|
|
|
maxLogFiles: 5,
|
2022-09-20 17:23:32 +05:30
|
|
|
sentryDsn: kDebugMode ? sentryDebugDSN : sentryDSN,
|
|
|
|
tunnel: sentryTunnel,
|
2022-06-11 13:53:52 +05:30
|
|
|
enableInDebugMode: true,
|
|
|
|
prefix: prefix,
|
|
|
|
),
|
|
|
|
);
|
2020-06-16 00:08:08 +05:30
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:46 +05:30
|
|
|
Future<void> _scheduleHeartBeat(
|
2022-11-06 15:46:14 +05:30
|
|
|
SharedPreferences prefs,
|
|
|
|
bool isBackground,
|
|
|
|
) async {
|
2021-05-07 22:56:51 +05:30
|
|
|
await prefs.setInt(
|
2022-06-11 13:53:52 +05:30
|
|
|
isBackground ? kLastBGTaskHeartBeatTime : kLastFGTaskHeartBeatTime,
|
|
|
|
DateTime.now().microsecondsSinceEpoch,
|
|
|
|
);
|
2021-05-07 22:56:51 +05:30
|
|
|
Future.delayed(kHeartBeatFrequency, () async {
|
2022-11-06 11:44:46 +05:30
|
|
|
_scheduleHeartBeat(prefs, isBackground);
|
2021-05-07 22:56:51 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-08 12:47:19 +05:30
|
|
|
Future<void> _scheduleFGSync(String caller) async {
|
|
|
|
await _sync(caller);
|
2021-05-08 23:23:17 +05:30
|
|
|
Future.delayed(kFGSyncFrequency, () async {
|
2022-11-06 16:06:33 +05:30
|
|
|
unawaited(_scheduleFGSync('fgSyncCron'));
|
2021-05-08 23:23:17 +05:30
|
|
|
});
|
2021-05-07 22:56:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void _scheduleBGTaskKill(String taskId) async {
|
|
|
|
if (await _isRunningInForeground()) {
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.info("Found app in FG, committing seppuku. $taskId");
|
2021-05-07 23:14:19 +05:30
|
|
|
await _killBGTask(taskId);
|
2021-05-07 22:56:51 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
Future.delayed(kHeartBeatFrequency, () async {
|
|
|
|
_scheduleBGTaskKill(taskId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-08 23:23:17 +05:30
|
|
|
Future<bool> _isRunningInForeground() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.reload();
|
|
|
|
final currentTime = DateTime.now().microsecondsSinceEpoch;
|
|
|
|
return (prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0) >
|
|
|
|
(currentTime - kFGTaskDeathTimeoutInMicroseconds);
|
|
|
|
}
|
|
|
|
|
2022-12-30 17:40:17 +05:30
|
|
|
Future<void> _killBGTask([String? taskId]) async {
|
2021-05-07 23:14:19 +05:30
|
|
|
await UploadLocksDB.instance.releaseLocksAcquiredByOwnerBefore(
|
2022-06-11 13:53:52 +05:30
|
|
|
ProcessType.background.toString(),
|
|
|
|
DateTime.now().microsecondsSinceEpoch,
|
|
|
|
);
|
2021-05-07 23:14:19 +05:30
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.remove(kLastBGTaskHeartBeatTime);
|
2021-10-22 23:12:35 +05:30
|
|
|
if (taskId != null) {
|
|
|
|
BackgroundFetch.finish(taskId);
|
|
|
|
}
|
2021-05-07 23:14:19 +05:30
|
|
|
}
|
|
|
|
|
2021-10-13 10:54:49 +05:30
|
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
2022-08-29 20:13:31 +05:30
|
|
|
final bool isRunningInFG = await _isRunningInForeground(); // hb
|
|
|
|
final bool isInForeground = AppLifecycleService.instance.isForeground;
|
2021-11-27 21:15:56 +05:30
|
|
|
if (_isProcessRunning) {
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.info(
|
2022-06-11 13:53:52 +05:30
|
|
|
"Background push received when app is alive and runningInFS: $isRunningInFG inForeground: $isInForeground",
|
|
|
|
);
|
2021-11-27 21:15:56 +05:30
|
|
|
if (PushService.shouldSync(message)) {
|
2022-03-08 12:47:19 +05:30
|
|
|
await _sync('firebaseBgSyncActiveProcess');
|
2021-11-27 21:15:56 +05:30
|
|
|
}
|
|
|
|
} else {
|
2021-10-13 10:54:49 +05:30
|
|
|
// App is dead
|
2022-06-11 13:53:52 +05:30
|
|
|
_runWithLogs(
|
|
|
|
() async {
|
|
|
|
_logger.info("Background push received");
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
_scheduleSuicide(kBGPushTimeout); // To prevent OS from punishing us
|
|
|
|
}
|
|
|
|
await _init(true, via: 'firebasePush');
|
|
|
|
if (PushService.shouldSync(message)) {
|
|
|
|
await _sync('firebaseBgSyncNoActiveProcess');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
prefix: "[fbg]",
|
|
|
|
);
|
2021-10-13 10:54:49 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-22 23:12:35 +05:30
|
|
|
|
2022-03-08 01:48:50 +05:30
|
|
|
Future<void> _logFGHeartBeatInfo() async {
|
2022-08-29 20:13:31 +05:30
|
|
|
final bool isRunningInFG = await _isRunningInForeground();
|
2022-03-08 01:48:50 +05:30
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.reload();
|
2022-08-29 20:13:31 +05:30
|
|
|
final lastFGTaskHeartBeatTime = prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0;
|
|
|
|
final String lastRun = lastFGTaskHeartBeatTime == 0
|
2022-03-08 01:48:50 +05:30
|
|
|
? 'never'
|
|
|
|
: DateTime.fromMicrosecondsSinceEpoch(lastFGTaskHeartBeatTime).toString();
|
|
|
|
_logger.info('isAlreaduunningFG: $isRunningInFG, last Beat: $lastRun');
|
|
|
|
}
|
|
|
|
|
2022-12-30 17:40:17 +05:30
|
|
|
void _scheduleSuicide(Duration duration, [String? taskID]) {
|
2022-08-29 20:13:31 +05:30
|
|
|
final taskIDVal = taskID ?? 'no taskID';
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.warning("Schedule seppuku taskID: $taskIDVal");
|
2021-10-22 23:12:35 +05:30
|
|
|
Future.delayed(duration, () {
|
2022-03-08 01:48:50 +05:30
|
|
|
_logger.warning("TLE, committing seppuku for taskID: $taskIDVal");
|
2021-10-22 23:12:35 +05:30
|
|
|
_killBGTask(taskID);
|
|
|
|
});
|
|
|
|
}
|