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:
Alessandro Portale
2022-11-25 19:43:34 +01:00
parent 34c206c700
commit 9441865714
4 changed files with 38 additions and 37 deletions

View File

@@ -158,29 +158,30 @@ bool AndroidAvdManager::removeAvd(const QString &name) const
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()) {
const QString configFilePath = avdPath.pathAppended("config.ini").toString();
QFile configFile(configFilePath);
if (configFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
QString newContent;
QTextStream textStream(&configFile);
if (!avdPath.exists())
return;
const FilePath configFilePath = avdPath / "config.ini";
FileReader reader;
if (!reader.fetch(configFilePath, QIODevice::ReadOnly | QIODevice::Text))
return;
FileSaver saver(configFilePath);
QTextStream textStream(reader.data());
while (!textStream.atEnd()) {
QString line = textStream.readLine();
if (!line.contains("hw.device.manufacturer"))
newContent.append(line + "\n");
else if (recoverMode)
newContent.append(line.replace("#", "") + "\n");
if (line.contains("hw.device.manufacturer")) {
if (recoverMode)
line.replace("#", "");
else
newContent.append("#" + line + "\n");
}
configFile.resize(0);
textStream << newContent;
configFile.close();
line.prepend("#");
}
line.append("\n");
saver.write(line.toUtf8());
}
saver.finalize();
}
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
aim to keep support for Qt Creator and Android Studio.
*/
QStringList allAvdErrorPaths;
QStringList avdErrorPaths;
FilePaths allAvdErrorPaths;
FilePaths avdErrorPaths;
do {
if (!AndroidAvdManager::avdManagerCommand(config, {"list", "avd"}, &output)) {
@@ -208,12 +209,12 @@ static AndroidDeviceInfoList listVirtualDevices(const AndroidConfig &config)
avdErrorPaths.clear();
avdList = parseAvdList(output, &avdErrorPaths);
allAvdErrorPaths << avdErrorPaths;
for (const QString &avdPathStr : std::as_const(avdErrorPaths))
avdConfigEditManufacturerTag(avdPathStr); // comment out manufacturer tag
for (const FilePath &avdPath : std::as_const(avdErrorPaths))
avdConfigEditManufacturerTag(avdPath); // comment out manufacturer tag
} while (!avdErrorPaths.isEmpty()); // try again
for (const QString &avdPathStr : std::as_const(allAvdErrorPaths))
avdConfigEditManufacturerTag(avdPathStr, true); // re-add manufacturer tag
for (const FilePath &avdPath : std::as_const(allAvdErrorPaths))
avdConfigEditManufacturerTag(avdPath, true); // re-add manufacturer tag
return avdList;
}

View File

@@ -86,19 +86,19 @@ static std::optional<AndroidDeviceInfo> parseAvd(const QStringList &deviceInfo)
return {};
}
AndroidDeviceInfoList parseAvdList(const QString &output, QStringList *avdErrorPaths)
AndroidDeviceInfoList parseAvdList(const QString &output, Utils::FilePaths *avdErrorPaths)
{
QTC_CHECK(avdErrorPaths);
AndroidDeviceInfoList avdList;
QStringList avdInfo;
using ErrorPath = QString;
using ErrorPath = Utils::FilePath;
using AvdResult = std::variant<std::monostate, AndroidDeviceInfo, ErrorPath>;
const auto parseAvdInfo = [](const QStringList &avdInfo) {
if (!avdInfo.filter(avdManufacturerError).isEmpty()) {
for (const QString &line : avdInfo) {
QString 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)) {
// armeabi-v7a devices can also run armeabi code

View File

@@ -8,7 +8,7 @@ namespace Internal {
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);
} // namespace Internal

View File

@@ -21,10 +21,10 @@ void tst_AvdManagerOutputParser::parse_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<AndroidDeviceInfoList>("output");
QTest::addColumn<QStringList>("errorPaths");
QTest::addColumn<Utils::FilePaths>("errorPaths");
QTest::newRow("none") << "Available Android Virtual Devices:\n"
<< AndroidDeviceInfoList() << QStringList();
<< AndroidDeviceInfoList() << Utils::FilePaths();
QTest::newRow("one") << "Available Android Virtual Devices:\n"
" Name: Test\n"
@@ -40,7 +40,7 @@ void tst_AvdManagerOutputParser::parse_data()
IDevice::DeviceConnected,
IDevice::Emulator,
Utils::FilePath::fromString(":/Test.avd")}})
<< QStringList();
<< Utils::FilePaths();
QTest::newRow("two") << "Available Android Virtual Devices:\n"
" Name: Test\n"
@@ -71,16 +71,16 @@ void tst_AvdManagerOutputParser::parse_data()
IDevice::Emulator,
Utils::FilePath::fromString(":/TestTablet.avd")}}
)
<< QStringList();
<< Utils::FilePaths();
}
void tst_AvdManagerOutputParser::parse()
{
QFETCH(QString, input);
QFETCH(AndroidDeviceInfoList, output);
QFETCH(QStringList, errorPaths);
QFETCH(Utils::FilePaths, errorPaths);
QStringList avdErrorPaths;
Utils::FilePaths avdErrorPaths;
const auto result = parseAvdList(input, &avdErrorPaths);
QCOMPARE(result, output);
QCOMPARE(avdErrorPaths, errorPaths);