blob: 81c6294e299ca12552b7bc530683441f51aa573c [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060053#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
55
56#include "rapidxml.hpp"
57#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050058#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Dees_Troy51a0e822012-09-05 15:24:24 -040060void curtainClose(void);
61
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
66static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010067pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
111 if(m_thread_running) {
112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100201
202 // remember actions that run in the caller thread
203 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
204 setActionsRunningInCallerThread.insert(it->first);
205
206 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100207 ADD_ACTION(flash);
208 ADD_ACTION(wipe);
209 ADD_ACTION(refreshsizes);
210 ADD_ACTION(nandroid);
211 ADD_ACTION(fixpermissions);
212 ADD_ACTION(dd);
213 ADD_ACTION(partitionsd);
214 ADD_ACTION(installhtcdumlock);
215 ADD_ACTION(htcdumlockrestoreboot);
216 ADD_ACTION(htcdumlockreflashrecovery);
217 ADD_ACTION(cmd);
218 ADD_ACTION(terminalcommand);
219 ADD_ACTION(reinjecttwrp);
220 ADD_ACTION(decrypt);
221 ADD_ACTION(adbsideload);
222 ADD_ACTION(openrecoveryscript);
223 ADD_ACTION(installsu);
224 ADD_ACTION(decrypt_backup);
225 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500226 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100227 ADD_ACTION(changefilesystem);
228 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100229 ADD_ACTION(twcmd);
that3f7b1ac2014-12-30 11:30:13 +0100230 }
231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600233 actions = FindNode(node, "actions");
234 if (actions) child = FindNode(actions, "action");
235 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 while (child)
240 {
241 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 attr = child->first_attribute("function");
244 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 action.mFunction = attr->value();
247 action.mArg = child->value();
248 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 child = child->next_sibling("action");
251 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600254 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (child)
256 {
257 attr = child->first_attribute("key");
258 if (attr)
259 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100260 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
261 for(size_t i = 0; i < keys.size(); ++i)
262 {
263 const int key = getKeyByName(keys[i]);
264 mKeys[key] = false;
265 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 }
267 else
268 {
269 attr = child->first_attribute("x");
270 if (!attr) return;
271 mActionX = atol(attr->value());
272 attr = child->first_attribute("y");
273 if (!attr) return;
274 mActionY = atol(attr->value());
275 attr = child->first_attribute("w");
276 if (!attr) return;
277 mActionW = atol(attr->value());
278 attr = child->first_attribute("h");
279 if (!attr) return;
280 mActionH = atol(attr->value());
281 }
282 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400283}
284
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500285int GUIAction::NotifyTouch(TOUCH_STATE state __unused, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400286{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 if (state == TOUCH_RELEASE)
288 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100293int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400294{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100295 std::map<int, bool>::iterator itr = mKeys.find(key);
296 if(itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100297 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100298
299 bool prevState = itr->second;
300 itr->second = down;
301
302 // If there is only one key for this action, wait for key up so it
303 // doesn't trigger with multi-key actions.
304 // Else, check if all buttons are pressed, then consume their release events
305 // so they don't trigger one-button actions and reset mKeys pressed status
306 if(mKeys.size() == 1) {
307 if(!down && prevState)
308 doActions();
309 } else if(down) {
310 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
311 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100312 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313 }
314
315 // Passed, all req buttons are pressed, reset them and consume release events
316 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
317 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
318 kb->ConsumeKeyRelease(itr->first);
319 itr->second = false;
320 }
321
322 doActions();
323 }
324
Dees_Troy51a0e822012-09-05 15:24:24 -0400325 return 0;
326}
327
Vojtech Bocek07220562014-02-08 02:05:33 +0100328int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400329{
Vojtech Bocek07220562014-02-08 02:05:33 +0100330 GUIObject::NotifyVarChange(varName, value);
331
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100332 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100334 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
340void GUIAction::simulate_progress_bar(void)
341{
Ethan Yonker74db1572015-10-28 12:44:49 -0500342 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 for (int i = 0; i < 5; i++)
344 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500345 if (PartitionManager.stop_backup.get_value()) {
346 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500347 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -0500348 DataManager::SetValue("ui_progress", 0);
349 PartitionManager.stop_backup.set_value(0);
350 return;
351 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400352 usleep(500000);
353 DataManager::SetValue("ui_progress", i * 20);
354 }
355}
356
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600357int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400358{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400360
361 DataManager::SetValue("ui_progress", 0);
362
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 if (filename.empty())
364 {
365 LOGERR("No file specified.\n");
366 return -1;
367 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400368
Ethan Yonker9598c072016-01-15 21:54:34 -0600369 if (!TWFunc::Path_Exists(filename)) {
370 if (!PartitionManager.Mount_By_Path(filename, true)) {
371 return -1;
372 }
373 if (!TWFunc::Path_Exists(filename)) {
374 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
375 return -1;
376 }
377 }
Dees_Troy657c3092012-09-10 20:32:10 -0400378
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 if (simulate) {
380 simulate_progress_bar();
381 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400382 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400383
384 // Now, check if we need to ensure TWRP remains installed...
385 struct stat st;
386 if (stat("/sbin/installTwrp", &st) == 0)
387 {
388 DataManager::SetValue("tw_operation", "Configuring TWRP");
389 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500390 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200391 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400392 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500393 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400394 }
395 }
396 }
397
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200398 // Done
399 DataManager::SetValue("ui_progress", 100);
400 DataManager::SetValue("ui_progress", 0);
401 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400402}
403
that73a52952015-01-28 23:07:34 +0100404GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100405{
that73a52952015-01-28 23:07:34 +0100406 string func = gui_parse_text(action.mFunction);
407 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
408 if (needsThread) {
409 if (func == "cancelbackup")
410 return THREAD_CANCEL;
411 else
412 return THREAD_ACTION;
413 }
414 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100415}
416
Dees_Troy51a0e822012-09-05 15:24:24 -0400417int GUIAction::doActions()
418{
that3f7b1ac2014-12-30 11:30:13 +0100419 if (mActions.size() < 1)
420 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421
that73a52952015-01-28 23:07:34 +0100422 // Determine in which thread to run the actions.
423 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
424 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100425 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100426 for (it = mActions.begin(); it != mActions.end(); ++it) {
427 ThreadType tt = getThreadType(*it);
428 if (tt == THREAD_NONE)
429 continue;
430 if (threadType == THREAD_NONE)
431 threadType = tt;
432 else if (threadType != tt) {
433 LOGERR("Can't mix normal and cancel actions in the same list.\n"
434 "Running the whole batch in the cancel thread.\n");
435 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100436 break;
437 }
that7d3b54f2015-01-09 22:52:51 +0100438 }
that73a52952015-01-28 23:07:34 +0100439
440 // Now run the actions in the desired thread.
441 switch (threadType) {
442 case THREAD_ACTION:
443 action_thread.threadActions(this);
444 break;
445
446 case THREAD_CANCEL:
447 cancel_thread.threadActions(this);
448 break;
449
450 default: {
451 // no iterators here because theme reloading might kill our object
452 const size_t cnt = mActions.size();
453 for (size_t i = 0; i < cnt; ++i)
454 doAction(mActions[i]);
455 }
thatc6085482015-01-09 22:12:43 +0100456 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400459}
460
that3f7b1ac2014-12-30 11:30:13 +0100461int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400462{
that3f7b1ac2014-12-30 11:30:13 +0100463 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
that3f7b1ac2014-12-30 11:30:13 +0100465 std::string function = gui_parse_text(action.mFunction);
466 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
that3f7b1ac2014-12-30 11:30:13 +0100468 // find function and execute it
469 mapFunc::const_iterator funcitr = mf.find(function);
470 if (funcitr != mf.end())
471 return (this->*funcitr->second)(arg);
472
473 LOGERR("Unknown action '%s'\n", function.c_str());
474 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
477void GUIAction::operation_start(const string operation_name)
478{
that3f7b1ac2014-12-30 11:30:13 +0100479 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000480 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 DataManager::SetValue(TW_ACTION_BUSY, 1);
482 DataManager::SetValue("ui_progress", 0);
483 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600485 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486}
487
that3f7b1ac2014-12-30 11:30:13 +0100488void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400489{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000490 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 DataManager::SetValue("ui_progress", 100);
493 if (simulate) {
494 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
495 if (simulate_fail != 0)
496 DataManager::SetValue("tw_operation_status", 1);
497 else
498 DataManager::SetValue("tw_operation_status", 0);
499 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500502 }
503 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500505 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 }
507 DataManager::SetValue("tw_operation_state", 1);
508 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500509 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200510 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000511 time(&Stop);
512 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600513 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100514 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515}
516
that3f7b1ac2014-12-30 11:30:13 +0100517int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400518{
that3f7b1ac2014-12-30 11:30:13 +0100519 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400520
that3f7b1ac2014-12-30 11:30:13 +0100521 sync();
522 DataManager::SetValue("tw_gui_done", 1);
523 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400524
that3f7b1ac2014-12-30 11:30:13 +0100525 return 0;
526}
Dees_Troy51a0e822012-09-05 15:24:24 -0400527
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500528int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100529{
that3f7b1ac2014-12-30 11:30:13 +0100530 gui_changePage("main");
531 return 0;
532}
Dees_Troy51a0e822012-09-05 15:24:24 -0400533
that3f7b1ac2014-12-30 11:30:13 +0100534int GUIAction::key(std::string arg)
535{
536 const int key = getKeyByName(arg);
537 PageManager::NotifyKey(key, true);
538 PageManager::NotifyKey(key, false);
539 return 0;
540}
541
542int GUIAction::page(std::string arg)
543{
LuK133762326f42015-09-30 19:57:46 +0200544 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100545 std::string page_name = gui_parse_text(arg);
546 return gui_changePage(page_name);
547}
548
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500549int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100550{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500551 PageManager::RequestReload();
552 // The actual reload is handled in pages.cpp in PageManager::RunReload()
553 // The reload will occur on the next Update or Render call and will
554 // be performed in the rendoer thread instead of the action thread
555 // to prevent crashing which could occur when we start deleting
556 // GUI resources in the action thread while we attempt to render
557 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100558 return 0;
559}
Dees_Troy51a0e822012-09-05 15:24:24 -0400560
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500561int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100562{
563 string Restore_Name;
564 DataManager::GetValue("tw_restore", Restore_Name);
565 PartitionManager.Set_Restore_Files(Restore_Name);
566 return 0;
567}
568
569int GUIAction::set(std::string arg)
570{
571 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200572 {
that3f7b1ac2014-12-30 11:30:13 +0100573 string varName = arg.substr(0, arg.find('='));
574 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400575
that3f7b1ac2014-12-30 11:30:13 +0100576 DataManager::GetValue(value, value);
577 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 }
that3f7b1ac2014-12-30 11:30:13 +0100579 else
580 DataManager::SetValue(arg, "1");
581 return 0;
582}
Dees_Troy51a0e822012-09-05 15:24:24 -0400583
that3f7b1ac2014-12-30 11:30:13 +0100584int GUIAction::clear(std::string arg)
585{
586 DataManager::SetValue(arg, "0");
587 return 0;
588}
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
that3f7b1ac2014-12-30 11:30:13 +0100590int GUIAction::mount(std::string arg)
591{
592 if (arg == "usb") {
593 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100595 PartitionManager.usb_storage_enable();
596 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500597 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100598 } else if (!simulate) {
599 PartitionManager.Mount_By_Path(arg, true);
600 PartitionManager.Add_MTP_Storage(arg);
601 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500602 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100603 return 0;
604}
605
606int GUIAction::unmount(std::string arg)
607{
608 if (arg == "usb") {
609 if (!simulate)
610 PartitionManager.usb_storage_disable();
611 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500612 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100613 DataManager::SetValue(TW_ACTION_BUSY, 0);
614 } else if (!simulate) {
615 PartitionManager.UnMount_By_Path(arg, true);
616 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100618 return 0;
619}
620
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500621int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100622{
623 operation_start("Restore Defaults");
624 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500625 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100626 else {
627 DataManager::ResetDefaults();
628 PartitionManager.Update_System_Details();
629 PartitionManager.Mount_Current_Storage(true);
630 }
631 operation_end(0);
632 return 0;
633}
634
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500635int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100636{
637 operation_start("Copy Log");
638 if (!simulate)
639 {
640 string dst;
641 PartitionManager.Mount_Current_Storage(true);
642 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
643 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
644 tw_set_default_metadata(dst.c_str());
645 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100647 } else
648 simulate_progress_bar();
649 operation_end(0);
650 return 0;
651}
652
653
654int GUIAction::compute(std::string arg)
655{
656 if (arg.find("+") != string::npos)
657 {
658 string varName = arg.substr(0, arg.find('+'));
659 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
660 int amount_to_add = atoi(string_to_add.c_str());
661 int value;
662
663 DataManager::GetValue(varName, value);
664 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400665 return 0;
666 }
that3f7b1ac2014-12-30 11:30:13 +0100667 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 {
that3f7b1ac2014-12-30 11:30:13 +0100669 string varName = arg.substr(0, arg.find('-'));
670 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
671 int amount_to_subtract = atoi(string_to_subtract.c_str());
672 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400673
that3f7b1ac2014-12-30 11:30:13 +0100674 DataManager::GetValue(varName, value);
675 value -= amount_to_subtract;
676 if (value <= 0)
677 value = 0;
678 DataManager::SetValue(varName, value);
679 return 0;
680 }
681 if (arg.find("*") != string::npos)
682 {
683 string varName = arg.substr(0, arg.find('*'));
684 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
685 int multiply_by = atoi(multiply_by_str.c_str());
686 int value;
687
688 DataManager::GetValue(varName, value);
689 DataManager::SetValue(varName, value*multiply_by);
690 return 0;
691 }
692 if (arg.find("/") != string::npos)
693 {
694 string varName = arg.substr(0, arg.find('/'));
695 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
696 int divide_by = atoi(divide_by_str.c_str());
697 int value;
698
699 if(divide_by != 0)
700 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100702 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 }
704 return 0;
705 }
that3f7b1ac2014-12-30 11:30:13 +0100706 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
707 return -1;
708}
Dees_Troy51a0e822012-09-05 15:24:24 -0400709
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500710int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100711{
712 string SelectedZone;
713 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
714 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
715 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
716
717 int dst;
718 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
719
720 string offset;
721 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
722
723 string NewTimeZone = Zone;
724 if (offset != "0")
725 NewTimeZone += ":" + offset;
726
727 if (dst != 0)
728 NewTimeZone += DSTZone;
729
730 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
731 DataManager::update_tz_environment_variables();
732 return 0;
733}
734
735int GUIAction::overlay(std::string arg)
736{
737 return gui_changeOverlay(arg);
738}
739
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500740int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100741{
742 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500743 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 return 0;
745 }
that3f7b1ac2014-12-30 11:30:13 +0100746 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
747 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
748 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100750 }
751 return 0;
752}
753
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500754int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100755{
756 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500757 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100759 } else {
760 zip_queue_index--;
761 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 }
that3f7b1ac2014-12-30 11:30:13 +0100763 return 0;
764}
Dees_Troy51a0e822012-09-05 15:24:24 -0400765
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500766int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100767{
768 zip_queue_index = 0;
769 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
770 return 0;
771}
Dees_Troy51a0e822012-09-05 15:24:24 -0400772
that3f7b1ac2014-12-30 11:30:13 +0100773int GUIAction::sleep(std::string arg)
774{
775 operation_start("Sleep");
776 usleep(atoi(arg.c_str()));
777 operation_end(0);
778 return 0;
779}
Dees Troyb21cc642013-09-10 17:36:41 +0000780
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500781int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100782{
783 operation_start("AppendDateToBackupName");
784 string Backup_Name;
785 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
786 Backup_Name += TWFunc::Get_Current_Date();
787 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
788 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
789 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
790 operation_end(0);
791 return 0;
792}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500793
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500794int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100795{
796 operation_start("GenerateBackupName");
797 TWFunc::Auto_Generate_Backup_Name();
798 operation_end(0);
799 return 0;
800}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500801
Ethan Yonker483e9f42016-01-11 22:21:18 -0600802int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100803{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600804 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100805 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500806
Ethan Yonker483e9f42016-01-11 22:21:18 -0600807 if (arg.empty())
808 arg = "tw_wipe_list";
809 DataManager::GetValue(arg, List);
810 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
811 if (!List.empty()) {
812 size_t start_pos = 0, end_pos = List.find(";", start_pos);
813 while (end_pos != string::npos && start_pos < List.size()) {
814 part_path = List.substr(start_pos, end_pos - start_pos);
815 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
816 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100817 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400818 } else {
that3f7b1ac2014-12-30 11:30:13 +0100819 count++;
820 }
821 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600822 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100823 }
824 DataManager::SetValue("tw_check_partition_list", count);
825 } else {
826 DataManager::SetValue("tw_check_partition_list", 0);
827 }
828 return 0;
829}
Dees_Troy51a0e822012-09-05 15:24:24 -0400830
Ethan Yonker483e9f42016-01-11 22:21:18 -0600831int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100832{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600833 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100834 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400835
Ethan Yonker483e9f42016-01-11 22:21:18 -0600836 if (arg.empty())
837 arg = "tw_wipe_list";
838 DataManager::GetValue(arg, List);
839 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
840 if (!List.empty()) {
841 size_t start_pos = 0, end_pos = List.find(";", start_pos);
842 part_path = List;
843 while (end_pos != string::npos && start_pos < List.size()) {
844 part_path = List.substr(start_pos, end_pos - start_pos);
845 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
846 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100847 // Do nothing
848 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600849 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100850 break;
851 }
852 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600853 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100854 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600855 if (!part_path.empty()) {
856 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100857 if (Part) {
858 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500859
that3f7b1ac2014-12-30 11:30:13 +0100860 DataManager::SetValue("tw_partition_name", Part->Display_Name);
861 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
862 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
863 DataManager::SetValue("tw_partition_size", Part->Size / mb);
864 DataManager::SetValue("tw_partition_used", Part->Used / mb);
865 DataManager::SetValue("tw_partition_free", Part->Free / mb);
866 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
867 DataManager::SetValue("tw_partition_removable", Part->Removable);
868 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
869
870 if (Part->Can_Repair())
871 DataManager::SetValue("tw_partition_can_repair", 1);
872 else
873 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500874 if (Part->Can_Resize())
875 DataManager::SetValue("tw_partition_can_resize", 1);
876 else
877 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600878 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100879 DataManager::SetValue("tw_partition_vfat", 1);
880 else
881 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600882 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100883 DataManager::SetValue("tw_partition_exfat", 1);
884 else
885 DataManager::SetValue("tw_partition_exfat", 0);
886 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
887 DataManager::SetValue("tw_partition_f2fs", 1);
888 else
889 DataManager::SetValue("tw_partition_f2fs", 0);
890 if (TWFunc::Path_Exists("/sbin/mke2fs"))
891 DataManager::SetValue("tw_partition_ext", 1);
892 else
893 DataManager::SetValue("tw_partition_ext", 0);
894 return 0;
895 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600896 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100897 }
898 }
899 }
900 DataManager::SetValue("tw_partition_name", "");
901 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600902 // Set this to 0 to prevent trying to partition this device, just in case
903 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100904 return 0;
905}
906
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500907int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100908{
909 time_t tm;
910 char path[256];
911 int path_len;
912 uid_t uid = -1;
913 gid_t gid = -1;
914
915 struct passwd *pwd = getpwnam("media_rw");
916 if(pwd) {
917 uid = pwd->pw_uid;
918 gid = pwd->pw_gid;
919 }
920
921 const std::string storage = DataManager::GetCurrentStoragePath();
922 if(PartitionManager.Is_Mounted_By_Path(storage)) {
923 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
924 } else {
925 strcpy(path, "/tmp/");
926 }
927
928 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
929 return 0;
930
931 tm = time(NULL);
932 path_len = strlen(path);
933
934 // Screenshot_2014-01-01-18-21-38.png
935 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
936
937 int res = gr_save_screenshot(path);
938 if(res == 0) {
939 chmod(path, 0666);
940 chown(path, uid, gid);
941
that677b13f2015-12-26 23:57:31 +0100942 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100943
944 // blink to notify that the screenshow was taken
945 gr_color(255, 255, 255, 255);
946 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
947 gr_flip();
948 gui_forceRender();
949 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500950 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100951 }
952 return 0;
953}
954
955int GUIAction::setbrightness(std::string arg)
956{
957 return TWFunc::Set_Brightness(arg);
958}
959
960int GUIAction::fileexists(std::string arg)
961{
962 struct stat st;
963 string newpath = arg + "/.";
964
965 operation_start("FileExists");
966 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
967 operation_end(0);
968 else
969 operation_end(1);
970 return 0;
971}
972
thatcc8ddca2015-01-03 01:59:36 +0100973void GUIAction::reinject_after_flash()
974{
975 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500976 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100977 if (simulate) {
978 simulate_progress_bar();
979 } else {
980 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
981 if (Boot == NULL || Boot->Current_File_System != "emmc")
982 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
983 else {
984 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
985 TWFunc::Exec_Cmd(injectcmd);
986 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500987 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100988 }
989 }
990}
991
that3f7b1ac2014-12-30 11:30:13 +0100992int GUIAction::flash(std::string arg)
993{
994 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600995 // We're going to jump to this page first, like a loading page
996 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100997 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200998 string zip_path = zip_queue[i];
999 size_t slashpos = zip_path.find_last_of('/');
1000 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001001 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001002 DataManager::SetValue("tw_filename", zip_path);
1003 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001004 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1005
1006 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001007 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001008 TWFunc::SetPerformanceMode(false);
1009 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001010 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001011 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001012 break;
that3f7b1ac2014-12-30 11:30:13 +01001013 }
1014 }
1015 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001016
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001017 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001018 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001019 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001020 }
that3f7b1ac2014-12-30 11:30:13 +01001021
thatcc8ddca2015-01-03 01:59:36 +01001022 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001023 PartitionManager.Update_System_Details();
1024 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001025 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001026 return 0;
1027}
1028
1029int GUIAction::wipe(std::string arg)
1030{
1031 operation_start("Format");
1032 DataManager::SetValue("tw_partition", arg);
1033
1034 int ret_val = false;
1035
1036 if (simulate) {
1037 simulate_progress_bar();
1038 } else {
1039 if (arg == "data")
1040 ret_val = PartitionManager.Factory_Reset();
1041 else if (arg == "battery")
1042 ret_val = PartitionManager.Wipe_Battery_Stats();
1043 else if (arg == "rotate")
1044 ret_val = PartitionManager.Wipe_Rotate_Data();
1045 else if (arg == "dalvik")
1046 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1047 else if (arg == "DATAMEDIA") {
1048 ret_val = PartitionManager.Format_Data();
1049 } else if (arg == "INTERNAL") {
1050 int has_datamedia, dual_storage;
1051
1052 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1053 if (has_datamedia) {
1054 ret_val = PartitionManager.Wipe_Media_From_Data();
1055 } else {
1056 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1057 }
1058 } else if (arg == "EXTERNAL") {
1059 string External_Path;
1060
1061 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1062 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1063 } else if (arg == "ANDROIDSECURE") {
1064 ret_val = PartitionManager.Wipe_Android_Secure();
1065 } else if (arg == "LIST") {
1066 string Wipe_List, wipe_path;
1067 bool skip = false;
1068 ret_val = true;
1069 TWPartition* wipe_part = NULL;
1070
1071 DataManager::GetValue("tw_wipe_list", Wipe_List);
1072 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1073 if (!Wipe_List.empty()) {
1074 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1075 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1076 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1077 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1078 if (wipe_path == "/and-sec") {
1079 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001080 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001081 ret_val = false;
1082 break;
1083 } else {
1084 skip = true;
1085 }
1086 } else if (wipe_path == "DALVIK") {
1087 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001088 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001089 ret_val = false;
1090 break;
1091 } else {
1092 skip = true;
1093 }
1094 } else if (wipe_path == "INTERNAL") {
1095 if (!PartitionManager.Wipe_Media_From_Data()) {
1096 ret_val = false;
1097 break;
1098 } else {
1099 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001100 }
1101 }
that3f7b1ac2014-12-30 11:30:13 +01001102 if (!skip) {
1103 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001104 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001105 ret_val = false;
1106 break;
1107 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1108 arg = wipe_path;
1109 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 } else {
that3f7b1ac2014-12-30 11:30:13 +01001111 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 }
that3f7b1ac2014-12-30 11:30:13 +01001113 start_pos = end_pos + 1;
1114 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 }
1116 }
that3f7b1ac2014-12-30 11:30:13 +01001117 } else
1118 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001119#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001120 if (arg == DataManager::GetSettingsStoragePath()) {
1121 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1122 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001123
that3f7b1ac2014-12-30 11:30:13 +01001124 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1125 LOGINFO("Making TWRP folder and saving settings.\n");
1126 Storage_Path += "/TWRP";
1127 mkdir(Storage_Path.c_str(), 0777);
1128 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001129 } else {
that3f7b1ac2014-12-30 11:30:13 +01001130 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1131 }
1132 }
1133#endif
1134 }
1135 PartitionManager.Update_System_Details();
1136 if (ret_val)
1137 ret_val = 0; // 0 is success
1138 else
1139 ret_val = 1; // 1 is failure
1140 operation_end(ret_val);
1141 return 0;
1142}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001143
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001144int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001145{
1146 operation_start("Refreshing Sizes");
1147 if (simulate) {
1148 simulate_progress_bar();
1149 } else
1150 PartitionManager.Update_System_Details();
1151 operation_end(0);
1152 return 0;
1153}
1154
1155int GUIAction::nandroid(std::string arg)
1156{
that3f7b1ac2014-12-30 11:30:13 +01001157 if (simulate) {
that73a52952015-01-28 23:07:34 +01001158 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001159 DataManager::SetValue("tw_partition", "Simulation");
1160 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001161 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001162 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001163 operation_start("Nandroid");
1164 int ret = 0;
1165
that3f7b1ac2014-12-30 11:30:13 +01001166 if (arg == "backup") {
1167 string Backup_Name;
1168 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001169 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1170 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001171 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1172 ret = PartitionManager.Run_Backup();
1173 }
1174 else {
1175 operation_end(1);
1176 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001177 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001178 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001179 } else if (arg == "restore") {
1180 string Restore_Name;
1181 DataManager::GetValue("tw_restore", Restore_Name);
1182 ret = PartitionManager.Run_Restore(Restore_Name);
1183 } else {
1184 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001185 return -1;
1186 }
1187 DataManager::SetValue("tw_encrypt_backup", 0);
1188 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001189 if (ret == false)
1190 ret = 1; // 1 for failure
1191 else
1192 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001193 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001194 }
1195 else {
1196 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001197 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 ret = 0;
1199 }
that73a52952015-01-28 23:07:34 +01001200 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 return ret;
1202 }
1203 return 0;
1204}
1205
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001206int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001207 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001208 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001209 }
1210 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001211 int op_status = PartitionManager.Cancel_Backup();
1212 if (op_status != 0)
1213 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001214 }
1215
1216 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001217}
Dees_Troy51a0e822012-09-05 15:24:24 -04001218
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001219int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001220{
that73a52952015-01-28 23:07:34 +01001221 int op_status = 0;
1222
that3f7b1ac2014-12-30 11:30:13 +01001223 operation_start("Fix Permissions");
1224 LOGINFO("fix permissions started!\n");
1225 if (simulate) {
1226 simulate_progress_bar();
1227 } else {
that73a52952015-01-28 23:07:34 +01001228 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001229 if (op_status != 0)
1230 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001231 }
that73a52952015-01-28 23:07:34 +01001232 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001233 return 0;
1234}
1235
1236int GUIAction::dd(std::string arg)
1237{
1238 operation_start("imaging");
1239
1240 if (simulate) {
1241 simulate_progress_bar();
1242 } else {
1243 string cmd = "dd " + arg;
1244 TWFunc::Exec_Cmd(cmd);
1245 }
1246 operation_end(0);
1247 return 0;
1248}
1249
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001250int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001251{
1252 operation_start("Partition SD Card");
1253 int ret_val = 0;
1254
1255 if (simulate) {
1256 simulate_progress_bar();
1257 } else {
1258 int allow_partition;
1259 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1260 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001261 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001262 } else {
1263 if (!PartitionManager.Partition_SDCard())
1264 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001265 }
that3f7b1ac2014-12-30 11:30:13 +01001266 }
1267 operation_end(ret_val);
1268 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001269
that3f7b1ac2014-12-30 11:30:13 +01001270}
Dees_Troy51a0e822012-09-05 15:24:24 -04001271
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001272int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001273{
1274 operation_start("Install HTC Dumlock");
1275 if (simulate) {
1276 simulate_progress_bar();
1277 } else
1278 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001279
that3f7b1ac2014-12-30 11:30:13 +01001280 operation_end(0);
1281 return 0;
1282}
Dees_Troy51a0e822012-09-05 15:24:24 -04001283
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001284int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001285{
1286 operation_start("HTC Dumlock Restore Boot");
1287 if (simulate) {
1288 simulate_progress_bar();
1289 } else
1290 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001291
that3f7b1ac2014-12-30 11:30:13 +01001292 operation_end(0);
1293 return 0;
1294}
Dees_Troy51a0e822012-09-05 15:24:24 -04001295
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001296int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001297{
1298 operation_start("HTC Dumlock Reflash Recovery");
1299 if (simulate) {
1300 simulate_progress_bar();
1301 } else
1302 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001303
that3f7b1ac2014-12-30 11:30:13 +01001304 operation_end(0);
1305 return 0;
1306}
Dees_Troy51a0e822012-09-05 15:24:24 -04001307
that3f7b1ac2014-12-30 11:30:13 +01001308int GUIAction::cmd(std::string arg)
1309{
1310 int op_status = 0;
1311
1312 operation_start("Command");
1313 LOGINFO("Running command: '%s'\n", arg.c_str());
1314 if (simulate) {
1315 simulate_progress_bar();
1316 } else {
1317 op_status = TWFunc::Exec_Cmd(arg);
1318 if (op_status != 0)
1319 op_status = 1;
1320 }
1321
1322 operation_end(op_status);
1323 return 0;
1324}
1325
1326int GUIAction::terminalcommand(std::string arg)
1327{
1328 int op_status = 0;
1329 string cmdpath, command;
1330
1331 DataManager::GetValue("tw_terminal_location", cmdpath);
1332 operation_start("CommandOutput");
1333 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1334 if (simulate) {
1335 simulate_progress_bar();
1336 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001337 } else if (arg == "exit") {
1338 LOGINFO("Exiting terminal\n");
1339 operation_end(op_status);
1340 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001341 } else {
1342 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1343 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001344 DataManager::SetValue("tw_terminal_state", 1);
1345 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001346 FILE* fp;
1347 char line[512];
1348
1349 fp = popen(command.c_str(), "r");
1350 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001351 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001352 } else {
1353 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1354 struct timeval timeout;
1355 fd_set fdset;
1356
1357 while(keep_going)
1358 {
1359 FD_ZERO(&fdset);
1360 FD_SET(fd, &fdset);
1361 timeout.tv_sec = 0;
1362 timeout.tv_usec = 400000;
1363 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1364 if (has_data == 0) {
1365 // Timeout reached
1366 DataManager::GetValue("tw_terminal_state", check);
1367 if (check == 0) {
1368 keep_going = 0;
1369 }
1370 } else if (has_data < 0) {
1371 // End of execution
1372 keep_going = 0;
1373 } else {
1374 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001375 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001376 gui_print("%s", line); // Display output
1377 else
1378 keep_going = 0; // Done executing
1379 }
1380 }
1381 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001382 }
thatc6085482015-01-09 22:12:43 +01001383 DataManager::SetValue("tw_operation_status", 0);
1384 DataManager::SetValue("tw_operation_state", 1);
1385 DataManager::SetValue("tw_terminal_state", 0);
1386 DataManager::SetValue("tw_background_thread_running", 0);
1387 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001388 }
1389 return 0;
1390}
1391
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001392int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001393{
1394 int op_status = 0;
1395
1396 LOGINFO("Sending kill command...\n");
1397 operation_start("KillCommand");
1398 DataManager::SetValue("tw_operation_status", 0);
1399 DataManager::SetValue("tw_operation_state", 1);
1400 DataManager::SetValue("tw_terminal_state", 0);
1401 DataManager::SetValue("tw_background_thread_running", 0);
1402 DataManager::SetValue(TW_ACTION_BUSY, 0);
1403 return 0;
1404}
1405
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001406int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001407{
1408 int op_status = 0;
1409 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001411 if (simulate) {
1412 simulate_progress_bar();
1413 } else {
1414 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001415 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001416 }
1417
1418 operation_end(op_status);
1419 return 0;
1420}
1421
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001422int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001423{
1424 int op_status = 0;
1425
1426 operation_start("CheckBackupName");
1427 if (simulate) {
1428 simulate_progress_bar();
1429 } else {
1430 op_status = PartitionManager.Check_Backup_Name(true);
1431 if (op_status != 0)
1432 op_status = 1;
1433 }
1434
1435 operation_end(op_status);
1436 return 0;
1437}
1438
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001439int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001440{
1441 int op_status = 0;
1442
1443 operation_start("Decrypt");
1444 if (simulate) {
1445 simulate_progress_bar();
1446 } else {
1447 string Password;
1448 DataManager::GetValue("tw_crypto_password", Password);
1449 op_status = PartitionManager.Decrypt_Device(Password);
1450 if (op_status != 0)
1451 op_status = 1;
1452 else {
that3f7b1ac2014-12-30 11:30:13 +01001453
1454 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1455
Ethan Yonkercf50da52015-01-12 21:59:07 -06001456 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001457
Ethan Yonkercf50da52015-01-12 21:59:07 -06001458 // Check for a custom theme and load it if exists
1459 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1460 if (has_datamedia != 0) {
1461 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001462 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001463 } else {
1464 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001465 }
1466 }
1467 }
1468 }
1469
1470 operation_end(op_status);
1471 return 0;
1472}
1473
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001474int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001475{
1476 operation_start("Sideload");
1477 if (simulate) {
1478 simulate_progress_bar();
1479 operation_end(0);
1480 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001481 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001482 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1483
1484 // wait for the adb connection
1485 int ret = apply_from_adb("/", &sideload_child_pid);
1486 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1487
1488 if (ret != 0) {
1489 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001491 ret = 1; // failure
1492 } else {
1493 int wipe_cache = 0;
1494 int wipe_dalvik = 0;
1495 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1496
1497 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1498 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1499 PartitionManager.Wipe_By_Path("/cache");
1500 if (wipe_dalvik)
1501 PartitionManager.Wipe_Dalvik_Cache();
1502 } else {
1503 ret = 1; // failure
1504 }
thatcc8ddca2015-01-03 01:59:36 +01001505 }
thatc6085482015-01-09 22:12:43 +01001506 if (sideload_child_pid) {
1507 LOGINFO("Signaling child sideload process to exit.\n");
1508 struct stat st;
1509 // Calling stat() on this magic filename signals the minadbd
1510 // subprocess to shut down.
1511 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1512 int status;
1513 LOGINFO("Waiting for child sideload process to exit.\n");
1514 waitpid(sideload_child_pid, &status, 0);
1515 }
Dees Troy28a85d22015-04-28 02:03:16 +00001516 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001517 TWFunc::Toggle_MTP(mtp_was_enabled);
1518 reinject_after_flash();
1519 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001520 }
that3f7b1ac2014-12-30 11:30:13 +01001521 return 0;
1522}
1523
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001524int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001525{
that3f7b1ac2014-12-30 11:30:13 +01001526 struct stat st;
1527 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001529 LOGINFO("Signaling child sideload process to exit.\n");
1530 // Calling stat() on this magic filename signals the minadbd
1531 // subprocess to shut down.
1532 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1533 if (!sideload_child_pid) {
1534 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001535 return 0;
1536 }
thatcc8ddca2015-01-03 01:59:36 +01001537 ::sleep(1);
1538 LOGINFO("Killing child sideload process.\n");
1539 kill(sideload_child_pid, SIGTERM);
1540 int status;
1541 LOGINFO("Waiting for child sideload process to exit.\n");
1542 waitpid(sideload_child_pid, &status, 0);
1543 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001544 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1545 return 0;
1546}
1547
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001548int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001549{
1550 operation_start("OpenRecoveryScript");
1551 if (simulate) {
1552 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001553 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001554 } else {
that10ae24f2015-12-26 20:53:51 +01001555 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001556 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001557 }
1558 return 0;
1559}
1560
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001561int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001562{
1563 int op_status = 0;
1564
1565 operation_start("Install SuperSU");
1566 if (simulate) {
1567 simulate_progress_bar();
1568 } else {
1569 if (!TWFunc::Install_SuperSU())
1570 op_status = 1;
1571 }
1572
1573 operation_end(op_status);
1574 return 0;
1575}
1576
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001577int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001578{
1579 int op_status = 0;
1580
1581 operation_start("Fixing Superuser Permissions");
1582 if (simulate) {
1583 simulate_progress_bar();
1584 } else {
1585 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1586 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1587 }
1588
1589 operation_end(op_status);
1590 return 0;
1591}
1592
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001593int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001594{
1595 int op_status = 0;
1596
1597 operation_start("Try Restore Decrypt");
1598 if (simulate) {
1599 simulate_progress_bar();
1600 } else {
1601 string Restore_Path, Filename, Password;
1602 DataManager::GetValue("tw_restore", Restore_Path);
1603 Restore_Path += "/";
1604 DataManager::GetValue("tw_restore_password", Password);
1605 TWFunc::SetPerformanceMode(true);
1606 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1607 op_status = 0; // success
1608 else
1609 op_status = 1; // fail
1610 TWFunc::SetPerformanceMode(false);
1611 }
1612
1613 operation_end(op_status);
1614 return 0;
1615}
1616
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001617int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001618{
1619 int op_status = 0;
1620
1621 operation_start("Repair Partition");
1622 if (simulate) {
1623 simulate_progress_bar();
1624 } else {
1625 string part_path;
1626 DataManager::GetValue("tw_partition_mount_point", part_path);
1627 if (PartitionManager.Repair_By_Path(part_path, true)) {
1628 op_status = 0; // success
1629 } else {
that3f7b1ac2014-12-30 11:30:13 +01001630 op_status = 1; // fail
1631 }
1632 }
1633
1634 operation_end(op_status);
1635 return 0;
1636}
1637
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001638int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001639{
1640 int op_status = 0;
1641
1642 operation_start("Resize Partition");
1643 if (simulate) {
1644 simulate_progress_bar();
1645 } else {
1646 string part_path;
1647 DataManager::GetValue("tw_partition_mount_point", part_path);
1648 if (PartitionManager.Resize_By_Path(part_path, true)) {
1649 op_status = 0; // success
1650 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001651 op_status = 1; // fail
1652 }
1653 }
1654
1655 operation_end(op_status);
1656 return 0;
1657}
1658
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001659int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001660{
1661 int op_status = 0;
1662
1663 operation_start("Change File System");
1664 if (simulate) {
1665 simulate_progress_bar();
1666 } else {
1667 string part_path, file_system;
1668 DataManager::GetValue("tw_partition_mount_point", part_path);
1669 DataManager::GetValue("tw_action_new_file_system", file_system);
1670 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1671 op_status = 0; // success
1672 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001673 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001674 op_status = 1; // fail
1675 }
1676 }
1677 PartitionManager.Update_System_Details();
1678 operation_end(op_status);
1679 return 0;
1680}
1681
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001682int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001683{
1684 int op_status = 0;
1685
1686 operation_start("Start MTP");
1687 if (PartitionManager.Enable_MTP())
1688 op_status = 0; // success
1689 else
1690 op_status = 1; // fail
1691
1692 operation_end(op_status);
1693 return 0;
1694}
1695
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001696int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001697{
1698 int op_status = 0;
1699
1700 operation_start("Stop MTP");
1701 if (PartitionManager.Disable_MTP())
1702 op_status = 0; // success
1703 else
1704 op_status = 1; // fail
1705
1706 operation_end(op_status);
1707 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001710int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001711{
1712 int op_status = 0;
1713
1714 operation_start("Flash Image");
1715 string path, filename, full_filename;
1716 DataManager::GetValue("tw_zip_location", path);
1717 DataManager::GetValue("tw_file", filename);
1718 full_filename = path + "/" + filename;
1719 if (PartitionManager.Flash_Image(full_filename))
1720 op_status = 0; // success
1721 else
1722 op_status = 1; // fail
1723
1724 operation_end(op_status);
1725 return 0;
1726}
1727
that10ae24f2015-12-26 20:53:51 +01001728int GUIAction::twcmd(std::string arg)
1729{
1730 operation_start("TWRP CLI Command");
1731 if (simulate)
1732 simulate_progress_bar();
1733 else
1734 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1735 operation_end(0);
1736 return 0;
1737}
1738
Dees_Troy51a0e822012-09-05 15:24:24 -04001739int GUIAction::getKeyByName(std::string key)
1740{
that8834a0f2016-01-05 23:29:30 +01001741 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001742 else if (key == "menu") return KEY_MENU;
1743 else if (key == "back") return KEY_BACK;
1744 else if (key == "search") return KEY_SEARCH;
1745 else if (key == "voldown") return KEY_VOLUMEDOWN;
1746 else if (key == "volup") return KEY_VOLUMEUP;
1747 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001748 int ret_val;
1749 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1750 if (!ret_val)
1751 return KEY_POWER;
1752 else
1753 return ret_val;
1754 }
1755
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001756 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001757}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001758
1759int GUIAction::checkpartitionlifetimewrites(std::string arg)
1760{
1761 int op_status = 0;
1762 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1763
1764 operation_start("Check Partition Lifetime Writes");
1765 if (sys) {
1766 if (sys->Check_Lifetime_Writes() != 0)
1767 DataManager::SetValue("tw_lifetime_writes", 1);
1768 else
1769 DataManager::SetValue("tw_lifetime_writes", 0);
1770 op_status = 0; // success
1771 } else {
1772 DataManager::SetValue("tw_lifetime_writes", 1);
1773 op_status = 1; // fail
1774 }
1775
1776 operation_end(op_status);
1777 return 0;
1778}
1779
1780int GUIAction::mountsystemtoggle(std::string arg)
1781{
1782 int op_status = 0;
1783 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001784 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001785
1786 operation_start("Toggle System Mount");
1787 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1788 op_status = 1; // fail
1789 } else {
1790 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1791 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001792 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001793 DataManager::SetValue("tw_mount_system_ro", 0);
1794 Part->Change_Mount_Read_Only(false);
1795 } else {
1796 DataManager::SetValue("tw_mount_system_ro", 1);
1797 Part->Change_Mount_Read_Only(true);
1798 }
1799 if (remount_system) {
1800 Part->Mount(true);
1801 }
1802 op_status = 0; // success
1803 } else {
1804 op_status = 1; // fail
1805 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001806 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1807 if (Part) {
1808 if (arg == "0") {
1809 Part->Change_Mount_Read_Only(false);
1810 } else {
1811 Part->Change_Mount_Read_Only(true);
1812 }
1813 if (remount_vendor) {
1814 Part->Mount(true);
1815 }
1816 op_status = 0; // success
1817 } else {
1818 op_status = 1; // fail
1819 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001820 }
1821
1822 operation_end(op_status);
1823 return 0;
1824}
Ethan Yonker74db1572015-10-28 12:44:49 -05001825
1826int GUIAction::setlanguage(std::string arg __unused)
1827{
1828 int op_status = 0;
1829
1830 operation_start("Set Language");
1831 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1832 PageManager::RequestReload();
1833 op_status = 0; // success
1834
1835 operation_end(op_status);
1836 return 0;
1837}