Android: Supply newly created device with path

Otherwise, the newly created device is going to be removed
and recreated on subsequent HandleAvdsListChange()
causing flickering of the devices combobox in settings.

Change-Id: Idcf58f24eb7f4d4200881cfa912d0866f54d362a
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Jarek Kobus
2024-05-14 09:32:37 +02:00
parent dda9234255
commit 60f4822d5f
4 changed files with 42 additions and 31 deletions

View File

@@ -644,6 +644,18 @@ QString AndroidDeviceManager::getRunningAvdsSerialNumber(const QString &name) co
return {}; return {};
} }
static FilePath avdFilePath()
{
QString avdEnvVar = qtcEnvironmentVariable("ANDROID_AVD_HOME");
if (avdEnvVar.isEmpty()) {
avdEnvVar = qtcEnvironmentVariable("ANDROID_SDK_HOME");
if (avdEnvVar.isEmpty())
avdEnvVar = qtcEnvironmentVariable("HOME");
avdEnvVar.append("/.android/avd");
}
return FilePath::fromUserInput(avdEnvVar);
}
void AndroidDeviceManager::setupDevicesWatcher() void AndroidDeviceManager::setupDevicesWatcher()
{ {
if (!androidConfig().adbToolPath().exists()) { if (!androidConfig().adbToolPath().exists()) {
@@ -686,15 +698,7 @@ void AndroidDeviceManager::setupDevicesWatcher()
// Setup AVD filesystem watcher to listen for changes when an avd is created/deleted, // Setup AVD filesystem watcher to listen for changes when an avd is created/deleted,
// or started/stopped // or started/stopped
QString avdEnvVar = qtcEnvironmentVariable("ANDROID_AVD_HOME"); m_avdFileSystemWatcher.addPath(avdFilePath().toString());
if (avdEnvVar.isEmpty()) {
avdEnvVar = qtcEnvironmentVariable("ANDROID_SDK_HOME");
if (avdEnvVar.isEmpty())
avdEnvVar = qtcEnvironmentVariable("HOME");
avdEnvVar.append("/.android/avd");
}
const FilePath avdPath = FilePath::fromUserInput(avdEnvVar);
m_avdFileSystemWatcher.addPath(avdPath.toString());
connect(&m_avdsFutureWatcher, &QFutureWatcherBase::finished, connect(&m_avdsFutureWatcher, &QFutureWatcherBase::finished,
this, &AndroidDeviceManager::HandleAvdsListChange); this, &AndroidDeviceManager::HandleAvdsListChange);
connect(&m_avdFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, [this] { connect(&m_avdFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, [this] {
@@ -708,6 +712,25 @@ void AndroidDeviceManager::setupDevicesWatcher()
updateAvdsList(); updateAvdsList();
} }
IDevice::Ptr AndroidDeviceManager::createDeviceFromInfo(const CreateAvdInfo &info)
{
if (info.apiLevel < 0) {
qCWarning(androidDeviceLog) << "System image of the created AVD is nullptr";
return IDevice::Ptr();
}
AndroidDevice *dev = new AndroidDevice;
const Utils::Id deviceId = AndroidDevice::idFromAvdInfo(info);
dev->setupId(IDevice::AutoDetected, deviceId);
dev->setMachineType(IDevice::Emulator);
dev->settings()->displayName.setValue(info.name);
dev->setDeviceState(IDevice::DeviceConnected);
dev->setAvdPath(avdFilePath() / (info.name + ".avd"));
dev->setExtraData(Constants::AndroidAvdName, info.name);
dev->setExtraData(Constants::AndroidCpuAbi, {info.abi});
dev->setExtraData(Constants::AndroidSdk, info.apiLevel);
return IDevice::Ptr(dev);
}
void AndroidDeviceManager::HandleAvdsListChange() void AndroidDeviceManager::HandleAvdsListChange()
{ {
DeviceManager *const devMgr = DeviceManager::instance(); DeviceManager *const devMgr = DeviceManager::instance();
@@ -892,16 +915,15 @@ public:
if (dialog.exec() != QDialog::Accepted) if (dialog.exec() != QDialog::Accepted)
return IDevice::Ptr(); return IDevice::Ptr();
const IDevice::Ptr dev = dialog.device(); const IDevice::Ptr dev = AndroidDeviceManager::createDeviceFromInfo(dialog.avdInfo());
if (const auto androidDev = static_cast<AndroidDevice *>(dev.get())) { if (const auto androidDev = static_cast<AndroidDevice *>(dev.get())) {
qCDebug(androidDeviceLog, "Created new Android AVD id \"%s\".", qCDebug(androidDeviceLog, "Created new Android AVD id \"%s\".",
qPrintable(androidDev->avdName())); qPrintable(androidDev->avdName()));
} else { return dev;
AndroidDeviceWidget::criticalDialog(
Tr::tr("The device info returned from AvdDialog is invalid."));
} }
AndroidDeviceWidget::criticalDialog(
return IDevice::Ptr(dev); Tr::tr("The device info returned from AvdDialog is invalid."));
return IDevice::Ptr();
}); });
} }
}; };

View File

@@ -89,6 +89,8 @@ public:
QString getRunningAvdsSerialNumber(const QString &name) const; QString getRunningAvdsSerialNumber(const QString &name) const;
static ProjectExplorer::IDevice::Ptr createDeviceFromInfo(const CreateAvdInfo &info);
private: private:
explicit AndroidDeviceManager(QObject *parent); explicit AndroidDeviceManager(QObject *parent);
~AndroidDeviceManager(); ~AndroidDeviceManager();

View File

@@ -151,22 +151,9 @@ bool AvdDialog::isValid() const
return !name().isEmpty() && systemImage() && systemImage()->isValid() && !abi().isEmpty(); return !name().isEmpty() && systemImage() && systemImage()->isValid() && !abi().isEmpty();
} }
IDevice::Ptr AvdDialog::device() const CreateAvdInfo AvdDialog::avdInfo() const
{ {
if (m_createdAvdInfo.apiLevel < 0) { return m_createdAvdInfo;
qCWarning(avdDialogLog) << "System image of the created AVD is nullptr";
return IDevice::Ptr();
}
AndroidDevice *dev = new AndroidDevice;
const Utils::Id deviceId = AndroidDevice::idFromAvdInfo(m_createdAvdInfo);
dev->setupId(IDevice::AutoDetected, deviceId);
dev->setMachineType(IDevice::Emulator);
dev->settings()->displayName.setValue(m_createdAvdInfo.name);
dev->setDeviceState(IDevice::DeviceConnected);
dev->setExtraData(Constants::AndroidAvdName, m_createdAvdInfo.name);
dev->setExtraData(Constants::AndroidCpuAbi, {m_createdAvdInfo.abi});
dev->setExtraData(Constants::AndroidSdk, m_createdAvdInfo.apiLevel);
return IDevice::Ptr(dev);
} }
AvdDialog::DeviceType AvdDialog::tagToDeviceType(const QString &type_tag) AvdDialog::DeviceType AvdDialog::tagToDeviceType(const QString &type_tag)

View File

@@ -35,7 +35,7 @@ public:
enum DeviceType { Phone, Tablet, Automotive, TV, Wear, Desktop, PhoneOrTablet }; enum DeviceType { Phone, Tablet, Automotive, TV, Wear, Desktop, PhoneOrTablet };
ProjectExplorer::IDevice::Ptr device() const; CreateAvdInfo avdInfo() const;
const SystemImage *systemImage() const; const SystemImage *systemImage() const;
QString name() const; QString name() const;