-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpmmanager.cpp
139 lines (110 loc) · 3.38 KB
/
pmmanager.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
138
139
#include "pmmanager.h"
#include "ui_pmmanager.h"
PMManager::PMManager(QWidget *parent, QString device) :
QWidget(parent),
ui(new Ui::PMManager)
{
ui->setupUi(this);
model = new QStringListModel(this);
adb = new QProcess();
currentDevice = device;
// now add data to ui
parseData();
}
PMManager::~PMManager()
{
delete ui;
}
QString PMManager::substring(QString string, int start, int end)
{
return string.mid(start, end-start);
}
QString PMManager::removeGarbage(QString string)
{
return string.remove("package:", Qt::CaseInsensitive);
}
void PMManager::parseData()
{
// start process
QString program = QString("adb -s %1 shell pm list packages -3").arg(currentDevice);
qDebug() << "program is: " << program;
adb->start(program);
adb->waitForFinished();
// get output data
QByteArray outputData = adb->readAllStandardOutput();
qDebug() << outputData;
int lastIndex = 0;
// reset List
List.clear();
// parse data into list
for(int i = 0; i < outputData.size(); i++)
{
if(outputData[i] == '\n')
{
QString string = substring(QString(outputData), lastIndex, i);
string = removeGarbage(string);
lastIndex = i;
List << string;
}
}
// set model list
model->setStringList(List);
ui->listView->setModel(model);
}
void PMManager::on_uninstall_clicked()
{
// prompt user if they want to keep data
QMessageBox msgBox;
msgBox.setText("Do you want to keep app data?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Yes:
{
QModelIndex index = ui->listView->currentIndex();
QString itemText = index.data(Qt::DisplayRole).toString();
// start process
QString program = QString("adb -s %1 uninstall -k %2").arg(currentDevice, itemText);
qDebug() << "program is: " << program;
adb->start(program);
adb->waitForFinished(1000);
parseData();
}
break;
case QMessageBox::No:
{
QModelIndex index = ui->listView->currentIndex();
QString itemText = index.data(Qt::DisplayRole).toString();
// start process
QString program = QString("adb -s %1 uninstall %2").arg(currentDevice, itemText);
qDebug() << "program is: " << program;
adb->start(program);
adb->waitForFinished(1000);
parseData();
}
break;
default:
break;
}
}
void PMManager::on_install_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Any"), "~/", tr("APK Files (*.apk)"));
qDebug() << "Selected file is: " << fileName;
QString program = QString("adb -s %1 install \"%2\"").arg(currentDevice, fileName);
program.remove(QRegExp("[\\n\\t\\r]"));
program.remove(QChar('\\', Qt::CaseInsensitive));
qDebug() << "program is: " << program;
// create process object
QProcess myProcess;
myProcess.start(program);
myProcess.waitForFinished();
// read adb devices data to array
QByteArray output = myProcess.readAllStandardOutput();
QByteArray badOutput = myProcess.readAllStandardError();
qDebug() << "output is: " << output;
qDebug() << "bad output is: " << badOutput;
parseData();
}