import 'app_widgets.dart'; import '../data/agent_models.dart'; import 'agent_model.dart'; import 'path_picker.dart'; class AgentView extends StatefulWidget { const AgentView({super.key, required this.model}); final AgentModel model; @override State createState() => _AgentViewState(); } class _AgentViewState extends State { final Map _fields = {}; AgentModel get m => widget.model; Widget field( String key, String label, String value, void Function(String) change, { bool secret = false, int lines = 1, }) { final controller = _fields.putIfAbsent( key, () => TextEditingController(text: value), ); if (controller.text != value) { controller.text = value; } return Padding( padding: const EdgeInsets.only(bottom: 12), child: TextField( controller: controller, onChanged: change, obscureText: secret, maxLines: lines, minLines: lines > 1 ? 6 : 1, style: lines > 1 ? const TextStyle(fontFamily: 'Consolas', fontSize: 12) : null, decoration: InputDecoration( labelText: label, suffixIcon: ['configPath', 'admin', 'enroll'].contains(key) ? IconButton( tooltip: '选择文件并保存', icon: const Icon(Icons.folder_open), onPressed: m.busy ? null : () => m.run(() async { final selected = await pickPath(controller.text); if (selected == null || !mounted) { return; } controller.text = selected; change(selected); await m.savePath?.call(key, selected); m.notice = '文件位置已保存'; }), ) : null, border: const OutlineInputBorder(), ), ), ); } Widget button( String text, Future Function() action, { bool? enabled, IconData? icon, bool inline = false, }) => Padding( padding: EdgeInsets.only(right: 8, bottom: inline ? 0 : 8), child: icon == null ? OutlinedButton( onPressed: enabled == false || m.busy ? null : action, child: Text(text), ) : OutlinedButton.icon( onPressed: enabled == false || m.busy ? null : action, icon: Icon(icon, size: 16), label: Text(text), ), ); Widget leftAligned(Widget child) => Align(alignment: Alignment.centerLeft, child: child); Widget cardTitle(IconData icon, String text) => Padding( padding: const EdgeInsets.only(bottom: 12), child: Row( children: [ Icon(icon, size: 18, color: const Color(0xFF305DDB)), const SizedBox(width: 8), Expanded( child: Text( text, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600), ), ), ], ), ); Widget card(List children) => Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: children, ), ), ); @override Widget build(BuildContext context) => ListenableBuilder( listenable: m, builder: (context, _) => Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Wrap( crossAxisAlignment: WrapCrossAlignment.center, children: [ Checkbox( value: m.localMode, onChanged: m.busy ? null : (v) => m.setLocal(v ?? false), ), const SizedBox(width: 6), const Text('管理本机 Agent'), const SizedBox(width: 16), button('刷新 Agent', m.refresh, icon: Icons.refresh, inline: true), Checkbox( value: m.autoRefresh, onChanged: (v) => setState(() => m.autoRefresh = v ?? false), ), const SizedBox(width: 6), const Text('自动刷新'), ], ), if (m.localMode) ...[ field('configPath', '本机配置路径', m.configPath, (v) => m.configPath = v), Wrap( children: [ button( '启动后台 Agent', m.startLocal, icon: Icons.play_circle_outline, ), button('安装开机服务', m.installLocal, icon: Icons.power_settings_new), ], ), ] else Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: field( 'admin', 'admin.env 路径', m.adminPath, (v) => m.adminPath = v, ), ), const SizedBox(width: 12), Padding( padding: const EdgeInsets.only(bottom: 2), child: button( '导入管理员凭据', m.importAdmin, icon: Icons.file_upload, inline: true, ), ), ], ), Text(m.notice, style: const TextStyle(color: Colors.blueGrey)), const SizedBox(height: 10), SizedBox( height: 3, child: m.busy ? const LinearProgressIndicator(minHeight: 3) : null, ), Expanded( child: LayoutBuilder( builder: (context, size) { final list = Card( child: Padding( padding: const EdgeInsets.all(8), child: m.agents.isEmpty ? const Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( Icons.dns_outlined, size: 40, color: Colors.blueGrey, ), SizedBox(height: 12), Text( '暂无 Agent', style: TextStyle(color: Colors.blueGrey), ), ], ), ) : ListView( padding: EdgeInsets.zero, children: [ for (final agent in m.agents) ListTile( selected: agent.id == m.selectedId, title: Text(agent.id), subtitle: Text(agent.description), leading: Icon( Icons.computer, color: agent.online ? Colors.green : Colors.grey, ), onTap: m.busy ? null : () => m.select(agent.id), ), ], ), ), ); final detail = ListView( children: [ card([ cardTitle( Icons.computer, m.selected?.description ?? '选择 Agent / 被控电脑', ), if (m.selected case final agent?) Text( '${agent.hostname} · ${agent.os}/${agent.arch} · v${agent.version} · PID ${agent.pid} · 配置版本 ${agent.revision}', ), const SizedBox(height: 12), Wrap( children: [ button( '读取配置', m.loadConfig, enabled: m.canManage, icon: Icons.download, ), button( '开启 USB 自动注册', () => m.setAutoUsb(true), enabled: m.canManage, icon: Icons.usb, ), button( '关闭 USB 自动注册', () => m.setAutoUsb(false), enabled: m.canManage, icon: Icons.usb_off, ), button( '取消所选 USB 设备的忽略', m.unignoreUsb, enabled: m.canManage, icon: Icons.undo, ), button( '重启 Agent', m.restart, enabled: m.canManage && m.selected?.supervised == true, icon: Icons.restart_alt, ), ], ), ]), const SizedBox(height: 12), card([ cardTitle(Icons.phone_android, '手机管理'), leftAligned( button( '发现 ADB 设备', m.discoverDevices, enabled: m.canManage, icon: Icons.search, ), ), 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 ?? []) ListTile( selected: device.id == m.selectedPhone, title: Text(device.description), subtitle: device.lastError == null ? null : Text(device.lastError!), onTap: m.busy ? null : () => setState(() => m.selectedPhone = device.id), ), Wrap( children: [ button( '启用', () => m.deviceOperation('device_enable', enabled: true), enabled: m.canDevice, icon: Icons.play_arrow, ), button( '停用', () => m.deviceOperation( 'device_enable', enabled: false, ), enabled: m.canDevice, icon: Icons.pause, ), button( '移除配置', () => m.deviceOperation('device_remove'), enabled: m.canDevice, icon: Icons.delete_outline, ), ], ), field('device', '设备 ID', m.deviceId, (v) => m.deviceId = v), field( 'target', '手机 adbd 地址', m.target, (v) => m.target = v, ), ExpansionTile( title: const Text('通过已有 ADB server 接入'), children: [ field( 'serial', 'ADB 序列号(与手机地址二选一)', m.adbSerial, (v) => m.adbSerial = v, ), field( 'adbserver', 'ADB server 地址(留空使用默认值)', m.adbServer, (v) => m.adbServer = v, ), const Text('此模式仍要求手机开启 TCP adbd。'), ], ), leftAligned( button( '添加手机', m.addDevice, enabled: m.canManage, icon: Icons.add, ), ), ]), const SizedBox(height: 12), card([ cardTitle(Icons.tune, 'Agent 配置'), const SizedBox(height: 10), field( 'json', '读取、编辑 Agent JSON 配置', m.configText, (v) => m.configText = v, lines: 12, ), leftAligned( button( '保存并应用配置', m.saveConfig, enabled: m.canManage, icon: Icons.save, ), ), if (m.localMode) ...[ field( 'enroll', 'agent.env 路径', m.enrollmentPath, (v) => m.enrollmentPath = v, ), leftAligned( button( '导入设备端凭据', m.importEnrollment, icon: Icons.file_upload, ), ), field( 'agentToken', 'ADS_AGENT_TOKEN', m.enrollmentToken, (v) => m.enrollmentToken = v, secret: true, ), leftAligned( button( '初始化本机配置', m.initializeLocal, icon: Icons.settings, ), ), ], ]), const SizedBox(height: 12), card([ Wrap( crossAxisAlignment: WrapCrossAlignment.center, children: [ button( '读取日志', m.readLogs, enabled: m.canManage, icon: Icons.receipt_long, inline: true, ), Checkbox( value: m.followLogs, onChanged: (v) => setState(() => m.followLogs = v ?? false), ), const SizedBox(width: 6), const Text('持续跟随日志'), ], ), SizedBox( height: 200, child: SingleChildScrollView( child: SelectableText( m.logText, style: const TextStyle( fontFamily: 'Consolas', fontSize: 12, ), ), ), ), ]), ], ); return size.maxWidth < 800 ? Column( children: [ SizedBox(height: 160, child: list), Expanded(child: detail), ], ) : Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SizedBox(width: 280, child: list), const SizedBox(width: 12), Expanded(child: detail), ], ); }, ), ), ], ), ); @override void dispose() { for (final field in _fields.values) { field.dispose(); } super.dispose(); } }