From b3bb49aee6621f7acafc8599e706f7bff4a410e2 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Thu, 3 Mar 2022 17:24:26 +0100 Subject: [PATCH] update codestyle --- .../api/flutter_management_api_impl.dart | 2 +- lib/android/api/flutter_oath_api_impl.dart | 4 ++-- lib/android/command_providers.dart | 19 +++++++++------- lib/android/info/state.dart | 17 -------------- lib/android/init.dart | 14 +++++------- lib/android/oath/command_providers.dart | 22 +++++++++---------- lib/android/oath/state.dart | 4 ++-- lib/android/state.dart | 2 +- lib/android/views/tap_request_dialog.dart | 2 +- lib/app/app.dart | 2 ++ lib/app/navigation_service.dart | 2 -- lib/core/state.dart | 1 + lib/main.dart | 11 +++++++--- 13 files changed, 45 insertions(+), 57 deletions(-) delete mode 100644 lib/android/info/state.dart diff --git a/lib/android/api/flutter_management_api_impl.dart b/lib/android/api/flutter_management_api_impl.dart index 6188dff3..0f7e42f9 100644 --- a/lib/android/api/flutter_management_api_impl.dart +++ b/lib/android/api/flutter_management_api_impl.dart @@ -14,6 +14,6 @@ class FManagementApiImpl extends FManagementApi { @override Future updateDeviceInfo(String deviceInfoJson) async { _log.info('Received: $deviceInfoJson'); - _ref.read(yubikeyDataCommandProvider.notifier).set(deviceInfoJson); + _ref.read(androidYubikeyProvider.notifier).set(deviceInfoJson); } } diff --git a/lib/android/api/flutter_oath_api_impl.dart b/lib/android/api/flutter_oath_api_impl.dart index c20e1eab..5bcdf9bd 100644 --- a/lib/android/api/flutter_oath_api_impl.dart +++ b/lib/android/api/flutter_oath_api_impl.dart @@ -14,12 +14,12 @@ class FOathApiImpl extends FOathApi { @override Future updateOathCredentials(String credentialListJson) async { _log.info('Received: $credentialListJson'); - _ref.read(oathPairsCommandProvider.notifier).set(credentialListJson); + _ref.read(androidCredentialsProvider.notifier).set(credentialListJson); } @override Future updateSession(String sessionJson) async { _log.info('Received: $sessionJson'); - _ref.read(oathStateCommandProvider.notifier).set(sessionJson); + _ref.read(androidStateProvider.notifier).set(sessionJson); } } diff --git a/lib/android/command_providers.dart b/lib/android/command_providers.dart index dc64cbd2..98b7f114 100644 --- a/lib/android/command_providers.dart +++ b/lib/android/command_providers.dart @@ -7,24 +7,27 @@ import '../app/models.dart'; import '../core/models.dart'; import '../management/models.dart'; -final log = Logger('yubikeyDataCommandProvider'); +final _log = Logger('yubikeyDataCommandProvider'); -final yubikeyDataCommandProvider = - StateNotifierProvider((ref) { - return YubikeyDataCommandProvider(null); +final androidYubikeyProvider = + StateNotifierProvider<_YubikeyProvider, YubiKeyData?>((ref) { + return _YubikeyProvider(null); }); -class YubikeyDataCommandProvider extends StateNotifier { - YubikeyDataCommandProvider(YubiKeyData? yubiKeyData) : super(yubiKeyData); +class _YubikeyProvider extends StateNotifier { + _YubikeyProvider(YubiKeyData? yubiKeyData) : super(yubiKeyData); void set(String input) { try { if (input.isEmpty) { - log.info('Yubikey was detached.'); + _log.info('Yubikey was detached.'); state = null; return; } + /// a workaround for yubikeys without DEVICE_INFO + /// once we have support functionality implemented, + /// the following block will not be needed if (input == 'NO_FEATURE_DEVICE_INFO') { // empty data to show some general information in the app DeviceConfig config = DeviceConfig({}, 0, 0, 0); @@ -47,7 +50,7 @@ class YubikeyDataCommandProvider extends StateNotifier { : DeviceNode.usbYubiKey(DevicePath([]), name, -1, deviceInfo); state = YubiKeyData(deviceNode, name, deviceInfo); } on Exception catch (e) { - log.info('Invalid data for yubikey: $input. $e'); + _log.info('Invalid data for yubikey: $input. $e'); state = null; } } diff --git a/lib/android/info/state.dart b/lib/android/info/state.dart deleted file mode 100644 index 5b505fe1..00000000 --- a/lib/android/info/state.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:logging/logging.dart'; - -import '../command_providers.dart'; -import '../init.dart'; - -final log = Logger('android.info.state'); - -void setupInfoPageMethodChannel(ref) { - methodChannel.setMethodCallHandler((call) async { - log.info('Received: $call'); - switch (call.method) { - case 'deviceInfo': - ref.read(yubikeyDataCommandProvider.notifier).set(call.arguments); - break; - } - }); -} diff --git a/lib/android/init.dart b/lib/android/init.dart index ac595e4d..e0746469 100644 --- a/lib/android/init.dart +++ b/lib/android/init.dart @@ -1,19 +1,17 @@ import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; -import 'package:yubico_authenticator/android/oath/state.dart'; -import 'package:yubico_authenticator/android/state.dart'; -import 'package:yubico_authenticator/android/views/tap_request_dialog.dart'; -import 'package:yubico_authenticator/app/state.dart'; -import 'package:yubico_authenticator/oath/state.dart'; + +import 'oath/state.dart'; +import 'state.dart'; +import 'views/tap_request_dialog.dart'; +import '../app/state.dart'; +import '../oath/state.dart'; final _log = Logger('android.init'); -const methodChannel = MethodChannel('com.yubico.yubikit_android/channel'); - initializeLogging() { Logger.root.onRecord.listen((record) { if (record.level >= Logger.root.level) { diff --git a/lib/android/oath/command_providers.dart b/lib/android/oath/command_providers.dart index 8079047d..b0ed0fbc 100644 --- a/lib/android/oath/command_providers.dart +++ b/lib/android/oath/command_providers.dart @@ -4,13 +4,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../oath/models.dart'; -final oathStateCommandProvider = - StateNotifierProvider((ref) { - return OathStateCommandProvider(null); +final androidStateProvider = + StateNotifierProvider<_StateProvider, OathState?>((ref) { + return _StateProvider(null); }); -class OathStateCommandProvider extends StateNotifier { - OathStateCommandProvider(OathState? oathState) : super(oathState); +class _StateProvider extends StateNotifier { + _StateProvider(OathState? oathState) : super(oathState); void set(String input) { var resultJson = jsonDecode(input); @@ -22,15 +22,13 @@ class OathStateCommandProvider extends StateNotifier { } } -final oathPairsCommandProvider = - StateNotifierProvider>( - (ref) { - return OathCredentialsCommandProvider([]); +final androidCredentialsProvider = + StateNotifierProvider<_CredentialsProvider, List>((ref) { + return _CredentialsProvider([]); }); -class OathCredentialsCommandProvider extends StateNotifier> { - OathCredentialsCommandProvider(List credentials) - : super(credentials); +class _CredentialsProvider extends StateNotifier> { + _CredentialsProvider(List credentials) : super(credentials); void set(String input) { var result = jsonDecode(input); diff --git a/lib/android/oath/state.dart b/lib/android/oath/state.dart index c784d4f7..0c065a66 100755 --- a/lib/android/oath/state.dart +++ b/lib/android/oath/state.dart @@ -24,7 +24,7 @@ final oathApiProvider = StateProvider((_) => OathApi()); final androidOathStateProvider = StateNotifierProvider.autoDispose .family((ref, devicePath) => _AndroidOathStateNotifier( - ref.watch(oathStateCommandProvider), ref.watch(oathApiProvider))); + ref.watch(androidStateProvider), ref.watch(oathApiProvider))); class _AndroidOathStateNotifier extends OathStateNotifier { final OathApi _api; @@ -100,7 +100,7 @@ final androidCredentialListProvider = StateNotifierProvider.autoDispose (ref, devicePath) { var notifier = _AndroidCredentialListNotifier( ref.watch(oathApiProvider), - ref.watch(oathPairsCommandProvider), + ref.watch(androidCredentialsProvider), ref.watch(oathStateProvider(devicePath).select((s) => s?.locked ?? true)), ); ref.listen(windowStateProvider, (_, windowState) { diff --git a/lib/android/state.dart b/lib/android/state.dart index 17bc44d1..1792be21 100644 --- a/lib/android/state.dart +++ b/lib/android/state.dart @@ -42,4 +42,4 @@ final androidAttachedDevicesProvider = Provider>((ref) { }); final androidDeviceDataProvider = - Provider((ref) => ref.watch(yubikeyDataCommandProvider)); + Provider((ref) => ref.watch(androidYubikeyProvider)); diff --git a/lib/android/views/tap_request_dialog.dart b/lib/android/views/tap_request_dialog.dart index 18fbb6ac..8ef04cdc 100755 --- a/lib/android/views/tap_request_dialog.dart +++ b/lib/android/views/tap_request_dialog.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:yubico_authenticator/android/api/impl.dart'; +import '../api/impl.dart'; import '../../app/navigation_service.dart'; class FDialogApiImpl extends FDialogApi { diff --git a/lib/app/app.dart b/lib/app/app.dart index 3a3c896c..a8f24ea2 100755 --- a/lib/app/app.dart +++ b/lib/app/app.dart @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'state.dart'; import '../theme.dart'; +import 'navigation_service.dart'; class YubicoAuthenticatorApp extends ConsumerWidget { final Widget page; @@ -12,6 +13,7 @@ class YubicoAuthenticatorApp extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { return MaterialApp( + navigatorKey: NavigationService.navigatorKey, title: 'Yubico Authenticator', theme: AppTheme.lightTheme, darkTheme: AppTheme.darkTheme, diff --git a/lib/app/navigation_service.dart b/lib/app/navigation_service.dart index 618beb60..c296884b 100644 --- a/lib/app/navigation_service.dart +++ b/lib/app/navigation_service.dart @@ -1,7 +1,5 @@ import 'package:flutter/widgets.dart'; -/// Temporary service which is used to get context of the top app widget -/// currently used by nfc operation dialogs class NavigationService { static GlobalKey navigatorKey = GlobalKey(); } diff --git a/lib/core/state.dart b/lib/core/state.dart index d659cd72..d71343be 100644 --- a/lib/core/state.dart +++ b/lib/core/state.dart @@ -22,3 +22,4 @@ class LogLevelNotifier extends StateNotifier { } final isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux; +final isAndroid = Platform.isAndroid; diff --git a/lib/main.dart b/lib/main.dart index 63abcf89..c0daa28e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,14 +4,14 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:logging/logging.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'android/init.dart' as android; import 'app/app.dart'; import 'app/views/main_page.dart'; import 'core/state.dart'; import 'desktop/init.dart' as desktop; - import 'error_page.dart'; final _log = Logger('main'); @@ -26,7 +26,9 @@ void main() async { if (isDesktop) { desktop.initializeLogging(); } - // TODO: android.initializeLogging() to set up bridge to logcat. + if (isAndroid) { + android.initializeLogging(); + } } WidgetsFlutterBinding.ensureInitialized(); @@ -41,6 +43,9 @@ void main() async { if (isDesktop) { _log.config('Initializing desktop platform.'); overrides.addAll(await desktop.initializeAndGetOverrides(prefs)); + } else if (isAndroid) { + _log.config('Initializing Android platform.'); + overrides.addAll(await android.initializeAndGetOverrides()); } page = const MainPage(); } catch (e) {