blob: 9f746c3484b77e25dc929d86fb642add8c25d077 [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 if (mKeys.empty())
296 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100298 std::map<int, bool>::iterator itr = mKeys.find(key);
299 if(itr == mKeys.end())
300 return 0;
301
302 bool prevState = itr->second;
303 itr->second = down;
304
305 // If there is only one key for this action, wait for key up so it
306 // doesn't trigger with multi-key actions.
307 // Else, check if all buttons are pressed, then consume their release events
308 // so they don't trigger one-button actions and reset mKeys pressed status
309 if(mKeys.size() == 1) {
310 if(!down && prevState)
311 doActions();
312 } else if(down) {
313 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
314 if(!itr->second)
315 return 0;
316 }
317
318 // Passed, all req buttons are pressed, reset them and consume release events
319 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
320 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
321 kb->ConsumeKeyRelease(itr->first);
322 itr->second = false;
323 }
324
325 doActions();
326 }
327
Dees_Troy51a0e822012-09-05 15:24:24 -0400328 return 0;
329}
330
Vojtech Bocek07220562014-02-08 02:05:33 +0100331int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400332{
Vojtech Bocek07220562014-02-08 02:05:33 +0100333 GUIObject::NotifyVarChange(varName, value);
334
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100337 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
343void GUIAction::simulate_progress_bar(void)
344{
Ethan Yonker74db1572015-10-28 12:44:49 -0500345 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 for (int i = 0; i < 5; i++)
347 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500348 if (PartitionManager.stop_backup.get_value()) {
349 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500350 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -0500351 DataManager::SetValue("ui_progress", 0);
352 PartitionManager.stop_backup.set_value(0);
353 return;
354 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 usleep(500000);
356 DataManager::SetValue("ui_progress", i * 20);
357 }
358}
359
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600360int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400361{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200362 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400363
364 DataManager::SetValue("ui_progress", 0);
365
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 if (filename.empty())
367 {
368 LOGERR("No file specified.\n");
369 return -1;
370 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400373 return -1;
374
Dees_Troy51a0e822012-09-05 15:24:24 -0400375 if (simulate) {
376 simulate_progress_bar();
377 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400378 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
380 // Now, check if we need to ensure TWRP remains installed...
381 struct stat st;
382 if (stat("/sbin/installTwrp", &st) == 0)
383 {
384 DataManager::SetValue("tw_operation", "Configuring TWRP");
385 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500386 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200387 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400388 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500389 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400390 }
391 }
392 }
393
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 // Done
395 DataManager::SetValue("ui_progress", 100);
396 DataManager::SetValue("ui_progress", 0);
397 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400398}
399
that73a52952015-01-28 23:07:34 +0100400GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100401{
that73a52952015-01-28 23:07:34 +0100402 string func = gui_parse_text(action.mFunction);
403 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
404 if (needsThread) {
405 if (func == "cancelbackup")
406 return THREAD_CANCEL;
407 else
408 return THREAD_ACTION;
409 }
410 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100411}
412
Dees_Troy51a0e822012-09-05 15:24:24 -0400413int GUIAction::doActions()
414{
that3f7b1ac2014-12-30 11:30:13 +0100415 if (mActions.size() < 1)
416 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417
that73a52952015-01-28 23:07:34 +0100418 // Determine in which thread to run the actions.
419 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
420 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100421 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100422 for (it = mActions.begin(); it != mActions.end(); ++it) {
423 ThreadType tt = getThreadType(*it);
424 if (tt == THREAD_NONE)
425 continue;
426 if (threadType == THREAD_NONE)
427 threadType = tt;
428 else if (threadType != tt) {
429 LOGERR("Can't mix normal and cancel actions in the same list.\n"
430 "Running the whole batch in the cancel thread.\n");
431 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100432 break;
433 }
that7d3b54f2015-01-09 22:52:51 +0100434 }
that73a52952015-01-28 23:07:34 +0100435
436 // Now run the actions in the desired thread.
437 switch (threadType) {
438 case THREAD_ACTION:
439 action_thread.threadActions(this);
440 break;
441
442 case THREAD_CANCEL:
443 cancel_thread.threadActions(this);
444 break;
445
446 default: {
447 // no iterators here because theme reloading might kill our object
448 const size_t cnt = mActions.size();
449 for (size_t i = 0; i < cnt; ++i)
450 doAction(mActions[i]);
451 }
thatc6085482015-01-09 22:12:43 +0100452 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400453
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200454 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400455}
456
that3f7b1ac2014-12-30 11:30:13 +0100457int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400458{
that3f7b1ac2014-12-30 11:30:13 +0100459 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
that3f7b1ac2014-12-30 11:30:13 +0100461 std::string function = gui_parse_text(action.mFunction);
462 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400463
that3f7b1ac2014-12-30 11:30:13 +0100464 // find function and execute it
465 mapFunc::const_iterator funcitr = mf.find(function);
466 if (funcitr != mf.end())
467 return (this->*funcitr->second)(arg);
468
469 LOGERR("Unknown action '%s'\n", function.c_str());
470 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400471}
472
473void GUIAction::operation_start(const string operation_name)
474{
that3f7b1ac2014-12-30 11:30:13 +0100475 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000476 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 DataManager::SetValue(TW_ACTION_BUSY, 1);
478 DataManager::SetValue("ui_progress", 0);
479 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600481 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482}
483
that3f7b1ac2014-12-30 11:30:13 +0100484void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400485{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000486 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 DataManager::SetValue("ui_progress", 100);
489 if (simulate) {
490 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
491 if (simulate_fail != 0)
492 DataManager::SetValue("tw_operation_status", 1);
493 else
494 DataManager::SetValue("tw_operation_status", 0);
495 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500496 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500498 }
499 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500501 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 }
503 DataManager::SetValue("tw_operation_state", 1);
504 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500505 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200506 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000507 time(&Stop);
508 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600509 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100510 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511}
512
that3f7b1ac2014-12-30 11:30:13 +0100513int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400514{
that3f7b1ac2014-12-30 11:30:13 +0100515 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400516
that3f7b1ac2014-12-30 11:30:13 +0100517 sync();
518 DataManager::SetValue("tw_gui_done", 1);
519 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400520
that3f7b1ac2014-12-30 11:30:13 +0100521 return 0;
522}
Dees_Troy51a0e822012-09-05 15:24:24 -0400523
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500524int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100525{
that3f7b1ac2014-12-30 11:30:13 +0100526 gui_changePage("main");
527 return 0;
528}
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
that3f7b1ac2014-12-30 11:30:13 +0100530int GUIAction::key(std::string arg)
531{
532 const int key = getKeyByName(arg);
533 PageManager::NotifyKey(key, true);
534 PageManager::NotifyKey(key, false);
535 return 0;
536}
537
538int GUIAction::page(std::string arg)
539{
LuK133762326f42015-09-30 19:57:46 +0200540 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100541 std::string page_name = gui_parse_text(arg);
542 return gui_changePage(page_name);
543}
544
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500545int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100546{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500547 PageManager::RequestReload();
548 // The actual reload is handled in pages.cpp in PageManager::RunReload()
549 // The reload will occur on the next Update or Render call and will
550 // be performed in the rendoer thread instead of the action thread
551 // to prevent crashing which could occur when we start deleting
552 // GUI resources in the action thread while we attempt to render
553 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100554 return 0;
555}
Dees_Troy51a0e822012-09-05 15:24:24 -0400556
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500557int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100558{
559 string Restore_Name;
560 DataManager::GetValue("tw_restore", Restore_Name);
561 PartitionManager.Set_Restore_Files(Restore_Name);
562 return 0;
563}
564
565int GUIAction::set(std::string arg)
566{
567 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200568 {
that3f7b1ac2014-12-30 11:30:13 +0100569 string varName = arg.substr(0, arg.find('='));
570 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400571
that3f7b1ac2014-12-30 11:30:13 +0100572 DataManager::GetValue(value, value);
573 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200574 }
that3f7b1ac2014-12-30 11:30:13 +0100575 else
576 DataManager::SetValue(arg, "1");
577 return 0;
578}
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
that3f7b1ac2014-12-30 11:30:13 +0100580int GUIAction::clear(std::string arg)
581{
582 DataManager::SetValue(arg, "0");
583 return 0;
584}
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
that3f7b1ac2014-12-30 11:30:13 +0100586int GUIAction::mount(std::string arg)
587{
588 if (arg == "usb") {
589 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400590 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100591 PartitionManager.usb_storage_enable();
592 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500593 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100594 } else if (!simulate) {
595 PartitionManager.Mount_By_Path(arg, true);
596 PartitionManager.Add_MTP_Storage(arg);
597 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500598 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100599 return 0;
600}
601
602int GUIAction::unmount(std::string arg)
603{
604 if (arg == "usb") {
605 if (!simulate)
606 PartitionManager.usb_storage_disable();
607 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500608 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100609 DataManager::SetValue(TW_ACTION_BUSY, 0);
610 } else if (!simulate) {
611 PartitionManager.UnMount_By_Path(arg, true);
612 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500613 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100614 return 0;
615}
616
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500617int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100618{
619 operation_start("Restore Defaults");
620 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500621 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100622 else {
623 DataManager::ResetDefaults();
624 PartitionManager.Update_System_Details();
625 PartitionManager.Mount_Current_Storage(true);
626 }
627 operation_end(0);
628 return 0;
629}
630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500631int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100632{
633 operation_start("Copy Log");
634 if (!simulate)
635 {
636 string dst;
637 PartitionManager.Mount_Current_Storage(true);
638 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
639 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
640 tw_set_default_metadata(dst.c_str());
641 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500642 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100643 } else
644 simulate_progress_bar();
645 operation_end(0);
646 return 0;
647}
648
649
650int GUIAction::compute(std::string arg)
651{
652 if (arg.find("+") != string::npos)
653 {
654 string varName = arg.substr(0, arg.find('+'));
655 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
656 int amount_to_add = atoi(string_to_add.c_str());
657 int value;
658
659 DataManager::GetValue(varName, value);
660 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 return 0;
662 }
that3f7b1ac2014-12-30 11:30:13 +0100663 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 {
that3f7b1ac2014-12-30 11:30:13 +0100665 string varName = arg.substr(0, arg.find('-'));
666 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
667 int amount_to_subtract = atoi(string_to_subtract.c_str());
668 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400669
that3f7b1ac2014-12-30 11:30:13 +0100670 DataManager::GetValue(varName, value);
671 value -= amount_to_subtract;
672 if (value <= 0)
673 value = 0;
674 DataManager::SetValue(varName, value);
675 return 0;
676 }
677 if (arg.find("*") != string::npos)
678 {
679 string varName = arg.substr(0, arg.find('*'));
680 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
681 int multiply_by = atoi(multiply_by_str.c_str());
682 int value;
683
684 DataManager::GetValue(varName, value);
685 DataManager::SetValue(varName, value*multiply_by);
686 return 0;
687 }
688 if (arg.find("/") != string::npos)
689 {
690 string varName = arg.substr(0, arg.find('/'));
691 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
692 int divide_by = atoi(divide_by_str.c_str());
693 int value;
694
695 if(divide_by != 0)
696 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100698 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 }
700 return 0;
701 }
that3f7b1ac2014-12-30 11:30:13 +0100702 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
703 return -1;
704}
Dees_Troy51a0e822012-09-05 15:24:24 -0400705
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500706int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100707{
708 string SelectedZone;
709 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
710 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
711 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
712
713 int dst;
714 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
715
716 string offset;
717 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
718
719 string NewTimeZone = Zone;
720 if (offset != "0")
721 NewTimeZone += ":" + offset;
722
723 if (dst != 0)
724 NewTimeZone += DSTZone;
725
726 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
727 DataManager::update_tz_environment_variables();
728 return 0;
729}
730
731int GUIAction::overlay(std::string arg)
732{
733 return gui_changeOverlay(arg);
734}
735
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500736int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100737{
738 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500739 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 return 0;
741 }
that3f7b1ac2014-12-30 11:30:13 +0100742 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
743 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
744 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100746 }
747 return 0;
748}
749
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500750int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100751{
752 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400754 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100755 } else {
756 zip_queue_index--;
757 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 }
that3f7b1ac2014-12-30 11:30:13 +0100759 return 0;
760}
Dees_Troy51a0e822012-09-05 15:24:24 -0400761
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500762int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100763{
764 zip_queue_index = 0;
765 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
766 return 0;
767}
Dees_Troy51a0e822012-09-05 15:24:24 -0400768
that3f7b1ac2014-12-30 11:30:13 +0100769int GUIAction::sleep(std::string arg)
770{
771 operation_start("Sleep");
772 usleep(atoi(arg.c_str()));
773 operation_end(0);
774 return 0;
775}
Dees Troyb21cc642013-09-10 17:36:41 +0000776
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500777int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100778{
779 operation_start("AppendDateToBackupName");
780 string Backup_Name;
781 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
782 Backup_Name += TWFunc::Get_Current_Date();
783 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
784 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
785 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
786 operation_end(0);
787 return 0;
788}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500789
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500790int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100791{
792 operation_start("GenerateBackupName");
793 TWFunc::Auto_Generate_Backup_Name();
794 operation_end(0);
795 return 0;
796}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500797
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500798int GUIAction::checkpartitionlist(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100799{
800 string Wipe_List, wipe_path;
801 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500802
that3f7b1ac2014-12-30 11:30:13 +0100803 DataManager::GetValue("tw_wipe_list", Wipe_List);
804 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
805 if (!Wipe_List.empty()) {
806 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
807 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
808 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
809 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
810 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
811 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400812 } else {
that3f7b1ac2014-12-30 11:30:13 +0100813 count++;
814 }
815 start_pos = end_pos + 1;
816 end_pos = Wipe_List.find(";", start_pos);
817 }
818 DataManager::SetValue("tw_check_partition_list", count);
819 } else {
820 DataManager::SetValue("tw_check_partition_list", 0);
821 }
822 return 0;
823}
Dees_Troy51a0e822012-09-05 15:24:24 -0400824
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500825int GUIAction::getpartitiondetails(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100826{
827 string Wipe_List, wipe_path;
828 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829
that3f7b1ac2014-12-30 11:30:13 +0100830 DataManager::GetValue("tw_wipe_list", Wipe_List);
831 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
832 if (!Wipe_List.empty()) {
833 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
834 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
835 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
836 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
837 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
838 // Do nothing
839 } else {
840 DataManager::SetValue("tw_partition_path", wipe_path);
841 break;
842 }
843 start_pos = end_pos + 1;
844 end_pos = Wipe_List.find(";", start_pos);
845 }
846 if (!wipe_path.empty()) {
847 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
848 if (Part) {
849 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500850
that3f7b1ac2014-12-30 11:30:13 +0100851 DataManager::SetValue("tw_partition_name", Part->Display_Name);
852 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
853 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
854 DataManager::SetValue("tw_partition_size", Part->Size / mb);
855 DataManager::SetValue("tw_partition_used", Part->Used / mb);
856 DataManager::SetValue("tw_partition_free", Part->Free / mb);
857 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
858 DataManager::SetValue("tw_partition_removable", Part->Removable);
859 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
860
861 if (Part->Can_Repair())
862 DataManager::SetValue("tw_partition_can_repair", 1);
863 else
864 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500865 if (Part->Can_Resize())
866 DataManager::SetValue("tw_partition_can_resize", 1);
867 else
868 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600869 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100870 DataManager::SetValue("tw_partition_vfat", 1);
871 else
872 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600873 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100874 DataManager::SetValue("tw_partition_exfat", 1);
875 else
876 DataManager::SetValue("tw_partition_exfat", 0);
877 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
878 DataManager::SetValue("tw_partition_f2fs", 1);
879 else
880 DataManager::SetValue("tw_partition_f2fs", 0);
881 if (TWFunc::Path_Exists("/sbin/mke2fs"))
882 DataManager::SetValue("tw_partition_ext", 1);
883 else
884 DataManager::SetValue("tw_partition_ext", 0);
885 return 0;
886 } else {
887 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
888 }
889 }
890 }
891 DataManager::SetValue("tw_partition_name", "");
892 DataManager::SetValue("tw_partition_file_system", "");
893 return 0;
894}
895
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500896int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100897{
898 time_t tm;
899 char path[256];
900 int path_len;
901 uid_t uid = -1;
902 gid_t gid = -1;
903
904 struct passwd *pwd = getpwnam("media_rw");
905 if(pwd) {
906 uid = pwd->pw_uid;
907 gid = pwd->pw_gid;
908 }
909
910 const std::string storage = DataManager::GetCurrentStoragePath();
911 if(PartitionManager.Is_Mounted_By_Path(storage)) {
912 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
913 } else {
914 strcpy(path, "/tmp/");
915 }
916
917 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
918 return 0;
919
920 tm = time(NULL);
921 path_len = strlen(path);
922
923 // Screenshot_2014-01-01-18-21-38.png
924 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
925
926 int res = gr_save_screenshot(path);
927 if(res == 0) {
928 chmod(path, 0666);
929 chown(path, uid, gid);
930
that677b13f2015-12-26 23:57:31 +0100931 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100932
933 // blink to notify that the screenshow was taken
934 gr_color(255, 255, 255, 255);
935 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
936 gr_flip();
937 gui_forceRender();
938 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500939 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100940 }
941 return 0;
942}
943
944int GUIAction::setbrightness(std::string arg)
945{
946 return TWFunc::Set_Brightness(arg);
947}
948
949int GUIAction::fileexists(std::string arg)
950{
951 struct stat st;
952 string newpath = arg + "/.";
953
954 operation_start("FileExists");
955 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
956 operation_end(0);
957 else
958 operation_end(1);
959 return 0;
960}
961
thatcc8ddca2015-01-03 01:59:36 +0100962void GUIAction::reinject_after_flash()
963{
964 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500965 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100966 if (simulate) {
967 simulate_progress_bar();
968 } else {
969 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
970 if (Boot == NULL || Boot->Current_File_System != "emmc")
971 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
972 else {
973 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
974 TWFunc::Exec_Cmd(injectcmd);
975 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500976 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100977 }
978 }
979}
980
that3f7b1ac2014-12-30 11:30:13 +0100981int GUIAction::flash(std::string arg)
982{
983 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600984 // We're going to jump to this page first, like a loading page
985 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100986 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200987 string zip_path = zip_queue[i];
988 size_t slashpos = zip_path.find_last_of('/');
989 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100990 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200991 DataManager::SetValue("tw_filename", zip_path);
992 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100993 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
994
995 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +0200996 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100997 TWFunc::SetPerformanceMode(false);
998 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500999 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001000 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001001 break;
that3f7b1ac2014-12-30 11:30:13 +01001002 }
1003 }
1004 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001005
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001006 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001007 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001008 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001009 }
that3f7b1ac2014-12-30 11:30:13 +01001010
thatcc8ddca2015-01-03 01:59:36 +01001011 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001012 PartitionManager.Update_System_Details();
1013 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001014 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001015 return 0;
1016}
1017
1018int GUIAction::wipe(std::string arg)
1019{
1020 operation_start("Format");
1021 DataManager::SetValue("tw_partition", arg);
1022
1023 int ret_val = false;
1024
1025 if (simulate) {
1026 simulate_progress_bar();
1027 } else {
1028 if (arg == "data")
1029 ret_val = PartitionManager.Factory_Reset();
1030 else if (arg == "battery")
1031 ret_val = PartitionManager.Wipe_Battery_Stats();
1032 else if (arg == "rotate")
1033 ret_val = PartitionManager.Wipe_Rotate_Data();
1034 else if (arg == "dalvik")
1035 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1036 else if (arg == "DATAMEDIA") {
1037 ret_val = PartitionManager.Format_Data();
1038 } else if (arg == "INTERNAL") {
1039 int has_datamedia, dual_storage;
1040
1041 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1042 if (has_datamedia) {
1043 ret_val = PartitionManager.Wipe_Media_From_Data();
1044 } else {
1045 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1046 }
1047 } else if (arg == "EXTERNAL") {
1048 string External_Path;
1049
1050 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1051 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1052 } else if (arg == "ANDROIDSECURE") {
1053 ret_val = PartitionManager.Wipe_Android_Secure();
1054 } else if (arg == "LIST") {
1055 string Wipe_List, wipe_path;
1056 bool skip = false;
1057 ret_val = true;
1058 TWPartition* wipe_part = NULL;
1059
1060 DataManager::GetValue("tw_wipe_list", Wipe_List);
1061 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1062 if (!Wipe_List.empty()) {
1063 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1064 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1065 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1066 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1067 if (wipe_path == "/and-sec") {
1068 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001069 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001070 ret_val = false;
1071 break;
1072 } else {
1073 skip = true;
1074 }
1075 } else if (wipe_path == "DALVIK") {
1076 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001077 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001078 ret_val = false;
1079 break;
1080 } else {
1081 skip = true;
1082 }
1083 } else if (wipe_path == "INTERNAL") {
1084 if (!PartitionManager.Wipe_Media_From_Data()) {
1085 ret_val = false;
1086 break;
1087 } else {
1088 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001089 }
1090 }
that3f7b1ac2014-12-30 11:30:13 +01001091 if (!skip) {
1092 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001093 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001094 ret_val = false;
1095 break;
1096 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1097 arg = wipe_path;
1098 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001099 } else {
that3f7b1ac2014-12-30 11:30:13 +01001100 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 }
that3f7b1ac2014-12-30 11:30:13 +01001102 start_pos = end_pos + 1;
1103 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001104 }
1105 }
that3f7b1ac2014-12-30 11:30:13 +01001106 } else
1107 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001108#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001109 if (arg == DataManager::GetSettingsStoragePath()) {
1110 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1111 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001112
that3f7b1ac2014-12-30 11:30:13 +01001113 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1114 LOGINFO("Making TWRP folder and saving settings.\n");
1115 Storage_Path += "/TWRP";
1116 mkdir(Storage_Path.c_str(), 0777);
1117 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 } else {
that3f7b1ac2014-12-30 11:30:13 +01001119 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1120 }
1121 }
1122#endif
1123 }
1124 PartitionManager.Update_System_Details();
1125 if (ret_val)
1126 ret_val = 0; // 0 is success
1127 else
1128 ret_val = 1; // 1 is failure
1129 operation_end(ret_val);
1130 return 0;
1131}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001132
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001133int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001134{
1135 operation_start("Refreshing Sizes");
1136 if (simulate) {
1137 simulate_progress_bar();
1138 } else
1139 PartitionManager.Update_System_Details();
1140 operation_end(0);
1141 return 0;
1142}
1143
1144int GUIAction::nandroid(std::string arg)
1145{
that3f7b1ac2014-12-30 11:30:13 +01001146 if (simulate) {
that73a52952015-01-28 23:07:34 +01001147 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001148 DataManager::SetValue("tw_partition", "Simulation");
1149 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001150 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001151 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001152 operation_start("Nandroid");
1153 int ret = 0;
1154
that3f7b1ac2014-12-30 11:30:13 +01001155 if (arg == "backup") {
1156 string Backup_Name;
1157 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001158 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1159 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001160 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1161 ret = PartitionManager.Run_Backup();
1162 }
1163 else {
1164 operation_end(1);
1165 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001166 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001167 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001168 } else if (arg == "restore") {
1169 string Restore_Name;
1170 DataManager::GetValue("tw_restore", Restore_Name);
1171 ret = PartitionManager.Run_Restore(Restore_Name);
1172 } else {
1173 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001174 return -1;
1175 }
1176 DataManager::SetValue("tw_encrypt_backup", 0);
1177 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001178 if (ret == false)
1179 ret = 1; // 1 for failure
1180 else
1181 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001182 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001183 }
1184 else {
1185 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001186 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 ret = 0;
1188 }
that73a52952015-01-28 23:07:34 +01001189 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001190 return ret;
1191 }
1192 return 0;
1193}
1194
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001195int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 }
1199 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001200 int op_status = PartitionManager.Cancel_Backup();
1201 if (op_status != 0)
1202 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001203 }
1204
1205 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001206}
Dees_Troy51a0e822012-09-05 15:24:24 -04001207
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001208int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001209{
that73a52952015-01-28 23:07:34 +01001210 int op_status = 0;
1211
that3f7b1ac2014-12-30 11:30:13 +01001212 operation_start("Fix Permissions");
1213 LOGINFO("fix permissions started!\n");
1214 if (simulate) {
1215 simulate_progress_bar();
1216 } else {
that73a52952015-01-28 23:07:34 +01001217 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001218 if (op_status != 0)
1219 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001220 }
that73a52952015-01-28 23:07:34 +01001221 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001222 return 0;
1223}
1224
1225int GUIAction::dd(std::string arg)
1226{
1227 operation_start("imaging");
1228
1229 if (simulate) {
1230 simulate_progress_bar();
1231 } else {
1232 string cmd = "dd " + arg;
1233 TWFunc::Exec_Cmd(cmd);
1234 }
1235 operation_end(0);
1236 return 0;
1237}
1238
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001239int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001240{
1241 operation_start("Partition SD Card");
1242 int ret_val = 0;
1243
1244 if (simulate) {
1245 simulate_progress_bar();
1246 } else {
1247 int allow_partition;
1248 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1249 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001250 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001251 } else {
1252 if (!PartitionManager.Partition_SDCard())
1253 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001254 }
that3f7b1ac2014-12-30 11:30:13 +01001255 }
1256 operation_end(ret_val);
1257 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001258
that3f7b1ac2014-12-30 11:30:13 +01001259}
Dees_Troy51a0e822012-09-05 15:24:24 -04001260
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001261int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001262{
1263 operation_start("Install HTC Dumlock");
1264 if (simulate) {
1265 simulate_progress_bar();
1266 } else
1267 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001268
that3f7b1ac2014-12-30 11:30:13 +01001269 operation_end(0);
1270 return 0;
1271}
Dees_Troy51a0e822012-09-05 15:24:24 -04001272
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001273int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001274{
1275 operation_start("HTC Dumlock Restore Boot");
1276 if (simulate) {
1277 simulate_progress_bar();
1278 } else
1279 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001280
that3f7b1ac2014-12-30 11:30:13 +01001281 operation_end(0);
1282 return 0;
1283}
Dees_Troy51a0e822012-09-05 15:24:24 -04001284
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001285int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001286{
1287 operation_start("HTC Dumlock Reflash Recovery");
1288 if (simulate) {
1289 simulate_progress_bar();
1290 } else
1291 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001292
that3f7b1ac2014-12-30 11:30:13 +01001293 operation_end(0);
1294 return 0;
1295}
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
that3f7b1ac2014-12-30 11:30:13 +01001297int GUIAction::cmd(std::string arg)
1298{
1299 int op_status = 0;
1300
1301 operation_start("Command");
1302 LOGINFO("Running command: '%s'\n", arg.c_str());
1303 if (simulate) {
1304 simulate_progress_bar();
1305 } else {
1306 op_status = TWFunc::Exec_Cmd(arg);
1307 if (op_status != 0)
1308 op_status = 1;
1309 }
1310
1311 operation_end(op_status);
1312 return 0;
1313}
1314
1315int GUIAction::terminalcommand(std::string arg)
1316{
1317 int op_status = 0;
1318 string cmdpath, command;
1319
1320 DataManager::GetValue("tw_terminal_location", cmdpath);
1321 operation_start("CommandOutput");
1322 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1323 if (simulate) {
1324 simulate_progress_bar();
1325 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001326 } else if (arg == "exit") {
1327 LOGINFO("Exiting terminal\n");
1328 operation_end(op_status);
1329 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001330 } else {
1331 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1332 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001333 DataManager::SetValue("tw_terminal_state", 1);
1334 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001335 FILE* fp;
1336 char line[512];
1337
1338 fp = popen(command.c_str(), "r");
1339 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001340 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001341 } else {
1342 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1343 struct timeval timeout;
1344 fd_set fdset;
1345
1346 while(keep_going)
1347 {
1348 FD_ZERO(&fdset);
1349 FD_SET(fd, &fdset);
1350 timeout.tv_sec = 0;
1351 timeout.tv_usec = 400000;
1352 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1353 if (has_data == 0) {
1354 // Timeout reached
1355 DataManager::GetValue("tw_terminal_state", check);
1356 if (check == 0) {
1357 keep_going = 0;
1358 }
1359 } else if (has_data < 0) {
1360 // End of execution
1361 keep_going = 0;
1362 } else {
1363 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001364 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001365 gui_print("%s", line); // Display output
1366 else
1367 keep_going = 0; // Done executing
1368 }
1369 }
1370 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001371 }
thatc6085482015-01-09 22:12:43 +01001372 DataManager::SetValue("tw_operation_status", 0);
1373 DataManager::SetValue("tw_operation_state", 1);
1374 DataManager::SetValue("tw_terminal_state", 0);
1375 DataManager::SetValue("tw_background_thread_running", 0);
1376 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001377 }
1378 return 0;
1379}
1380
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001381int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001382{
1383 int op_status = 0;
1384
1385 LOGINFO("Sending kill command...\n");
1386 operation_start("KillCommand");
1387 DataManager::SetValue("tw_operation_status", 0);
1388 DataManager::SetValue("tw_operation_state", 1);
1389 DataManager::SetValue("tw_terminal_state", 0);
1390 DataManager::SetValue("tw_background_thread_running", 0);
1391 DataManager::SetValue(TW_ACTION_BUSY, 0);
1392 return 0;
1393}
1394
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001395int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001396{
1397 int op_status = 0;
1398 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001399 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001400 if (simulate) {
1401 simulate_progress_bar();
1402 } else {
1403 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001404 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001405 }
1406
1407 operation_end(op_status);
1408 return 0;
1409}
1410
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001411int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001412{
1413 int op_status = 0;
1414
1415 operation_start("CheckBackupName");
1416 if (simulate) {
1417 simulate_progress_bar();
1418 } else {
1419 op_status = PartitionManager.Check_Backup_Name(true);
1420 if (op_status != 0)
1421 op_status = 1;
1422 }
1423
1424 operation_end(op_status);
1425 return 0;
1426}
1427
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001428int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001429{
1430 int op_status = 0;
1431
1432 operation_start("Decrypt");
1433 if (simulate) {
1434 simulate_progress_bar();
1435 } else {
1436 string Password;
1437 DataManager::GetValue("tw_crypto_password", Password);
1438 op_status = PartitionManager.Decrypt_Device(Password);
1439 if (op_status != 0)
1440 op_status = 1;
1441 else {
that3f7b1ac2014-12-30 11:30:13 +01001442
1443 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1444
Ethan Yonkercf50da52015-01-12 21:59:07 -06001445 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001446
Ethan Yonkercf50da52015-01-12 21:59:07 -06001447 // Check for a custom theme and load it if exists
1448 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1449 if (has_datamedia != 0) {
1450 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001451 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001452 } else {
1453 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001454 }
1455 }
1456 }
1457 }
1458
1459 operation_end(op_status);
1460 return 0;
1461}
1462
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001463int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001464{
1465 operation_start("Sideload");
1466 if (simulate) {
1467 simulate_progress_bar();
1468 operation_end(0);
1469 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001471 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1472
1473 // wait for the adb connection
1474 int ret = apply_from_adb("/", &sideload_child_pid);
1475 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1476
1477 if (ret != 0) {
1478 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001480 ret = 1; // failure
1481 } else {
1482 int wipe_cache = 0;
1483 int wipe_dalvik = 0;
1484 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1485
1486 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1487 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1488 PartitionManager.Wipe_By_Path("/cache");
1489 if (wipe_dalvik)
1490 PartitionManager.Wipe_Dalvik_Cache();
1491 } else {
1492 ret = 1; // failure
1493 }
thatcc8ddca2015-01-03 01:59:36 +01001494 }
thatc6085482015-01-09 22:12:43 +01001495 if (sideload_child_pid) {
1496 LOGINFO("Signaling child sideload process to exit.\n");
1497 struct stat st;
1498 // Calling stat() on this magic filename signals the minadbd
1499 // subprocess to shut down.
1500 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1501 int status;
1502 LOGINFO("Waiting for child sideload process to exit.\n");
1503 waitpid(sideload_child_pid, &status, 0);
1504 }
Dees Troy28a85d22015-04-28 02:03:16 +00001505 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001506 TWFunc::Toggle_MTP(mtp_was_enabled);
1507 reinject_after_flash();
1508 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001509 }
that3f7b1ac2014-12-30 11:30:13 +01001510 return 0;
1511}
1512
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001513int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001514{
that3f7b1ac2014-12-30 11:30:13 +01001515 struct stat st;
1516 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001517 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001518 LOGINFO("Signaling child sideload process to exit.\n");
1519 // Calling stat() on this magic filename signals the minadbd
1520 // subprocess to shut down.
1521 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1522 if (!sideload_child_pid) {
1523 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001524 return 0;
1525 }
thatcc8ddca2015-01-03 01:59:36 +01001526 ::sleep(1);
1527 LOGINFO("Killing child sideload process.\n");
1528 kill(sideload_child_pid, SIGTERM);
1529 int status;
1530 LOGINFO("Waiting for child sideload process to exit.\n");
1531 waitpid(sideload_child_pid, &status, 0);
1532 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001533 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1534 return 0;
1535}
1536
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001537int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001538{
1539 operation_start("OpenRecoveryScript");
1540 if (simulate) {
1541 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001542 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001543 } else {
that10ae24f2015-12-26 20:53:51 +01001544 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001545 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001546 }
1547 return 0;
1548}
1549
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001550int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001551{
1552 int op_status = 0;
1553
1554 operation_start("Install SuperSU");
1555 if (simulate) {
1556 simulate_progress_bar();
1557 } else {
1558 if (!TWFunc::Install_SuperSU())
1559 op_status = 1;
1560 }
1561
1562 operation_end(op_status);
1563 return 0;
1564}
1565
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001566int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001567{
1568 int op_status = 0;
1569
1570 operation_start("Fixing Superuser Permissions");
1571 if (simulate) {
1572 simulate_progress_bar();
1573 } else {
1574 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1575 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1576 }
1577
1578 operation_end(op_status);
1579 return 0;
1580}
1581
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001582int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001583{
1584 int op_status = 0;
1585
1586 operation_start("Try Restore Decrypt");
1587 if (simulate) {
1588 simulate_progress_bar();
1589 } else {
1590 string Restore_Path, Filename, Password;
1591 DataManager::GetValue("tw_restore", Restore_Path);
1592 Restore_Path += "/";
1593 DataManager::GetValue("tw_restore_password", Password);
1594 TWFunc::SetPerformanceMode(true);
1595 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1596 op_status = 0; // success
1597 else
1598 op_status = 1; // fail
1599 TWFunc::SetPerformanceMode(false);
1600 }
1601
1602 operation_end(op_status);
1603 return 0;
1604}
1605
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001606int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001607{
1608 int op_status = 0;
1609
1610 operation_start("Repair Partition");
1611 if (simulate) {
1612 simulate_progress_bar();
1613 } else {
1614 string part_path;
1615 DataManager::GetValue("tw_partition_mount_point", part_path);
1616 if (PartitionManager.Repair_By_Path(part_path, true)) {
1617 op_status = 0; // success
1618 } else {
that3f7b1ac2014-12-30 11:30:13 +01001619 op_status = 1; // fail
1620 }
1621 }
1622
1623 operation_end(op_status);
1624 return 0;
1625}
1626
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001627int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001628{
1629 int op_status = 0;
1630
1631 operation_start("Resize Partition");
1632 if (simulate) {
1633 simulate_progress_bar();
1634 } else {
1635 string part_path;
1636 DataManager::GetValue("tw_partition_mount_point", part_path);
1637 if (PartitionManager.Resize_By_Path(part_path, true)) {
1638 op_status = 0; // success
1639 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001640 op_status = 1; // fail
1641 }
1642 }
1643
1644 operation_end(op_status);
1645 return 0;
1646}
1647
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001648int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001649{
1650 int op_status = 0;
1651
1652 operation_start("Change File System");
1653 if (simulate) {
1654 simulate_progress_bar();
1655 } else {
1656 string part_path, file_system;
1657 DataManager::GetValue("tw_partition_mount_point", part_path);
1658 DataManager::GetValue("tw_action_new_file_system", file_system);
1659 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1660 op_status = 0; // success
1661 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001662 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001663 op_status = 1; // fail
1664 }
1665 }
1666 PartitionManager.Update_System_Details();
1667 operation_end(op_status);
1668 return 0;
1669}
1670
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001671int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001672{
1673 int op_status = 0;
1674
1675 operation_start("Start MTP");
1676 if (PartitionManager.Enable_MTP())
1677 op_status = 0; // success
1678 else
1679 op_status = 1; // fail
1680
1681 operation_end(op_status);
1682 return 0;
1683}
1684
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001685int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001686{
1687 int op_status = 0;
1688
1689 operation_start("Stop MTP");
1690 if (PartitionManager.Disable_MTP())
1691 op_status = 0; // success
1692 else
1693 op_status = 1; // fail
1694
1695 operation_end(op_status);
1696 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001697}
1698
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001699int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001700{
1701 int op_status = 0;
1702
1703 operation_start("Flash Image");
1704 string path, filename, full_filename;
1705 DataManager::GetValue("tw_zip_location", path);
1706 DataManager::GetValue("tw_file", filename);
1707 full_filename = path + "/" + filename;
1708 if (PartitionManager.Flash_Image(full_filename))
1709 op_status = 0; // success
1710 else
1711 op_status = 1; // fail
1712
1713 operation_end(op_status);
1714 return 0;
1715}
1716
that10ae24f2015-12-26 20:53:51 +01001717int GUIAction::twcmd(std::string arg)
1718{
1719 operation_start("TWRP CLI Command");
1720 if (simulate)
1721 simulate_progress_bar();
1722 else
1723 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1724 operation_end(0);
1725 return 0;
1726}
1727
Dees_Troy51a0e822012-09-05 15:24:24 -04001728int GUIAction::getKeyByName(std::string key)
1729{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001730 if (key == "home") return KEY_HOME;
1731 else if (key == "menu") return KEY_MENU;
1732 else if (key == "back") return KEY_BACK;
1733 else if (key == "search") return KEY_SEARCH;
1734 else if (key == "voldown") return KEY_VOLUMEDOWN;
1735 else if (key == "volup") return KEY_VOLUMEUP;
1736 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001737 int ret_val;
1738 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1739 if (!ret_val)
1740 return KEY_POWER;
1741 else
1742 return ret_val;
1743 }
1744
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001745 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001746}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001747
1748int GUIAction::checkpartitionlifetimewrites(std::string arg)
1749{
1750 int op_status = 0;
1751 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1752
1753 operation_start("Check Partition Lifetime Writes");
1754 if (sys) {
1755 if (sys->Check_Lifetime_Writes() != 0)
1756 DataManager::SetValue("tw_lifetime_writes", 1);
1757 else
1758 DataManager::SetValue("tw_lifetime_writes", 0);
1759 op_status = 0; // success
1760 } else {
1761 DataManager::SetValue("tw_lifetime_writes", 1);
1762 op_status = 1; // fail
1763 }
1764
1765 operation_end(op_status);
1766 return 0;
1767}
1768
1769int GUIAction::mountsystemtoggle(std::string arg)
1770{
1771 int op_status = 0;
1772 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001773 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001774
1775 operation_start("Toggle System Mount");
1776 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1777 op_status = 1; // fail
1778 } else {
1779 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1780 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001781 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001782 DataManager::SetValue("tw_mount_system_ro", 0);
1783 Part->Change_Mount_Read_Only(false);
1784 } else {
1785 DataManager::SetValue("tw_mount_system_ro", 1);
1786 Part->Change_Mount_Read_Only(true);
1787 }
1788 if (remount_system) {
1789 Part->Mount(true);
1790 }
1791 op_status = 0; // success
1792 } else {
1793 op_status = 1; // fail
1794 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001795 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1796 if (Part) {
1797 if (arg == "0") {
1798 Part->Change_Mount_Read_Only(false);
1799 } else {
1800 Part->Change_Mount_Read_Only(true);
1801 }
1802 if (remount_vendor) {
1803 Part->Mount(true);
1804 }
1805 op_status = 0; // success
1806 } else {
1807 op_status = 1; // fail
1808 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001809 }
1810
1811 operation_end(op_status);
1812 return 0;
1813}
Ethan Yonker74db1572015-10-28 12:44:49 -05001814
1815int GUIAction::setlanguage(std::string arg __unused)
1816{
1817 int op_status = 0;
1818
1819 operation_start("Set Language");
1820 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1821 PageManager::RequestReload();
1822 op_status = 0; // success
1823
1824 operation_end(op_status);
1825 return 0;
1826}