From 2d1d4d9ba3a08de6c27b2e41bdb429d6b96b5401 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 21 May 2024 18:58:18 +0200 Subject: [PATCH] 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 --- src/plugins/android/androidconfigurations.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/plugins/android/androidconfigurations.cpp b/src/plugins/android/androidconfigurations.cpp index 8a4a0465c3b..db284270804 100644 --- a/src/plugins/android/androidconfigurations.cpp +++ b/src/plugins/android/androidconfigurations.cpp @@ -731,9 +731,18 @@ QList connectedDevices(QString *error) bool isConnected(const QString &serialNumber) { - const QList devices = connectedDevices(); - for (const AndroidDeviceInfo &device : devices) { - if (device.serialNumber == serialNumber) + Process adbProcess; + adbProcess.setCommand({adbToolPath(), {"devices"}}); + 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 false;