Add opt-in physical USB device enrollment and remembered choices in 0.2.7

This commit is contained in:
tongtong committed 2026-11-14 04:06:01 +08:00
1 parent dc29a20a41
commit 208d37b7d8
20 files changed
+515 -18

No files matched your search

+34
View File
@@ -192,6 +192,40 @@ class AgentModel extends ChangeNotifier {
.convert(await _call({'op': 'config_get'}));
notice = '已读取配置,凭据不在配置中。';
});
Future<void> setAutoUsb(bool enabled) => run(() async {
final config = Map<String, Object?>.from(
await _call({'op': 'config_get'}) as Map,
);
final expected = Map<String, Object?>.from(config);
config['auto_register_usb'] = enabled;
await _call({
'op': 'config_set',
'config': config,
'expected_config': expected,
});
configText = const JsonEncoder.withIndent(' ').convert(config);
await _refresh();
notice = enabled ? '已开启真实 USB 自动注册。' : '已关闭自动注册,已有设备保留。';
});
Future<void> unignoreUsb() => run(() async {
final serial = adbSerial.trim();
if (serial.isEmpty) throw StateError('请先发现并选择需要取消忽略的 USB 设备');
final config = Map<String, Object?>.from(
await _call({'op': 'config_get'}) as Map,
);
final expected = Map<String, Object?>.from(config);
config['ignored_usb_serials'] =
(config['ignored_usb_serials'] as List? ?? [])
.where((s) => s != serial)
.toList();
await _call({
'op': 'config_set',
'config': config,
'expected_config': expected,
});
configText = const JsonEncoder.withIndent(' ').convert(config);
notice = '已取消该设备的忽略记录;开启自动注册后将重新检查。';
});
Future<void> saveConfig() => run(() async {
final data = jsonDecode(configText) as Map<String, Object?>;
await _call({'op': 'config_set', 'config': data});
+15
View File
@@ -171,6 +171,21 @@ class _AgentViewState extends State<AgentView> {
Wrap(
children: [
button('读取配置', m.loadConfig, enabled: m.canManage),
button(
'开启 USB 自动注册',
() => m.setAutoUsb(true),
enabled: m.canManage,
),
button(
'关闭 USB 自动注册',
() => m.setAutoUsb(false),
enabled: m.canManage,
),
button(
'取消所选 USB 设备的忽略',
m.unignoreUsb,
enabled: m.canManage,
),
button(
'重启 Agent',
m.restart,
+1 -1
View File
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.2.6+7
version: 0.2.7+8
environment:
sdk: ^3.14.0
+34 -1
View File
@@ -44,7 +44,12 @@ class FakeApi implements AgentApi {
'configured': false,
},
],
'config_get' => {'id': agent, 'devices': <Object?>[]},
'config_get' => {
'id': agent,
'devices': <Object?>[],
'ignored_usb_serials': ['USB-A', 'USB-B'],
'usb_device_ids': {'USB-A': 'stable-id'},
},
'logs' => {
'instance': 'i',
'next': 1,
@@ -72,6 +77,34 @@ class FakeApi implements AgentApi {
}
void main() {
test(
'USB policy applies only to selected agent and preserves identity memory',
() async {
final api = FakeApi();
final m = AgentModel(AgentRepository(api));
await m.refresh();
m.select('two');
await m.setAutoUsb(true);
final enabled = api.calls.lastWhere((c) => c['op'] == 'config_set');
expect(enabled['agent'], 'two');
expect((enabled['config'] as Map)['auto_register_usb'], true);
expect((enabled['config'] as Map)['usb_device_ids'], {
'USB-A': 'stable-id',
});
m.adbSerial = 'USB-A';
await m.unignoreUsb();
expect((api.calls.last['config'] as Map)['ignored_usb_serials'], [
'USB-B',
]);
await m.setAutoUsb(false);
expect(
(api.calls.lastWhere((c) => c['op'] == 'config_set')['config']
as Map)['auto_register_usb'],
false,
);
m.dispose();
},
);
test(
'selected agent scopes mutations and log secrets are redacted',
() async {