85 lines
3.0 KiB
C++
85 lines
3.0 KiB
C++
#include "AdbUtils.h"
|
|
#include <QProcess>
|
|
#include <QString>
|
|
#include <QDir>
|
|
#include <fstream>
|
|
|
|
AdbUtils::AdbUtils(QObject *parent) : QObject(parent) {
|
|
}
|
|
|
|
bool AdbUtils::checkDevices() {
|
|
QProcess process;
|
|
process.start("adb devices");
|
|
process.waitForFinished();
|
|
QString output = process.readAllStandardOutput();
|
|
return output.contains("device");
|
|
}
|
|
|
|
bool AdbUtils::startApp(const QString &packageName) {
|
|
QProcess process;
|
|
QString command = QString("adb shell monkey -p %1 -c android.intent.category.LAUNCHER 1").arg(packageName);
|
|
process.start(command);
|
|
process.waitForFinished();
|
|
return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
|
|
}
|
|
|
|
bool AdbUtils::dumpAndPullUiXml(const QString &localPath) {
|
|
QProcess process;
|
|
process.start("adb shell uiautomator dump /sdcard/view.xml");
|
|
process.waitForFinished();
|
|
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
|
|
qDebug() << "Ошибка в получении дампа UI на устройстве";
|
|
return false;
|
|
}
|
|
|
|
QString pullCmd = QString("adb pull /sdcard/view.xml %1").arg(localPath);
|
|
process.start(pullCmd);
|
|
process.waitForFinished();
|
|
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
|
|
qDebug() << "Ошибка в передаче xml файла на компьютер";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AdbUtils::tap(int x, int y) {
|
|
QProcess process;
|
|
QString cmd = QString("adb shell input tap %1 %2").arg(x).arg(y);
|
|
process.start(cmd);
|
|
process.waitForFinished();
|
|
return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
|
|
}
|
|
|
|
bool AdbUtils::swipe(const int x1, const int y1, const int x2, const int y2) {
|
|
QProcess process;
|
|
QString cmd = QString("adb shell input swipe %1 %2 %3 %4").arg(x1).arg(y1).arg(x2).arg(y2);
|
|
process.start(cmd);
|
|
process.waitForFinished();
|
|
return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
|
|
}
|
|
|
|
bool AdbUtils::inputText(const QString &text) {
|
|
QProcess process;
|
|
QString cmd = QString("adb shell input text \"%1\"").arg(text);
|
|
process.start(cmd);
|
|
process.waitForFinished();
|
|
return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
|
|
}
|
|
|
|
// bool AdbUtils::takeScreenshot(const QString& filename) {
|
|
// QProcess process;
|
|
// QDir().mkdir("screens"); // Создаем папку, если её нет
|
|
// QString cmd = QString("adb exec-out screencap -p > screens/%1").arg(filename);
|
|
// process.start(cmd);
|
|
// process.waitForFinished();
|
|
// return process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0;
|
|
// }
|
|
|
|
// void AdbUtils::log(const std::string& message) {
|
|
// std::ofstream out("log.txt", std::ios::app);
|
|
// std::time_t t = std::time(nullptr);
|
|
// out << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << message << std::endl;
|
|
// std::cout << message << std::endl;
|
|
// }
|