2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-02-08 19:12:49 +03:00
|
|
|
* Copyright (C) 2022-2023 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';
|
|
|
|
|
|
|
|
import '../app/models.dart';
|
|
|
|
import '../app/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';
|
|
|
|
|
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> {
|
|
|
|
NfcStateNotifier(): super(false);
|
|
|
|
|
|
|
|
void setNfcEnabled(bool value) {
|
|
|
|
state = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
final androidNfcStateProvider = StateNotifierProvider<NfcStateNotifier, bool>((ref) =>
|
|
|
|
NfcStateNotifier()
|
|
|
|
);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-02 20:02:32 +03:00
|
|
|
class AndroidSubPageNotifier extends CurrentAppNotifier {
|
|
|
|
AndroidSubPageNotifier(super.supportedApps) {
|
2022-03-14 13:48:39 +03:00
|
|
|
_handleSubPage(state);
|
2022-03-03 18:43:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-03-14 13:48:39 +03:00
|
|
|
void setCurrentApp(Application app) {
|
|
|
|
super.setCurrentApp(app);
|
|
|
|
_handleSubPage(app);
|
2022-03-03 18:43:36 +03:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:48:39 +03:00
|
|
|
void _handleSubPage(Application subPage) async {
|
2022-08-16 15:05:53 +03:00
|
|
|
await _contextChannel.invokeMethod('setContext', {'index': subPage.index});
|
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
|
|
|
}
|
|
|
|
|
2022-06-28 20:51:58 +03:00
|
|
|
final androidDeviceDataProvider = Provider<AsyncValue<YubiKeyData>>(
|
|
|
|
(ref) => ref.watch(androidYubikeyProvider));
|
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;
|
|
|
|
}
|
|
|
|
}
|