feat: discover ADB devices from agent management
This commit is contained in:
1 parent
8ecfe880cc
commit
3a7bb071f0
12 files changed
+132
-5
No files matched your search
@@ -24,6 +24,25 @@ class AgentModel extends ChangeNotifier {
|
||||
configPath = '',
|
||||
configText = '',
|
||||
logText = '';
|
||||
List<JsonMap> discoveredDevices = [];
|
||||
Future<void> discoverDevices() => run(() async {
|
||||
final rows = await _call({'op': 'devices_discover'});
|
||||
discoveredDevices = (rows! as List<Object?>)
|
||||
.map((v) => Map<String, Object?>.from(v! as Map))
|
||||
.toList();
|
||||
notice =
|
||||
'发现 ${discoveredDevices.length} 台 ADB 设备;选择后确认添加。USB 接入仍须启用 TCP adbd。';
|
||||
});
|
||||
void chooseDiscovered(JsonMap row) {
|
||||
final serial = row['serial']! as String;
|
||||
final tcp = RegExp(r'^.+:\d+$').hasMatch(serial);
|
||||
target = tcp ? serial : '';
|
||||
adbSerial = tcp ? '' : serial;
|
||||
deviceId = 'phone-${DateTime.now().microsecondsSinceEpoch}';
|
||||
notice = '已填入添加表单,请确认设备 ID 后添加手机。';
|
||||
_notify();
|
||||
}
|
||||
|
||||
String deviceId = '', target = '', adbSerial = '', adbServer = '';
|
||||
String notice = '远程管理需要独立管理员凭据;也可选择管理本机 Agent。';
|
||||
Timer? _timer;
|
||||
@@ -105,6 +124,7 @@ class AgentModel extends ChangeNotifier {
|
||||
|
||||
void select(String? id) {
|
||||
if (id != selectedId) {
|
||||
discoveredDevices = [];
|
||||
selectedId = id;
|
||||
_cursor = 0;
|
||||
_instance = '';
|
||||
|
||||
@@ -166,6 +166,24 @@ class _AgentViewState extends State<AgentView> {
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
button(
|
||||
'发现 ADB 设备',
|
||||
m.discoverDevices,
|
||||
enabled: m.canManage,
|
||||
),
|
||||
for (final row in m.discoveredDevices)
|
||||
ListTile(
|
||||
title: Text('${row['serial']} · ${row['state']}'),
|
||||
subtitle: Text(
|
||||
row['configured'] == true ? '已添加' : '未添加',
|
||||
),
|
||||
onTap:
|
||||
!m.busy &&
|
||||
row['configured'] != true &&
|
||||
row['state'] == 'device'
|
||||
? () => m.chooseDiscovered(row)
|
||||
: null,
|
||||
),
|
||||
for (final device
|
||||
in m.selected?.devices ?? <ManagedPhone>[])
|
||||
ListTile(
|
||||
|
||||
@@ -37,6 +37,13 @@ class FakeApi implements AgentApi {
|
||||
Future<Object?> request(String agent, JsonMap operation) async {
|
||||
calls.add({...operation, 'agent': agent});
|
||||
return switch (operation['op']) {
|
||||
'devices_discover' => [
|
||||
<String, Object?>{
|
||||
'serial': '192.168.1.2:5555',
|
||||
'state': 'device',
|
||||
'configured': false,
|
||||
},
|
||||
],
|
||||
'config_get' => {'id': agent, 'devices': <Object?>[]},
|
||||
'logs' => {
|
||||
'instance': 'i',
|
||||
@@ -81,6 +88,10 @@ void main() {
|
||||
await m.deviceOperation('device_enable', enabled: false);
|
||||
expect(api.calls.last['id'], 'two-phone');
|
||||
expect(api.calls.last['enabled'], false);
|
||||
await m.discoverDevices();
|
||||
m.chooseDiscovered(m.discoveredDevices.single);
|
||||
expect(m.target, '192.168.1.2:5555');
|
||||
expect(m.adbSerial, isEmpty);
|
||||
await m.readLogs();
|
||||
expect(m.logText, contains('[REDACTED]'));
|
||||
expect(m.logText, isNot(contains('private-token')));
|
||||
|
||||
Reference in new issue
Block a user