Android: Optimize isConnected()

Before, the implementation used connectedDevices() function
which constructs the detailed info about each device
found, including running 3 extra processes for each device
in a loop. For the purpose of isConnected() - the detailed
info isn't really necessary - it's enough to parse the output
of the {adbToolPath(), {"devices"}} command and detect if
the requested serialNumber appears there. We skip executing
3 additional processes for each device found.

Change-Id: I34f79fb56b4aa4d52a284bc2c67ae01a3467b505
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Jarek Kobus
2024-05-21 18:58:18 +02:00
parent 9a4cae42c0
commit 2d1d4d9ba3

View File

@@ -731,9 +731,18 @@ QList<AndroidDeviceInfo> connectedDevices(QString *error)
bool isConnected(const QString &serialNumber) bool isConnected(const QString &serialNumber)
{ {
const QList<AndroidDeviceInfo> devices = connectedDevices(); Process adbProcess;
for (const AndroidDeviceInfo &device : devices) { adbProcess.setCommand({adbToolPath(), {"devices"}});
if (device.serialNumber == serialNumber) adbProcess.runBlocking();
if (adbProcess.result() != ProcessResult::FinishedWithSuccess)
return false;
// mid(1) - remove "List of devices attached" header line.
// Example output: "List of devices attached\nemulator-5554\tdevice\n\n".
const QStringList lines = adbProcess.allOutput().split('\n', Qt::SkipEmptyParts).mid(1);
for (const QString &line : lines) {
// skip the daemon logs
if (!line.startsWith("* daemon") && line.left(line.indexOf('\t')).trimmed() == serialNumber)
return true; return true;
} }
return false; return false;