-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppMain.cpp
231 lines (207 loc) · 7.67 KB
/
AppMain.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "AppMain.h"
#define APP_MODEL AppModel::instance()
#define REG_MAIL_CTR RegMailController::instance()
#define REG_DEVICE_INFO_CTR RegDeviceInfoController::instance()
#define REG_FBACC_CTR RegFBController::instance()
AppMain* AppMain::m_instance = nullptr;
AppMain::AppMain(QObject *parent) : QObject(parent)
{
m_currentActivity = QString("---");
m_currentExcuteStep = AppEnums::E_EXCUTE_CHANGE_INFO;
}
AppMain *AppMain::instance()
{
if(m_instance == nullptr){
m_instance = new AppMain();
}
return m_instance;
}
void AppMain::wipeData()
{
LOG << "[AppMain]";
for(int i = 0; i < APP_MODEL->appDataList().length(); i++){
APP_DATA* app = dynamic_cast<APP_DATA*>(APP_MODEL->appDataList().at(i));
if(app != nullptr && app->checkedState()){
ADBCommand::clearCacheOfPackage(app->packageName());
}else{
// Do nothing
}
}
}
void AppMain::killCurrentApp()
{
ADBCommand::killSpecificApp(this->getCurrentPackage());
}
void AppMain::loadConfig()
{
LOG << "[AppMain]";
if(!QFile(QDir::currentPath() + "/" + CONFIG_FILE_NAME).exists()){
this->saveConfig();
}else{
QJsonObject config = this->loadJson(CONFIG_FILE_NAME).object();
APP_MODEL->setSaveToLocal(config[SAVE_LOCAL_FIELD].toBool());
APP_MODEL->setSaveToServer(config[SAVE_SERVER_FIELD].toBool());
APP_MODEL->setUseKeyboard(config[USE_KEYBOARD_FIELD].toBool());
APP_MODEL->setRegFacebookOption(config[REG_FAEBOOK_FIELD].toBool());
APP_MODEL->setRecoveryEmail(config[RECOVERY_EMAIL_FIELD].toBool());
APP_MODEL->setNameLang(config[NAME_LANG_FIELD].toString());
QJsonObject appDataObj = config[APP_DATA_FIELD].toObject();
if(!appDataObj.isEmpty()){
QJsonObject appData;
for(int i = 0; i < APP_MODEL->appDataList().length(); i++){
APP_DATA* app = dynamic_cast<APP_DATA*>(APP_MODEL->appDataList().at(i));
QString packageName = app->packageName();
bool state = appDataObj[packageName].toBool();
app->setCheckedState(state);
}
}else{
LOG << "[AppMain]" << " appDataObj is empty";
}
}
}
void AppMain::saveConfig()
{
LOG << "[AppMain]";
QJsonObject config;
QJsonObject appData;
for(int i = 0; i < APP_MODEL->appDataList().length(); i++){
APP_DATA* app = dynamic_cast<APP_DATA*>(APP_MODEL->appDataList().at(i));
QString packageName = app->packageName();
appData[packageName] = app->checkedState();
}
config[SAVE_LOCAL_FIELD] = APP_MODEL->saveToLocal();
config[SAVE_SERVER_FIELD] = APP_MODEL->saveToServer();
config[USE_KEYBOARD_FIELD] = APP_MODEL->useKeyboard();
config[REG_FAEBOOK_FIELD] = APP_MODEL->regFacebookOption();
config[RECOVERY_EMAIL_FIELD] = APP_MODEL->recoveryEmail();
config[NAME_LANG_FIELD] = APP_MODEL->nameLang();
config[APP_DATA_FIELD] = appData;
this->saveJson(QJsonDocument(config),CONFIG_FILE_NAME);
}
QJsonDocument AppMain::loadJson(QString fileName)
{
LOG << "[AppMain]";
QFile jsonFile(fileName);
jsonFile.open(QFile::ReadOnly);
return QJsonDocument().fromJson(jsonFile.readAll());
}
void AppMain::saveJson(QJsonDocument document, QString fileName)
{
LOG << "[AppMain]";
QFile jsonFile(fileName);
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
void AppMain::initApplication()
{
LOG << "[AppMain]";
this->loadConfig();
REG_DEVICE_INFO_CTR->initRegDeviceInfoController();
REG_MAIL_CTR->initRegMailController();
REG_FBACC_CTR->initRegFBController();
QObject::connect(this, SIGNAL(processFinished(int,int)), this, SLOT(onProcessFinished(int,int)));
// QObject::connect(this, SIGNAL(currentActivityChanged()), REG_DEVICE_INFO_CTR, SLOT(onCurrentActivityChanged()));
// QObject::connect(this, SIGNAL(currentActivityChanged()), REG_MAIL_CTR, SLOT(onCurrentActivityChanged()));
// QObject::connect(this, SIGNAL(currentActivityChanged()), REG_FBACC_CTR, SLOT(onCurrentActivityChanged()));
QObject::connect(APP_MODEL, SIGNAL(signalStartProgram(QString)), this, SLOT(startProgram(QString)));
QObject::connect(APP_MODEL, SIGNAL(signalCloseProgram(QString)), this, SLOT(closeProgram(QString)));
QObject::connect(APP_MODEL, SIGNAL(signalSaveSettingConfig()), this, SLOT(saveConfig()));
}
void AppMain::startProgram(QString tokenID)
{
LOG << "[AppMain] " << tokenID;
this->restartProgram();
multiThreadController.startNewThreads();
emit multiThreadController.operate("Start new thread");
}
void AppMain::closeProgram(QString tokenID)
{
LOG << "AppMain" << tokenID;
QCoreApplication::quit();
}
void AppMain::restartProgram()
{
LOG << "AppMain";
REG_MAIL_CTR->setUserInforToReg();
this->wipeData();
if(this->getCurrentActivity() == HOME_SCREEN){
emit this-> currentActivityChanged();
}else{
ADBCommand::goHomeScreen();
}
}
QString AppMain::getCurrentActivity()
{
return m_currentActivity;
}
void AppMain::setCurrentActivity(QString _activity)
{
if(m_currentActivity != _activity){
if(_activity == UNKNOW_SCREEN){
// Do nothing
}else{
QMutex mutex;
mutex.lock();
m_currentActivity = _activity;
mutex.unlock();
this->setCurrentPackage(m_currentActivity.split("/").at(0));
emit currentActivityChanged();
}
}
}
QString AppMain::getCurrentPackage()
{
return m_currentPackage;
}
void AppMain::setCurrentPackage(QString _package)
{
if(m_currentPackage != _package){
m_currentPackage = _package;
}
}
AppEnums::E_EXCUTE_STEPS AppMain::currentExcuteStep()
{
return m_currentExcuteStep;
}
void AppMain::setCurrentExcuteStep(AppEnums::E_EXCUTE_STEPS step)
{
if(m_currentExcuteStep != step){
LOG << "[AppMain]" << step;
m_currentExcuteStep = step;
}
}
void AppMain::onProcessFinished(int currentStep, int exitCode)
{
LOG << "[AppMain]" << "currentStep: " << (currentStep == 0? "CHANGE DEVICE" : (currentStep == 1? "REG GMAIL" : "REG FACEBOOK")) << " --- exitCode: " << exitCode;
if(exitCode == 1){
LOG << "[AppMain]" << "Process incompleted! -> Restart process";
this->setCurrentExcuteStep(AppEnums::E_EXCUTE_CHANGE_INFO);
this->restartProgram();
}else if(exitCode == 0 ){
if(currentStep == AppEnums::E_EXCUTE_CHANGE_INFO){
this->setCurrentExcuteStep(AppEnums::E_EXCUTE_REG_GMAIL);
LOG << "[AppMain]" << "Change infor device completed -> Reboot device!!!";
ADBCommand::rebootDevice();
}else if(currentStep == AppEnums::E_EXCUTE_REG_GMAIL){
REG_FBACC_CTR->setUserInfo(REG_MAIL_CTR->getEmailInfor());
LOG << "[AppMain]" << "Reg gmail completed! -> Start reg facebook";
if(APP_MODEL->regFacebookOption()){
this->setCurrentExcuteStep(AppEnums::E_EXCUTE_REG_FACBOOK);
if(this->getCurrentActivity() == HOME_SCREEN){
emit this-> currentActivityChanged();
}else{
ADBCommand::goHomeScreen();
}
}else{
REG_MAIL_CTR->saveEmailToOutput();
this->setCurrentExcuteStep(AppEnums::E_EXCUTE_CHANGE_INFO);
this->restartProgram();
}
}else if(currentStep == AppEnums::E_EXCUTE_REG_FACBOOK){
REG_MAIL_CTR->saveEmailToOutput();
LOG << "[AppMain]" << "Process completed! -> Restart process";
this->setCurrentExcuteStep(AppEnums::E_EXCUTE_CHANGE_INFO);
this->restartProgram();
}
}
}