forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadbstatethread.cpp
127 lines (114 loc) · 4.02 KB
/
adbstatethread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "adbstatethread.hpp"
#include <QtCore/QProcess>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QFile>
#include <QtCore/QDir>
#include <cassert>
#include <QtDebug>
#include <QtCore/QThread>
#include "bhj_help.hpp"
#include "t1wrench.h"
bool gScreenCapJpg;
AdbStateThread::AdbStateThread(QObject* parent)
: QThread(parent)
{
}
AdbStateThread::~AdbStateThread()
{
}
void die(const char* how)
{
fprintf(stderr, "%s\n", how);
exit(1);
}
QString getExecutionOutput(const QString& cmd, const QStringList& args)
{
QProcess p;
p.setStandardOutputFile(QDir::temp().filePath(QString().sprintf("adb-state-repair-center.%lx", long(QThread::currentThreadId()))));
p.setStandardErrorFile(QDir::temp().filePath(QString().sprintf("adb-state-repair-center.%lx", long(QThread::currentThreadId()))));
p.start(cmd, args);
if (!p.waitForFinished(-1)) {
p.kill();
p.waitForFinished();
} else {
QFile result(QDir::temp().filePath(QString().sprintf("adb-state-repair-center.%lx", long(QThread::currentThreadId()))));
result.open(QIODevice::ReadOnly);
QString ret = result.readAll();
ret.replace("\r", "");
while (ret.endsWith("\n")) {
ret.chop(1);
}
result.close();
result.remove();
return ret;
}
return "";
}
QString getStcmdCmdResult(const QString& cmd, bool stripRetCode)
{
QString res = getExecutionOutput("adb shell stcmd-subcase.sh " + cmd);
if (stripRetCode && res.endsWith("ret: 0")) {
res = res.left(res.length() - strlen("ret: 0"));
}
while (res.endsWith("\n")) {
res.chop(1);
}
return res;
}
QString getExecutionOutput(const QString& cmd)
{
if (cmd.contains(" ")) {
QStringList args = cmd.split(" ");
QString new_cmd = args.front();
args.pop_front();
return getExecutionOutput(new_cmd, args);
} else
return getExecutionOutput(cmd, QStringList());
}
void AdbStateThread::run()
{
QDir tmpdir = QDir::temp();
QString version = getExecutionOutput("the-true-adb version");
bool adb_1031 = 0;
if (version.contains("1.0.31")) {
adb_1031 = 1;
}
while (1) {
// at the start, suppose the adb not connected.
QStringList args1 = QStringList() << "shell" << "sh" << "-c" << "'if test \"$(getprop sys.boot_completed)\" = 1; then uname || busybox uname || { echo -n Lin && echo -n ux; }; fi'";
QStringList args2 = QStringList() << "shell" << "sh" << "-c" << "'sleep 300; uname || busybox uname || { echo -n Lin && echo -n ux; }'";
if (adb_1031) {
QString last = args1.last();
args1.pop_back();
args1 += last.replace('\'', ' ');
last = args2.last();
args2.pop_back();
args2 += last.replace('\'', ' ');
}
QString uname = getExecutionOutput("the-true-adb", args1);
static QString lastAdbState;
if (uname.contains("Linux")) {
qDebug() << "adb output is " << uname;
if (lastAdbState != "Online") {
lastAdbState = "Online";
QString screencapHelp = getExecutionOutput("the-true-adb shell screencap -h");
qDebug() << screencapHelp;
if (screencapHelp.contains("jpg")) {
gScreenCapJpg = 1;
} else {
gScreenCapJpg = 0;
}
}
emit adbStateUpdate("Online");
system("the-true-adb shell am startservice -n com.bhj.setclip/.PutClipService --ei getapk 1");
system("the-true-adb forward tcp:28888 localabstract:T1Wrench");
QString apk = getExecutionOutput("the-true-adb shell cat /sdcard/setclip-apk.txt");
qSystem("the-true-adb shell env CLASSPATH=" + apk + " app_process /system/bin/ Input");
} else {
lastAdbState = "Offline";
qDebug() << "Offline: adb output is " << uname;
emit adbStateUpdate("Offline");
QThread::msleep(500);
}
}
}