|
| 1 | +#include <QDebug> |
| 2 | +#include <QRegularExpression> |
| 3 | +#include "devicelistworker.h" |
| 4 | +#include "processutils.h" |
| 5 | + |
| 6 | +DeviceListWorker::DeviceListWorker(QObject *parent) |
| 7 | + : QObject(parent) |
| 8 | +{ |
| 9 | +} |
| 10 | + |
| 11 | +void DeviceListWorker::listDevices() |
| 12 | +{ |
| 13 | + emit started(); |
| 14 | + |
| 15 | + const QString adb = ProcessUtils::adbExe(); |
| 16 | + if (adb.isEmpty()) { |
| 17 | + emit error(tr("ADB not found. Please configure it in Settings.")); |
| 18 | + emit finished(); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Get device list with details |
| 23 | + ProcessResult result = ProcessUtils::runCommand(adb, QStringList() << "devices" << "-l"); |
| 24 | + if (result.code != 0) { |
| 25 | + emit error(tr("Failed to query devices: %1").arg(result.error.join("\n"))); |
| 26 | + emit finished(); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + QList<DeviceInfo> devices; |
| 31 | + |
| 32 | + // Parse device list |
| 33 | + bool foundHeader = false; |
| 34 | + for (const QString &line : result.output) { |
| 35 | + QString trimmed = line.trimmed(); |
| 36 | + if (trimmed.isEmpty()) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + if (!foundHeader) { |
| 40 | + if (trimmed.startsWith("List of devices")) { |
| 41 | + foundHeader = true; |
| 42 | + } |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + DeviceInfo device = parseDeviceLine(trimmed); |
| 47 | + if (!device.serial.isEmpty()) { |
| 48 | + // Get additional device properties if not already found |
| 49 | + if (device.model.isEmpty()) { |
| 50 | + device.model = getDeviceProperty(device.serial, "ro.product.model"); |
| 51 | + } |
| 52 | + device.androidSdkVersion = getDeviceProperty(device.serial, "ro.build.version.sdk"); |
| 53 | + device.androidVersion = getDeviceProperty(device.serial, "ro.build.version.release"); |
| 54 | + devices.append(device); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + emit devicesListed(devices); |
| 59 | + emit finished(); |
| 60 | +} |
| 61 | + |
| 62 | +DeviceInfo DeviceListWorker::parseDeviceLine(const QString &line) |
| 63 | +{ |
| 64 | + DeviceInfo device; |
| 65 | + |
| 66 | + // Format: SERIAL STATUS [PROPERTY:VALUE]... |
| 67 | + // Example: emulator-5554 device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64xa |
| 68 | + QRegularExpression regex(R"(^(\S+)\s+(\S+)(?:\s+(.+))?$)"); |
| 69 | + QRegularExpressionMatch match = regex.match(line); |
| 70 | + |
| 71 | + if (match.hasMatch()) { |
| 72 | + device.serial = match.captured(1); |
| 73 | + device.status = match.captured(2); |
| 74 | + |
| 75 | + // Parse properties from the rest of the line |
| 76 | + QString properties = match.captured(3); |
| 77 | + if (!properties.isEmpty()) { |
| 78 | + QRegularExpression propRegex(R"((\w+):([^\s]+))"); |
| 79 | + QRegularExpressionMatchIterator propIter = propRegex.globalMatch(properties); |
| 80 | + while (propIter.hasNext()) { |
| 81 | + QRegularExpressionMatch propMatch = propIter.next(); |
| 82 | + QString key = propMatch.captured(1); |
| 83 | + QString value = propMatch.captured(2); |
| 84 | + if (key == "model") { |
| 85 | + device.model = value; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return device; |
| 92 | +} |
| 93 | + |
| 94 | +QString DeviceListWorker::getDeviceProperty(const QString &serial, const QString &property) |
| 95 | +{ |
| 96 | + const QString adb = ProcessUtils::adbExe(); |
| 97 | + if (adb.isEmpty()) { |
| 98 | + return QString(); |
| 99 | + } |
| 100 | + |
| 101 | + QStringList args; |
| 102 | + args << "-s" << serial << "shell" << "getprop" << property; |
| 103 | + ProcessResult result = ProcessUtils::runCommand(adb, args, 5); |
| 104 | + |
| 105 | + if (result.code == 0 && !result.output.isEmpty()) { |
| 106 | + return result.output.first().trimmed(); |
| 107 | + } |
| 108 | + |
| 109 | + return QString(); |
| 110 | +} |
| 111 | + |
0 commit comments