handle method
Implementation
@override
Future<StructuredResponse> handle(GlintSession session, CallToolRequest request) async {
final args = request.arguments ?? const {};
final all = argBool(args, 'all') ?? false;
final force = argBool(args, 'force') ?? false;
final attachedDevice = session.active?.device;
final adbPath = attachedDevice is AndroidDevice ? attachedDevice.adbPath : resolveAdbPath(null) ?? 'adb';
const launcher = AppLauncher();
final scan = await DeviceDiscovery(adbPath: adbPath).scan();
if (all) {
if (scan.devices.isEmpty) {
return StructuredResponse(
summary: 'no booted devices',
data: const {'shutDown': []},
);
}
final foreign = [
for (final d in scan.devices)
if (!session.ownsDevice(d.id)) d.id
];
if (foreign.isNotEmpty && !force) return _notOwned(foreign);
final done = <String>[];
for (final d in scan.devices) {
final err = await launcher.shutdown(d.platform, d.id, adbPath: adbPath);
done.add(err == null ? d.id : '${d.id} (failed: $err)');
}
await session.detachAll();
return StructuredResponse(
summary: 'shut down ${scan.devices.length} device(s)',
data: {'shutDown': done},
);
}
final deviceArg = args['device'] as String?;
final deviceId = deviceArg ?? (session.isAttached ? session.device.id : null);
if (deviceId == null) {
return StructuredResponse.error(
summary: 'no device — pass device, all:true, or attach first',
errorKind: GlintErrorKind.invalidArgument,
nextSteps: const ['pass device:"<udid/serial>" or all:true'],
);
}
if (isPhysicalIosUdid(deviceId)) {
return StructuredResponse.notOnThisDevice(Ability.shutdown, iphoneAbilities.gaps[Ability.shutdown]!);
}
if (!force && !session.ownsDevice(deviceId)) return _notOwned([deviceId]);
final platform = _platformOf(deviceId, scan, session);
final isThisDevice = session.hasApp(deviceId);
if (isThisDevice) await session.detach(deviceId: deviceId);
final err = await launcher.shutdown(platform, deviceId, adbPath: adbPath);
if (err != null) {
return StructuredResponse.error(
summary: 'could not shut down $deviceId',
errorKind: GlintErrorKind.backendToolError,
detail: err,
);
}
return StructuredResponse(
summary: 'shut down $deviceId',
data: {'device': deviceId, if (isThisDevice) 'detached': true},
);
}