diff --git a/docs/changelog.md b/docs/changelog.md index 0ffb4051..c614fb31 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Mobileraker - Changelog +## [2.6.12] - 2024-02-20 + +### Bug Fixes + +- Fixed an issue that prevented the app from starting on some android devices. + ## [2.6.11] - 2024-02-02 ### Major Updates diff --git a/lib/app_setup.dart b/lib/app_setup.dart index 4fa5bfb1..f5399b3d 100644 --- a/lib/app_setup.dart +++ b/lib/app_setup.dart @@ -44,10 +44,14 @@ import 'package:worker_manager/worker_manager.dart'; part 'app_setup.g.dart'; +const _hiveKeyName = 'hive_key'; + setupBoxes() async { await Hive.initFlutter(); - Uint8List keyMaterial = await _hiveKey(); + // For the key is not needed. It can be used to encrypt the data in the box. But this is not a high security app with sensitive data. + // Caused problems on some devices and the key is not used for hive encryption. + // Uint8List keyMaterial = await _hiveKey(); // Ignore old/deperecates types! // 2 - WebcamSetting @@ -98,7 +102,8 @@ setupBoxes() async { // Hive.deleteBoxFromDisk('printers'); try { - await openBoxes(keyMaterial); + // await openBoxes(keyMaterial); + await openBoxes(); Hive.box("printers").values.forEach((element) { logger.i('Machine in box is ${element.logName}#${element.hashCode}'); // ToDo remove after machine migration! @@ -130,7 +135,6 @@ setupBoxes() async { } Future _hiveKey() async { - const keyName = 'hive_key'; /// due to the move to encSharedPref it could be that the hive_key is still in the normmal shared pref /// Therfore first try to load it from the secureShared pref else try the normal one else generate a new one @@ -141,38 +145,35 @@ Future _hiveKey() async { aOptions: AndroidOptions(encryptedSharedPreferences: false), ); - Uint8List? encryptionKey; - try { - encryptionKey = await secureStorage.read(key: keyName).then((value) => value?.let(base64Decode)); - } on PlatformException catch (e) { - logger.e('Error while reading hive_key from secure storage', e); - encryptionKey = await nonEncSharedPrefSecureStorage - .read(key: keyName) - .then((value) => value?.let(base64Decode)) - .onError((error, stackTrace) { - logger.e('Error while reading hive_key from non-encryptedSharedPreferences', error, stackTrace); - return null; - }); - await nonEncSharedPrefSecureStorage.delete(key: keyName); - await secureStorage.write( - key: keyName, - value: encryptionKey?.let(base64Encode), - ); - logger.e( - 'Transfered hive_key from non-encryptedSharedPreferences to secureStorage using encryptedSharedPreferences', - ); + Uint8List? encryptionKey = await _readStorage(secureStorage); + if (encryptionKey != null) { + return encryptionKey; } + encryptionKey ??= await _readStorage(nonEncSharedPrefSecureStorage); if (encryptionKey != null) { + await secureStorage.write(key: _hiveKeyName, value: encryptionKey.let(base64Encode)); + await nonEncSharedPrefSecureStorage.delete(key: _hiveKeyName); return encryptionKey; } final key = Hive.generateSecureKey(); - await secureStorage.write(key: keyName, value: base64UrlEncode(key)); + await secureStorage.write(key: _hiveKeyName, value: base64UrlEncode(key)); return Uint8List.fromList(key); } -Future> openBoxes(Uint8List _) { +Future _readStorage(FlutterSecureStorage storage) async { + try { + String? value = await storage.read(key: _hiveKeyName); + return value?.let(base64Decode); + } catch (e) { + logger.e('Error while reading $_hiveKeyName from storage', e); + return null; + } +} + +// Future> openBoxes(Uint8List _) { +Future> openBoxes() { return Future.wait([ Hive.openBox('printers').then(_migrateMachine), Hive.openBox('uuidbox'),