describeRunningApps method
Recover the device an app runs on from its VM service URI — when several sims share 127.0.0.1
the port is ambiguous, but the listening process reveals its sim.
Describe every running app in scan: device (iOS via the flutter_tools
process chain, Android via adb forward) plus bundle identity when the
app's Info.plist is reachable. Best-effort per app; never throws.
Implementation
Future<List<RunningApp>> describeRunningApps(DiscoveryResult scan) async {
final out = <RunningApp>[];
for (final uri in scan.vmUris) {
AppDeviceLink? link;
DevicePlatform? platform;
try {
link = await _correlateIosSim(uri.port, scan.devices);
if (link != null) platform = DevicePlatform.ios;
if (link == null && scan.devicesFor(DevicePlatform.android).isNotEmpty) {
link = await _correlateAndroid(uri.port);
if (link != null) platform = DevicePlatform.android;
}
} on Object {
link = null;
}
var bundleId = link?.bundleId;
var displayName = link?.displayName;
if (link != null && platform == DevicePlatform.ios && displayName == null) {
final info = await appInfoForDevice(link.deviceId);
bundleId ??= info?.$1;
displayName ??= info?.$2;
}
BootedDevice? dev;
for (final d in scan.devices) {
if (d.id == link?.deviceId) dev = d;
}
out.add(RunningApp(
vmUri: uri,
platform: platform ?? dev?.platform,
deviceId: link?.deviceId,
deviceName: dev?.name,
bundleId: bundleId,
displayName: displayName,
appName: link?.appName,
));
}
return collapseSameApp(out);
}