yubioath-flutter/lib/android/init.dart

145 lines
4.6 KiB
Dart
Raw Normal View History

2022-03-03 18:43:36 +03:00
import 'dart:async';
2022-08-31 16:49:43 +03:00
import 'dart:convert';
2022-03-03 18:43:36 +03:00
2022-05-11 15:02:31 +03:00
import 'package:flutter/foundation.dart';
2022-03-03 18:43:36 +03:00
import 'package:flutter/material.dart';
2022-08-31 16:49:43 +03:00
import 'package:flutter/services.dart';
2022-03-03 18:43:36 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
2022-04-13 16:26:32 +03:00
import 'package:yubico_authenticator/android/logger.dart';
2022-09-06 17:52:17 +03:00
import 'package:yubico_authenticator/android/views/beta_dialog.dart';
import 'package:yubico_authenticator/android/window_state_provider.dart';
2022-05-09 09:32:44 +03:00
import 'package:yubico_authenticator/app/logging.dart';
2022-03-03 18:43:36 +03:00
import '../app/app.dart';
import '../app/models.dart';
2022-04-05 12:10:37 +03:00
import '../app/state.dart';
import '../app/views/main_page.dart';
import '../core/state.dart';
2022-03-16 12:37:52 +03:00
import '../management/state.dart';
2022-03-28 11:58:09 +03:00
import '../oath/state.dart';
2022-03-16 12:37:52 +03:00
import 'management/state.dart';
2022-03-03 19:24:26 +03:00
import 'oath/state.dart';
2022-03-28 11:58:09 +03:00
import 'qr_scanner/qr_scanner_provider.dart';
import 'state.dart';
2022-08-16 15:05:53 +03:00
import 'tap_request_dialog.dart';
2022-03-03 18:43:36 +03:00
Future<Widget> initialize() async {
2022-09-08 19:34:47 +03:00
_initSystemUi();
2022-05-11 15:02:31 +03:00
if (kDebugMode) {
Logger.root.level = Levels.DEBUG;
}
2022-08-31 16:49:43 +03:00
_initLicenses();
return ProviderScope(
overrides: [
supportedAppsProvider.overrideWithValue([
Application.oath,
]),
prefProvider.overrideWithValue(await SharedPreferences.getInstance()),
2022-05-11 16:47:35 +03:00
logLevelProvider.overrideWithProvider(androidLogProvider),
attachedDevicesProvider
.overrideWithProvider(androidAttachedDevicesProvider),
currentDeviceDataProvider.overrideWithProvider(androidDeviceDataProvider),
oathStateProvider.overrideWithProvider(androidOathStateProvider),
credentialListProvider
.overrideWithProvider(androidCredentialListProvider),
currentAppProvider.overrideWithProvider(androidSubPageProvider),
managementStateProvider.overrideWithProvider(androidManagementState),
2022-03-28 11:58:09 +03:00
currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider),
qrScannerProvider.overrideWithProvider(androidQrScannerProvider),
windowStateProvider.overrideWithProvider(androidWindowStateProvider)
],
child: DismissKeyboard(
child: YubicoAuthenticatorApp(page: Consumer(
builder: (context, ref, child) {
// activates the sub page provider
ref.read(androidSubPageProvider);
// activates window state provider
ref.read(androidWindowStateProvider);
/// initializes global handler for dialogs
2022-08-16 15:05:53 +03:00
ref.read(androidDialogProvider);
/// if the beta dialog was not shown yet, this will show it
requestBetaDialog(ref);
2022-09-06 17:52:17 +03:00
return const MainPage();
},
)),
),
);
2022-03-03 18:43:36 +03:00
}
class DismissKeyboard extends StatelessWidget {
final Widget child;
2022-08-31 16:49:43 +03:00
const DismissKeyboard({super.key, required this.child});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
// De-select any selected node when tapping outside.
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus &&
currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus?.unfocus();
}
},
child: child,
);
}
}
2022-08-31 16:49:43 +03:00
2022-09-08 19:34:47 +03:00
void _initSystemUi() async {
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge,
overlays: SystemUiOverlay.values);
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
2022-09-09 12:24:33 +03:00
systemNavigationBarColor: Colors.transparent,
systemNavigationBarContrastEnforced: true));
2022-09-08 19:34:47 +03:00
}
2022-09-08 19:34:47 +03:00
void _initLicenses() async {
const licenseDir = 'assets/licenses/android';
2022-08-31 16:49:43 +03:00
final androidProjectsToLicenseUrl = await rootBundle.loadStructuredData<List>(
'$licenseDir/android.json',
2022-08-31 16:49:43 +03:00
(value) async => jsonDecode(value),
);
// mapping from url to license text
final fileMap = await rootBundle.loadStructuredData<Map>(
'$licenseDir/map.json',
2022-09-08 19:34:47 +03:00
(value) async => jsonDecode(value),
2022-08-31 16:49:43 +03:00
);
2022-09-01 17:31:37 +03:00
final urlToLicense = <String, String>{};
fileMap.forEach((url, file) async {
String licenseText = url;
try {
licenseText = await rootBundle.loadString('$licenseDir/$file');
urlToLicense[url] = licenseText;
} catch (_) {
// failed to read license file, will use the url
}
2022-08-31 16:49:43 +03:00
});
if (androidProjectsToLicenseUrl.isNotEmpty) {
LicenseRegistry.addLicense(() async* {
for (final e in androidProjectsToLicenseUrl) {
var licenseUrl = e['PackageLicense'];
var content = licenseUrl;
if (urlToLicense.containsKey(licenseUrl)) {
content = '${urlToLicense[licenseUrl]}\n\n$licenseUrl\n\n';
}
yield LicenseEntryWithLineBreaks([e['PackageName']], content);
}
});
}
}