forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adbnotificationthread.cpp
137 lines (113 loc) · 3.84 KB
/
adbnotificationthread.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
128
129
130
131
132
133
134
135
136
137
#include "adbnotificationthread.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 "wrench.h"
#include <QTime>
#include <QtCore/QJsonDocument>
#include <QJsonObject>
#include <QtCore/QDateTime>
AdbNotificationThread::AdbNotificationThread(QObject* parent)
: QThread(parent)
{
notificationSocket = NULL;
mConnectTimer = NULL;
}
AdbNotificationThread::~AdbNotificationThread()
{
if (notificationSocket) {
notificationSocket->close();
delete notificationSocket;
}
}
void AdbNotificationThread::timeOut()
{
}
void AdbNotificationThread::onDisconnected()
{
if (!mConnectTimer) {
mConnectTimer = new QTimer();
mConnectTimer->setSingleShot(true);
connect(mConnectTimer, SIGNAL(timeout()), this, SLOT(onDisconnected()));
}
emit adbNotificationState("Notification-Offline");
// at the start, suppose the adb not connected.
if (notificationSocket) {
delete notificationSocket;
}
notificationSocket = new QTcpSocket();
QString adb_serial = AdbClient::getAdbSerial();
if (adb_serial.isEmpty()) {
AdbClient::doAdbForward("host:forward:tcp:58888;localabstract:WrenchNotifications");
} else {
AdbClient::doAdbForward("host-serial:" + adb_serial + ":forward:tcp:58888;localabstract:WrenchNotifications");
}
notificationSocket->connectToHost("127.0.0.1", 58888 + QProcessEnvironment::systemEnvironment().value("WRENCH_INSTANCE", "0").toInt(), QIODevice::ReadWrite);
if (!notificationSocket->waitForConnected()) {
mConnectTimer->start(1000);
return;
}
if (!notificationSocket->waitForReadyRead(500)) {
qDebug() << "Can't wait for notification init read\n";
mConnectTimer->start(1000);
return;
}
if (QString(notificationSocket->readLine()).contains("notification ready")) {
notificationSocket->write("hello\n");
notificationSocket->flush();
connect(notificationSocket, SIGNAL(readChannelFinished()), this, SLOT(onDisconnected()), Qt::QueuedConnection);
connect(notificationSocket, SIGNAL(readyRead()), this, SLOT(onNewNotification()));
notificationSocket->write("list\n");
emit adbNotificationState("Notification-Online");
} else {
qDebug() << "Can't read from notification";
mConnectTimer->start(1000);
return;
}
}
void AdbNotificationThread::onNewNotification()
{
QByteArray bytes = notificationSocket->readLine();
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
if (bytes == mLastJsonStr) {
if (currentTime - mLastTime < 2000) {
return;
}
}
mLastJsonStr = bytes;
mLastTime = currentTime;
QJsonDocument jdoc = QJsonDocument::fromJson(bytes);
if (!jdoc.isObject()) {
qDebug() << "Not a json object" << jdoc;
return;
}
QJsonObject jobj = jdoc.object();
QString key = jobj.value("key").toString();
QString pkg = jobj.value("pkg").toString();
QString title = jobj.value("title").toString();
QString text = jobj.value("text").toString();
QString ticker = jobj.value("ticker").toString();
emit adbNotificationArrived(key, pkg, title, text, ticker);
qDebug() << key << "(" << pkg << "): " << title << "(" << text << ")";
if (notificationSocket->bytesAvailable() > 0) {
qDebug() << "Still more notifications readable";
onNewNotification();
}
}
void AdbNotificationThread::run()
{
this->moveToThread(this);
onDisconnected();
exec();
}
void AdbNotificationThread::onAdbNotificationClicked(const QString& key)
{
QString toWrite = "click " + key + "\n";
notificationSocket->write(toWrite.toUtf8());
notificationSocket->flush();
}