2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* 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-01-27 14:34:29 +03:00
|
|
|
import 'dart:async';
|
2022-02-25 15:17:52 +03:00
|
|
|
import 'dart:io';
|
2022-01-27 14:34:29 +03:00
|
|
|
|
2022-09-23 11:17:28 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-21 16:29:34 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-09-21 16:29:34 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2022-03-21 11:34:45 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
2022-05-03 12:24:25 +03:00
|
|
|
import 'package:yubico_authenticator/app/logging.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
import '../app/models.dart';
|
2022-03-21 11:34:45 +03:00
|
|
|
import '../app/state.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import '../core/state.dart';
|
|
|
|
import 'models.dart';
|
|
|
|
import 'rpc.dart';
|
|
|
|
|
2022-02-21 11:38:09 +03:00
|
|
|
final _log = Logger('state');
|
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
// This must be initialized before use in initialize.dart.
|
|
|
|
final rpcProvider = Provider<RpcSession>((ref) {
|
|
|
|
throw UnimplementedError();
|
|
|
|
});
|
|
|
|
|
|
|
|
final rpcStateProvider = StateNotifierProvider<_RpcStateNotifier, RpcState>(
|
2022-03-25 10:36:29 +03:00
|
|
|
(ref) => _RpcStateNotifier(ref.watch(rpcProvider)),
|
2022-01-27 14:34:29 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
class _RpcStateNotifier extends StateNotifier<RpcState> {
|
|
|
|
final RpcSession rpc;
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-03-16 16:55:21 +03:00
|
|
|
_RpcStateNotifier(this.rpc) : super(const RpcState('unknown', false)) {
|
2022-01-27 14:34:29 +03:00
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
_init() async {
|
|
|
|
final response = await rpc.command('get', []);
|
|
|
|
if (mounted) {
|
2022-03-16 16:55:21 +03:00
|
|
|
state = RpcState.fromJson(response['data']);
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final _windowStateProvider =
|
|
|
|
StateNotifierProvider<_WindowStateNotifier, WindowState>(
|
|
|
|
(ref) => _WindowStateNotifier());
|
|
|
|
|
|
|
|
final desktopWindowStateProvider = Provider<WindowState>(
|
|
|
|
(ref) => ref.watch(_windowStateProvider),
|
|
|
|
);
|
|
|
|
|
|
|
|
class _WindowStateNotifier extends StateNotifier<WindowState>
|
|
|
|
with WindowListener {
|
|
|
|
Timer? _idleTimer;
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
_WindowStateNotifier()
|
|
|
|
: super(WindowState(focused: true, visible: true, active: true)) {
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init() async {
|
|
|
|
windowManager.addListener(this);
|
2022-02-25 15:17:52 +03:00
|
|
|
// isFocused is not supported on Linux, assume focused
|
|
|
|
if (!Platform.isLinux) {
|
|
|
|
_idleTimer = Timer(const Duration(seconds: 5), () async {
|
|
|
|
final focused = await windowManager.isFocused();
|
|
|
|
if (mounted && !focused) {
|
|
|
|
state = state.copyWith(active: false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
windowManager.removeListener(this);
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
set state(WindowState value) {
|
2022-05-03 12:24:25 +03:00
|
|
|
_log.debug('Window state changed: $value');
|
2022-01-27 14:34:29 +03:00
|
|
|
super.state = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onWindowEvent(String eventName) {
|
|
|
|
if (mounted) {
|
|
|
|
switch (eventName) {
|
|
|
|
case 'blur':
|
|
|
|
state = state.copyWith(focused: false);
|
|
|
|
_idleTimer?.cancel();
|
2022-02-25 12:14:25 +03:00
|
|
|
_idleTimer = Timer(const Duration(seconds: 5), () async {
|
2022-02-25 15:17:52 +03:00
|
|
|
if (mounted) {
|
2022-01-27 14:34:29 +03:00
|
|
|
state = state.copyWith(active: false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'focus':
|
|
|
|
state = state.copyWith(focused: true, active: true);
|
|
|
|
_idleTimer?.cancel();
|
|
|
|
break;
|
|
|
|
case 'minimize':
|
|
|
|
state = state.copyWith(visible: false, active: false);
|
|
|
|
_idleTimer?.cancel();
|
|
|
|
break;
|
|
|
|
case 'restore':
|
|
|
|
state = state.copyWith(visible: true, active: true);
|
2022-02-25 15:17:52 +03:00
|
|
|
_idleTimer?.cancel();
|
2022-01-27 14:34:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
2022-05-03 12:24:25 +03:00
|
|
|
_log.traffic('Window event ignored: $eventName');
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 11:34:45 +03:00
|
|
|
|
2022-09-21 16:29:34 +03:00
|
|
|
final desktopClipboardProvider = Provider<AppClipboard>(
|
|
|
|
(ref) => _DesktopClipboard(),
|
|
|
|
);
|
|
|
|
|
|
|
|
class _DesktopClipboard extends AppClipboard {
|
|
|
|
@override
|
|
|
|
bool platformGivesFeedback() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> setText(String toClipboard, {bool isSensitive = false}) async {
|
|
|
|
await Clipboard.setData(ClipboardData(text: toClipboard));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:17:28 +03:00
|
|
|
final desktopSupportedThemesProvider = StateProvider<List<ThemeMode>>(
|
|
|
|
(ref) => ThemeMode.values,
|
|
|
|
);
|
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
class DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier {
|
2022-03-21 11:34:45 +03:00
|
|
|
static const String _lastDevice = 'APP_STATE_LAST_DEVICE';
|
2022-11-30 17:27:32 +03:00
|
|
|
DeviceNode? _deviceNode;
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
@override
|
|
|
|
DeviceNode? build() {
|
|
|
|
SharedPreferences prefs = ref.watch(prefProvider);
|
|
|
|
var devices = ref.watch(attachedDevicesProvider);
|
2022-03-21 11:34:45 +03:00
|
|
|
|
2022-11-30 17:27:32 +03:00
|
|
|
if (!devices.contains(_deviceNode)) {
|
|
|
|
final lastDevice = prefs.getString(_lastDevice) ?? '';
|
2022-03-21 11:34:45 +03:00
|
|
|
try {
|
2022-11-30 17:27:32 +03:00
|
|
|
_deviceNode = devices.firstWhere((dev) => dev.path.key == lastDevice,
|
2022-03-21 11:34:45 +03:00
|
|
|
orElse: () => devices.whereType<UsbYubiKeyNode>().first);
|
|
|
|
} on StateError {
|
2022-11-30 17:27:32 +03:00
|
|
|
_deviceNode = null;
|
2022-03-21 11:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:27:32 +03:00
|
|
|
return _deviceNode;
|
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;
|
2022-11-30 17:27:32 +03:00
|
|
|
ref.read(prefProvider).setString(_lastDevice, device?.path.key ?? '');
|
2022-03-21 11:34:45 +03:00
|
|
|
}
|
|
|
|
}
|