1
0
forked from BRT/arc
arc/automation_script/adb_fun.cpp
2025-04-25 15:42:51 +04:00

61 lines
1.7 KiB
C++

#include "adb_fun.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <array>
#include <ctime>
#include <iomanip>
std::string exec(const std::string& cmd) {
std::array<char, 256> buffer{};
std::string result;
FILE* pipe = _popen(cmd.c_str(), "r");
if (!pipe) return "ERROR";
while (fgets(buffer.data(), buffer.size(), pipe)) {
result += buffer.data();
}
_pclose(pipe);
return result;
}
bool checkDevices() {
std::string out = exec("adb devices");
return out.find("\tdevice") != std::string::npos;
}
bool startApp(const std::string& packageName) {
std::string cmd = "adb shell monkey -p " + packageName + " -c android.intent.category.LAUNCHER 1";
return system(cmd.c_str()) == 0;
}
bool tap(int x, int y) {
std::stringstream ss;
ss << "adb shell input tap " << x << " " << y;
return system(ss.str().c_str()) == 0;
}
bool swipe(const int x1, const int y1, const int x2, const int y2) {
std::stringstream ss;
ss << "adb shell input swipe " << x1 << " " << y1 << " " << x2 << " " << y2;
return system(ss.str().c_str()) == 0;
}
bool inputText(const std::string& text) {
std::stringstream ss;
ss << "adb shell input text \"" << text << "\"";
return system(ss.str().c_str()) == 0;
}
bool takeScreenshot(const std::string& filename) {
system("mkdir screens 2>nul"); // Windows
std::string cmd = "adb exec-out screencap -p > screens/" + filename;
return system(cmd.c_str()) == 0;
}
void 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;
}