From 45611e841bed7dbdfef4f6b12ac645fee1c119b2 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Sun, 30 May 2021 19:23:39 +0300 Subject: [PATCH] Android: avoid blocking call to show avd selection for default device If a user already chose a default device to run, we don't need to create the whole AVD selection dialog, but rather straightforward to get the device info if it's already connected, otherwise the dialog opens for selection as usual. Change-Id: I5fee9411d755e89da23a8d365f284e8b52808a07 Reviewed-by: hjk Reviewed-by: Alessandro Portale --- src/plugins/android/androidconfigurations.cpp | 15 +++++-- src/plugins/android/androiddevicedialog.cpp | 43 ++++++++++++------- src/plugins/android/androiddevicedialog.h | 6 ++- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/plugins/android/androidconfigurations.cpp b/src/plugins/android/androidconfigurations.cpp index d80529c78bb..7b00fb7f4f2 100644 --- a/src/plugins/android/androidconfigurations.cpp +++ b/src/plugins/android/androidconfigurations.cpp @@ -1066,13 +1066,20 @@ AndroidDeviceInfo AndroidConfigurations::showDeviceDialog(Project *project, if (!serialNumber.isEmpty()) break; } + + const AndroidDeviceInfo defaultDevice = AndroidDeviceDialog::defaultDeviceInfo(serialNumber); + if (defaultDevice.isValid()) + return defaultDevice; + AndroidDeviceDialog dialog(apiLevel, abis, serialNumber, Core::ICore::dialogParent()); - AndroidDeviceInfo info = dialog.device(); + AndroidDeviceInfo info = dialog.showAndGetSelectedDevice(); if (dialog.saveDeviceSelection() && info.isValid()) { - const QString serialNumber = info.type == AndroidDeviceInfo::Hardware ? + const QString newSerialNumber = info.type == AndroidDeviceInfo::Hardware ? info.serialNumber : info.avdname; - if (!serialNumber.isEmpty()) - AndroidConfigurations::setDefaultDevice(project, AndroidManager::devicePreferredAbi(info.cpuAbi, abis), serialNumber); + if (!newSerialNumber.isEmpty()) { + const QString preferredAbi = AndroidManager::devicePreferredAbi(info.cpuAbi, abis); + AndroidConfigurations::setDefaultDevice(project, preferredAbi, newSerialNumber); + } } return info; } diff --git a/src/plugins/android/androiddevicedialog.cpp b/src/plugins/android/androiddevicedialog.cpp index 80121444354..9e33808ebb6 100644 --- a/src/plugins/android/androiddevicedialog.cpp +++ b/src/plugins/android/androiddevicedialog.cpp @@ -44,6 +44,8 @@ using namespace Android::Internal; namespace Android { namespace Internal { +QVector AndroidDeviceDialog::m_connectedDevices = {}; + // yeah, writing tree models is fun! class AndroidDeviceModelNode { @@ -481,26 +483,30 @@ AndroidDeviceDialog::~AndroidDeviceDialog() delete m_ui; } -AndroidDeviceInfo AndroidDeviceDialog::device() +AndroidDeviceInfo AndroidDeviceDialog::defaultDeviceInfo(const QString &serialNumber) { + AndroidDeviceDialog::updateConnectedDevicesList(); + + if (serialNumber.isEmpty()) + return {}; + + return Utils::findOrDefault(m_connectedDevices, [serialNumber](const AndroidDeviceInfo &info) { + return info.serialNumber == serialNumber || info.avdname == serialNumber; + }); +} + +AndroidDeviceInfo AndroidDeviceDialog::showAndGetSelectedDevice() +{ + auto dev = defaultDeviceInfo(m_defaultDevice); + if (dev.isValid()) + return dev; + refreshDeviceList(); - if (!m_defaultDevice.isEmpty()) { - auto device = std::find_if(m_connectedDevices.cbegin(), - m_connectedDevices.cend(), - [this](const AndroidDeviceInfo &info) { - return info.serialNumber == m_defaultDevice || - info.avdname == m_defaultDevice; - }); - - if (device != m_connectedDevices.cend()) - return *device; - m_defaultDevice.clear(); - } - if (exec() == QDialog::Accepted) return m_model->device(m_ui->deviceView->currentIndex()); - return AndroidDeviceInfo(); + + return {}; } bool AndroidDeviceDialog::saveDeviceSelection() const @@ -508,11 +514,16 @@ bool AndroidDeviceDialog::saveDeviceSelection() const return m_ui->defaultDeviceCheckBox->isChecked(); } +void AndroidDeviceDialog::updateConnectedDevicesList() +{ + m_connectedDevices = AndroidConfig::connectedDevices(AndroidConfigurations::currentConfig() + .adbToolPath()); +} + void AndroidDeviceDialog::refreshDeviceList() { m_ui->refreshDevicesButton->setEnabled(false); m_progressIndicator->show(); - m_connectedDevices = AndroidConfig::connectedDevices(AndroidConfigurations::currentConfig().adbToolPath()); m_futureWatcherRefreshDevices.setFuture(m_avdManager->avdList()); } diff --git a/src/plugins/android/androiddevicedialog.h b/src/plugins/android/androiddevicedialog.h index 4bb9a55e470..33bd2871e9c 100644 --- a/src/plugins/android/androiddevicedialog.h +++ b/src/plugins/android/androiddevicedialog.h @@ -56,7 +56,8 @@ public: const QString &serialNumber, QWidget *parent = nullptr); ~AndroidDeviceDialog() override; - AndroidDeviceInfo device(); + AndroidDeviceInfo showAndGetSelectedDevice(); + static AndroidDeviceInfo defaultDeviceInfo(const QString &serialNumber); bool saveDeviceSelection() const; @@ -68,6 +69,7 @@ private: void devicesRefreshed(); void enableOkayButton(); void defaultDeviceClear(); + static void updateConnectedDevicesList(); AndroidDeviceModel *m_model; Ui::AndroidDeviceDialog *m_ui; @@ -76,8 +78,8 @@ private: QStringList m_abis; QString m_avdNameFromAdd; QString m_defaultDevice; + static QVector m_connectedDevices; std::unique_ptr m_avdManager; - QVector m_connectedDevices; QFutureWatcher m_futureWatcherAddDevice; QFutureWatcher m_futureWatcherRefreshDevices; };