2021-04-16 18:54:02 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:event_bus/event_bus.dart';
|
2021-04-16 18:54:02 +02:00
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
|
|
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-03-27 10:36:54 +01:00
|
|
|
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:kiwi/kiwi.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/di_container.dart';
|
|
|
|
import 'package:nc_photos/entity/album.dart';
|
2022-07-05 22:20:24 +02:00
|
|
|
import 'package:nc_photos/entity/album/data_source.dart';
|
2023-04-13 17:32:31 +02:00
|
|
|
import 'package:nc_photos/entity/album/data_source2.dart';
|
|
|
|
import 'package:nc_photos/entity/album/repo2.dart';
|
2023-07-03 19:23:42 +02:00
|
|
|
import 'package:nc_photos/entity/face_recognition_person/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/face_recognition_person/repo.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:nc_photos/entity/favorite.dart';
|
|
|
|
import 'package:nc_photos/entity/favorite/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
|
|
|
import 'package:nc_photos/entity/file/data_source.dart';
|
2024-01-12 19:12:32 +01:00
|
|
|
import 'package:nc_photos/entity/file/data_source2.dart';
|
|
|
|
import 'package:nc_photos/entity/file/repo.dart';
|
2024-07-08 18:57:03 +02:00
|
|
|
import 'package:nc_photos/entity/image_location/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/image_location/repo.dart';
|
2022-05-05 16:06:47 +02:00
|
|
|
import 'package:nc_photos/entity/local_file.dart';
|
|
|
|
import 'package:nc_photos/entity/local_file/data_source.dart';
|
2023-04-13 17:32:31 +02:00
|
|
|
import 'package:nc_photos/entity/nc_album/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/nc_album/repo.dart';
|
2023-07-17 09:35:45 +02:00
|
|
|
import 'package:nc_photos/entity/pref.dart';
|
2024-05-24 21:35:58 +02:00
|
|
|
import 'package:nc_photos/entity/pref/provider/secure_storage.dart';
|
2023-07-17 09:35:45 +02:00
|
|
|
import 'package:nc_photos/entity/pref/provider/shared_preferences.dart';
|
|
|
|
import 'package:nc_photos/entity/pref_util.dart' as pref_util;
|
2023-07-16 13:30:23 +02:00
|
|
|
import 'package:nc_photos/entity/recognize_face/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/recognize_face/repo.dart';
|
2022-08-06 06:21:11 +02:00
|
|
|
import 'package:nc_photos/entity/search.dart';
|
|
|
|
import 'package:nc_photos/entity/search/data_source.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:nc_photos/entity/share.dart';
|
|
|
|
import 'package:nc_photos/entity/share/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/sharee.dart';
|
|
|
|
import 'package:nc_photos/entity/sharee/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/tag.dart';
|
|
|
|
import 'package:nc_photos/entity/tag/data_source.dart';
|
|
|
|
import 'package:nc_photos/entity/tagged_file.dart';
|
|
|
|
import 'package:nc_photos/entity/tagged_file/data_source.dart';
|
|
|
|
import 'package:nc_photos/k.dart' as k;
|
|
|
|
import 'package:nc_photos/mobile/android/android_info.dart';
|
|
|
|
import 'package:nc_photos/mobile/self_signed_cert_manager.dart';
|
2021-04-16 18:54:02 +02:00
|
|
|
import 'package:nc_photos/object_extension.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
import 'package:nc_photos/platform/features.dart' as features;
|
2024-05-27 18:26:06 +02:00
|
|
|
import 'package:nc_photos/session_storage.dart';
|
2022-09-23 10:37:34 +02:00
|
|
|
import 'package:nc_photos/touch_manager.dart';
|
2023-12-06 15:05:33 +01:00
|
|
|
import 'package:np_db/np_db.dart';
|
2023-09-12 18:29:44 +02:00
|
|
|
import 'package:np_gps_map/np_gps_map.dart';
|
2024-07-30 19:34:06 +02:00
|
|
|
import 'package:np_http/np_http.dart';
|
2023-08-17 18:36:51 +02:00
|
|
|
import 'package:np_log/np_log.dart' as np_log;
|
2023-08-27 12:58:05 +02:00
|
|
|
import 'package:np_platform_util/np_platform_util.dart';
|
2022-06-12 07:45:51 +02:00
|
|
|
import 'package:visibility_detector/visibility_detector.dart';
|
2022-03-27 19:55:31 +02:00
|
|
|
|
2022-07-05 22:20:24 +02:00
|
|
|
enum InitIsolateType {
|
|
|
|
main,
|
2022-07-28 11:13:39 +02:00
|
|
|
|
|
|
|
/// Isolates with Flutter engine, e.g., those spawned by flutter_isolate or
|
|
|
|
/// flutter_background_service
|
|
|
|
flutterIsolate,
|
2022-07-05 22:20:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> init(InitIsolateType isolateType) async {
|
2022-06-06 10:59:42 +02:00
|
|
|
if (_hasInitedInThisIsolate) {
|
2022-08-20 12:31:54 +02:00
|
|
|
_log.warning("[init] Already initialized in this isolate");
|
2022-06-06 10:59:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initLog();
|
2022-08-14 19:40:44 +02:00
|
|
|
await _initDeviceInfo();
|
2022-03-27 19:55:31 +02:00
|
|
|
_initKiwi();
|
|
|
|
await _initPref();
|
|
|
|
await _initAccountPrefs();
|
|
|
|
_initEquatable();
|
|
|
|
if (features.isSupportSelfSignedCert) {
|
2024-06-19 10:17:24 +02:00
|
|
|
await _initSelfSignedCertManager();
|
2022-03-27 19:55:31 +02:00
|
|
|
}
|
2024-08-05 19:22:46 +02:00
|
|
|
await initHttp(
|
|
|
|
appVersion: k.versionStr,
|
|
|
|
isNewHttpEngine: Pref().isNewHttpEngine() ?? false,
|
|
|
|
);
|
2022-07-05 22:20:24 +02:00
|
|
|
await _initDiContainer(isolateType);
|
2022-06-12 07:45:51 +02:00
|
|
|
_initVisibilityDetector();
|
2024-07-22 17:16:26 +02:00
|
|
|
initGpsMap();
|
2024-05-27 18:26:06 +02:00
|
|
|
// init session storage
|
|
|
|
SessionStorage();
|
2022-08-29 20:21:36 +02:00
|
|
|
|
2021-04-16 18:54:02 +02:00
|
|
|
await _initFirebase();
|
2021-03-27 10:36:54 +01:00
|
|
|
await _initAds();
|
|
|
|
|
2022-06-06 10:59:42 +02:00
|
|
|
_hasInitedInThisIsolate = true;
|
2022-03-27 19:55:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:59:42 +02:00
|
|
|
void initLog() {
|
|
|
|
if (_hasInitedInThisIsolate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:36:51 +02:00
|
|
|
np_log.initLog(
|
2024-10-20 15:49:53 +02:00
|
|
|
isDebugMode: np_log.isDevMode,
|
2023-08-17 18:36:51 +02:00
|
|
|
print: (log) => debugPrint(log, wrapWidth: 1024),
|
2021-04-16 18:54:02 +02:00
|
|
|
onLog: (record) {
|
|
|
|
if (_shouldReportCrashlytics(record)) {
|
|
|
|
FirebaseCrashlytics.instance.recordError(
|
|
|
|
record.error,
|
|
|
|
record.stackTrace,
|
|
|
|
reason: record.message,
|
|
|
|
printDetails: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2023-08-17 18:36:51 +02:00
|
|
|
);
|
2022-03-27 19:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initPref() async {
|
|
|
|
final provider = PrefSharedPreferencesProvider();
|
|
|
|
await provider.init();
|
|
|
|
final pref = Pref.scoped(provider);
|
|
|
|
Pref.setGlobalInstance(pref);
|
|
|
|
|
|
|
|
if (Pref().getLastVersion() == null) {
|
|
|
|
if (Pref().getSetupProgress() == null) {
|
|
|
|
// new install
|
|
|
|
await Pref().setLastVersion(k.version);
|
|
|
|
} else {
|
|
|
|
// v6 is the last version without saving the version number in pref
|
|
|
|
await Pref().setLastVersion(6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initAccountPrefs() async {
|
|
|
|
for (final a in Pref().getAccounts3Or([])) {
|
|
|
|
try {
|
|
|
|
AccountPref.setGlobalInstance(a, await pref_util.loadAccountPref(a));
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout("[_initAccountPrefs] Failed reading pref for account: $a", e,
|
|
|
|
stackTrace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initDeviceInfo() async {
|
2023-08-27 12:58:05 +02:00
|
|
|
if (getRawPlatform() == NpPlatform.android) {
|
2022-03-27 19:55:31 +02:00
|
|
|
await AndroidInfo.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _initKiwi() {
|
|
|
|
final kiwi = KiwiContainer();
|
|
|
|
kiwi.registerInstance<EventBus>(EventBus());
|
|
|
|
}
|
|
|
|
|
|
|
|
void _initEquatable() {
|
|
|
|
EquatableConfig.stringify = false;
|
|
|
|
}
|
|
|
|
|
2024-06-19 10:17:24 +02:00
|
|
|
Future<void> _initSelfSignedCertManager() async {
|
|
|
|
try {
|
|
|
|
return SelfSignedCertManager().init();
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout("[_initSelfSignedCertManager] Failed to load self signed certs",
|
|
|
|
e, stackTrace);
|
|
|
|
}
|
2022-03-27 19:55:31 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 22:20:24 +02:00
|
|
|
Future<void> _initDiContainer(InitIsolateType isolateType) async {
|
|
|
|
final c = DiContainer.late();
|
|
|
|
c.pref = Pref();
|
2024-05-24 21:35:58 +02:00
|
|
|
c.securePref = await _createSecurePref();
|
2023-12-06 15:05:33 +01:00
|
|
|
c.npDb = await _createDb(isolateType);
|
2022-07-05 22:20:24 +02:00
|
|
|
|
|
|
|
c.albumRepo = AlbumRepo(AlbumCachedDataSource(c));
|
2023-04-13 17:32:31 +02:00
|
|
|
c.albumRepoRemote = AlbumRepo(AlbumRemoteDataSource());
|
2022-07-05 22:20:24 +02:00
|
|
|
c.albumRepoLocal = AlbumRepo(AlbumSqliteDbDataSource(c));
|
2023-04-13 17:32:31 +02:00
|
|
|
c.albumRepo2 = CachedAlbumRepo2(
|
2023-12-06 15:05:33 +01:00
|
|
|
const AlbumRemoteDataSource2(), AlbumSqliteDbDataSource2(c.npDb));
|
2023-04-13 17:32:31 +02:00
|
|
|
c.albumRepo2Remote = const BasicAlbumRepo2(AlbumRemoteDataSource2());
|
2023-12-06 15:05:33 +01:00
|
|
|
c.albumRepo2Local = BasicAlbumRepo2(AlbumSqliteDbDataSource2(c.npDb));
|
2022-07-05 22:20:24 +02:00
|
|
|
c.fileRepo = FileRepo(FileCachedDataSource(c));
|
|
|
|
c.fileRepoRemote = const FileRepo(FileWebdavDataSource());
|
|
|
|
c.fileRepoLocal = FileRepo(FileSqliteDbDataSource(c));
|
2024-01-12 19:12:32 +01:00
|
|
|
c.fileRepo2 =
|
|
|
|
CachedFileRepo(const FileRemoteDataSource(), FileNpDbDataSource(c.npDb));
|
|
|
|
c.fileRepo2Remote = const BasicFileRepo(FileRemoteDataSource());
|
|
|
|
c.fileRepo2Local = BasicFileRepo(FileNpDbDataSource(c.npDb));
|
2022-07-05 22:20:24 +02:00
|
|
|
c.shareRepo = ShareRepo(ShareRemoteDataSource());
|
|
|
|
c.shareeRepo = ShareeRepo(ShareeRemoteDataSource());
|
|
|
|
c.favoriteRepo = const FavoriteRepo(FavoriteRemoteDataSource());
|
|
|
|
c.tagRepo = const TagRepo(TagRemoteDataSource());
|
2022-08-05 11:28:28 +02:00
|
|
|
c.tagRepoRemote = const TagRepo(TagRemoteDataSource());
|
2023-12-06 15:05:33 +01:00
|
|
|
c.tagRepoLocal = TagRepo(TagSqliteDbDataSource(c.npDb));
|
2022-07-05 22:20:24 +02:00
|
|
|
c.taggedFileRepo = const TaggedFileRepo(TaggedFileRemoteDataSource());
|
2022-09-07 19:01:50 +02:00
|
|
|
c.searchRepo = SearchRepo(SearchSqliteDbDataSource(c));
|
2023-04-13 17:32:31 +02:00
|
|
|
c.ncAlbumRepo = CachedNcAlbumRepo(
|
2023-12-06 15:05:33 +01:00
|
|
|
const NcAlbumRemoteDataSource(), NcAlbumSqliteDbDataSource(c.npDb));
|
2023-04-13 17:32:31 +02:00
|
|
|
c.ncAlbumRepoRemote = const BasicNcAlbumRepo(NcAlbumRemoteDataSource());
|
2023-12-06 15:05:33 +01:00
|
|
|
c.ncAlbumRepoLocal = BasicNcAlbumRepo(NcAlbumSqliteDbDataSource(c.npDb));
|
2023-07-04 19:34:39 +02:00
|
|
|
c.faceRecognitionPersonRepo = const BasicFaceRecognitionPersonRepo(
|
|
|
|
FaceRecognitionPersonRemoteDataSource());
|
|
|
|
c.faceRecognitionPersonRepoRemote = const BasicFaceRecognitionPersonRepo(
|
|
|
|
FaceRecognitionPersonRemoteDataSource());
|
|
|
|
c.faceRecognitionPersonRepoLocal = BasicFaceRecognitionPersonRepo(
|
2023-12-06 15:05:33 +01:00
|
|
|
FaceRecognitionPersonSqliteDbDataSource(c.npDb));
|
2023-07-16 13:30:23 +02:00
|
|
|
c.recognizeFaceRepo =
|
|
|
|
const BasicRecognizeFaceRepo(RecognizeFaceRemoteDataSource());
|
|
|
|
c.recognizeFaceRepoRemote =
|
|
|
|
const BasicRecognizeFaceRepo(RecognizeFaceRemoteDataSource());
|
|
|
|
c.recognizeFaceRepoLocal =
|
2023-12-06 15:05:33 +01:00
|
|
|
BasicRecognizeFaceRepo(RecognizeFaceSqliteDbDataSource(c.npDb));
|
2024-07-08 18:57:03 +02:00
|
|
|
c.imageLocationRepo =
|
|
|
|
BasicImageLocationRepo(ImageLocationNpDbDataSource(c.npDb));
|
2023-04-13 17:32:31 +02:00
|
|
|
|
2022-09-23 10:37:34 +02:00
|
|
|
c.touchManager = TouchManager(c);
|
2022-07-05 22:20:24 +02:00
|
|
|
|
2023-08-27 12:58:05 +02:00
|
|
|
if (getRawPlatform() == NpPlatform.android) {
|
2022-05-05 16:06:47 +02:00
|
|
|
// local file currently only supported on Android
|
2022-07-05 22:20:24 +02:00
|
|
|
c.localFileRepo = const LocalFileRepo(LocalFileMediaStoreDataSource());
|
2022-05-05 16:06:47 +02:00
|
|
|
}
|
2022-07-05 22:20:24 +02:00
|
|
|
|
|
|
|
KiwiContainer().registerInstance<DiContainer>(c);
|
2022-03-27 19:55:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-12 07:45:51 +02:00
|
|
|
void _initVisibilityDetector() {
|
|
|
|
VisibilityDetectorController.instance.updateInterval = Duration.zero;
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:05:33 +01:00
|
|
|
Future<NpDb> _createDb(InitIsolateType isolateType) async {
|
|
|
|
final npDb = NpDb();
|
2023-12-11 16:46:37 +01:00
|
|
|
final androidSdk =
|
|
|
|
getRawPlatform() == NpPlatform.android ? AndroidInfo().sdkInt : null;
|
2023-12-06 15:05:33 +01:00
|
|
|
if (isolateType == InitIsolateType.main) {
|
2023-12-11 16:46:37 +01:00
|
|
|
await npDb.initMainIsolate(androidSdk: androidSdk);
|
2023-12-06 15:05:33 +01:00
|
|
|
} else {
|
2023-12-11 16:46:37 +01:00
|
|
|
await npDb.initBackgroundIsolate(androidSdk: androidSdk);
|
2022-07-05 22:20:24 +02:00
|
|
|
}
|
2023-12-06 15:05:33 +01:00
|
|
|
return npDb;
|
2022-07-05 22:20:24 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 21:35:58 +02:00
|
|
|
Future<Pref> _createSecurePref() async {
|
|
|
|
final provider = PrefSecureStorageProvider();
|
|
|
|
await provider.init();
|
|
|
|
return Pref.scoped(provider);
|
|
|
|
}
|
|
|
|
|
2021-03-27 10:36:54 +01:00
|
|
|
Future<InitializationStatus> _initAds() {
|
|
|
|
return MobileAds.instance.initialize();
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:54:02 +02:00
|
|
|
Future<void> _initFirebase() async {
|
|
|
|
await Firebase.initializeApp();
|
|
|
|
|
|
|
|
// Crashlytics
|
|
|
|
if (features.isSupportCrashlytics) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false);
|
|
|
|
await FirebaseCrashlytics.instance.deleteUnsentReports();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _shouldReportCrashlytics(LogRecord record) {
|
|
|
|
if (kDebugMode ||
|
|
|
|
!features.isSupportCrashlytics ||
|
|
|
|
record.level < Level.SHOUT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final e = record.error;
|
|
|
|
// We ignore these SocketExceptions as they are likely caused by an unstable
|
|
|
|
// internet connection
|
|
|
|
// 7: No address associated with hostname
|
|
|
|
// 101: Network is unreachable
|
|
|
|
// 103: Software caused connection abort
|
|
|
|
// 104: Connection reset by peer
|
|
|
|
// 110: Connection timed out
|
|
|
|
// 113: No route to host
|
|
|
|
if (e is SocketException &&
|
|
|
|
e.osError?.errorCode.isIn([7, 101, 103, 104, 110, 113]) == true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-27 19:55:31 +02:00
|
|
|
final _log = Logger("app_init");
|
2022-06-06 10:59:42 +02:00
|
|
|
var _hasInitedInThisIsolate = false;
|