2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2021-05-06 15:19:56 +02:00
|
|
|
|
|
|
|
|
#include "avdmanageroutputparser.h"
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QLoggingCategory>
|
2022-02-02 16:32:24 +01:00
|
|
|
#include <QRegularExpression>
|
2021-05-06 15:19:56 +02:00
|
|
|
#include <QSettings>
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
#include <optional>
|
2022-08-19 14:47:59 +02:00
|
|
|
#include <variant>
|
|
|
|
|
|
2021-05-06 15:19:56 +02:00
|
|
|
namespace {
|
|
|
|
|
Q_LOGGING_CATEGORY(avdOutputParserLog, "qtc.android.avdOutputParser", QtWarningMsg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avd list keys to parse avd
|
|
|
|
|
const char avdInfoNameKey[] = "Name:";
|
|
|
|
|
const char avdInfoPathKey[] = "Path:";
|
|
|
|
|
const char avdInfoAbiKey[] = "abi.type";
|
|
|
|
|
const char avdInfoTargetKey[] = "target";
|
|
|
|
|
const char avdInfoErrorKey[] = "Error:";
|
|
|
|
|
|
|
|
|
|
namespace Android {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Parses the \a line for a [spaces]key[spaces]value[spaces] pattern and returns
|
|
|
|
|
\c true if the key is found, \c false otherwise. The value is copied into \a value.
|
|
|
|
|
*/
|
|
|
|
|
static bool valueForKey(QString key, const QString &line, QString *value = nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto trimmedInput = line.trimmed();
|
|
|
|
|
if (trimmedInput.startsWith(key)) {
|
|
|
|
|
if (value)
|
|
|
|
|
*value = trimmedInput.section(key, 1, 1).trimmed();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
static std::optional<AndroidDeviceInfo> parseAvd(const QStringList &deviceInfo)
|
2021-05-06 15:19:56 +02:00
|
|
|
{
|
2021-05-07 11:17:29 +02:00
|
|
|
AndroidDeviceInfo avd;
|
2021-05-06 15:19:56 +02:00
|
|
|
for (const QString &line : deviceInfo) {
|
|
|
|
|
QString value;
|
|
|
|
|
if (valueForKey(avdInfoErrorKey, line)) {
|
|
|
|
|
qCDebug(avdOutputParserLog) << "Avd Parsing: Skip avd device. Error key found:" << line;
|
2021-05-07 11:17:29 +02:00
|
|
|
return {};
|
2021-05-06 15:19:56 +02:00
|
|
|
} else if (valueForKey(avdInfoNameKey, line, &value)) {
|
2021-10-13 21:18:13 +03:00
|
|
|
avd.avdName = value;
|
2021-05-06 15:19:56 +02:00
|
|
|
} else if (valueForKey(avdInfoPathKey, line, &value)) {
|
2021-09-23 17:38:58 +02:00
|
|
|
const Utils::FilePath avdPath = Utils::FilePath::fromUserInput(value);
|
2021-10-13 21:16:47 +03:00
|
|
|
avd.avdPath = avdPath;
|
2021-05-06 15:19:56 +02:00
|
|
|
if (avdPath.exists()) {
|
|
|
|
|
// Get ABI.
|
|
|
|
|
const Utils::FilePath configFile = avdPath.pathAppended("config.ini");
|
|
|
|
|
QSettings config(configFile.toString(), QSettings::IniFormat);
|
|
|
|
|
value = config.value(avdInfoAbiKey).toString();
|
|
|
|
|
if (!value.isEmpty())
|
2021-05-07 11:17:29 +02:00
|
|
|
avd.cpuAbi << value;
|
2021-05-06 15:19:56 +02:00
|
|
|
else
|
|
|
|
|
qCDebug(avdOutputParserLog) << "Avd Parsing: Cannot find ABI:" << configFile;
|
|
|
|
|
|
|
|
|
|
// Get Target
|
2021-10-13 21:18:13 +03:00
|
|
|
const QString avdInfoFileName = avd.avdName + ".ini";
|
2021-05-06 15:19:56 +02:00
|
|
|
const Utils::FilePath avdInfoFile = avdPath.parentDir().pathAppended(
|
|
|
|
|
avdInfoFileName);
|
|
|
|
|
QSettings avdInfo(avdInfoFile.toString(), QSettings::IniFormat);
|
|
|
|
|
value = avdInfo.value(avdInfoTargetKey).toString();
|
|
|
|
|
if (!value.isEmpty())
|
2022-02-02 16:32:24 +01:00
|
|
|
avd.sdk = platformNameToApiLevel(value);
|
2021-05-06 15:19:56 +02:00
|
|
|
else
|
|
|
|
|
qCDebug(avdOutputParserLog)
|
|
|
|
|
<< "Avd Parsing: Cannot find sdk API:" << avdInfoFile.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 11:17:29 +02:00
|
|
|
if (avd != AndroidDeviceInfo())
|
|
|
|
|
return avd;
|
|
|
|
|
return {};
|
2021-05-06 15:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 19:43:34 +01:00
|
|
|
AndroidDeviceInfoList parseAvdList(const QString &output, Utils::FilePaths *avdErrorPaths)
|
2021-05-06 15:19:56 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(avdErrorPaths);
|
|
|
|
|
AndroidDeviceInfoList avdList;
|
|
|
|
|
QStringList avdInfo;
|
2022-11-25 19:43:34 +01:00
|
|
|
using ErrorPath = Utils::FilePath;
|
2022-08-19 14:47:59 +02:00
|
|
|
using AvdResult = std::variant<std::monostate, AndroidDeviceInfo, ErrorPath>;
|
2021-05-06 15:19:56 +02:00
|
|
|
const auto parseAvdInfo = [](const QStringList &avdInfo) {
|
|
|
|
|
if (!avdInfo.filter(avdManufacturerError).isEmpty()) {
|
|
|
|
|
for (const QString &line : avdInfo) {
|
|
|
|
|
QString value;
|
|
|
|
|
if (valueForKey(avdInfoPathKey, line, &value))
|
2022-11-25 19:43:34 +01:00
|
|
|
return AvdResult(Utils::FilePath::fromString(value)); // error path
|
2021-05-06 15:19:56 +02:00
|
|
|
}
|
2022-08-26 10:30:00 +02:00
|
|
|
} else if (std::optional<AndroidDeviceInfo> avd = parseAvd(avdInfo)) {
|
2021-05-06 15:19:56 +02:00
|
|
|
// armeabi-v7a devices can also run armeabi code
|
2021-10-06 22:35:12 +03:00
|
|
|
if (avd->cpuAbi.contains(Constants::ANDROID_ABI_ARMEABI_V7A))
|
|
|
|
|
avd->cpuAbi << Constants::ANDROID_ABI_ARMEABI;
|
|
|
|
|
avd->state = IDevice::DeviceConnected;
|
|
|
|
|
avd->type = IDevice::Emulator;
|
2021-05-07 11:17:29 +02:00
|
|
|
return AvdResult(*avd);
|
2021-05-06 15:19:56 +02:00
|
|
|
} else {
|
|
|
|
|
qCDebug(avdOutputParserLog) << "Avd Parsing: Parsing failed: " << avdInfo;
|
|
|
|
|
}
|
|
|
|
|
return AvdResult();
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-28 18:22:44 +03:00
|
|
|
const auto lines = output.split('\n');
|
|
|
|
|
for (const QString &line : lines) {
|
2021-05-06 15:19:56 +02:00
|
|
|
if (line.startsWith("---------") || line.isEmpty()) {
|
|
|
|
|
const AvdResult result = parseAvdInfo(avdInfo);
|
2022-08-19 14:47:59 +02:00
|
|
|
if (auto info = std::get_if<AndroidDeviceInfo>(&result))
|
2021-05-06 15:19:56 +02:00
|
|
|
avdList << *info;
|
2022-08-19 14:47:59 +02:00
|
|
|
else if (auto errorPath = std::get_if<ErrorPath>(&result))
|
2021-05-06 15:19:56 +02:00
|
|
|
*avdErrorPaths << *errorPath;
|
|
|
|
|
avdInfo.clear();
|
|
|
|
|
} else {
|
|
|
|
|
avdInfo << line;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 14:05:12 +02:00
|
|
|
return Utils::sorted(std::move(avdList));
|
2021-05-06 15:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-02 16:32:24 +01:00
|
|
|
int platformNameToApiLevel(const QString &platformName)
|
|
|
|
|
{
|
|
|
|
|
int apiLevel = -1;
|
|
|
|
|
static const QRegularExpression re("(android-)(?<apiLevel>[0-9A-Z]{1,})",
|
|
|
|
|
QRegularExpression::CaseInsensitiveOption);
|
|
|
|
|
QRegularExpressionMatch match = re.match(platformName);
|
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
|
QString apiLevelStr = match.captured("apiLevel");
|
|
|
|
|
bool isUInt;
|
|
|
|
|
apiLevel = apiLevelStr.toUInt(&isUInt);
|
|
|
|
|
if (!isUInt) {
|
|
|
|
|
if (apiLevelStr == 'Q')
|
|
|
|
|
apiLevel = 29;
|
|
|
|
|
else if (apiLevelStr == 'R')
|
|
|
|
|
apiLevel = 30;
|
|
|
|
|
else if (apiLevelStr == 'S')
|
|
|
|
|
apiLevel = 31;
|
2022-03-06 00:52:04 +02:00
|
|
|
else if (apiLevelStr == "Tiramisu")
|
|
|
|
|
apiLevel = 33;
|
2022-02-02 16:32:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return apiLevel;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 15:19:56 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Android
|