Repository navigation
Expand file tree
/
Copy pathappmanager.cpp
More file actions
175 lines (161 loc) · 6.1 KB
/
Copy pathappmanager.cpp
File metadata and controls
175 lines (161 loc) · 6.1 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "appmanager.h"
#include "devicerecord.h"
#include "getadbdata.h"
#include "logfile.h"
#include "forcequitdialog.h"
#include <QMessageBox>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDir>
#include <QWidget>
AppManager::AppManager(QObject *parent)
: QObject(parent)
{
}
void AppManager::stopApp(QWidget *parentWidget, const DeviceRecord & /*device*/,
const QString &adbPrefix, const QString &databaseDir,
const QString &configBasename)
{
QString jsonPath = QDir(databaseDir).filePath(configBasename);
QString stopapp = "org.xbmc.kodi";
QJsonObject jsonObj;
QFile file(jsonPath);
if (file.exists()) {
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Cannot read %1.").arg(configBasename));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
file.close();
if (doc.isObject()) {
jsonObj = doc.object();
if (jsonObj.contains("stopapp"))
stopapp = jsonObj["stopapp"].toString();
}
} else {
jsonObj["stopapp"] = stopapp;
}
forcequitDialog dialog(false, stopapp, parentWidget);
dialog.setWindowModality(Qt::WindowModal);
if (dialog.exec() != QDialog::Accepted)
return;
QString package = dialog.packagename();
if (package.isEmpty()) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Invalid package name."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
QString cstring = adbPrefix + " shell am force-stop " + package;
QString command = getadbOutput(cstring);
if (command.contains("error", Qt::CaseInsensitive)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(QStringLiteral("Warning"));
msgBox.setText(QStringLiteral("Failed to stop application."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
}
logfile(cstring);
logfile(command);
if (stopapp != package) {
jsonObj["stopapp"] = package;
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Cannot save configuration."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
file.write(QJsonDocument(jsonObj).toJson());
file.close();
}
}
void AppManager::startApp(QWidget *parentWidget, const DeviceRecord & /*device*/,
const QString &adbPrefix, const QString &databaseDir,
const QString &configBasename)
{
QString jsonPath = QDir(databaseDir).filePath(configBasename);
QString startapp = "org.xbmc.kodi/org.xbmc.kodi.Splash";
QJsonObject jsonObj;
QFile file(jsonPath);
if (file.exists()) {
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Cannot read %1.").arg(configBasename));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
file.close();
if (doc.isObject()) {
jsonObj = doc.object();
if (jsonObj.contains("startapp"))
startapp = jsonObj["startapp"].toString();
}
} else {
jsonObj["startapp"] = startapp;
}
forcequitDialog dialog(true, startapp, parentWidget);
dialog.setWindowModality(Qt::WindowModal);
if (dialog.exec() != QDialog::Accepted)
return;
QString package = dialog.packagename();
if (package.isEmpty()) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Invalid package name."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
QString cstring = adbPrefix + " shell am start -n " + package;
QString command = getadbOutput(cstring);
if (command.contains("error", Qt::CaseInsensitive)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(QStringLiteral("Warning"));
msgBox.setText(QStringLiteral("Failed to start application."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
}
logfile(cstring);
logfile(command);
if (startapp != package) {
jsonObj["startapp"] = package;
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox msgBox(parentWidget);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(QStringLiteral("Error"));
msgBox.setText(QStringLiteral("Cannot save configuration."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
return;
}
file.write(QJsonDocument(jsonObj).toJson());
file.close();
}
}