blob: 656c687b925d910ee81d7c7a97ad51baddb090eb [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
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400370 return -1;
371
Dees_Troy51a0e822012-09-05 15:24:24 -0400372 if (simulate) {
373 simulate_progress_bar();
374 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400375 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
377 // Now, check if we need to ensure TWRP remains installed...
378 struct stat st;
379 if (stat("/sbin/installTwrp", &st) == 0)
380 {
381 DataManager::SetValue("tw_operation", "Configuring TWRP");
382 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500383 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200384 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400385 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500386 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400387 }
388 }
389 }
390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 // Done
392 DataManager::SetValue("ui_progress", 100);
393 DataManager::SetValue("ui_progress", 0);
394 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400395}
396
that73a52952015-01-28 23:07:34 +0100397GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100398{
that73a52952015-01-28 23:07:34 +0100399 string func = gui_parse_text(action.mFunction);
400 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
401 if (needsThread) {
402 if (func == "cancelbackup")
403 return THREAD_CANCEL;
404 else
405 return THREAD_ACTION;
406 }
407 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100408}
409
Dees_Troy51a0e822012-09-05 15:24:24 -0400410int GUIAction::doActions()
411{
that3f7b1ac2014-12-30 11:30:13 +0100412 if (mActions.size() < 1)
413 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414
that73a52952015-01-28 23:07:34 +0100415 // Determine in which thread to run the actions.
416 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
417 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100418 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100419 for (it = mActions.begin(); it != mActions.end(); ++it) {
420 ThreadType tt = getThreadType(*it);
421 if (tt == THREAD_NONE)
422 continue;
423 if (threadType == THREAD_NONE)
424 threadType = tt;
425 else if (threadType != tt) {
426 LOGERR("Can't mix normal and cancel actions in the same list.\n"
427 "Running the whole batch in the cancel thread.\n");
428 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100429 break;
430 }
that7d3b54f2015-01-09 22:52:51 +0100431 }
that73a52952015-01-28 23:07:34 +0100432
433 // Now run the actions in the desired thread.
434 switch (threadType) {
435 case THREAD_ACTION:
436 action_thread.threadActions(this);
437 break;
438
439 case THREAD_CANCEL:
440 cancel_thread.threadActions(this);
441 break;
442
443 default: {
444 // no iterators here because theme reloading might kill our object
445 const size_t cnt = mActions.size();
446 for (size_t i = 0; i < cnt; ++i)
447 doAction(mActions[i]);
448 }
thatc6085482015-01-09 22:12:43 +0100449 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400450
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200451 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452}
453
that3f7b1ac2014-12-30 11:30:13 +0100454int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400455{
that3f7b1ac2014-12-30 11:30:13 +0100456 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
that3f7b1ac2014-12-30 11:30:13 +0100458 std::string function = gui_parse_text(action.mFunction);
459 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
that3f7b1ac2014-12-30 11:30:13 +0100461 // find function and execute it
462 mapFunc::const_iterator funcitr = mf.find(function);
463 if (funcitr != mf.end())
464 return (this->*funcitr->second)(arg);
465
466 LOGERR("Unknown action '%s'\n", function.c_str());
467 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400468}
469
470void GUIAction::operation_start(const string operation_name)
471{
that3f7b1ac2014-12-30 11:30:13 +0100472 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000473 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 DataManager::SetValue(TW_ACTION_BUSY, 1);
475 DataManager::SetValue("ui_progress", 0);
476 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600478 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
that3f7b1ac2014-12-30 11:30:13 +0100481void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400482{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000483 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 DataManager::SetValue("ui_progress", 100);
486 if (simulate) {
487 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
488 if (simulate_fail != 0)
489 DataManager::SetValue("tw_operation_status", 1);
490 else
491 DataManager::SetValue("tw_operation_status", 0);
492 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500493 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 }
496 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500498 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 }
500 DataManager::SetValue("tw_operation_state", 1);
501 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500502 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200503 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000504 time(&Stop);
505 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600506 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100507 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508}
509
that3f7b1ac2014-12-30 11:30:13 +0100510int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400511{
that3f7b1ac2014-12-30 11:30:13 +0100512 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400513
that3f7b1ac2014-12-30 11:30:13 +0100514 sync();
515 DataManager::SetValue("tw_gui_done", 1);
516 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400517
that3f7b1ac2014-12-30 11:30:13 +0100518 return 0;
519}
Dees_Troy51a0e822012-09-05 15:24:24 -0400520
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500521int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100522{
that3f7b1ac2014-12-30 11:30:13 +0100523 gui_changePage("main");
524 return 0;
525}
Dees_Troy51a0e822012-09-05 15:24:24 -0400526
that3f7b1ac2014-12-30 11:30:13 +0100527int GUIAction::key(std::string arg)
528{
529 const int key = getKeyByName(arg);
530 PageManager::NotifyKey(key, true);
531 PageManager::NotifyKey(key, false);
532 return 0;
533}
534
535int GUIAction::page(std::string arg)
536{
LuK133762326f42015-09-30 19:57:46 +0200537 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100538 std::string page_name = gui_parse_text(arg);
539 return gui_changePage(page_name);
540}
541
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500542int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100543{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500544 PageManager::RequestReload();
545 // The actual reload is handled in pages.cpp in PageManager::RunReload()
546 // The reload will occur on the next Update or Render call and will
547 // be performed in the rendoer thread instead of the action thread
548 // to prevent crashing which could occur when we start deleting
549 // GUI resources in the action thread while we attempt to render
550 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100551 return 0;
552}
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
556 string Restore_Name;
557 DataManager::GetValue("tw_restore", Restore_Name);
558 PartitionManager.Set_Restore_Files(Restore_Name);
559 return 0;
560}
561
562int GUIAction::set(std::string arg)
563{
564 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200565 {
that3f7b1ac2014-12-30 11:30:13 +0100566 string varName = arg.substr(0, arg.find('='));
567 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400568
that3f7b1ac2014-12-30 11:30:13 +0100569 DataManager::GetValue(value, value);
570 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200571 }
that3f7b1ac2014-12-30 11:30:13 +0100572 else
573 DataManager::SetValue(arg, "1");
574 return 0;
575}
Dees_Troy51a0e822012-09-05 15:24:24 -0400576
that3f7b1ac2014-12-30 11:30:13 +0100577int GUIAction::clear(std::string arg)
578{
579 DataManager::SetValue(arg, "0");
580 return 0;
581}
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
that3f7b1ac2014-12-30 11:30:13 +0100583int GUIAction::mount(std::string arg)
584{
585 if (arg == "usb") {
586 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100588 PartitionManager.usb_storage_enable();
589 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500590 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100591 } else if (!simulate) {
592 PartitionManager.Mount_By_Path(arg, true);
593 PartitionManager.Add_MTP_Storage(arg);
594 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500595 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100596 return 0;
597}
598
599int GUIAction::unmount(std::string arg)
600{
601 if (arg == "usb") {
602 if (!simulate)
603 PartitionManager.usb_storage_disable();
604 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500605 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100606 DataManager::SetValue(TW_ACTION_BUSY, 0);
607 } else if (!simulate) {
608 PartitionManager.UnMount_By_Path(arg, true);
609 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500610 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100611 return 0;
612}
613
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500614int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100615{
616 operation_start("Restore Defaults");
617 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100619 else {
620 DataManager::ResetDefaults();
621 PartitionManager.Update_System_Details();
622 PartitionManager.Mount_Current_Storage(true);
623 }
624 operation_end(0);
625 return 0;
626}
627
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500628int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100629{
630 operation_start("Copy Log");
631 if (!simulate)
632 {
633 string dst;
634 PartitionManager.Mount_Current_Storage(true);
635 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
636 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
637 tw_set_default_metadata(dst.c_str());
638 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500639 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100640 } else
641 simulate_progress_bar();
642 operation_end(0);
643 return 0;
644}
645
646
647int GUIAction::compute(std::string arg)
648{
649 if (arg.find("+") != string::npos)
650 {
651 string varName = arg.substr(0, arg.find('+'));
652 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
653 int amount_to_add = atoi(string_to_add.c_str());
654 int value;
655
656 DataManager::GetValue(varName, value);
657 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400658 return 0;
659 }
that3f7b1ac2014-12-30 11:30:13 +0100660 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 {
that3f7b1ac2014-12-30 11:30:13 +0100662 string varName = arg.substr(0, arg.find('-'));
663 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
664 int amount_to_subtract = atoi(string_to_subtract.c_str());
665 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400666
that3f7b1ac2014-12-30 11:30:13 +0100667 DataManager::GetValue(varName, value);
668 value -= amount_to_subtract;
669 if (value <= 0)
670 value = 0;
671 DataManager::SetValue(varName, value);
672 return 0;
673 }
674 if (arg.find("*") != string::npos)
675 {
676 string varName = arg.substr(0, arg.find('*'));
677 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
678 int multiply_by = atoi(multiply_by_str.c_str());
679 int value;
680
681 DataManager::GetValue(varName, value);
682 DataManager::SetValue(varName, value*multiply_by);
683 return 0;
684 }
685 if (arg.find("/") != string::npos)
686 {
687 string varName = arg.substr(0, arg.find('/'));
688 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
689 int divide_by = atoi(divide_by_str.c_str());
690 int value;
691
692 if(divide_by != 0)
693 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100695 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 }
697 return 0;
698 }
that3f7b1ac2014-12-30 11:30:13 +0100699 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
700 return -1;
701}
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500703int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100704{
705 string SelectedZone;
706 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
707 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
708 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
709
710 int dst;
711 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
712
713 string offset;
714 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
715
716 string NewTimeZone = Zone;
717 if (offset != "0")
718 NewTimeZone += ":" + offset;
719
720 if (dst != 0)
721 NewTimeZone += DSTZone;
722
723 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
724 DataManager::update_tz_environment_variables();
725 return 0;
726}
727
728int GUIAction::overlay(std::string arg)
729{
730 return gui_changeOverlay(arg);
731}
732
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500733int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100734{
735 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500736 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 return 0;
738 }
that3f7b1ac2014-12-30 11:30:13 +0100739 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
740 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
741 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100743 }
744 return 0;
745}
746
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500747int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100748{
749 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500750 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100752 } else {
753 zip_queue_index--;
754 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 }
that3f7b1ac2014-12-30 11:30:13 +0100756 return 0;
757}
Dees_Troy51a0e822012-09-05 15:24:24 -0400758
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500759int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100760{
761 zip_queue_index = 0;
762 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
763 return 0;
764}
Dees_Troy51a0e822012-09-05 15:24:24 -0400765
that3f7b1ac2014-12-30 11:30:13 +0100766int GUIAction::sleep(std::string arg)
767{
768 operation_start("Sleep");
769 usleep(atoi(arg.c_str()));
770 operation_end(0);
771 return 0;
772}
Dees Troyb21cc642013-09-10 17:36:41 +0000773
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500774int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100775{
776 operation_start("AppendDateToBackupName");
777 string Backup_Name;
778 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
779 Backup_Name += TWFunc::Get_Current_Date();
780 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
781 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
782 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
783 operation_end(0);
784 return 0;
785}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500786
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500787int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100788{
789 operation_start("GenerateBackupName");
790 TWFunc::Auto_Generate_Backup_Name();
791 operation_end(0);
792 return 0;
793}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500795int GUIAction::checkpartitionlist(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100796{
797 string Wipe_List, wipe_path;
798 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500799
that3f7b1ac2014-12-30 11:30:13 +0100800 DataManager::GetValue("tw_wipe_list", Wipe_List);
801 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
802 if (!Wipe_List.empty()) {
803 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
804 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
805 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
806 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
807 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
808 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 } else {
that3f7b1ac2014-12-30 11:30:13 +0100810 count++;
811 }
812 start_pos = end_pos + 1;
813 end_pos = Wipe_List.find(";", start_pos);
814 }
815 DataManager::SetValue("tw_check_partition_list", count);
816 } else {
817 DataManager::SetValue("tw_check_partition_list", 0);
818 }
819 return 0;
820}
Dees_Troy51a0e822012-09-05 15:24:24 -0400821
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500822int GUIAction::getpartitiondetails(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100823{
824 string Wipe_List, wipe_path;
825 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826
that3f7b1ac2014-12-30 11:30:13 +0100827 DataManager::GetValue("tw_wipe_list", Wipe_List);
828 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
829 if (!Wipe_List.empty()) {
830 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
831 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
832 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
833 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
834 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
835 // Do nothing
836 } else {
837 DataManager::SetValue("tw_partition_path", wipe_path);
838 break;
839 }
840 start_pos = end_pos + 1;
841 end_pos = Wipe_List.find(";", start_pos);
842 }
843 if (!wipe_path.empty()) {
844 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
845 if (Part) {
846 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500847
that3f7b1ac2014-12-30 11:30:13 +0100848 DataManager::SetValue("tw_partition_name", Part->Display_Name);
849 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
850 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
851 DataManager::SetValue("tw_partition_size", Part->Size / mb);
852 DataManager::SetValue("tw_partition_used", Part->Used / mb);
853 DataManager::SetValue("tw_partition_free", Part->Free / mb);
854 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
855 DataManager::SetValue("tw_partition_removable", Part->Removable);
856 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
857
858 if (Part->Can_Repair())
859 DataManager::SetValue("tw_partition_can_repair", 1);
860 else
861 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500862 if (Part->Can_Resize())
863 DataManager::SetValue("tw_partition_can_resize", 1);
864 else
865 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600866 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100867 DataManager::SetValue("tw_partition_vfat", 1);
868 else
869 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600870 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100871 DataManager::SetValue("tw_partition_exfat", 1);
872 else
873 DataManager::SetValue("tw_partition_exfat", 0);
874 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
875 DataManager::SetValue("tw_partition_f2fs", 1);
876 else
877 DataManager::SetValue("tw_partition_f2fs", 0);
878 if (TWFunc::Path_Exists("/sbin/mke2fs"))
879 DataManager::SetValue("tw_partition_ext", 1);
880 else
881 DataManager::SetValue("tw_partition_ext", 0);
882 return 0;
883 } else {
884 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
885 }
886 }
887 }
888 DataManager::SetValue("tw_partition_name", "");
889 DataManager::SetValue("tw_partition_file_system", "");
890 return 0;
891}
892
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500893int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100894{
895 time_t tm;
896 char path[256];
897 int path_len;
898 uid_t uid = -1;
899 gid_t gid = -1;
900
901 struct passwd *pwd = getpwnam("media_rw");
902 if(pwd) {
903 uid = pwd->pw_uid;
904 gid = pwd->pw_gid;
905 }
906
907 const std::string storage = DataManager::GetCurrentStoragePath();
908 if(PartitionManager.Is_Mounted_By_Path(storage)) {
909 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
910 } else {
911 strcpy(path, "/tmp/");
912 }
913
914 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
915 return 0;
916
917 tm = time(NULL);
918 path_len = strlen(path);
919
920 // Screenshot_2014-01-01-18-21-38.png
921 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
922
923 int res = gr_save_screenshot(path);
924 if(res == 0) {
925 chmod(path, 0666);
926 chown(path, uid, gid);
927
that677b13f2015-12-26 23:57:31 +0100928 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100929
930 // blink to notify that the screenshow was taken
931 gr_color(255, 255, 255, 255);
932 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
933 gr_flip();
934 gui_forceRender();
935 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500936 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100937 }
938 return 0;
939}
940
941int GUIAction::setbrightness(std::string arg)
942{
943 return TWFunc::Set_Brightness(arg);
944}
945
946int GUIAction::fileexists(std::string arg)
947{
948 struct stat st;
949 string newpath = arg + "/.";
950
951 operation_start("FileExists");
952 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
953 operation_end(0);
954 else
955 operation_end(1);
956 return 0;
957}
958
thatcc8ddca2015-01-03 01:59:36 +0100959void GUIAction::reinject_after_flash()
960{
961 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500962 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100963 if (simulate) {
964 simulate_progress_bar();
965 } else {
966 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
967 if (Boot == NULL || Boot->Current_File_System != "emmc")
968 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
969 else {
970 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
971 TWFunc::Exec_Cmd(injectcmd);
972 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500973 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100974 }
975 }
976}
977
that3f7b1ac2014-12-30 11:30:13 +0100978int GUIAction::flash(std::string arg)
979{
980 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600981 // We're going to jump to this page first, like a loading page
982 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100983 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200984 string zip_path = zip_queue[i];
985 size_t slashpos = zip_path.find_last_of('/');
986 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100987 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200988 DataManager::SetValue("tw_filename", zip_path);
989 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100990 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
991
992 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +0200993 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100994 TWFunc::SetPerformanceMode(false);
995 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500996 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +0100997 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600998 break;
that3f7b1ac2014-12-30 11:30:13 +0100999 }
1000 }
1001 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001002
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001003 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001004 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001005 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001006 }
that3f7b1ac2014-12-30 11:30:13 +01001007
thatcc8ddca2015-01-03 01:59:36 +01001008 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001009 PartitionManager.Update_System_Details();
1010 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001011 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001012 return 0;
1013}
1014
1015int GUIAction::wipe(std::string arg)
1016{
1017 operation_start("Format");
1018 DataManager::SetValue("tw_partition", arg);
1019
1020 int ret_val = false;
1021
1022 if (simulate) {
1023 simulate_progress_bar();
1024 } else {
1025 if (arg == "data")
1026 ret_val = PartitionManager.Factory_Reset();
1027 else if (arg == "battery")
1028 ret_val = PartitionManager.Wipe_Battery_Stats();
1029 else if (arg == "rotate")
1030 ret_val = PartitionManager.Wipe_Rotate_Data();
1031 else if (arg == "dalvik")
1032 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1033 else if (arg == "DATAMEDIA") {
1034 ret_val = PartitionManager.Format_Data();
1035 } else if (arg == "INTERNAL") {
1036 int has_datamedia, dual_storage;
1037
1038 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1039 if (has_datamedia) {
1040 ret_val = PartitionManager.Wipe_Media_From_Data();
1041 } else {
1042 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1043 }
1044 } else if (arg == "EXTERNAL") {
1045 string External_Path;
1046
1047 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1048 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1049 } else if (arg == "ANDROIDSECURE") {
1050 ret_val = PartitionManager.Wipe_Android_Secure();
1051 } else if (arg == "LIST") {
1052 string Wipe_List, wipe_path;
1053 bool skip = false;
1054 ret_val = true;
1055 TWPartition* wipe_part = NULL;
1056
1057 DataManager::GetValue("tw_wipe_list", Wipe_List);
1058 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1059 if (!Wipe_List.empty()) {
1060 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1061 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1062 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1063 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1064 if (wipe_path == "/and-sec") {
1065 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001066 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001067 ret_val = false;
1068 break;
1069 } else {
1070 skip = true;
1071 }
1072 } else if (wipe_path == "DALVIK") {
1073 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001074 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001075 ret_val = false;
1076 break;
1077 } else {
1078 skip = true;
1079 }
1080 } else if (wipe_path == "INTERNAL") {
1081 if (!PartitionManager.Wipe_Media_From_Data()) {
1082 ret_val = false;
1083 break;
1084 } else {
1085 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001086 }
1087 }
that3f7b1ac2014-12-30 11:30:13 +01001088 if (!skip) {
1089 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001090 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001091 ret_val = false;
1092 break;
1093 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1094 arg = wipe_path;
1095 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 } else {
that3f7b1ac2014-12-30 11:30:13 +01001097 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 }
that3f7b1ac2014-12-30 11:30:13 +01001099 start_pos = end_pos + 1;
1100 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001101 }
1102 }
that3f7b1ac2014-12-30 11:30:13 +01001103 } else
1104 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001105#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001106 if (arg == DataManager::GetSettingsStoragePath()) {
1107 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1108 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001109
that3f7b1ac2014-12-30 11:30:13 +01001110 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1111 LOGINFO("Making TWRP folder and saving settings.\n");
1112 Storage_Path += "/TWRP";
1113 mkdir(Storage_Path.c_str(), 0777);
1114 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 } else {
that3f7b1ac2014-12-30 11:30:13 +01001116 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1117 }
1118 }
1119#endif
1120 }
1121 PartitionManager.Update_System_Details();
1122 if (ret_val)
1123 ret_val = 0; // 0 is success
1124 else
1125 ret_val = 1; // 1 is failure
1126 operation_end(ret_val);
1127 return 0;
1128}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001129
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001130int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001131{
1132 operation_start("Refreshing Sizes");
1133 if (simulate) {
1134 simulate_progress_bar();
1135 } else
1136 PartitionManager.Update_System_Details();
1137 operation_end(0);
1138 return 0;
1139}
1140
1141int GUIAction::nandroid(std::string arg)
1142{
that3f7b1ac2014-12-30 11:30:13 +01001143 if (simulate) {
that73a52952015-01-28 23:07:34 +01001144 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001145 DataManager::SetValue("tw_partition", "Simulation");
1146 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001147 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001148 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001149 operation_start("Nandroid");
1150 int ret = 0;
1151
that3f7b1ac2014-12-30 11:30:13 +01001152 if (arg == "backup") {
1153 string Backup_Name;
1154 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001155 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1156 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001157 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1158 ret = PartitionManager.Run_Backup();
1159 }
1160 else {
1161 operation_end(1);
1162 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001163 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001164 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001165 } else if (arg == "restore") {
1166 string Restore_Name;
1167 DataManager::GetValue("tw_restore", Restore_Name);
1168 ret = PartitionManager.Run_Restore(Restore_Name);
1169 } else {
1170 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001171 return -1;
1172 }
1173 DataManager::SetValue("tw_encrypt_backup", 0);
1174 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001175 if (ret == false)
1176 ret = 1; // 1 for failure
1177 else
1178 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001179 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001180 }
1181 else {
1182 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001183 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001184 ret = 0;
1185 }
that73a52952015-01-28 23:07:34 +01001186 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 return ret;
1188 }
1189 return 0;
1190}
1191
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001192int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001193 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001194 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 }
1196 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 int op_status = PartitionManager.Cancel_Backup();
1198 if (op_status != 0)
1199 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001200 }
1201
1202 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001203}
Dees_Troy51a0e822012-09-05 15:24:24 -04001204
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001205int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001206{
that73a52952015-01-28 23:07:34 +01001207 int op_status = 0;
1208
that3f7b1ac2014-12-30 11:30:13 +01001209 operation_start("Fix Permissions");
1210 LOGINFO("fix permissions started!\n");
1211 if (simulate) {
1212 simulate_progress_bar();
1213 } else {
that73a52952015-01-28 23:07:34 +01001214 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001215 if (op_status != 0)
1216 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001217 }
that73a52952015-01-28 23:07:34 +01001218 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001219 return 0;
1220}
1221
1222int GUIAction::dd(std::string arg)
1223{
1224 operation_start("imaging");
1225
1226 if (simulate) {
1227 simulate_progress_bar();
1228 } else {
1229 string cmd = "dd " + arg;
1230 TWFunc::Exec_Cmd(cmd);
1231 }
1232 operation_end(0);
1233 return 0;
1234}
1235
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001236int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001237{
1238 operation_start("Partition SD Card");
1239 int ret_val = 0;
1240
1241 if (simulate) {
1242 simulate_progress_bar();
1243 } else {
1244 int allow_partition;
1245 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1246 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001247 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001248 } else {
1249 if (!PartitionManager.Partition_SDCard())
1250 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001251 }
that3f7b1ac2014-12-30 11:30:13 +01001252 }
1253 operation_end(ret_val);
1254 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001255
that3f7b1ac2014-12-30 11:30:13 +01001256}
Dees_Troy51a0e822012-09-05 15:24:24 -04001257
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001258int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001259{
1260 operation_start("Install HTC Dumlock");
1261 if (simulate) {
1262 simulate_progress_bar();
1263 } else
1264 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001265
that3f7b1ac2014-12-30 11:30:13 +01001266 operation_end(0);
1267 return 0;
1268}
Dees_Troy51a0e822012-09-05 15:24:24 -04001269
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001270int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001271{
1272 operation_start("HTC Dumlock Restore Boot");
1273 if (simulate) {
1274 simulate_progress_bar();
1275 } else
1276 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001277
that3f7b1ac2014-12-30 11:30:13 +01001278 operation_end(0);
1279 return 0;
1280}
Dees_Troy51a0e822012-09-05 15:24:24 -04001281
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001282int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001283{
1284 operation_start("HTC Dumlock Reflash Recovery");
1285 if (simulate) {
1286 simulate_progress_bar();
1287 } else
1288 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001289
that3f7b1ac2014-12-30 11:30:13 +01001290 operation_end(0);
1291 return 0;
1292}
Dees_Troy51a0e822012-09-05 15:24:24 -04001293
that3f7b1ac2014-12-30 11:30:13 +01001294int GUIAction::cmd(std::string arg)
1295{
1296 int op_status = 0;
1297
1298 operation_start("Command");
1299 LOGINFO("Running command: '%s'\n", arg.c_str());
1300 if (simulate) {
1301 simulate_progress_bar();
1302 } else {
1303 op_status = TWFunc::Exec_Cmd(arg);
1304 if (op_status != 0)
1305 op_status = 1;
1306 }
1307
1308 operation_end(op_status);
1309 return 0;
1310}
1311
1312int GUIAction::terminalcommand(std::string arg)
1313{
1314 int op_status = 0;
1315 string cmdpath, command;
1316
1317 DataManager::GetValue("tw_terminal_location", cmdpath);
1318 operation_start("CommandOutput");
1319 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1320 if (simulate) {
1321 simulate_progress_bar();
1322 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001323 } else if (arg == "exit") {
1324 LOGINFO("Exiting terminal\n");
1325 operation_end(op_status);
1326 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001327 } else {
1328 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1329 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001330 DataManager::SetValue("tw_terminal_state", 1);
1331 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001332 FILE* fp;
1333 char line[512];
1334
1335 fp = popen(command.c_str(), "r");
1336 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001337 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001338 } else {
1339 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1340 struct timeval timeout;
1341 fd_set fdset;
1342
1343 while(keep_going)
1344 {
1345 FD_ZERO(&fdset);
1346 FD_SET(fd, &fdset);
1347 timeout.tv_sec = 0;
1348 timeout.tv_usec = 400000;
1349 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1350 if (has_data == 0) {
1351 // Timeout reached
1352 DataManager::GetValue("tw_terminal_state", check);
1353 if (check == 0) {
1354 keep_going = 0;
1355 }
1356 } else if (has_data < 0) {
1357 // End of execution
1358 keep_going = 0;
1359 } else {
1360 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001361 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001362 gui_print("%s", line); // Display output
1363 else
1364 keep_going = 0; // Done executing
1365 }
1366 }
1367 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001368 }
thatc6085482015-01-09 22:12:43 +01001369 DataManager::SetValue("tw_operation_status", 0);
1370 DataManager::SetValue("tw_operation_state", 1);
1371 DataManager::SetValue("tw_terminal_state", 0);
1372 DataManager::SetValue("tw_background_thread_running", 0);
1373 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001374 }
1375 return 0;
1376}
1377
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001378int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001379{
1380 int op_status = 0;
1381
1382 LOGINFO("Sending kill command...\n");
1383 operation_start("KillCommand");
1384 DataManager::SetValue("tw_operation_status", 0);
1385 DataManager::SetValue("tw_operation_state", 1);
1386 DataManager::SetValue("tw_terminal_state", 0);
1387 DataManager::SetValue("tw_background_thread_running", 0);
1388 DataManager::SetValue(TW_ACTION_BUSY, 0);
1389 return 0;
1390}
1391
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001392int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001393{
1394 int op_status = 0;
1395 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001397 if (simulate) {
1398 simulate_progress_bar();
1399 } else {
1400 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001401 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001402 }
1403
1404 operation_end(op_status);
1405 return 0;
1406}
1407
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001408int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001409{
1410 int op_status = 0;
1411
1412 operation_start("CheckBackupName");
1413 if (simulate) {
1414 simulate_progress_bar();
1415 } else {
1416 op_status = PartitionManager.Check_Backup_Name(true);
1417 if (op_status != 0)
1418 op_status = 1;
1419 }
1420
1421 operation_end(op_status);
1422 return 0;
1423}
1424
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001425int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001426{
1427 int op_status = 0;
1428
1429 operation_start("Decrypt");
1430 if (simulate) {
1431 simulate_progress_bar();
1432 } else {
1433 string Password;
1434 DataManager::GetValue("tw_crypto_password", Password);
1435 op_status = PartitionManager.Decrypt_Device(Password);
1436 if (op_status != 0)
1437 op_status = 1;
1438 else {
that3f7b1ac2014-12-30 11:30:13 +01001439
1440 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1441
Ethan Yonkercf50da52015-01-12 21:59:07 -06001442 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001443
Ethan Yonkercf50da52015-01-12 21:59:07 -06001444 // Check for a custom theme and load it if exists
1445 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1446 if (has_datamedia != 0) {
1447 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001448 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001449 } else {
1450 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001451 }
1452 }
1453 }
1454 }
1455
1456 operation_end(op_status);
1457 return 0;
1458}
1459
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001460int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001461{
1462 operation_start("Sideload");
1463 if (simulate) {
1464 simulate_progress_bar();
1465 operation_end(0);
1466 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001467 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001468 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1469
1470 // wait for the adb connection
1471 int ret = apply_from_adb("/", &sideload_child_pid);
1472 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1473
1474 if (ret != 0) {
1475 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001477 ret = 1; // failure
1478 } else {
1479 int wipe_cache = 0;
1480 int wipe_dalvik = 0;
1481 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1482
1483 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1484 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1485 PartitionManager.Wipe_By_Path("/cache");
1486 if (wipe_dalvik)
1487 PartitionManager.Wipe_Dalvik_Cache();
1488 } else {
1489 ret = 1; // failure
1490 }
thatcc8ddca2015-01-03 01:59:36 +01001491 }
thatc6085482015-01-09 22:12:43 +01001492 if (sideload_child_pid) {
1493 LOGINFO("Signaling child sideload process to exit.\n");
1494 struct stat st;
1495 // Calling stat() on this magic filename signals the minadbd
1496 // subprocess to shut down.
1497 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1498 int status;
1499 LOGINFO("Waiting for child sideload process to exit.\n");
1500 waitpid(sideload_child_pid, &status, 0);
1501 }
Dees Troy28a85d22015-04-28 02:03:16 +00001502 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001503 TWFunc::Toggle_MTP(mtp_was_enabled);
1504 reinject_after_flash();
1505 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001506 }
that3f7b1ac2014-12-30 11:30:13 +01001507 return 0;
1508}
1509
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001510int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001511{
that3f7b1ac2014-12-30 11:30:13 +01001512 struct stat st;
1513 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001514 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001515 LOGINFO("Signaling child sideload process to exit.\n");
1516 // Calling stat() on this magic filename signals the minadbd
1517 // subprocess to shut down.
1518 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1519 if (!sideload_child_pid) {
1520 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001521 return 0;
1522 }
thatcc8ddca2015-01-03 01:59:36 +01001523 ::sleep(1);
1524 LOGINFO("Killing child sideload process.\n");
1525 kill(sideload_child_pid, SIGTERM);
1526 int status;
1527 LOGINFO("Waiting for child sideload process to exit.\n");
1528 waitpid(sideload_child_pid, &status, 0);
1529 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001530 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1531 return 0;
1532}
1533
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001534int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001535{
1536 operation_start("OpenRecoveryScript");
1537 if (simulate) {
1538 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001539 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001540 } else {
that10ae24f2015-12-26 20:53:51 +01001541 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001542 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001543 }
1544 return 0;
1545}
1546
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001547int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001548{
1549 int op_status = 0;
1550
1551 operation_start("Install SuperSU");
1552 if (simulate) {
1553 simulate_progress_bar();
1554 } else {
1555 if (!TWFunc::Install_SuperSU())
1556 op_status = 1;
1557 }
1558
1559 operation_end(op_status);
1560 return 0;
1561}
1562
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001563int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001564{
1565 int op_status = 0;
1566
1567 operation_start("Fixing Superuser Permissions");
1568 if (simulate) {
1569 simulate_progress_bar();
1570 } else {
1571 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1572 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1573 }
1574
1575 operation_end(op_status);
1576 return 0;
1577}
1578
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001579int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001580{
1581 int op_status = 0;
1582
1583 operation_start("Try Restore Decrypt");
1584 if (simulate) {
1585 simulate_progress_bar();
1586 } else {
1587 string Restore_Path, Filename, Password;
1588 DataManager::GetValue("tw_restore", Restore_Path);
1589 Restore_Path += "/";
1590 DataManager::GetValue("tw_restore_password", Password);
1591 TWFunc::SetPerformanceMode(true);
1592 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1593 op_status = 0; // success
1594 else
1595 op_status = 1; // fail
1596 TWFunc::SetPerformanceMode(false);
1597 }
1598
1599 operation_end(op_status);
1600 return 0;
1601}
1602
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001603int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001604{
1605 int op_status = 0;
1606
1607 operation_start("Repair Partition");
1608 if (simulate) {
1609 simulate_progress_bar();
1610 } else {
1611 string part_path;
1612 DataManager::GetValue("tw_partition_mount_point", part_path);
1613 if (PartitionManager.Repair_By_Path(part_path, true)) {
1614 op_status = 0; // success
1615 } else {
that3f7b1ac2014-12-30 11:30:13 +01001616 op_status = 1; // fail
1617 }
1618 }
1619
1620 operation_end(op_status);
1621 return 0;
1622}
1623
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001624int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001625{
1626 int op_status = 0;
1627
1628 operation_start("Resize Partition");
1629 if (simulate) {
1630 simulate_progress_bar();
1631 } else {
1632 string part_path;
1633 DataManager::GetValue("tw_partition_mount_point", part_path);
1634 if (PartitionManager.Resize_By_Path(part_path, true)) {
1635 op_status = 0; // success
1636 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001637 op_status = 1; // fail
1638 }
1639 }
1640
1641 operation_end(op_status);
1642 return 0;
1643}
1644
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001645int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001646{
1647 int op_status = 0;
1648
1649 operation_start("Change File System");
1650 if (simulate) {
1651 simulate_progress_bar();
1652 } else {
1653 string part_path, file_system;
1654 DataManager::GetValue("tw_partition_mount_point", part_path);
1655 DataManager::GetValue("tw_action_new_file_system", file_system);
1656 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1657 op_status = 0; // success
1658 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001659 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001660 op_status = 1; // fail
1661 }
1662 }
1663 PartitionManager.Update_System_Details();
1664 operation_end(op_status);
1665 return 0;
1666}
1667
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001668int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001669{
1670 int op_status = 0;
1671
1672 operation_start("Start MTP");
1673 if (PartitionManager.Enable_MTP())
1674 op_status = 0; // success
1675 else
1676 op_status = 1; // fail
1677
1678 operation_end(op_status);
1679 return 0;
1680}
1681
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001682int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001683{
1684 int op_status = 0;
1685
1686 operation_start("Stop MTP");
1687 if (PartitionManager.Disable_MTP())
1688 op_status = 0; // success
1689 else
1690 op_status = 1; // fail
1691
1692 operation_end(op_status);
1693 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001694}
1695
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001696int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001697{
1698 int op_status = 0;
1699
1700 operation_start("Flash Image");
1701 string path, filename, full_filename;
1702 DataManager::GetValue("tw_zip_location", path);
1703 DataManager::GetValue("tw_file", filename);
1704 full_filename = path + "/" + filename;
1705 if (PartitionManager.Flash_Image(full_filename))
1706 op_status = 0; // success
1707 else
1708 op_status = 1; // fail
1709
1710 operation_end(op_status);
1711 return 0;
1712}
1713
that10ae24f2015-12-26 20:53:51 +01001714int GUIAction::twcmd(std::string arg)
1715{
1716 operation_start("TWRP CLI Command");
1717 if (simulate)
1718 simulate_progress_bar();
1719 else
1720 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1721 operation_end(0);
1722 return 0;
1723}
1724
Dees_Troy51a0e822012-09-05 15:24:24 -04001725int GUIAction::getKeyByName(std::string key)
1726{
that8834a0f2016-01-05 23:29:30 +01001727 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001728 else if (key == "menu") return KEY_MENU;
1729 else if (key == "back") return KEY_BACK;
1730 else if (key == "search") return KEY_SEARCH;
1731 else if (key == "voldown") return KEY_VOLUMEDOWN;
1732 else if (key == "volup") return KEY_VOLUMEUP;
1733 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001734 int ret_val;
1735 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1736 if (!ret_val)
1737 return KEY_POWER;
1738 else
1739 return ret_val;
1740 }
1741
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001742 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001743}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001744
1745int GUIAction::checkpartitionlifetimewrites(std::string arg)
1746{
1747 int op_status = 0;
1748 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1749
1750 operation_start("Check Partition Lifetime Writes");
1751 if (sys) {
1752 if (sys->Check_Lifetime_Writes() != 0)
1753 DataManager::SetValue("tw_lifetime_writes", 1);
1754 else
1755 DataManager::SetValue("tw_lifetime_writes", 0);
1756 op_status = 0; // success
1757 } else {
1758 DataManager::SetValue("tw_lifetime_writes", 1);
1759 op_status = 1; // fail
1760 }
1761
1762 operation_end(op_status);
1763 return 0;
1764}
1765
1766int GUIAction::mountsystemtoggle(std::string arg)
1767{
1768 int op_status = 0;
1769 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001770 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001771
1772 operation_start("Toggle System Mount");
1773 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1774 op_status = 1; // fail
1775 } else {
1776 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1777 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001778 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001779 DataManager::SetValue("tw_mount_system_ro", 0);
1780 Part->Change_Mount_Read_Only(false);
1781 } else {
1782 DataManager::SetValue("tw_mount_system_ro", 1);
1783 Part->Change_Mount_Read_Only(true);
1784 }
1785 if (remount_system) {
1786 Part->Mount(true);
1787 }
1788 op_status = 0; // success
1789 } else {
1790 op_status = 1; // fail
1791 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001792 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1793 if (Part) {
1794 if (arg == "0") {
1795 Part->Change_Mount_Read_Only(false);
1796 } else {
1797 Part->Change_Mount_Read_Only(true);
1798 }
1799 if (remount_vendor) {
1800 Part->Mount(true);
1801 }
1802 op_status = 0; // success
1803 } else {
1804 op_status = 1; // fail
1805 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001806 }
1807
1808 operation_end(op_status);
1809 return 0;
1810}
Ethan Yonker74db1572015-10-28 12:44:49 -05001811
1812int GUIAction::setlanguage(std::string arg __unused)
1813{
1814 int op_status = 0;
1815
1816 operation_start("Set Language");
1817 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1818 PageManager::RequestReload();
1819 op_status = 0; // success
1820
1821 operation_end(op_status);
1822 return 0;
1823}