blob: fc3973738d37766e7def9787e25978e6a8f5e195 [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"
Dees_Troy43d8b002012-09-17 16:00:01 -040052#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060053#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060054#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040055};
56
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Dees_Troy51a0e822012-09-05 15:24:24 -040061void curtainClose(void);
62
that3f7b1ac2014-12-30 11:30:13 +010063GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010064std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010065static string zip_queue[10];
66static int zip_queue_index;
67static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010068pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010069
that73a52952015-01-28 23:07:34 +010070static void *ActionThread_work_wrapper(void *data);
71
72class ActionThread
73{
74public:
75 ActionThread();
76 ~ActionThread();
77
78 void threadActions(GUIAction *act);
79 void run(void *data);
80private:
81 friend void *ActionThread_work_wrapper(void*);
82 struct ThreadData
83 {
84 ActionThread *this_;
85 GUIAction *act;
86 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
87 };
88
89 pthread_t m_thread;
90 bool m_thread_running;
91 pthread_mutex_t m_act_lock;
92};
93
94static ActionThread action_thread; // for all kinds of longer running actions
95static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010096
97static void *ActionThread_work_wrapper(void *data)
98{
that73a52952015-01-28 23:07:34 +010099 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +0100100 return NULL;
101}
102
103ActionThread::ActionThread()
104{
105 m_thread_running = false;
106 pthread_mutex_init(&m_act_lock, NULL);
107}
108
109ActionThread::~ActionThread()
110{
111 pthread_mutex_lock(&m_act_lock);
112 if(m_thread_running) {
113 pthread_mutex_unlock(&m_act_lock);
114 pthread_join(m_thread, NULL);
115 } else {
116 pthread_mutex_unlock(&m_act_lock);
117 }
118 pthread_mutex_destroy(&m_act_lock);
119}
120
that7d3b54f2015-01-09 22:52:51 +0100121void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100122{
123 pthread_mutex_lock(&m_act_lock);
124 if (m_thread_running) {
125 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100126 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
127 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100128 } else {
129 m_thread_running = true;
130 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100131 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100132 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
133 }
134}
135
136void ActionThread::run(void *data)
137{
138 ThreadData *d = (ThreadData*)data;
139 GUIAction* act = d->act;
140
141 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100142 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100143 act->doAction(*it);
144
145 pthread_mutex_lock(&m_act_lock);
146 m_thread_running = false;
147 pthread_mutex_unlock(&m_act_lock);
148 delete d;
149}
150
Dees_Troy51a0e822012-09-05 15:24:24 -0400151GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100152 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400153{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 xml_node<>* child;
155 xml_node<>* actions;
156 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
that3f7b1ac2014-12-30 11:30:13 +0100160 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100161#define ADD_ACTION(n) mf[#n] = &GUIAction::n
162#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100163 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100164 ADD_ACTION(reboot);
165 ADD_ACTION(home);
166 ADD_ACTION(key);
167 ADD_ACTION(page);
168 ADD_ACTION(reload);
169 ADD_ACTION(readBackup);
170 ADD_ACTION(set);
171 ADD_ACTION(clear);
172 ADD_ACTION(mount);
173 ADD_ACTION(unmount);
174 ADD_ACTION_EX("umount", unmount);
175 ADD_ACTION(restoredefaultsettings);
176 ADD_ACTION(copylog);
177 ADD_ACTION(compute);
178 ADD_ACTION_EX("addsubtract", compute);
179 ADD_ACTION(setguitimezone);
180 ADD_ACTION(overlay);
181 ADD_ACTION(queuezip);
182 ADD_ACTION(cancelzip);
183 ADD_ACTION(queueclear);
184 ADD_ACTION(sleep);
185 ADD_ACTION(appenddatetobackupname);
186 ADD_ACTION(generatebackupname);
187 ADD_ACTION(checkpartitionlist);
188 ADD_ACTION(getpartitiondetails);
189 ADD_ACTION(screenshot);
190 ADD_ACTION(setbrightness);
191 ADD_ACTION(fileexists);
192 ADD_ACTION(killterminal);
193 ADD_ACTION(checkbackupname);
194 ADD_ACTION(adbsideloadcancel);
195 ADD_ACTION(fixsu);
196 ADD_ACTION(startmtp);
197 ADD_ACTION(stopmtp);
198 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500199 ADD_ACTION(checkpartitionlifetimewrites);
200 ADD_ACTION(mountsystemtoggle);
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);
that3f7b1ac2014-12-30 11:30:13 +0100229 }
230
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600232 actions = FindNode(node, "actions");
233 if (actions) child = FindNode(actions, "action");
234 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 while (child)
239 {
240 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 attr = child->first_attribute("function");
243 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 action.mFunction = attr->value();
246 action.mArg = child->value();
247 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 child = child->next_sibling("action");
250 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600253 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 if (child)
255 {
256 attr = child->first_attribute("key");
257 if (attr)
258 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100259 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
260 for(size_t i = 0; i < keys.size(); ++i)
261 {
262 const int key = getKeyByName(keys[i]);
263 mKeys[key] = false;
264 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 }
266 else
267 {
268 attr = child->first_attribute("x");
269 if (!attr) return;
270 mActionX = atol(attr->value());
271 attr = child->first_attribute("y");
272 if (!attr) return;
273 mActionY = atol(attr->value());
274 attr = child->first_attribute("w");
275 if (!attr) return;
276 mActionW = atol(attr->value());
277 attr = child->first_attribute("h");
278 if (!attr) return;
279 mActionH = atol(attr->value());
280 }
281 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400282}
283
284int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
285{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 if (state == TOUCH_RELEASE)
287 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}
291
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100292int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400293{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100294 if (mKeys.empty())
295 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100297 std::map<int, bool>::iterator itr = mKeys.find(key);
298 if(itr == mKeys.end())
299 return 0;
300
301 bool prevState = itr->second;
302 itr->second = down;
303
304 // If there is only one key for this action, wait for key up so it
305 // doesn't trigger with multi-key actions.
306 // Else, check if all buttons are pressed, then consume their release events
307 // so they don't trigger one-button actions and reset mKeys pressed status
308 if(mKeys.size() == 1) {
309 if(!down && prevState)
310 doActions();
311 } else if(down) {
312 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
313 if(!itr->second)
314 return 0;
315 }
316
317 // Passed, all req buttons are pressed, reset them and consume release events
318 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
319 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
320 kb->ConsumeKeyRelease(itr->first);
321 itr->second = false;
322 }
323
324 doActions();
325 }
326
Dees_Troy51a0e822012-09-05 15:24:24 -0400327 return 0;
328}
329
Vojtech Bocek07220562014-02-08 02:05:33 +0100330int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400331{
Vojtech Bocek07220562014-02-08 02:05:33 +0100332 GUIObject::NotifyVarChange(varName, value);
333
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100334 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100336 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400338
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342void GUIAction::simulate_progress_bar(void)
343{
Dees_Troy2673cec2013-04-02 20:22:16 +0000344 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 for (int i = 0; i < 5; i++)
346 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500347 if (PartitionManager.stop_backup.get_value()) {
348 DataManager::SetValue("tw_cancel_backup", 1);
349 gui_print("Backup Canceled.\n");
350 DataManager::SetValue("ui_progress", 0);
351 PartitionManager.stop_backup.set_value(0);
352 return;
353 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 usleep(500000);
355 DataManager::SetValue("ui_progress", i * 20);
356 }
357}
358
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600359int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400360{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200361 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362
363 DataManager::SetValue("ui_progress", 0);
364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 if (filename.empty())
366 {
367 LOGERR("No file specified.\n");
368 return -1;
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400372 return -1;
373
Dees_Troy51a0e822012-09-05 15:24:24 -0400374 if (simulate) {
375 simulate_progress_bar();
376 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400377 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
379 // Now, check if we need to ensure TWRP remains installed...
380 struct stat st;
381 if (stat("/sbin/installTwrp", &st) == 0)
382 {
383 DataManager::SetValue("tw_operation", "Configuring TWRP");
384 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000385 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200386 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400387 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400389 }
390 }
391 }
392
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 // Done
394 DataManager::SetValue("ui_progress", 100);
395 DataManager::SetValue("ui_progress", 0);
396 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397}
398
that73a52952015-01-28 23:07:34 +0100399GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100400{
that73a52952015-01-28 23:07:34 +0100401 string func = gui_parse_text(action.mFunction);
402 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
403 if (needsThread) {
404 if (func == "cancelbackup")
405 return THREAD_CANCEL;
406 else
407 return THREAD_ACTION;
408 }
409 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100410}
411
Dees_Troy51a0e822012-09-05 15:24:24 -0400412int GUIAction::doActions()
413{
that3f7b1ac2014-12-30 11:30:13 +0100414 if (mActions.size() < 1)
415 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416
that73a52952015-01-28 23:07:34 +0100417 // Determine in which thread to run the actions.
418 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
419 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100420 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100421 for (it = mActions.begin(); it != mActions.end(); ++it) {
422 ThreadType tt = getThreadType(*it);
423 if (tt == THREAD_NONE)
424 continue;
425 if (threadType == THREAD_NONE)
426 threadType = tt;
427 else if (threadType != tt) {
428 LOGERR("Can't mix normal and cancel actions in the same list.\n"
429 "Running the whole batch in the cancel thread.\n");
430 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100431 break;
432 }
that7d3b54f2015-01-09 22:52:51 +0100433 }
that73a52952015-01-28 23:07:34 +0100434
435 // Now run the actions in the desired thread.
436 switch (threadType) {
437 case THREAD_ACTION:
438 action_thread.threadActions(this);
439 break;
440
441 case THREAD_CANCEL:
442 cancel_thread.threadActions(this);
443 break;
444
445 default: {
446 // no iterators here because theme reloading might kill our object
447 const size_t cnt = mActions.size();
448 for (size_t i = 0; i < cnt; ++i)
449 doAction(mActions[i]);
450 }
thatc6085482015-01-09 22:12:43 +0100451 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400454}
455
that3f7b1ac2014-12-30 11:30:13 +0100456int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400457{
that3f7b1ac2014-12-30 11:30:13 +0100458 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
that3f7b1ac2014-12-30 11:30:13 +0100460 std::string function = gui_parse_text(action.mFunction);
461 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462
that3f7b1ac2014-12-30 11:30:13 +0100463 // find function and execute it
464 mapFunc::const_iterator funcitr = mf.find(function);
465 if (funcitr != mf.end())
466 return (this->*funcitr->second)(arg);
467
468 LOGERR("Unknown action '%s'\n", function.c_str());
469 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400470}
471
472void GUIAction::operation_start(const string operation_name)
473{
that3f7b1ac2014-12-30 11:30:13 +0100474 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000475 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 DataManager::SetValue(TW_ACTION_BUSY, 1);
477 DataManager::SetValue("ui_progress", 0);
478 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600480 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481}
482
that3f7b1ac2014-12-30 11:30:13 +0100483void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400484{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000485 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 DataManager::SetValue("ui_progress", 100);
488 if (simulate) {
489 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
490 if (simulate_fail != 0)
491 DataManager::SetValue("tw_operation_status", 1);
492 else
493 DataManager::SetValue("tw_operation_status", 0);
494 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500497 }
498 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 }
502 DataManager::SetValue("tw_operation_state", 1);
503 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500504 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000505 time(&Stop);
506 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600507 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100508 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509}
510
that3f7b1ac2014-12-30 11:30:13 +0100511int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400512{
that3f7b1ac2014-12-30 11:30:13 +0100513 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400514
that3f7b1ac2014-12-30 11:30:13 +0100515 sync();
516 DataManager::SetValue("tw_gui_done", 1);
517 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400518
that3f7b1ac2014-12-30 11:30:13 +0100519 return 0;
520}
Dees_Troy51a0e822012-09-05 15:24:24 -0400521
that3f7b1ac2014-12-30 11:30:13 +0100522int GUIAction::home(std::string arg)
523{
524 PageManager::SelectPackage("TWRP");
525 gui_changePage("main");
526 return 0;
527}
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
that3f7b1ac2014-12-30 11:30:13 +0100529int GUIAction::key(std::string arg)
530{
531 const int key = getKeyByName(arg);
532 PageManager::NotifyKey(key, true);
533 PageManager::NotifyKey(key, false);
534 return 0;
535}
536
537int GUIAction::page(std::string arg)
538{
539 std::string page_name = gui_parse_text(arg);
540 return gui_changePage(page_name);
541}
542
543int GUIAction::reload(std::string arg)
544{
545 int check = 0, ret_val = 0;
546 std::string theme_path;
547
that3f7b1ac2014-12-30 11:30:13 +0100548 theme_path = DataManager::GetSettingsStoragePath();
549 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
550 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
551 check = 1;
552 }
553
554 theme_path += "/TWRP/theme/ui.zip";
555 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600556 {
that3f7b1ac2014-12-30 11:30:13 +0100557 // Loading the custom theme failed - try loading the stock theme
558 LOGINFO("Attempting to reload stock theme...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000559 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 {
that3f7b1ac2014-12-30 11:30:13 +0100561 LOGERR("Failed to load base packages.\n");
562 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 }
that3f7b1ac2014-12-30 11:30:13 +0100565 return 0;
566}
Dees_Troy51a0e822012-09-05 15:24:24 -0400567
that3f7b1ac2014-12-30 11:30:13 +0100568int GUIAction::readBackup(std::string arg)
569{
570 string Restore_Name;
571 DataManager::GetValue("tw_restore", Restore_Name);
572 PartitionManager.Set_Restore_Files(Restore_Name);
573 return 0;
574}
575
576int GUIAction::set(std::string arg)
577{
578 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200579 {
that3f7b1ac2014-12-30 11:30:13 +0100580 string varName = arg.substr(0, arg.find('='));
581 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
that3f7b1ac2014-12-30 11:30:13 +0100583 DataManager::GetValue(value, value);
584 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200585 }
that3f7b1ac2014-12-30 11:30:13 +0100586 else
587 DataManager::SetValue(arg, "1");
588 return 0;
589}
Dees_Troy51a0e822012-09-05 15:24:24 -0400590
that3f7b1ac2014-12-30 11:30:13 +0100591int GUIAction::clear(std::string arg)
592{
593 DataManager::SetValue(arg, "0");
594 return 0;
595}
Dees_Troy51a0e822012-09-05 15:24:24 -0400596
that3f7b1ac2014-12-30 11:30:13 +0100597int GUIAction::mount(std::string arg)
598{
599 if (arg == "usb") {
600 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100602 PartitionManager.usb_storage_enable();
603 else
604 gui_print("Simulating actions...\n");
605 } else if (!simulate) {
606 PartitionManager.Mount_By_Path(arg, true);
607 PartitionManager.Add_MTP_Storage(arg);
608 } else
609 gui_print("Simulating actions...\n");
610 return 0;
611}
612
613int GUIAction::unmount(std::string arg)
614{
615 if (arg == "usb") {
616 if (!simulate)
617 PartitionManager.usb_storage_disable();
618 else
619 gui_print("Simulating actions...\n");
620 DataManager::SetValue(TW_ACTION_BUSY, 0);
621 } else if (!simulate) {
622 PartitionManager.UnMount_By_Path(arg, true);
623 } else
624 gui_print("Simulating actions...\n");
625 return 0;
626}
627
628int GUIAction::restoredefaultsettings(std::string arg)
629{
630 operation_start("Restore Defaults");
631 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
632 gui_print("Simulating actions...\n");
633 else {
634 DataManager::ResetDefaults();
635 PartitionManager.Update_System_Details();
636 PartitionManager.Mount_Current_Storage(true);
637 }
638 operation_end(0);
639 return 0;
640}
641
642int GUIAction::copylog(std::string arg)
643{
644 operation_start("Copy Log");
645 if (!simulate)
646 {
647 string dst;
648 PartitionManager.Mount_Current_Storage(true);
649 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
650 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
651 tw_set_default_metadata(dst.c_str());
652 sync();
653 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
654 } else
655 simulate_progress_bar();
656 operation_end(0);
657 return 0;
658}
659
660
661int GUIAction::compute(std::string arg)
662{
663 if (arg.find("+") != string::npos)
664 {
665 string varName = arg.substr(0, arg.find('+'));
666 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
667 int amount_to_add = atoi(string_to_add.c_str());
668 int value;
669
670 DataManager::GetValue(varName, value);
671 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400672 return 0;
673 }
that3f7b1ac2014-12-30 11:30:13 +0100674 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 {
that3f7b1ac2014-12-30 11:30:13 +0100676 string varName = arg.substr(0, arg.find('-'));
677 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
678 int amount_to_subtract = atoi(string_to_subtract.c_str());
679 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400680
that3f7b1ac2014-12-30 11:30:13 +0100681 DataManager::GetValue(varName, value);
682 value -= amount_to_subtract;
683 if (value <= 0)
684 value = 0;
685 DataManager::SetValue(varName, value);
686 return 0;
687 }
688 if (arg.find("*") != string::npos)
689 {
690 string varName = arg.substr(0, arg.find('*'));
691 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
692 int multiply_by = atoi(multiply_by_str.c_str());
693 int value;
694
695 DataManager::GetValue(varName, value);
696 DataManager::SetValue(varName, value*multiply_by);
697 return 0;
698 }
699 if (arg.find("/") != string::npos)
700 {
701 string varName = arg.substr(0, arg.find('/'));
702 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
703 int divide_by = atoi(divide_by_str.c_str());
704 int value;
705
706 if(divide_by != 0)
707 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100709 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 }
711 return 0;
712 }
that3f7b1ac2014-12-30 11:30:13 +0100713 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
714 return -1;
715}
Dees_Troy51a0e822012-09-05 15:24:24 -0400716
that3f7b1ac2014-12-30 11:30:13 +0100717int GUIAction::setguitimezone(std::string arg)
718{
719 string SelectedZone;
720 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
721 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
722 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
723
724 int dst;
725 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
726
727 string offset;
728 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
729
730 string NewTimeZone = Zone;
731 if (offset != "0")
732 NewTimeZone += ":" + offset;
733
734 if (dst != 0)
735 NewTimeZone += DSTZone;
736
737 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
738 DataManager::update_tz_environment_variables();
739 return 0;
740}
741
742int GUIAction::overlay(std::string arg)
743{
744 return gui_changeOverlay(arg);
745}
746
747int GUIAction::queuezip(std::string arg)
748{
749 if (zip_queue_index >= 10) {
750 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 return 0;
752 }
that3f7b1ac2014-12-30 11:30:13 +0100753 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
754 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
755 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400756 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100757 }
758 return 0;
759}
760
761int GUIAction::cancelzip(std::string arg)
762{
763 if (zip_queue_index <= 0) {
764 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100766 } else {
767 zip_queue_index--;
768 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 }
that3f7b1ac2014-12-30 11:30:13 +0100770 return 0;
771}
Dees_Troy51a0e822012-09-05 15:24:24 -0400772
that3f7b1ac2014-12-30 11:30:13 +0100773int GUIAction::queueclear(std::string arg)
774{
775 zip_queue_index = 0;
776 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
777 return 0;
778}
Dees_Troy51a0e822012-09-05 15:24:24 -0400779
that3f7b1ac2014-12-30 11:30:13 +0100780int GUIAction::sleep(std::string arg)
781{
782 operation_start("Sleep");
783 usleep(atoi(arg.c_str()));
784 operation_end(0);
785 return 0;
786}
Dees Troyb21cc642013-09-10 17:36:41 +0000787
that3f7b1ac2014-12-30 11:30:13 +0100788int GUIAction::appenddatetobackupname(std::string arg)
789{
790 operation_start("AppendDateToBackupName");
791 string Backup_Name;
792 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
793 Backup_Name += TWFunc::Get_Current_Date();
794 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
795 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
796 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
797 operation_end(0);
798 return 0;
799}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500800
that3f7b1ac2014-12-30 11:30:13 +0100801int GUIAction::generatebackupname(std::string arg)
802{
803 operation_start("GenerateBackupName");
804 TWFunc::Auto_Generate_Backup_Name();
805 operation_end(0);
806 return 0;
807}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500808
that3f7b1ac2014-12-30 11:30:13 +0100809int GUIAction::checkpartitionlist(std::string arg)
810{
811 string Wipe_List, wipe_path;
812 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500813
that3f7b1ac2014-12-30 11:30:13 +0100814 DataManager::GetValue("tw_wipe_list", Wipe_List);
815 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
816 if (!Wipe_List.empty()) {
817 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
818 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
819 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
820 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
821 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
822 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 } else {
that3f7b1ac2014-12-30 11:30:13 +0100824 count++;
825 }
826 start_pos = end_pos + 1;
827 end_pos = Wipe_List.find(";", start_pos);
828 }
829 DataManager::SetValue("tw_check_partition_list", count);
830 } else {
831 DataManager::SetValue("tw_check_partition_list", 0);
832 }
833 return 0;
834}
Dees_Troy51a0e822012-09-05 15:24:24 -0400835
that3f7b1ac2014-12-30 11:30:13 +0100836int GUIAction::getpartitiondetails(std::string arg)
837{
838 string Wipe_List, wipe_path;
839 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400840
that3f7b1ac2014-12-30 11:30:13 +0100841 DataManager::GetValue("tw_wipe_list", Wipe_List);
842 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
843 if (!Wipe_List.empty()) {
844 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
845 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
846 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
847 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
848 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
849 // Do nothing
850 } else {
851 DataManager::SetValue("tw_partition_path", wipe_path);
852 break;
853 }
854 start_pos = end_pos + 1;
855 end_pos = Wipe_List.find(";", start_pos);
856 }
857 if (!wipe_path.empty()) {
858 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
859 if (Part) {
860 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500861
that3f7b1ac2014-12-30 11:30:13 +0100862 DataManager::SetValue("tw_partition_name", Part->Display_Name);
863 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
864 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
865 DataManager::SetValue("tw_partition_size", Part->Size / mb);
866 DataManager::SetValue("tw_partition_used", Part->Used / mb);
867 DataManager::SetValue("tw_partition_free", Part->Free / mb);
868 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
869 DataManager::SetValue("tw_partition_removable", Part->Removable);
870 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
871
872 if (Part->Can_Repair())
873 DataManager::SetValue("tw_partition_can_repair", 1);
874 else
875 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500876 if (Part->Can_Resize())
877 DataManager::SetValue("tw_partition_can_resize", 1);
878 else
879 DataManager::SetValue("tw_partition_can_resize", 0);
that3f7b1ac2014-12-30 11:30:13 +0100880 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
881 DataManager::SetValue("tw_partition_vfat", 1);
882 else
883 DataManager::SetValue("tw_partition_vfat", 0);
884 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
885 DataManager::SetValue("tw_partition_exfat", 1);
886 else
887 DataManager::SetValue("tw_partition_exfat", 0);
888 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
889 DataManager::SetValue("tw_partition_f2fs", 1);
890 else
891 DataManager::SetValue("tw_partition_f2fs", 0);
892 if (TWFunc::Path_Exists("/sbin/mke2fs"))
893 DataManager::SetValue("tw_partition_ext", 1);
894 else
895 DataManager::SetValue("tw_partition_ext", 0);
896 return 0;
897 } else {
898 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
899 }
900 }
901 }
902 DataManager::SetValue("tw_partition_name", "");
903 DataManager::SetValue("tw_partition_file_system", "");
904 return 0;
905}
906
907int GUIAction::screenshot(std::string arg)
908{
909 time_t tm;
910 char path[256];
911 int path_len;
912 uid_t uid = -1;
913 gid_t gid = -1;
914
915 struct passwd *pwd = getpwnam("media_rw");
916 if(pwd) {
917 uid = pwd->pw_uid;
918 gid = pwd->pw_gid;
919 }
920
921 const std::string storage = DataManager::GetCurrentStoragePath();
922 if(PartitionManager.Is_Mounted_By_Path(storage)) {
923 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
924 } else {
925 strcpy(path, "/tmp/");
926 }
927
928 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
929 return 0;
930
931 tm = time(NULL);
932 path_len = strlen(path);
933
934 // Screenshot_2014-01-01-18-21-38.png
935 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
936
937 int res = gr_save_screenshot(path);
938 if(res == 0) {
939 chmod(path, 0666);
940 chown(path, uid, gid);
941
942 gui_print("Screenshot was saved to %s\n", path);
943
944 // blink to notify that the screenshow was taken
945 gr_color(255, 255, 255, 255);
946 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
947 gr_flip();
948 gui_forceRender();
949 } else {
950 LOGERR("Failed to take a screenshot!\n");
951 }
952 return 0;
953}
954
955int GUIAction::setbrightness(std::string arg)
956{
957 return TWFunc::Set_Brightness(arg);
958}
959
960int GUIAction::fileexists(std::string arg)
961{
962 struct stat st;
963 string newpath = arg + "/.";
964
965 operation_start("FileExists");
966 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
967 operation_end(0);
968 else
969 operation_end(1);
970 return 0;
971}
972
thatcc8ddca2015-01-03 01:59:36 +0100973void GUIAction::reinject_after_flash()
974{
975 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
thatcc8ddca2015-01-03 01:59:36 +0100976 gui_print("Injecting TWRP into boot image...\n");
977 if (simulate) {
978 simulate_progress_bar();
979 } else {
980 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
981 if (Boot == NULL || Boot->Current_File_System != "emmc")
982 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
983 else {
984 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
985 TWFunc::Exec_Cmd(injectcmd);
986 }
987 gui_print("TWRP injection complete.\n");
988 }
989 }
990}
991
that3f7b1ac2014-12-30 11:30:13 +0100992int GUIAction::flash(std::string arg)
993{
994 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600995 // We're going to jump to this page first, like a loading page
996 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100997 for (i=0; i<zip_queue_index; i++) {
998 operation_start("Flashing");
999 DataManager::SetValue("tw_filename", zip_queue[i]);
1000 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1001
1002 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001003 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001004 TWFunc::SetPerformanceMode(false);
1005 if (ret_val != 0) {
1006 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +01001007 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001008 break;
that3f7b1ac2014-12-30 11:30:13 +01001009 }
1010 }
1011 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001012
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001013 if (wipe_cache) {
1014 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001015 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001016 }
that3f7b1ac2014-12-30 11:30:13 +01001017
thatcc8ddca2015-01-03 01:59:36 +01001018 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001019 PartitionManager.Update_System_Details();
1020 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001021 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001022 return 0;
1023}
1024
1025int GUIAction::wipe(std::string arg)
1026{
1027 operation_start("Format");
1028 DataManager::SetValue("tw_partition", arg);
1029
1030 int ret_val = false;
1031
1032 if (simulate) {
1033 simulate_progress_bar();
1034 } else {
1035 if (arg == "data")
1036 ret_val = PartitionManager.Factory_Reset();
1037 else if (arg == "battery")
1038 ret_val = PartitionManager.Wipe_Battery_Stats();
1039 else if (arg == "rotate")
1040 ret_val = PartitionManager.Wipe_Rotate_Data();
1041 else if (arg == "dalvik")
1042 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1043 else if (arg == "DATAMEDIA") {
1044 ret_val = PartitionManager.Format_Data();
1045 } else if (arg == "INTERNAL") {
1046 int has_datamedia, dual_storage;
1047
1048 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1049 if (has_datamedia) {
1050 ret_val = PartitionManager.Wipe_Media_From_Data();
1051 } else {
1052 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1053 }
1054 } else if (arg == "EXTERNAL") {
1055 string External_Path;
1056
1057 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1058 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1059 } else if (arg == "ANDROIDSECURE") {
1060 ret_val = PartitionManager.Wipe_Android_Secure();
1061 } else if (arg == "LIST") {
1062 string Wipe_List, wipe_path;
1063 bool skip = false;
1064 ret_val = true;
1065 TWPartition* wipe_part = NULL;
1066
1067 DataManager::GetValue("tw_wipe_list", Wipe_List);
1068 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1069 if (!Wipe_List.empty()) {
1070 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1071 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1072 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1073 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1074 if (wipe_path == "/and-sec") {
1075 if (!PartitionManager.Wipe_Android_Secure()) {
1076 LOGERR("Unable to wipe android secure\n");
1077 ret_val = false;
1078 break;
1079 } else {
1080 skip = true;
1081 }
1082 } else if (wipe_path == "DALVIK") {
1083 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1084 LOGERR("Failed to wipe dalvik\n");
1085 ret_val = false;
1086 break;
1087 } else {
1088 skip = true;
1089 }
1090 } else if (wipe_path == "INTERNAL") {
1091 if (!PartitionManager.Wipe_Media_From_Data()) {
1092 ret_val = false;
1093 break;
1094 } else {
1095 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001096 }
1097 }
that3f7b1ac2014-12-30 11:30:13 +01001098 if (!skip) {
1099 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1100 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1101 ret_val = false;
1102 break;
1103 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1104 arg = wipe_path;
1105 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001106 } else {
that3f7b1ac2014-12-30 11:30:13 +01001107 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 }
that3f7b1ac2014-12-30 11:30:13 +01001109 start_pos = end_pos + 1;
1110 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001111 }
1112 }
that3f7b1ac2014-12-30 11:30:13 +01001113 } else
1114 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001115#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001116 if (arg == DataManager::GetSettingsStoragePath()) {
1117 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1118 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001119
that3f7b1ac2014-12-30 11:30:13 +01001120 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1121 LOGINFO("Making TWRP folder and saving settings.\n");
1122 Storage_Path += "/TWRP";
1123 mkdir(Storage_Path.c_str(), 0777);
1124 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 } else {
that3f7b1ac2014-12-30 11:30:13 +01001126 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1127 }
1128 }
1129#endif
1130 }
1131 PartitionManager.Update_System_Details();
1132 if (ret_val)
1133 ret_val = 0; // 0 is success
1134 else
1135 ret_val = 1; // 1 is failure
1136 operation_end(ret_val);
1137 return 0;
1138}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001139
that3f7b1ac2014-12-30 11:30:13 +01001140int GUIAction::refreshsizes(std::string arg)
1141{
1142 operation_start("Refreshing Sizes");
1143 if (simulate) {
1144 simulate_progress_bar();
1145 } else
1146 PartitionManager.Update_System_Details();
1147 operation_end(0);
1148 return 0;
1149}
1150
1151int GUIAction::nandroid(std::string arg)
1152{
that3f7b1ac2014-12-30 11:30:13 +01001153 if (simulate) {
that73a52952015-01-28 23:07:34 +01001154 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001155 DataManager::SetValue("tw_partition", "Simulation");
1156 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001157 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001158 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001159 operation_start("Nandroid");
1160 int ret = 0;
1161
that3f7b1ac2014-12-30 11:30:13 +01001162 if (arg == "backup") {
1163 string Backup_Name;
1164 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1165 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1166 ret = PartitionManager.Run_Backup();
1167 }
1168 else {
1169 operation_end(1);
1170 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001171 }
1172 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1173 } else if (arg == "restore") {
1174 string Restore_Name;
1175 DataManager::GetValue("tw_restore", Restore_Name);
1176 ret = PartitionManager.Run_Restore(Restore_Name);
1177 } else {
1178 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001179 return -1;
1180 }
1181 DataManager::SetValue("tw_encrypt_backup", 0);
1182 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001183 if (ret == false)
1184 ret = 1; // 1 for failure
1185 else
1186 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001188 }
1189 else {
1190 DataManager::SetValue("tw_cancel_backup", 1);
1191 gui_print("Backup Canceled.\n");
1192 ret = 0;
1193 }
that73a52952015-01-28 23:07:34 +01001194 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 return ret;
1196 }
1197 return 0;
1198}
1199
1200int GUIAction::cancelbackup(std::string arg) {
1201 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001202 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001203 }
1204 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001205 int op_status = PartitionManager.Cancel_Backup();
1206 if (op_status != 0)
1207 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001208 }
1209
1210 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001211}
Dees_Troy51a0e822012-09-05 15:24:24 -04001212
that3f7b1ac2014-12-30 11:30:13 +01001213int GUIAction::fixpermissions(std::string arg)
1214{
that73a52952015-01-28 23:07:34 +01001215 int op_status = 0;
1216
that3f7b1ac2014-12-30 11:30:13 +01001217 operation_start("Fix Permissions");
1218 LOGINFO("fix permissions started!\n");
1219 if (simulate) {
1220 simulate_progress_bar();
1221 } else {
that73a52952015-01-28 23:07:34 +01001222 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001223 if (op_status != 0)
1224 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001225 }
that73a52952015-01-28 23:07:34 +01001226 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001227 return 0;
1228}
1229
1230int GUIAction::dd(std::string arg)
1231{
1232 operation_start("imaging");
1233
1234 if (simulate) {
1235 simulate_progress_bar();
1236 } else {
1237 string cmd = "dd " + arg;
1238 TWFunc::Exec_Cmd(cmd);
1239 }
1240 operation_end(0);
1241 return 0;
1242}
1243
1244int GUIAction::partitionsd(std::string arg)
1245{
1246 operation_start("Partition SD Card");
1247 int ret_val = 0;
1248
1249 if (simulate) {
1250 simulate_progress_bar();
1251 } else {
1252 int allow_partition;
1253 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1254 if (allow_partition == 0) {
1255 gui_print("This device does not have a real SD Card!\nAborting!\n");
1256 } else {
1257 if (!PartitionManager.Partition_SDCard())
1258 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001259 }
that3f7b1ac2014-12-30 11:30:13 +01001260 }
1261 operation_end(ret_val);
1262 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001263
that3f7b1ac2014-12-30 11:30:13 +01001264}
Dees_Troy51a0e822012-09-05 15:24:24 -04001265
that3f7b1ac2014-12-30 11:30:13 +01001266int GUIAction::installhtcdumlock(std::string arg)
1267{
1268 operation_start("Install HTC Dumlock");
1269 if (simulate) {
1270 simulate_progress_bar();
1271 } else
1272 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001273
that3f7b1ac2014-12-30 11:30:13 +01001274 operation_end(0);
1275 return 0;
1276}
Dees_Troy51a0e822012-09-05 15:24:24 -04001277
that3f7b1ac2014-12-30 11:30:13 +01001278int GUIAction::htcdumlockrestoreboot(std::string arg)
1279{
1280 operation_start("HTC Dumlock Restore Boot");
1281 if (simulate) {
1282 simulate_progress_bar();
1283 } else
1284 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001285
that3f7b1ac2014-12-30 11:30:13 +01001286 operation_end(0);
1287 return 0;
1288}
Dees_Troy51a0e822012-09-05 15:24:24 -04001289
that3f7b1ac2014-12-30 11:30:13 +01001290int GUIAction::htcdumlockreflashrecovery(std::string arg)
1291{
1292 operation_start("HTC Dumlock Reflash Recovery");
1293 if (simulate) {
1294 simulate_progress_bar();
1295 } else
1296 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001297
that3f7b1ac2014-12-30 11:30:13 +01001298 operation_end(0);
1299 return 0;
1300}
Dees_Troy51a0e822012-09-05 15:24:24 -04001301
that3f7b1ac2014-12-30 11:30:13 +01001302int GUIAction::cmd(std::string arg)
1303{
1304 int op_status = 0;
1305
1306 operation_start("Command");
1307 LOGINFO("Running command: '%s'\n", arg.c_str());
1308 if (simulate) {
1309 simulate_progress_bar();
1310 } else {
1311 op_status = TWFunc::Exec_Cmd(arg);
1312 if (op_status != 0)
1313 op_status = 1;
1314 }
1315
1316 operation_end(op_status);
1317 return 0;
1318}
1319
1320int GUIAction::terminalcommand(std::string arg)
1321{
1322 int op_status = 0;
1323 string cmdpath, command;
1324
1325 DataManager::GetValue("tw_terminal_location", cmdpath);
1326 operation_start("CommandOutput");
1327 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1328 if (simulate) {
1329 simulate_progress_bar();
1330 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001331 } else if (arg == "exit") {
1332 LOGINFO("Exiting terminal\n");
1333 operation_end(op_status);
1334 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001335 } else {
1336 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1337 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001338 DataManager::SetValue("tw_terminal_state", 1);
1339 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001340 FILE* fp;
1341 char line[512];
1342
1343 fp = popen(command.c_str(), "r");
1344 if (fp == NULL) {
1345 LOGERR("Error opening command to run.\n");
1346 } else {
1347 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1348 struct timeval timeout;
1349 fd_set fdset;
1350
1351 while(keep_going)
1352 {
1353 FD_ZERO(&fdset);
1354 FD_SET(fd, &fdset);
1355 timeout.tv_sec = 0;
1356 timeout.tv_usec = 400000;
1357 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1358 if (has_data == 0) {
1359 // Timeout reached
1360 DataManager::GetValue("tw_terminal_state", check);
1361 if (check == 0) {
1362 keep_going = 0;
1363 }
1364 } else if (has_data < 0) {
1365 // End of execution
1366 keep_going = 0;
1367 } else {
1368 // Try to read output
1369 memset(line, 0, sizeof(line));
xiaolue738da52015-02-22 20:49:35 +08001370 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001371 gui_print("%s", line); // Display output
1372 else
1373 keep_going = 0; // Done executing
1374 }
1375 }
1376 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001377 }
thatc6085482015-01-09 22:12:43 +01001378 DataManager::SetValue("tw_operation_status", 0);
1379 DataManager::SetValue("tw_operation_state", 1);
1380 DataManager::SetValue("tw_terminal_state", 0);
1381 DataManager::SetValue("tw_background_thread_running", 0);
1382 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001383 }
1384 return 0;
1385}
1386
1387int GUIAction::killterminal(std::string arg)
1388{
1389 int op_status = 0;
1390
1391 LOGINFO("Sending kill command...\n");
1392 operation_start("KillCommand");
1393 DataManager::SetValue("tw_operation_status", 0);
1394 DataManager::SetValue("tw_operation_state", 1);
1395 DataManager::SetValue("tw_terminal_state", 0);
1396 DataManager::SetValue("tw_background_thread_running", 0);
1397 DataManager::SetValue(TW_ACTION_BUSY, 0);
1398 return 0;
1399}
1400
1401int GUIAction::reinjecttwrp(std::string arg)
1402{
1403 int op_status = 0;
1404 operation_start("ReinjectTWRP");
1405 gui_print("Injecting TWRP into boot image...\n");
1406 if (simulate) {
1407 simulate_progress_bar();
1408 } else {
1409 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1410 gui_print("TWRP injection complete.\n");
1411 }
1412
1413 operation_end(op_status);
1414 return 0;
1415}
1416
1417int GUIAction::checkbackupname(std::string arg)
1418{
1419 int op_status = 0;
1420
1421 operation_start("CheckBackupName");
1422 if (simulate) {
1423 simulate_progress_bar();
1424 } else {
1425 op_status = PartitionManager.Check_Backup_Name(true);
1426 if (op_status != 0)
1427 op_status = 1;
1428 }
1429
1430 operation_end(op_status);
1431 return 0;
1432}
1433
1434int GUIAction::decrypt(std::string arg)
1435{
1436 int op_status = 0;
1437
1438 operation_start("Decrypt");
1439 if (simulate) {
1440 simulate_progress_bar();
1441 } else {
1442 string Password;
1443 DataManager::GetValue("tw_crypto_password", Password);
1444 op_status = PartitionManager.Decrypt_Device(Password);
1445 if (op_status != 0)
1446 op_status = 1;
1447 else {
that3f7b1ac2014-12-30 11:30:13 +01001448
1449 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1450
Ethan Yonkercf50da52015-01-12 21:59:07 -06001451 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001452
Ethan Yonkercf50da52015-01-12 21:59:07 -06001453 // Check for a custom theme and load it if exists
1454 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1455 if (has_datamedia != 0) {
1456 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001457 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001458 } else {
1459 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001460 }
1461 }
1462 }
1463 }
1464
1465 operation_end(op_status);
1466 return 0;
1467}
1468
thatcc8ddca2015-01-03 01:59:36 +01001469int GUIAction::adbsideload(std::string arg)
1470{
1471 operation_start("Sideload");
1472 if (simulate) {
1473 simulate_progress_bar();
1474 operation_end(0);
1475 } else {
thatc6085482015-01-09 22:12:43 +01001476 gui_print("Starting ADB sideload feature...\n");
1477 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1478
1479 // wait for the adb connection
1480 int ret = apply_from_adb("/", &sideload_child_pid);
1481 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1482
1483 if (ret != 0) {
1484 if (ret == -2)
1485 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1486 ret = 1; // failure
1487 } else {
1488 int wipe_cache = 0;
1489 int wipe_dalvik = 0;
1490 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1491
1492 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1493 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1494 PartitionManager.Wipe_By_Path("/cache");
1495 if (wipe_dalvik)
1496 PartitionManager.Wipe_Dalvik_Cache();
1497 } else {
1498 ret = 1; // failure
1499 }
thatcc8ddca2015-01-03 01:59:36 +01001500 }
thatc6085482015-01-09 22:12:43 +01001501 if (sideload_child_pid) {
1502 LOGINFO("Signaling child sideload process to exit.\n");
1503 struct stat st;
1504 // Calling stat() on this magic filename signals the minadbd
1505 // subprocess to shut down.
1506 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1507 int status;
1508 LOGINFO("Waiting for child sideload process to exit.\n");
1509 waitpid(sideload_child_pid, &status, 0);
1510 }
1511
1512 TWFunc::Toggle_MTP(mtp_was_enabled);
1513 reinject_after_flash();
1514 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001515 }
that3f7b1ac2014-12-30 11:30:13 +01001516 return 0;
1517}
1518
1519int GUIAction::adbsideloadcancel(std::string arg)
1520{
that3f7b1ac2014-12-30 11:30:13 +01001521 struct stat st;
1522 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1523 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001524 LOGINFO("Signaling child sideload process to exit.\n");
1525 // Calling stat() on this magic filename signals the minadbd
1526 // subprocess to shut down.
1527 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1528 if (!sideload_child_pid) {
1529 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001530 return 0;
1531 }
thatcc8ddca2015-01-03 01:59:36 +01001532 ::sleep(1);
1533 LOGINFO("Killing child sideload process.\n");
1534 kill(sideload_child_pid, SIGTERM);
1535 int status;
1536 LOGINFO("Waiting for child sideload process to exit.\n");
1537 waitpid(sideload_child_pid, &status, 0);
1538 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001539 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1540 return 0;
1541}
1542
1543int GUIAction::openrecoveryscript(std::string arg)
1544{
1545 operation_start("OpenRecoveryScript");
1546 if (simulate) {
1547 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001548 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001549 } else {
thatc6085482015-01-09 22:12:43 +01001550 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1551 // that we converted to ORS commands during boot in recovery.cpp.
1552 // Run those first.
1553 int reboot = 0;
1554 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1555 gui_print("Processing AOSP recovery commands...\n");
1556 if (OpenRecoveryScript::run_script_file() == 0) {
1557 reboot = 1;
1558 }
that3f7b1ac2014-12-30 11:30:13 +01001559 }
thatc6085482015-01-09 22:12:43 +01001560 // Check for the ORS file in /cache and attempt to run those commands.
1561 if (OpenRecoveryScript::check_for_script_file()) {
1562 gui_print("Processing OpenRecoveryScript file...\n");
1563 if (OpenRecoveryScript::run_script_file() == 0) {
1564 reboot = 1;
1565 }
1566 }
1567 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001568 // Disable stock recovery reflashing
1569 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001570 usleep(2000000); // Sleep for 2 seconds before rebooting
1571 TWFunc::tw_reboot(rb_system);
1572 } else {
1573 DataManager::SetValue("tw_page_done", 1);
1574 }
1575 operation_end(1);
1576 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001577 }
1578 return 0;
1579}
1580
1581int GUIAction::installsu(std::string arg)
1582{
1583 int op_status = 0;
1584
1585 operation_start("Install SuperSU");
1586 if (simulate) {
1587 simulate_progress_bar();
1588 } else {
1589 if (!TWFunc::Install_SuperSU())
1590 op_status = 1;
1591 }
1592
1593 operation_end(op_status);
1594 return 0;
1595}
1596
1597int GUIAction::fixsu(std::string arg)
1598{
1599 int op_status = 0;
1600
1601 operation_start("Fixing Superuser Permissions");
1602 if (simulate) {
1603 simulate_progress_bar();
1604 } else {
1605 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1606 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1607 }
1608
1609 operation_end(op_status);
1610 return 0;
1611}
1612
1613int GUIAction::decrypt_backup(std::string arg)
1614{
1615 int op_status = 0;
1616
1617 operation_start("Try Restore Decrypt");
1618 if (simulate) {
1619 simulate_progress_bar();
1620 } else {
1621 string Restore_Path, Filename, Password;
1622 DataManager::GetValue("tw_restore", Restore_Path);
1623 Restore_Path += "/";
1624 DataManager::GetValue("tw_restore_password", Password);
1625 TWFunc::SetPerformanceMode(true);
1626 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1627 op_status = 0; // success
1628 else
1629 op_status = 1; // fail
1630 TWFunc::SetPerformanceMode(false);
1631 }
1632
1633 operation_end(op_status);
1634 return 0;
1635}
1636
1637int GUIAction::repair(std::string arg)
1638{
1639 int op_status = 0;
1640
1641 operation_start("Repair Partition");
1642 if (simulate) {
1643 simulate_progress_bar();
1644 } else {
1645 string part_path;
1646 DataManager::GetValue("tw_partition_mount_point", part_path);
1647 if (PartitionManager.Repair_By_Path(part_path, true)) {
1648 op_status = 0; // success
1649 } else {
1650 LOGERR("Error repairing file system.\n");
1651 op_status = 1; // fail
1652 }
1653 }
1654
1655 operation_end(op_status);
1656 return 0;
1657}
1658
Ethan Yonkera2719152015-05-28 09:44:41 -05001659int GUIAction::resize(std::string arg)
1660{
1661 int op_status = 0;
1662
1663 operation_start("Resize Partition");
1664 if (simulate) {
1665 simulate_progress_bar();
1666 } else {
1667 string part_path;
1668 DataManager::GetValue("tw_partition_mount_point", part_path);
1669 if (PartitionManager.Resize_By_Path(part_path, true)) {
1670 op_status = 0; // success
1671 } else {
1672 LOGERR("Error resizing file system.\n");
1673 op_status = 1; // fail
1674 }
1675 }
1676
1677 operation_end(op_status);
1678 return 0;
1679}
1680
that3f7b1ac2014-12-30 11:30:13 +01001681int GUIAction::changefilesystem(std::string arg)
1682{
1683 int op_status = 0;
1684
1685 operation_start("Change File System");
1686 if (simulate) {
1687 simulate_progress_bar();
1688 } else {
1689 string part_path, file_system;
1690 DataManager::GetValue("tw_partition_mount_point", part_path);
1691 DataManager::GetValue("tw_action_new_file_system", file_system);
1692 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1693 op_status = 0; // success
1694 } else {
1695 LOGERR("Error changing file system.\n");
1696 op_status = 1; // fail
1697 }
1698 }
1699 PartitionManager.Update_System_Details();
1700 operation_end(op_status);
1701 return 0;
1702}
1703
1704int GUIAction::startmtp(std::string arg)
1705{
1706 int op_status = 0;
1707
1708 operation_start("Start MTP");
1709 if (PartitionManager.Enable_MTP())
1710 op_status = 0; // success
1711 else
1712 op_status = 1; // fail
1713
1714 operation_end(op_status);
1715 return 0;
1716}
1717
1718int GUIAction::stopmtp(std::string arg)
1719{
1720 int op_status = 0;
1721
1722 operation_start("Stop MTP");
1723 if (PartitionManager.Disable_MTP())
1724 op_status = 0; // success
1725 else
1726 op_status = 1; // fail
1727
1728 operation_end(op_status);
1729 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001730}
1731
Ethan Yonker96af84a2015-01-05 14:58:36 -06001732int GUIAction::flashimage(std::string arg)
1733{
1734 int op_status = 0;
1735
1736 operation_start("Flash Image");
1737 string path, filename, full_filename;
1738 DataManager::GetValue("tw_zip_location", path);
1739 DataManager::GetValue("tw_file", filename);
1740 full_filename = path + "/" + filename;
1741 if (PartitionManager.Flash_Image(full_filename))
1742 op_status = 0; // success
1743 else
1744 op_status = 1; // fail
1745
1746 operation_end(op_status);
1747 return 0;
1748}
1749
Dees_Troy51a0e822012-09-05 15:24:24 -04001750int GUIAction::getKeyByName(std::string key)
1751{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001752 if (key == "home") return KEY_HOME;
1753 else if (key == "menu") return KEY_MENU;
1754 else if (key == "back") return KEY_BACK;
1755 else if (key == "search") return KEY_SEARCH;
1756 else if (key == "voldown") return KEY_VOLUMEDOWN;
1757 else if (key == "volup") return KEY_VOLUMEUP;
1758 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001759 int ret_val;
1760 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1761 if (!ret_val)
1762 return KEY_POWER;
1763 else
1764 return ret_val;
1765 }
1766
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001767 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001768}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001769
1770int GUIAction::checkpartitionlifetimewrites(std::string arg)
1771{
1772 int op_status = 0;
1773 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1774
1775 operation_start("Check Partition Lifetime Writes");
1776 if (sys) {
1777 if (sys->Check_Lifetime_Writes() != 0)
1778 DataManager::SetValue("tw_lifetime_writes", 1);
1779 else
1780 DataManager::SetValue("tw_lifetime_writes", 0);
1781 op_status = 0; // success
1782 } else {
1783 DataManager::SetValue("tw_lifetime_writes", 1);
1784 op_status = 1; // fail
1785 }
1786
1787 operation_end(op_status);
1788 return 0;
1789}
1790
1791int GUIAction::mountsystemtoggle(std::string arg)
1792{
1793 int op_status = 0;
1794 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
1795
1796 operation_start("Toggle System Mount");
1797 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1798 op_status = 1; // fail
1799 } else {
1800 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1801 if (Part) {
1802 if (DataManager::GetIntValue("tw_mount_system_ro")) {
1803 DataManager::SetValue("tw_mount_system_ro", 0);
1804 Part->Change_Mount_Read_Only(false);
1805 } else {
1806 DataManager::SetValue("tw_mount_system_ro", 1);
1807 Part->Change_Mount_Read_Only(true);
1808 }
1809 if (remount_system) {
1810 Part->Mount(true);
1811 }
1812 op_status = 0; // success
1813 } else {
1814 op_status = 1; // fail
1815 }
1816 }
1817
1818 operation_end(op_status);
1819 return 0;
1820}