blob: e13d15c680c1d6692c63b18514af8ba6929b362e [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 Yonker483e9f42016-01-11 22:21:18 -0600795int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100796{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600797 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100798 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500799
Ethan Yonker483e9f42016-01-11 22:21:18 -0600800 if (arg.empty())
801 arg = "tw_wipe_list";
802 DataManager::GetValue(arg, List);
803 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
804 if (!List.empty()) {
805 size_t start_pos = 0, end_pos = List.find(";", start_pos);
806 while (end_pos != string::npos && start_pos < List.size()) {
807 part_path = List.substr(start_pos, end_pos - start_pos);
808 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
809 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100810 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 } else {
that3f7b1ac2014-12-30 11:30:13 +0100812 count++;
813 }
814 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600815 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100816 }
817 DataManager::SetValue("tw_check_partition_list", count);
818 } else {
819 DataManager::SetValue("tw_check_partition_list", 0);
820 }
821 return 0;
822}
Dees_Troy51a0e822012-09-05 15:24:24 -0400823
Ethan Yonker483e9f42016-01-11 22:21:18 -0600824int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100825{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600826 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100827 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400828
Ethan Yonker483e9f42016-01-11 22:21:18 -0600829 if (arg.empty())
830 arg = "tw_wipe_list";
831 DataManager::GetValue(arg, List);
832 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
833 if (!List.empty()) {
834 size_t start_pos = 0, end_pos = List.find(";", start_pos);
835 part_path = List;
836 while (end_pos != string::npos && start_pos < List.size()) {
837 part_path = List.substr(start_pos, end_pos - start_pos);
838 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
839 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100840 // Do nothing
841 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600842 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100843 break;
844 }
845 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600846 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100847 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600848 if (!part_path.empty()) {
849 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100850 if (Part) {
851 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500852
that3f7b1ac2014-12-30 11:30:13 +0100853 DataManager::SetValue("tw_partition_name", Part->Display_Name);
854 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
855 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
856 DataManager::SetValue("tw_partition_size", Part->Size / mb);
857 DataManager::SetValue("tw_partition_used", Part->Used / mb);
858 DataManager::SetValue("tw_partition_free", Part->Free / mb);
859 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
860 DataManager::SetValue("tw_partition_removable", Part->Removable);
861 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
862
863 if (Part->Can_Repair())
864 DataManager::SetValue("tw_partition_can_repair", 1);
865 else
866 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500867 if (Part->Can_Resize())
868 DataManager::SetValue("tw_partition_can_resize", 1);
869 else
870 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600871 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100872 DataManager::SetValue("tw_partition_vfat", 1);
873 else
874 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600875 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100876 DataManager::SetValue("tw_partition_exfat", 1);
877 else
878 DataManager::SetValue("tw_partition_exfat", 0);
879 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
880 DataManager::SetValue("tw_partition_f2fs", 1);
881 else
882 DataManager::SetValue("tw_partition_f2fs", 0);
883 if (TWFunc::Path_Exists("/sbin/mke2fs"))
884 DataManager::SetValue("tw_partition_ext", 1);
885 else
886 DataManager::SetValue("tw_partition_ext", 0);
887 return 0;
888 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100890 }
891 }
892 }
893 DataManager::SetValue("tw_partition_name", "");
894 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600895 // Set this to 0 to prevent trying to partition this device, just in case
896 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100897 return 0;
898}
899
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500900int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100901{
902 time_t tm;
903 char path[256];
904 int path_len;
905 uid_t uid = -1;
906 gid_t gid = -1;
907
908 struct passwd *pwd = getpwnam("media_rw");
909 if(pwd) {
910 uid = pwd->pw_uid;
911 gid = pwd->pw_gid;
912 }
913
914 const std::string storage = DataManager::GetCurrentStoragePath();
915 if(PartitionManager.Is_Mounted_By_Path(storage)) {
916 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
917 } else {
918 strcpy(path, "/tmp/");
919 }
920
921 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
922 return 0;
923
924 tm = time(NULL);
925 path_len = strlen(path);
926
927 // Screenshot_2014-01-01-18-21-38.png
928 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
929
930 int res = gr_save_screenshot(path);
931 if(res == 0) {
932 chmod(path, 0666);
933 chown(path, uid, gid);
934
that677b13f2015-12-26 23:57:31 +0100935 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100936
937 // blink to notify that the screenshow was taken
938 gr_color(255, 255, 255, 255);
939 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
940 gr_flip();
941 gui_forceRender();
942 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500943 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100944 }
945 return 0;
946}
947
948int GUIAction::setbrightness(std::string arg)
949{
950 return TWFunc::Set_Brightness(arg);
951}
952
953int GUIAction::fileexists(std::string arg)
954{
955 struct stat st;
956 string newpath = arg + "/.";
957
958 operation_start("FileExists");
959 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
960 operation_end(0);
961 else
962 operation_end(1);
963 return 0;
964}
965
thatcc8ddca2015-01-03 01:59:36 +0100966void GUIAction::reinject_after_flash()
967{
968 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500969 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100970 if (simulate) {
971 simulate_progress_bar();
972 } else {
973 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
974 if (Boot == NULL || Boot->Current_File_System != "emmc")
975 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
976 else {
977 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
978 TWFunc::Exec_Cmd(injectcmd);
979 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500980 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100981 }
982 }
983}
984
that3f7b1ac2014-12-30 11:30:13 +0100985int GUIAction::flash(std::string arg)
986{
987 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600988 // We're going to jump to this page first, like a loading page
989 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100990 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200991 string zip_path = zip_queue[i];
992 size_t slashpos = zip_path.find_last_of('/');
993 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100994 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200995 DataManager::SetValue("tw_filename", zip_path);
996 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100997 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
998
999 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001000 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001001 TWFunc::SetPerformanceMode(false);
1002 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001003 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001004 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001005 break;
that3f7b1ac2014-12-30 11:30:13 +01001006 }
1007 }
1008 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001009
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001010 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001011 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001012 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001013 }
that3f7b1ac2014-12-30 11:30:13 +01001014
thatcc8ddca2015-01-03 01:59:36 +01001015 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001016 PartitionManager.Update_System_Details();
1017 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001018 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001019 return 0;
1020}
1021
1022int GUIAction::wipe(std::string arg)
1023{
1024 operation_start("Format");
1025 DataManager::SetValue("tw_partition", arg);
1026
1027 int ret_val = false;
1028
1029 if (simulate) {
1030 simulate_progress_bar();
1031 } else {
1032 if (arg == "data")
1033 ret_val = PartitionManager.Factory_Reset();
1034 else if (arg == "battery")
1035 ret_val = PartitionManager.Wipe_Battery_Stats();
1036 else if (arg == "rotate")
1037 ret_val = PartitionManager.Wipe_Rotate_Data();
1038 else if (arg == "dalvik")
1039 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1040 else if (arg == "DATAMEDIA") {
1041 ret_val = PartitionManager.Format_Data();
1042 } else if (arg == "INTERNAL") {
1043 int has_datamedia, dual_storage;
1044
1045 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1046 if (has_datamedia) {
1047 ret_val = PartitionManager.Wipe_Media_From_Data();
1048 } else {
1049 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1050 }
1051 } else if (arg == "EXTERNAL") {
1052 string External_Path;
1053
1054 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1055 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1056 } else if (arg == "ANDROIDSECURE") {
1057 ret_val = PartitionManager.Wipe_Android_Secure();
1058 } else if (arg == "LIST") {
1059 string Wipe_List, wipe_path;
1060 bool skip = false;
1061 ret_val = true;
1062 TWPartition* wipe_part = NULL;
1063
1064 DataManager::GetValue("tw_wipe_list", Wipe_List);
1065 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1066 if (!Wipe_List.empty()) {
1067 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1068 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1069 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1070 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1071 if (wipe_path == "/and-sec") {
1072 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001073 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001074 ret_val = false;
1075 break;
1076 } else {
1077 skip = true;
1078 }
1079 } else if (wipe_path == "DALVIK") {
1080 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001081 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001082 ret_val = false;
1083 break;
1084 } else {
1085 skip = true;
1086 }
1087 } else if (wipe_path == "INTERNAL") {
1088 if (!PartitionManager.Wipe_Media_From_Data()) {
1089 ret_val = false;
1090 break;
1091 } else {
1092 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001093 }
1094 }
that3f7b1ac2014-12-30 11:30:13 +01001095 if (!skip) {
1096 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001097 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001098 ret_val = false;
1099 break;
1100 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1101 arg = wipe_path;
1102 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001103 } else {
that3f7b1ac2014-12-30 11:30:13 +01001104 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001105 }
that3f7b1ac2014-12-30 11:30:13 +01001106 start_pos = end_pos + 1;
1107 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001108 }
1109 }
that3f7b1ac2014-12-30 11:30:13 +01001110 } else
1111 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001112#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001113 if (arg == DataManager::GetSettingsStoragePath()) {
1114 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1115 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001116
that3f7b1ac2014-12-30 11:30:13 +01001117 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1118 LOGINFO("Making TWRP folder and saving settings.\n");
1119 Storage_Path += "/TWRP";
1120 mkdir(Storage_Path.c_str(), 0777);
1121 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001122 } else {
that3f7b1ac2014-12-30 11:30:13 +01001123 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1124 }
1125 }
1126#endif
1127 }
1128 PartitionManager.Update_System_Details();
1129 if (ret_val)
1130 ret_val = 0; // 0 is success
1131 else
1132 ret_val = 1; // 1 is failure
1133 operation_end(ret_val);
1134 return 0;
1135}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001136
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001137int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001138{
1139 operation_start("Refreshing Sizes");
1140 if (simulate) {
1141 simulate_progress_bar();
1142 } else
1143 PartitionManager.Update_System_Details();
1144 operation_end(0);
1145 return 0;
1146}
1147
1148int GUIAction::nandroid(std::string arg)
1149{
that3f7b1ac2014-12-30 11:30:13 +01001150 if (simulate) {
that73a52952015-01-28 23:07:34 +01001151 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001152 DataManager::SetValue("tw_partition", "Simulation");
1153 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001154 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001155 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001156 operation_start("Nandroid");
1157 int ret = 0;
1158
that3f7b1ac2014-12-30 11:30:13 +01001159 if (arg == "backup") {
1160 string Backup_Name;
1161 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001162 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1163 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001164 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1165 ret = PartitionManager.Run_Backup();
1166 }
1167 else {
1168 operation_end(1);
1169 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001170 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001171 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001172 } else if (arg == "restore") {
1173 string Restore_Name;
1174 DataManager::GetValue("tw_restore", Restore_Name);
1175 ret = PartitionManager.Run_Restore(Restore_Name);
1176 } else {
1177 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001178 return -1;
1179 }
1180 DataManager::SetValue("tw_encrypt_backup", 0);
1181 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001182 if (ret == false)
1183 ret = 1; // 1 for failure
1184 else
1185 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001186 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 }
1188 else {
1189 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001190 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 ret = 0;
1192 }
that73a52952015-01-28 23:07:34 +01001193 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001194 return ret;
1195 }
1196 return 0;
1197}
1198
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001199int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001200 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001202 }
1203 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001204 int op_status = PartitionManager.Cancel_Backup();
1205 if (op_status != 0)
1206 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001207 }
1208
1209 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001210}
Dees_Troy51a0e822012-09-05 15:24:24 -04001211
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001212int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001213{
that73a52952015-01-28 23:07:34 +01001214 int op_status = 0;
1215
that3f7b1ac2014-12-30 11:30:13 +01001216 operation_start("Fix Permissions");
1217 LOGINFO("fix permissions started!\n");
1218 if (simulate) {
1219 simulate_progress_bar();
1220 } else {
that73a52952015-01-28 23:07:34 +01001221 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001222 if (op_status != 0)
1223 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001224 }
that73a52952015-01-28 23:07:34 +01001225 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001226 return 0;
1227}
1228
1229int GUIAction::dd(std::string arg)
1230{
1231 operation_start("imaging");
1232
1233 if (simulate) {
1234 simulate_progress_bar();
1235 } else {
1236 string cmd = "dd " + arg;
1237 TWFunc::Exec_Cmd(cmd);
1238 }
1239 operation_end(0);
1240 return 0;
1241}
1242
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001243int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001244{
1245 operation_start("Partition SD Card");
1246 int ret_val = 0;
1247
1248 if (simulate) {
1249 simulate_progress_bar();
1250 } else {
1251 int allow_partition;
1252 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1253 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001254 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001255 } else {
1256 if (!PartitionManager.Partition_SDCard())
1257 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001258 }
that3f7b1ac2014-12-30 11:30:13 +01001259 }
1260 operation_end(ret_val);
1261 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262
that3f7b1ac2014-12-30 11:30:13 +01001263}
Dees_Troy51a0e822012-09-05 15:24:24 -04001264
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001265int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001266{
1267 operation_start("Install HTC Dumlock");
1268 if (simulate) {
1269 simulate_progress_bar();
1270 } else
1271 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001272
that3f7b1ac2014-12-30 11:30:13 +01001273 operation_end(0);
1274 return 0;
1275}
Dees_Troy51a0e822012-09-05 15:24:24 -04001276
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001277int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001278{
1279 operation_start("HTC Dumlock Restore Boot");
1280 if (simulate) {
1281 simulate_progress_bar();
1282 } else
1283 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001284
that3f7b1ac2014-12-30 11:30:13 +01001285 operation_end(0);
1286 return 0;
1287}
Dees_Troy51a0e822012-09-05 15:24:24 -04001288
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001289int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001290{
1291 operation_start("HTC Dumlock Reflash Recovery");
1292 if (simulate) {
1293 simulate_progress_bar();
1294 } else
1295 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
that3f7b1ac2014-12-30 11:30:13 +01001297 operation_end(0);
1298 return 0;
1299}
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
that3f7b1ac2014-12-30 11:30:13 +01001301int GUIAction::cmd(std::string arg)
1302{
1303 int op_status = 0;
1304
1305 operation_start("Command");
1306 LOGINFO("Running command: '%s'\n", arg.c_str());
1307 if (simulate) {
1308 simulate_progress_bar();
1309 } else {
1310 op_status = TWFunc::Exec_Cmd(arg);
1311 if (op_status != 0)
1312 op_status = 1;
1313 }
1314
1315 operation_end(op_status);
1316 return 0;
1317}
1318
1319int GUIAction::terminalcommand(std::string arg)
1320{
1321 int op_status = 0;
1322 string cmdpath, command;
1323
1324 DataManager::GetValue("tw_terminal_location", cmdpath);
1325 operation_start("CommandOutput");
1326 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1327 if (simulate) {
1328 simulate_progress_bar();
1329 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001330 } else if (arg == "exit") {
1331 LOGINFO("Exiting terminal\n");
1332 operation_end(op_status);
1333 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001334 } else {
1335 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1336 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001337 DataManager::SetValue("tw_terminal_state", 1);
1338 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001339 FILE* fp;
1340 char line[512];
1341
1342 fp = popen(command.c_str(), "r");
1343 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001345 } else {
1346 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1347 struct timeval timeout;
1348 fd_set fdset;
1349
1350 while(keep_going)
1351 {
1352 FD_ZERO(&fdset);
1353 FD_SET(fd, &fdset);
1354 timeout.tv_sec = 0;
1355 timeout.tv_usec = 400000;
1356 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1357 if (has_data == 0) {
1358 // Timeout reached
1359 DataManager::GetValue("tw_terminal_state", check);
1360 if (check == 0) {
1361 keep_going = 0;
1362 }
1363 } else if (has_data < 0) {
1364 // End of execution
1365 keep_going = 0;
1366 } else {
1367 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001368 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001369 gui_print("%s", line); // Display output
1370 else
1371 keep_going = 0; // Done executing
1372 }
1373 }
1374 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001375 }
thatc6085482015-01-09 22:12:43 +01001376 DataManager::SetValue("tw_operation_status", 0);
1377 DataManager::SetValue("tw_operation_state", 1);
1378 DataManager::SetValue("tw_terminal_state", 0);
1379 DataManager::SetValue("tw_background_thread_running", 0);
1380 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001381 }
1382 return 0;
1383}
1384
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001385int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001386{
1387 int op_status = 0;
1388
1389 LOGINFO("Sending kill command...\n");
1390 operation_start("KillCommand");
1391 DataManager::SetValue("tw_operation_status", 0);
1392 DataManager::SetValue("tw_operation_state", 1);
1393 DataManager::SetValue("tw_terminal_state", 0);
1394 DataManager::SetValue("tw_background_thread_running", 0);
1395 DataManager::SetValue(TW_ACTION_BUSY, 0);
1396 return 0;
1397}
1398
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001399int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001400{
1401 int op_status = 0;
1402 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001403 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001404 if (simulate) {
1405 simulate_progress_bar();
1406 } else {
1407 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001408 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001409 }
1410
1411 operation_end(op_status);
1412 return 0;
1413}
1414
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001415int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001416{
1417 int op_status = 0;
1418
1419 operation_start("CheckBackupName");
1420 if (simulate) {
1421 simulate_progress_bar();
1422 } else {
1423 op_status = PartitionManager.Check_Backup_Name(true);
1424 if (op_status != 0)
1425 op_status = 1;
1426 }
1427
1428 operation_end(op_status);
1429 return 0;
1430}
1431
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001432int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001433{
1434 int op_status = 0;
1435
1436 operation_start("Decrypt");
1437 if (simulate) {
1438 simulate_progress_bar();
1439 } else {
1440 string Password;
1441 DataManager::GetValue("tw_crypto_password", Password);
1442 op_status = PartitionManager.Decrypt_Device(Password);
1443 if (op_status != 0)
1444 op_status = 1;
1445 else {
that3f7b1ac2014-12-30 11:30:13 +01001446
1447 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1448
Ethan Yonkercf50da52015-01-12 21:59:07 -06001449 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001450
Ethan Yonkercf50da52015-01-12 21:59:07 -06001451 // Check for a custom theme and load it if exists
1452 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1453 if (has_datamedia != 0) {
1454 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001455 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001456 } else {
1457 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001458 }
1459 }
1460 }
1461 }
1462
1463 operation_end(op_status);
1464 return 0;
1465}
1466
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001467int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001468{
1469 operation_start("Sideload");
1470 if (simulate) {
1471 simulate_progress_bar();
1472 operation_end(0);
1473 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001474 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001475 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1476
1477 // wait for the adb connection
1478 int ret = apply_from_adb("/", &sideload_child_pid);
1479 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1480
1481 if (ret != 0) {
1482 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001484 ret = 1; // failure
1485 } else {
1486 int wipe_cache = 0;
1487 int wipe_dalvik = 0;
1488 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1489
1490 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1491 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1492 PartitionManager.Wipe_By_Path("/cache");
1493 if (wipe_dalvik)
1494 PartitionManager.Wipe_Dalvik_Cache();
1495 } else {
1496 ret = 1; // failure
1497 }
thatcc8ddca2015-01-03 01:59:36 +01001498 }
thatc6085482015-01-09 22:12:43 +01001499 if (sideload_child_pid) {
1500 LOGINFO("Signaling child sideload process to exit.\n");
1501 struct stat st;
1502 // Calling stat() on this magic filename signals the minadbd
1503 // subprocess to shut down.
1504 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1505 int status;
1506 LOGINFO("Waiting for child sideload process to exit.\n");
1507 waitpid(sideload_child_pid, &status, 0);
1508 }
Dees Troy28a85d22015-04-28 02:03:16 +00001509 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001510 TWFunc::Toggle_MTP(mtp_was_enabled);
1511 reinject_after_flash();
1512 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001513 }
that3f7b1ac2014-12-30 11:30:13 +01001514 return 0;
1515}
1516
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001517int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001518{
that3f7b1ac2014-12-30 11:30:13 +01001519 struct stat st;
1520 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001521 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001522 LOGINFO("Signaling child sideload process to exit.\n");
1523 // Calling stat() on this magic filename signals the minadbd
1524 // subprocess to shut down.
1525 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1526 if (!sideload_child_pid) {
1527 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001528 return 0;
1529 }
thatcc8ddca2015-01-03 01:59:36 +01001530 ::sleep(1);
1531 LOGINFO("Killing child sideload process.\n");
1532 kill(sideload_child_pid, SIGTERM);
1533 int status;
1534 LOGINFO("Waiting for child sideload process to exit.\n");
1535 waitpid(sideload_child_pid, &status, 0);
1536 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001537 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1538 return 0;
1539}
1540
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001541int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001542{
1543 operation_start("OpenRecoveryScript");
1544 if (simulate) {
1545 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001546 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001547 } else {
that10ae24f2015-12-26 20:53:51 +01001548 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001549 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001550 }
1551 return 0;
1552}
1553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001554int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001555{
1556 int op_status = 0;
1557
1558 operation_start("Install SuperSU");
1559 if (simulate) {
1560 simulate_progress_bar();
1561 } else {
1562 if (!TWFunc::Install_SuperSU())
1563 op_status = 1;
1564 }
1565
1566 operation_end(op_status);
1567 return 0;
1568}
1569
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001570int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001571{
1572 int op_status = 0;
1573
1574 operation_start("Fixing Superuser Permissions");
1575 if (simulate) {
1576 simulate_progress_bar();
1577 } else {
1578 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1579 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1580 }
1581
1582 operation_end(op_status);
1583 return 0;
1584}
1585
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001586int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001587{
1588 int op_status = 0;
1589
1590 operation_start("Try Restore Decrypt");
1591 if (simulate) {
1592 simulate_progress_bar();
1593 } else {
1594 string Restore_Path, Filename, Password;
1595 DataManager::GetValue("tw_restore", Restore_Path);
1596 Restore_Path += "/";
1597 DataManager::GetValue("tw_restore_password", Password);
1598 TWFunc::SetPerformanceMode(true);
1599 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1600 op_status = 0; // success
1601 else
1602 op_status = 1; // fail
1603 TWFunc::SetPerformanceMode(false);
1604 }
1605
1606 operation_end(op_status);
1607 return 0;
1608}
1609
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001610int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001611{
1612 int op_status = 0;
1613
1614 operation_start("Repair Partition");
1615 if (simulate) {
1616 simulate_progress_bar();
1617 } else {
1618 string part_path;
1619 DataManager::GetValue("tw_partition_mount_point", part_path);
1620 if (PartitionManager.Repair_By_Path(part_path, true)) {
1621 op_status = 0; // success
1622 } else {
that3f7b1ac2014-12-30 11:30:13 +01001623 op_status = 1; // fail
1624 }
1625 }
1626
1627 operation_end(op_status);
1628 return 0;
1629}
1630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001631int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001632{
1633 int op_status = 0;
1634
1635 operation_start("Resize Partition");
1636 if (simulate) {
1637 simulate_progress_bar();
1638 } else {
1639 string part_path;
1640 DataManager::GetValue("tw_partition_mount_point", part_path);
1641 if (PartitionManager.Resize_By_Path(part_path, true)) {
1642 op_status = 0; // success
1643 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001644 op_status = 1; // fail
1645 }
1646 }
1647
1648 operation_end(op_status);
1649 return 0;
1650}
1651
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001652int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001653{
1654 int op_status = 0;
1655
1656 operation_start("Change File System");
1657 if (simulate) {
1658 simulate_progress_bar();
1659 } else {
1660 string part_path, file_system;
1661 DataManager::GetValue("tw_partition_mount_point", part_path);
1662 DataManager::GetValue("tw_action_new_file_system", file_system);
1663 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1664 op_status = 0; // success
1665 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001666 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001667 op_status = 1; // fail
1668 }
1669 }
1670 PartitionManager.Update_System_Details();
1671 operation_end(op_status);
1672 return 0;
1673}
1674
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001675int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001676{
1677 int op_status = 0;
1678
1679 operation_start("Start MTP");
1680 if (PartitionManager.Enable_MTP())
1681 op_status = 0; // success
1682 else
1683 op_status = 1; // fail
1684
1685 operation_end(op_status);
1686 return 0;
1687}
1688
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001689int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001690{
1691 int op_status = 0;
1692
1693 operation_start("Stop MTP");
1694 if (PartitionManager.Disable_MTP())
1695 op_status = 0; // success
1696 else
1697 op_status = 1; // fail
1698
1699 operation_end(op_status);
1700 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001701}
1702
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001703int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001704{
1705 int op_status = 0;
1706
1707 operation_start("Flash Image");
1708 string path, filename, full_filename;
1709 DataManager::GetValue("tw_zip_location", path);
1710 DataManager::GetValue("tw_file", filename);
1711 full_filename = path + "/" + filename;
1712 if (PartitionManager.Flash_Image(full_filename))
1713 op_status = 0; // success
1714 else
1715 op_status = 1; // fail
1716
1717 operation_end(op_status);
1718 return 0;
1719}
1720
that10ae24f2015-12-26 20:53:51 +01001721int GUIAction::twcmd(std::string arg)
1722{
1723 operation_start("TWRP CLI Command");
1724 if (simulate)
1725 simulate_progress_bar();
1726 else
1727 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1728 operation_end(0);
1729 return 0;
1730}
1731
Dees_Troy51a0e822012-09-05 15:24:24 -04001732int GUIAction::getKeyByName(std::string key)
1733{
that8834a0f2016-01-05 23:29:30 +01001734 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001735 else if (key == "menu") return KEY_MENU;
1736 else if (key == "back") return KEY_BACK;
1737 else if (key == "search") return KEY_SEARCH;
1738 else if (key == "voldown") return KEY_VOLUMEDOWN;
1739 else if (key == "volup") return KEY_VOLUMEUP;
1740 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001741 int ret_val;
1742 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1743 if (!ret_val)
1744 return KEY_POWER;
1745 else
1746 return ret_val;
1747 }
1748
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001749 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001750}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001751
1752int GUIAction::checkpartitionlifetimewrites(std::string arg)
1753{
1754 int op_status = 0;
1755 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1756
1757 operation_start("Check Partition Lifetime Writes");
1758 if (sys) {
1759 if (sys->Check_Lifetime_Writes() != 0)
1760 DataManager::SetValue("tw_lifetime_writes", 1);
1761 else
1762 DataManager::SetValue("tw_lifetime_writes", 0);
1763 op_status = 0; // success
1764 } else {
1765 DataManager::SetValue("tw_lifetime_writes", 1);
1766 op_status = 1; // fail
1767 }
1768
1769 operation_end(op_status);
1770 return 0;
1771}
1772
1773int GUIAction::mountsystemtoggle(std::string arg)
1774{
1775 int op_status = 0;
1776 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001777 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001778
1779 operation_start("Toggle System Mount");
1780 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1781 op_status = 1; // fail
1782 } else {
1783 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1784 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001785 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001786 DataManager::SetValue("tw_mount_system_ro", 0);
1787 Part->Change_Mount_Read_Only(false);
1788 } else {
1789 DataManager::SetValue("tw_mount_system_ro", 1);
1790 Part->Change_Mount_Read_Only(true);
1791 }
1792 if (remount_system) {
1793 Part->Mount(true);
1794 }
1795 op_status = 0; // success
1796 } else {
1797 op_status = 1; // fail
1798 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001799 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1800 if (Part) {
1801 if (arg == "0") {
1802 Part->Change_Mount_Read_Only(false);
1803 } else {
1804 Part->Change_Mount_Read_Only(true);
1805 }
1806 if (remount_vendor) {
1807 Part->Mount(true);
1808 }
1809 op_status = 0; // success
1810 } else {
1811 op_status = 1; // fail
1812 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001813 }
1814
1815 operation_end(op_status);
1816 return 0;
1817}
Ethan Yonker74db1572015-10-28 12:44:49 -05001818
1819int GUIAction::setlanguage(std::string arg __unused)
1820{
1821 int op_status = 0;
1822
1823 operation_start("Set Language");
1824 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1825 PageManager::RequestReload();
1826 op_status = 0; // success
1827
1828 operation_end(op_status);
1829 return 0;
1830}