2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2024-03-15 16:24:42 +03:00
|
|
|
* Copyright (C) 2022-2024 Yubico.
|
2022-10-04 13:12:54 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-16 15:05:53 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-03-03 18:43:36 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-03-14 16:31:11 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2023-03-03 18:09:40 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-03-03 18:43:36 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
import '../app/logging.dart';
|
2022-03-03 18:43:36 +03:00
|
|
|
import '../app/models.dart';
|
|
|
|
import '../app/state.dart';
|
2023-03-03 18:09:40 +03:00
|
|
|
import '../core/state.dart';
|
2022-09-21 16:29:34 +03:00
|
|
|
import 'app_methods.dart';
|
2022-08-16 15:05:53 +03:00
|
|
|
import 'devices.dart';
|
2023-03-03 18:09:40 +03:00
|
|
|
import 'models.dart';
|
2022-08-16 15:05:53 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
final _log = Logger('android.state');
|
|
|
|
|
2022-08-19 10:36:45 +03:00
|
|
|
const _contextChannel = MethodChannel('android.state.appContext');
|
2022-09-08 13:17:44 +03:00
|
|
|
|
|
|
|
final androidAllowScreenshotsProvider =
|
|
|
|
StateNotifierProvider<AllowScreenshotsNotifier, bool>(
|
|
|
|
(ref) => AllowScreenshotsNotifier(),
|
|
|
|
);
|
|
|
|
|
|
|
|
class AllowScreenshotsNotifier extends StateNotifier<bool> {
|
|
|
|
AllowScreenshotsNotifier() : super(false);
|
|
|
|
|
|
|
|
void setAllowScreenshots(bool value) async {
|
2022-09-08 14:15:38 +03:00
|
|
|
final result =
|
2022-09-21 16:29:34 +03:00
|
|
|
await appMethodsChannel.invokeMethod('allowScreenshots', value);
|
2022-09-08 14:15:38 +03:00
|
|
|
if (mounted) {
|
|
|
|
state = result;
|
|
|
|
}
|
2022-09-08 13:17:44 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 18:43:36 +03:00
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
final androidClipboardProvider = Provider<AppClipboard>(
|
|
|
|
(ref) => _AndroidClipboard(ref),
|
|
|
|
);
|
|
|
|
|
|
|
|
class _AndroidClipboard extends AppClipboard {
|
|
|
|
final ProviderRef<AppClipboard> _ref;
|
|
|
|
|
|
|
|
const _AndroidClipboard(this._ref);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool platformGivesFeedback() {
|
2022-09-22 18:28:52 +03:00
|
|
|
return _ref.read(androidSdkVersionProvider) >= 33;
|
2022-09-21 16:29:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setText(String toClipboard, {bool isSensitive = false}) async {
|
|
|
|
await setPrimaryClip(toClipboard, isSensitive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 19:12:49 +03:00
|
|
|
class NfcStateNotifier extends StateNotifier<bool> {
|
2023-03-03 18:09:40 +03:00
|
|
|
NfcStateNotifier() : super(false);
|
2023-02-08 19:12:49 +03:00
|
|
|
|
|
|
|
void setNfcEnabled(bool value) {
|
|
|
|
state = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:24:42 +03:00
|
|
|
final androidSectionPriority = Provider<List<Section>>((ref) => []);
|
|
|
|
|
2022-09-22 18:28:52 +03:00
|
|
|
final androidSdkVersionProvider = Provider<int>((ref) => -1);
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2023-02-08 19:12:49 +03:00
|
|
|
final androidNfcSupportProvider = Provider<bool>((ref) => false);
|
|
|
|
|
2023-03-03 18:09:40 +03:00
|
|
|
final androidNfcStateProvider =
|
|
|
|
StateNotifierProvider<NfcStateNotifier, bool>((ref) => NfcStateNotifier());
|
2023-02-08 19:12:49 +03:00
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
final androidSupportedThemesProvider = StateProvider<List<ThemeMode>>((ref) {
|
2022-09-22 18:28:52 +03:00
|
|
|
if (ref.read(androidSdkVersionProvider) < 29) {
|
2022-10-07 15:02:24 +03:00
|
|
|
// the user can select from light or dark theme of the app
|
2022-09-21 16:29:34 +03:00
|
|
|
return [ThemeMode.light, ThemeMode.dark];
|
|
|
|
} else {
|
2022-10-07 15:02:24 +03:00
|
|
|
// the user can also select system theme on newer Android versions
|
2022-09-21 16:29:34 +03:00
|
|
|
return ThemeMode.values;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
class AndroidAppContextHandler {
|
2024-03-13 18:01:12 +03:00
|
|
|
Future<void> switchAppContext(Section section) async {
|
|
|
|
await _contextChannel.invokeMethod('setContext', {'index': section.index});
|
2024-02-21 19:22:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final androidAppContextHandler =
|
2024-03-14 16:31:11 +03:00
|
|
|
Provider<AndroidAppContextHandler>((ref) => AndroidAppContextHandler());
|
|
|
|
|
|
|
|
CurrentSectionNotifier androidCurrentSectionNotifier(Ref ref) {
|
2024-03-15 16:24:42 +03:00
|
|
|
final notifier = AndroidCurrentSectionNotifier(
|
|
|
|
ref.watch(androidSectionPriority), ref.watch(androidAppContextHandler));
|
2024-03-14 16:31:11 +03:00
|
|
|
ref.listen<AsyncValue<YubiKeyData>>(currentDeviceDataProvider, (_, data) {
|
|
|
|
notifier._notifyDeviceChanged(data.whenOrNull(data: ((data) => data)));
|
|
|
|
}, fireImmediately: true);
|
|
|
|
return notifier;
|
|
|
|
}
|
2024-02-21 19:22:56 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
class AndroidCurrentSectionNotifier extends CurrentSectionNotifier {
|
2024-03-15 16:24:42 +03:00
|
|
|
final List<Section> _supportedSectionsByPriority;
|
2024-03-14 16:31:11 +03:00
|
|
|
final AndroidAppContextHandler _appContextHandler;
|
2024-02-21 19:22:56 +03:00
|
|
|
|
2024-03-15 16:24:42 +03:00
|
|
|
AndroidCurrentSectionNotifier(
|
|
|
|
this._supportedSectionsByPriority,
|
|
|
|
this._appContextHandler,
|
2024-04-05 16:13:13 +03:00
|
|
|
) : super(Section.home);
|
2022-03-03 18:43:36 +03:00
|
|
|
|
|
|
|
@override
|
2024-03-08 19:35:50 +03:00
|
|
|
void setCurrentSection(Section section) {
|
2024-03-14 16:31:11 +03:00
|
|
|
state = section;
|
|
|
|
_log.debug('Setting current section to $section');
|
|
|
|
_appContextHandler.switchAppContext(state);
|
2022-03-03 18:43:36 +03:00
|
|
|
}
|
2024-02-01 18:53:17 +03:00
|
|
|
|
2024-03-14 16:31:11 +03:00
|
|
|
void _notifyDeviceChanged(YubiKeyData? data) {
|
|
|
|
if (data == null) {
|
|
|
|
_log.debug('Keeping current section because key was disconnected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:24:42 +03:00
|
|
|
final supportedSections = _supportedSectionsByPriority.where(
|
2024-03-14 16:31:11 +03:00
|
|
|
(e) => e.getAvailability(data) == Availability.enabled,
|
|
|
|
);
|
|
|
|
|
2024-03-15 16:24:42 +03:00
|
|
|
if (supportedSections.contains(state)) {
|
2024-03-14 16:31:11 +03:00
|
|
|
// the key supports current section
|
|
|
|
_log.debug('Keeping current section because new key support $state');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:24:42 +03:00
|
|
|
setCurrentSection(supportedSections.firstOrNull ?? Section.home);
|
2024-02-01 18:53:17 +03:00
|
|
|
}
|
2022-03-03 18:43:36 +03:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
class AndroidAttachedDevicesNotifier extends AttachedDevicesNotifier {
|
|
|
|
@override
|
|
|
|
List<DeviceNode> build() => ref
|
|
|
|
.watch(androidDeviceDataProvider)
|
|
|
|
.maybeWhen(data: (data) => [data.node], orElse: () => []);
|
2022-03-11 15:53:28 +03:00
|
|
|
}
|
|
|
|
|
2024-08-22 16:33:25 +03:00
|
|
|
final androidDeviceDataProvider = Provider<AsyncValue<YubiKeyData>>((ref) {
|
|
|
|
return ref.watch(androidYubikeyProvider).when(data: (d) {
|
|
|
|
if (d.name == 'restricted-nfc' || d.name == 'unknown-device') {
|
|
|
|
return AsyncError(d.name, StackTrace.current);
|
|
|
|
}
|
|
|
|
return AsyncData(d);
|
|
|
|
}, error: (Object error, StackTrace stackTrace) {
|
|
|
|
return AsyncError(error, stackTrace);
|
|
|
|
}, loading: () {
|
|
|
|
return const AsyncLoading();
|
|
|
|
});
|
|
|
|
});
|
2022-03-21 11:34:45 +03:00
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
class AndroidCurrentDeviceNotifier extends CurrentDeviceNotifier {
|
|
|
|
@override
|
|
|
|
DeviceNode? build() =>
|
|
|
|
ref.watch(androidYubikeyProvider).whenOrNull(data: (data) => data.node);
|
2022-03-21 11:34:45 +03:00
|
|
|
|
|
|
|
@override
|
2022-06-07 16:13:11 +03:00
|
|
|
setCurrentDevice(DeviceNode? device) {
|
2022-03-21 11:34:45 +03:00
|
|
|
state = device;
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 18:09:40 +03:00
|
|
|
|
|
|
|
final androidNfcTapActionProvider =
|
|
|
|
StateNotifierProvider<NfcTapActionNotifier, NfcTapAction>(
|
|
|
|
(ref) => NfcTapActionNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class NfcTapActionNotifier extends StateNotifier<NfcTapAction> {
|
|
|
|
static const _prefNfcOpenApp = 'prefNfcOpenApp';
|
|
|
|
static const _prefNfcCopyOtp = 'prefNfcCopyOtp';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
NfcTapActionNotifier._(this._prefs, super._state);
|
|
|
|
|
|
|
|
factory NfcTapActionNotifier(SharedPreferences prefs) {
|
|
|
|
final launchApp = prefs.getBool(_prefNfcOpenApp) ?? true;
|
|
|
|
final copyOtp = prefs.getBool(_prefNfcCopyOtp) ?? false;
|
|
|
|
final NfcTapAction action;
|
|
|
|
if (launchApp && copyOtp) {
|
2023-10-19 17:35:45 +03:00
|
|
|
action = NfcTapAction.launchAndCopy;
|
2023-03-03 18:09:40 +03:00
|
|
|
} else if (copyOtp) {
|
|
|
|
action = NfcTapAction.copy;
|
2023-10-19 17:35:45 +03:00
|
|
|
} else if (launchApp) {
|
2023-03-03 18:09:40 +03:00
|
|
|
action = NfcTapAction.launch;
|
2023-10-19 17:35:45 +03:00
|
|
|
} else {
|
|
|
|
action = NfcTapAction.noAction;
|
2023-03-03 18:09:40 +03:00
|
|
|
}
|
|
|
|
return NfcTapActionNotifier._(prefs, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setTapAction(NfcTapAction value) async {
|
|
|
|
if (state != value) {
|
|
|
|
state = value;
|
2023-10-19 17:35:45 +03:00
|
|
|
await _prefs.setBool(_prefNfcOpenApp,
|
|
|
|
value == NfcTapAction.launch || value == NfcTapAction.launchAndCopy);
|
|
|
|
await _prefs.setBool(_prefNfcCopyOtp,
|
|
|
|
value == NfcTapAction.copy || value == NfcTapAction.launchAndCopy);
|
2023-03-03 18:09:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Get these from Android
|
|
|
|
final androidNfcSupportedKbdLayoutsProvider =
|
|
|
|
Provider<List<String>>((ref) => ['US', 'DE', 'DE-CH']);
|
|
|
|
|
|
|
|
final androidNfcKbdLayoutProvider =
|
|
|
|
StateNotifierProvider<NfcKbdLayoutNotifier, String>(
|
|
|
|
(ref) => NfcKbdLayoutNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class NfcKbdLayoutNotifier extends StateNotifier<String> {
|
|
|
|
static const String _defaultClipKbdLayout = 'US';
|
|
|
|
static const _prefClipKbdLayout = 'prefClipKbdLayout';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
NfcKbdLayoutNotifier(this._prefs)
|
|
|
|
: super(_prefs.getString(_prefClipKbdLayout) ?? _defaultClipKbdLayout);
|
|
|
|
|
|
|
|
Future<void> setKeyboardLayout(String value) async {
|
|
|
|
if (state != value) {
|
|
|
|
state = value;
|
|
|
|
await _prefs.setString(_prefClipKbdLayout, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final androidNfcBypassTouchProvider =
|
|
|
|
StateNotifierProvider<NfcBypassTouchNotifier, bool>(
|
|
|
|
(ref) => NfcBypassTouchNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class NfcBypassTouchNotifier extends StateNotifier<bool> {
|
|
|
|
static const _prefNfcBypassTouch = 'prefNfcBypassTouch';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
NfcBypassTouchNotifier(this._prefs)
|
|
|
|
: super(_prefs.getBool(_prefNfcBypassTouch) ?? false);
|
|
|
|
|
|
|
|
Future<void> setNfcBypassTouch(bool value) async {
|
|
|
|
if (state != value) {
|
|
|
|
state = value;
|
|
|
|
await _prefs.setBool(_prefNfcBypassTouch, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final androidNfcSilenceSoundsProvider =
|
|
|
|
StateNotifierProvider<NfcSilenceSoundsNotifier, bool>(
|
|
|
|
(ref) => NfcSilenceSoundsNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class NfcSilenceSoundsNotifier extends StateNotifier<bool> {
|
|
|
|
static const _prefNfcSilenceSounds = 'prefNfcSilenceSounds';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
NfcSilenceSoundsNotifier(this._prefs)
|
|
|
|
: super(_prefs.getBool(_prefNfcSilenceSounds) ?? false);
|
|
|
|
|
|
|
|
Future<void> setNfcSilenceSounds(bool value) async {
|
|
|
|
if (state != value) {
|
|
|
|
state = value;
|
|
|
|
await _prefs.setBool(_prefNfcSilenceSounds, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final androidUsbLaunchAppProvider =
|
|
|
|
StateNotifierProvider<UsbLaunchAppNotifier, bool>(
|
|
|
|
(ref) => UsbLaunchAppNotifier(ref.watch(prefProvider)));
|
|
|
|
|
|
|
|
class UsbLaunchAppNotifier extends StateNotifier<bool> {
|
|
|
|
static const _prefUsbOpenApp = 'prefUsbOpenApp';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
UsbLaunchAppNotifier(this._prefs)
|
|
|
|
: super(_prefs.getBool(_prefUsbOpenApp) ?? false);
|
|
|
|
|
|
|
|
Future<void> setUsbLaunchApp(bool value) async {
|
|
|
|
if (state != value) {
|
|
|
|
state = value;
|
|
|
|
await _prefs.setBool(_prefUsbOpenApp, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|