forked from qt-creator/qt-creator
Android: FilePath-ify AVD handling code
Change-Id: Id08414f8fb9ce7f4fac5221cd24392e25f02f00d Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -158,29 +158,30 @@ bool AndroidAvdManager::removeAvd(const QString &name) const
|
|||||||
return proc.result() == ProcessResult::FinishedWithSuccess;
|
return proc.result() == ProcessResult::FinishedWithSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void avdConfigEditManufacturerTag(const QString &avdPathStr, bool recoverMode = false)
|
static void avdConfigEditManufacturerTag(const FilePath &avdPath, bool recoverMode = false)
|
||||||
{
|
{
|
||||||
const FilePath avdPath = FilePath::fromString(avdPathStr);
|
if (!avdPath.exists())
|
||||||
if (avdPath.exists()) {
|
return;
|
||||||
const QString configFilePath = avdPath.pathAppended("config.ini").toString();
|
|
||||||
QFile configFile(configFilePath);
|
const FilePath configFilePath = avdPath / "config.ini";
|
||||||
if (configFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
FileReader reader;
|
||||||
QString newContent;
|
if (!reader.fetch(configFilePath, QIODevice::ReadOnly | QIODevice::Text))
|
||||||
QTextStream textStream(&configFile);
|
return;
|
||||||
|
|
||||||
|
FileSaver saver(configFilePath);
|
||||||
|
QTextStream textStream(reader.data());
|
||||||
while (!textStream.atEnd()) {
|
while (!textStream.atEnd()) {
|
||||||
QString line = textStream.readLine();
|
QString line = textStream.readLine();
|
||||||
if (!line.contains("hw.device.manufacturer"))
|
if (line.contains("hw.device.manufacturer")) {
|
||||||
newContent.append(line + "\n");
|
if (recoverMode)
|
||||||
else if (recoverMode)
|
line.replace("#", "");
|
||||||
newContent.append(line.replace("#", "") + "\n");
|
|
||||||
else
|
else
|
||||||
newContent.append("#" + line + "\n");
|
line.prepend("#");
|
||||||
}
|
|
||||||
configFile.resize(0);
|
|
||||||
textStream << newContent;
|
|
||||||
configFile.close();
|
|
||||||
}
|
}
|
||||||
|
line.append("\n");
|
||||||
|
saver.write(line.toUtf8());
|
||||||
}
|
}
|
||||||
|
saver.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
static AndroidDeviceInfoList listVirtualDevices(const AndroidConfig &config)
|
static AndroidDeviceInfoList listVirtualDevices(const AndroidConfig &config)
|
||||||
@@ -195,8 +196,8 @@ static AndroidDeviceInfoList listVirtualDevices(const AndroidConfig &config)
|
|||||||
otherwise, Android Studio would give an error during parsing also. So this fix
|
otherwise, Android Studio would give an error during parsing also. So this fix
|
||||||
aim to keep support for Qt Creator and Android Studio.
|
aim to keep support for Qt Creator and Android Studio.
|
||||||
*/
|
*/
|
||||||
QStringList allAvdErrorPaths;
|
FilePaths allAvdErrorPaths;
|
||||||
QStringList avdErrorPaths;
|
FilePaths avdErrorPaths;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!AndroidAvdManager::avdManagerCommand(config, {"list", "avd"}, &output)) {
|
if (!AndroidAvdManager::avdManagerCommand(config, {"list", "avd"}, &output)) {
|
||||||
@@ -208,12 +209,12 @@ static AndroidDeviceInfoList listVirtualDevices(const AndroidConfig &config)
|
|||||||
avdErrorPaths.clear();
|
avdErrorPaths.clear();
|
||||||
avdList = parseAvdList(output, &avdErrorPaths);
|
avdList = parseAvdList(output, &avdErrorPaths);
|
||||||
allAvdErrorPaths << avdErrorPaths;
|
allAvdErrorPaths << avdErrorPaths;
|
||||||
for (const QString &avdPathStr : std::as_const(avdErrorPaths))
|
for (const FilePath &avdPath : std::as_const(avdErrorPaths))
|
||||||
avdConfigEditManufacturerTag(avdPathStr); // comment out manufacturer tag
|
avdConfigEditManufacturerTag(avdPath); // comment out manufacturer tag
|
||||||
} while (!avdErrorPaths.isEmpty()); // try again
|
} while (!avdErrorPaths.isEmpty()); // try again
|
||||||
|
|
||||||
for (const QString &avdPathStr : std::as_const(allAvdErrorPaths))
|
for (const FilePath &avdPath : std::as_const(allAvdErrorPaths))
|
||||||
avdConfigEditManufacturerTag(avdPathStr, true); // re-add manufacturer tag
|
avdConfigEditManufacturerTag(avdPath, true); // re-add manufacturer tag
|
||||||
|
|
||||||
return avdList;
|
return avdList;
|
||||||
}
|
}
|
||||||
|
@@ -86,19 +86,19 @@ static std::optional<AndroidDeviceInfo> parseAvd(const QStringList &deviceInfo)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
AndroidDeviceInfoList parseAvdList(const QString &output, QStringList *avdErrorPaths)
|
AndroidDeviceInfoList parseAvdList(const QString &output, Utils::FilePaths *avdErrorPaths)
|
||||||
{
|
{
|
||||||
QTC_CHECK(avdErrorPaths);
|
QTC_CHECK(avdErrorPaths);
|
||||||
AndroidDeviceInfoList avdList;
|
AndroidDeviceInfoList avdList;
|
||||||
QStringList avdInfo;
|
QStringList avdInfo;
|
||||||
using ErrorPath = QString;
|
using ErrorPath = Utils::FilePath;
|
||||||
using AvdResult = std::variant<std::monostate, AndroidDeviceInfo, ErrorPath>;
|
using AvdResult = std::variant<std::monostate, AndroidDeviceInfo, ErrorPath>;
|
||||||
const auto parseAvdInfo = [](const QStringList &avdInfo) {
|
const auto parseAvdInfo = [](const QStringList &avdInfo) {
|
||||||
if (!avdInfo.filter(avdManufacturerError).isEmpty()) {
|
if (!avdInfo.filter(avdManufacturerError).isEmpty()) {
|
||||||
for (const QString &line : avdInfo) {
|
for (const QString &line : avdInfo) {
|
||||||
QString value;
|
QString value;
|
||||||
if (valueForKey(avdInfoPathKey, line, &value))
|
if (valueForKey(avdInfoPathKey, line, &value))
|
||||||
return AvdResult(value); // error path
|
return AvdResult(Utils::FilePath::fromString(value)); // error path
|
||||||
}
|
}
|
||||||
} else if (std::optional<AndroidDeviceInfo> avd = parseAvd(avdInfo)) {
|
} else if (std::optional<AndroidDeviceInfo> avd = parseAvd(avdInfo)) {
|
||||||
// armeabi-v7a devices can also run armeabi code
|
// armeabi-v7a devices can also run armeabi code
|
||||||
|
@@ -8,7 +8,7 @@ namespace Internal {
|
|||||||
|
|
||||||
const char avdManufacturerError[] = "no longer exists as a device";
|
const char avdManufacturerError[] = "no longer exists as a device";
|
||||||
|
|
||||||
AndroidDeviceInfoList parseAvdList(const QString &output, QStringList *avdErrorPaths);
|
AndroidDeviceInfoList parseAvdList(const QString &output, Utils::FilePaths *avdErrorPaths);
|
||||||
int platformNameToApiLevel(const QString &platformName);
|
int platformNameToApiLevel(const QString &platformName);
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
@@ -21,10 +21,10 @@ void tst_AvdManagerOutputParser::parse_data()
|
|||||||
{
|
{
|
||||||
QTest::addColumn<QString>("input");
|
QTest::addColumn<QString>("input");
|
||||||
QTest::addColumn<AndroidDeviceInfoList>("output");
|
QTest::addColumn<AndroidDeviceInfoList>("output");
|
||||||
QTest::addColumn<QStringList>("errorPaths");
|
QTest::addColumn<Utils::FilePaths>("errorPaths");
|
||||||
|
|
||||||
QTest::newRow("none") << "Available Android Virtual Devices:\n"
|
QTest::newRow("none") << "Available Android Virtual Devices:\n"
|
||||||
<< AndroidDeviceInfoList() << QStringList();
|
<< AndroidDeviceInfoList() << Utils::FilePaths();
|
||||||
|
|
||||||
QTest::newRow("one") << "Available Android Virtual Devices:\n"
|
QTest::newRow("one") << "Available Android Virtual Devices:\n"
|
||||||
" Name: Test\n"
|
" Name: Test\n"
|
||||||
@@ -40,7 +40,7 @@ void tst_AvdManagerOutputParser::parse_data()
|
|||||||
IDevice::DeviceConnected,
|
IDevice::DeviceConnected,
|
||||||
IDevice::Emulator,
|
IDevice::Emulator,
|
||||||
Utils::FilePath::fromString(":/Test.avd")}})
|
Utils::FilePath::fromString(":/Test.avd")}})
|
||||||
<< QStringList();
|
<< Utils::FilePaths();
|
||||||
|
|
||||||
QTest::newRow("two") << "Available Android Virtual Devices:\n"
|
QTest::newRow("two") << "Available Android Virtual Devices:\n"
|
||||||
" Name: Test\n"
|
" Name: Test\n"
|
||||||
@@ -71,16 +71,16 @@ void tst_AvdManagerOutputParser::parse_data()
|
|||||||
IDevice::Emulator,
|
IDevice::Emulator,
|
||||||
Utils::FilePath::fromString(":/TestTablet.avd")}}
|
Utils::FilePath::fromString(":/TestTablet.avd")}}
|
||||||
)
|
)
|
||||||
<< QStringList();
|
<< Utils::FilePaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_AvdManagerOutputParser::parse()
|
void tst_AvdManagerOutputParser::parse()
|
||||||
{
|
{
|
||||||
QFETCH(QString, input);
|
QFETCH(QString, input);
|
||||||
QFETCH(AndroidDeviceInfoList, output);
|
QFETCH(AndroidDeviceInfoList, output);
|
||||||
QFETCH(QStringList, errorPaths);
|
QFETCH(Utils::FilePaths, errorPaths);
|
||||||
|
|
||||||
QStringList avdErrorPaths;
|
Utils::FilePaths avdErrorPaths;
|
||||||
const auto result = parseAvdList(input, &avdErrorPaths);
|
const auto result = parseAvdList(input, &avdErrorPaths);
|
||||||
QCOMPARE(result, output);
|
QCOMPARE(result, output);
|
||||||
QCOMPARE(avdErrorPaths, errorPaths);
|
QCOMPARE(avdErrorPaths, errorPaths);
|
||||||
|
Reference in New Issue
Block a user