diff --git a/android/adb/AdbUtils.cpp b/android/adb/AdbUtils.cpp index d73d1cf..4ece65f 100644 --- a/android/adb/AdbUtils.cpp +++ b/android/adb/AdbUtils.cpp @@ -9,12 +9,36 @@ AdbUtils::AdbUtils(QObject *parent) : QObject(parent) { QString AdbUtils::adbPath() { #ifdef Q_OS_WIN - return QCoreApplication::applicationDirPath() + "/tools/adb/adb.exe"; + return QCoreApplication::applicationDirPath() + "/tools/adb/windows/adb.exe"; #else return QCoreApplication::applicationDirPath() + "/tools/adb/macos/adb"; #endif } +bool AdbUtils::startProcess(const QString &cmd, QString *stdOut = nullptr, QString *stdErr = nullptr) { + QProcess process; + +#ifdef Q_OS_WIN + process.start("cmd.exe", {"/C", cmd}); +#else + process.start("sh", {"-c", cmd}); +#endif + + if (!process.waitForFinished() || + process.exitStatus() != QProcess::NormalExit || + process.exitCode() != 0) { + if (stdErr) { + *stdErr = QString::fromUtf8(process.readAllStandardError()); + } + return false; + } + + if (stdOut) { + *stdOut = QString::fromUtf8(process.readAllStandardOutput()); + } + return true; +} + // adb shell dumpsys package ru.rshb.dbo | grep permission // TODO доделать обработку пермишинов // adb shell pm revoke ru.rshb.dbo android.permission.WRITE_CONTACTS; adb shell pm revoke ru.rshb.dbo android.permission.READ_CONTACTS; adb shell pm revoke ru.rshb.dbo android.permission.ACCESS_COARSE_LOCATION; adb shell pm revoke ru.rshb.dbo android.permission.ACCESS_FINE_LOCATION @@ -26,7 +50,7 @@ QString AdbUtils::adbPath() { */ QList > AdbUtils::getListDevices() { QProcess process; - process.start("adb", {"devices"}); + process.start(adbPath(), {"devices"}); process.waitForFinished(); const QString output = process.readAllStandardOutput(); @@ -56,7 +80,7 @@ QList > AdbUtils::getListDevices() { QString AdbUtils::tryToGetRunningAppPid(const QString &deviceId, const QString &packageName) { QProcess process; - process.start("adb", { + process.start(adbPath(), { "-s", deviceId, "shell", "pidof", packageName }); @@ -67,7 +91,7 @@ QString AdbUtils::tryToGetRunningAppPid(const QString &deviceId, const QString & bool AdbUtils::tryToStartApplication(const QString &deviceId, const QString &packageName) { QProcess process; - process.start("adb", { + process.start(adbPath(), { "-s", deviceId, "shell", "monkey", "-p", packageName, @@ -90,7 +114,7 @@ bool AdbUtils::tryToStartApplication(const QString &deviceId, const QString &pac bool AdbUtils::tryToKillApplication(const QString &deviceId, const QString &packageName) { QProcess process; - process.start("adb", { + process.start(adbPath(), { "-s", deviceId, "shell", "am", "force-stop", packageName @@ -101,65 +125,73 @@ bool AdbUtils::tryToKillApplication(const QString &deviceId, const QString &pack // idx - временный лог // adb -s 6edc4a47 shell 'uiautomator dump /sdcard/uidump.xml' && adb -s 6edc4a47 pull /sdcard/uidump.xml step_1.xml && adb -s 6edc4a47 shell 'rm /sdcard/uidump.xml' -QString AdbUtils::getScreenDumpAsXml(const QString &deviceId, const int idx, const bool screenshot) { - // TODO убрать логирование в xml файл - // "adb -s %1 pull /sdcard/uidump.xml step_%2.xml && " - QProcess process; - QString xml; - if (screenshot) { - xml = QString( - "adb -s %1 shell 'uiautomator dump /sdcard/uidump.xml' && " - "adb -s %1 pull /sdcard/uidump.xml step_%2.xml && " - "adb -s %1 shell 'cat /sdcard/uidump.xml' && " - "adb -s %1 shell 'rm /sdcard/uidump.xml'" - ).arg(deviceId, QString::number(idx)); - } else { - xml = QString( - "adb -s %1 shell 'uiautomator dump /sdcard/uidump.xml' && " - "adb -s %1 shell 'cat /sdcard/uidump.xml' && " - "adb -s %1 shell 'rm /sdcard/uidump.xml'" - ).arg(deviceId); +QString AdbUtils::getScreenDumpAsXml(const QString &deviceId, int idx, bool screenshot) { + const QString adb = adbPath(); + QString output; + + // 1. Dump UI hierarchy + QString dumpCmd = QString("%1 -s %2 shell uiautomator dump /sdcard/uidump.xml").arg(adb, deviceId); + if (!startProcess(dumpCmd)) { + qWarning() << "Failed to dump UI hierarchy."; + return {}; } - - process.start("sh", {"-c", xml}); - process.waitForFinished(); - + // 2. Optionally pull and save the XML file if (screenshot) { - // TODO делаем скрин, убрать потом - // adb -s 6edc4a47 shell 'screencap -p /sdcard/screen.png' && adb -s 6edc4a47 pull /sdcard/screen.png screen_step_1.png && adb -s 6edc4a47 shell 'rm /sdcard/screen.png' - QProcess processScreenshot; - QString screenshot = QString( - "adb -s %1 shell 'screencap -p /sdcard/screen.png' && " - "adb -s %1 pull /sdcard/screen.png screen_step_%2.png && " - "adb -s %1 shell 'rm /sdcard/screen.png'" - ).arg(deviceId, QString::number(idx)); - processScreenshot.start("sh", {"-c", screenshot}); - processScreenshot.waitForFinished(); + QString pullCmd = QString("%1 -s %2 pull /sdcard/uidump.xml step_%3.xml").arg(adb, deviceId).arg(idx); + if (!startProcess(pullCmd)) { + qWarning() << "Failed to pull UIDump XML."; + return {}; + } } - - // чтобы убедиться, что мы прочитали без ошибок - if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) { + // 3. Read the XML content + QString catCmd = QString("%1 -s %2 shell cat /sdcard/uidump.xml").arg(adb, deviceId); + QString xmlOut; + if (!startProcess(catCmd, &xmlOut)) { + qWarning() << "Failed to read UIDump XML."; return {}; } - QByteArray xmlBytes = process.readAllStandardOutput(); - // UI hierchary dumped to: /sdcard/uidump.xml\n - if (const int startIndex = xmlBytes.indexOf(" AdbUtils::getListApps(const QString &deviceId) { QProcess process; - process.start("adb", { + process.start(adbPath(), { "-s", deviceId, "shell", "pm list", "packages", "-3" diff --git a/android/adb/AdbUtils.h b/android/adb/AdbUtils.h index 30778de..d9a6ec8 100644 --- a/android/adb/AdbUtils.h +++ b/android/adb/AdbUtils.h @@ -40,4 +40,7 @@ public: static bool goBack(const QString &deviceId); static QSet getListApps(const QString &deviceId); + +private: + static bool startProcess(const QString &cmd, QString *stdOut, QString *stdErr); }; diff --git a/database/app_data.db b/database/app_data.db index 79ac493..9704212 100644 Binary files a/database/app_data.db and b/database/app_data.db differ diff --git a/main.cpp b/main.cpp index c4b8a84..678b242 100644 --- a/main.cpp +++ b/main.cpp @@ -145,11 +145,9 @@ int main(int argc, char *argv[]) { // 79641815146 - Мама Альфа // 79199196105 - Папа рсхб - // startNetworkService(app); - // startDeviceScreener(app); - // startEventHandler(app); - - AdbUtils::takeXmlDump("29301FDH20036X", "test"); + startNetworkService(app); + startDeviceScreener(app); + startEventHandler(app); MainWindow window; window.show();