blob: 97d95f170fad5e9e07abafb419fa0cf32f819e11 [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
xiaolue738da52015-02-22 20:49:35 +08001369 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001370 gui_print("%s", line); // Display output
1371 else
1372 keep_going = 0; // Done executing
1373 }
1374 }
1375 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001376 }
thatc6085482015-01-09 22:12:43 +01001377 DataManager::SetValue("tw_operation_status", 0);
1378 DataManager::SetValue("tw_operation_state", 1);
1379 DataManager::SetValue("tw_terminal_state", 0);
1380 DataManager::SetValue("tw_background_thread_running", 0);
1381 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001382 }
1383 return 0;
1384}
1385
1386int GUIAction::killterminal(std::string arg)
1387{
1388 int op_status = 0;
1389
1390 LOGINFO("Sending kill command...\n");
1391 operation_start("KillCommand");
1392 DataManager::SetValue("tw_operation_status", 0);
1393 DataManager::SetValue("tw_operation_state", 1);
1394 DataManager::SetValue("tw_terminal_state", 0);
1395 DataManager::SetValue("tw_background_thread_running", 0);
1396 DataManager::SetValue(TW_ACTION_BUSY, 0);
1397 return 0;
1398}
1399
1400int GUIAction::reinjecttwrp(std::string arg)
1401{
1402 int op_status = 0;
1403 operation_start("ReinjectTWRP");
1404 gui_print("Injecting TWRP into boot image...\n");
1405 if (simulate) {
1406 simulate_progress_bar();
1407 } else {
1408 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1409 gui_print("TWRP injection complete.\n");
1410 }
1411
1412 operation_end(op_status);
1413 return 0;
1414}
1415
1416int GUIAction::checkbackupname(std::string arg)
1417{
1418 int op_status = 0;
1419
1420 operation_start("CheckBackupName");
1421 if (simulate) {
1422 simulate_progress_bar();
1423 } else {
1424 op_status = PartitionManager.Check_Backup_Name(true);
1425 if (op_status != 0)
1426 op_status = 1;
1427 }
1428
1429 operation_end(op_status);
1430 return 0;
1431}
1432
1433int GUIAction::decrypt(std::string arg)
1434{
1435 int op_status = 0;
1436
1437 operation_start("Decrypt");
1438 if (simulate) {
1439 simulate_progress_bar();
1440 } else {
1441 string Password;
1442 DataManager::GetValue("tw_crypto_password", Password);
1443 op_status = PartitionManager.Decrypt_Device(Password);
1444 if (op_status != 0)
1445 op_status = 1;
1446 else {
that3f7b1ac2014-12-30 11:30:13 +01001447
1448 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1449
Ethan Yonkercf50da52015-01-12 21:59:07 -06001450 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001451
Ethan Yonkercf50da52015-01-12 21:59:07 -06001452 // Check for a custom theme and load it if exists
1453 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1454 if (has_datamedia != 0) {
1455 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001456 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001457 } else {
1458 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001459 }
1460 }
1461 }
1462 }
1463
1464 operation_end(op_status);
1465 return 0;
1466}
1467
thatcc8ddca2015-01-03 01:59:36 +01001468int GUIAction::adbsideload(std::string arg)
1469{
1470 operation_start("Sideload");
1471 if (simulate) {
1472 simulate_progress_bar();
1473 operation_end(0);
1474 } else {
thatc6085482015-01-09 22:12:43 +01001475 gui_print("Starting ADB sideload feature...\n");
1476 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1477
1478 // wait for the adb connection
1479 int ret = apply_from_adb("/", &sideload_child_pid);
1480 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1481
1482 if (ret != 0) {
1483 if (ret == -2)
1484 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1485 ret = 1; // failure
1486 } else {
1487 int wipe_cache = 0;
1488 int wipe_dalvik = 0;
1489 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1490
1491 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1492 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1493 PartitionManager.Wipe_By_Path("/cache");
1494 if (wipe_dalvik)
1495 PartitionManager.Wipe_Dalvik_Cache();
1496 } else {
1497 ret = 1; // failure
1498 }
thatcc8ddca2015-01-03 01:59:36 +01001499 }
thatc6085482015-01-09 22:12:43 +01001500 if (sideload_child_pid) {
1501 LOGINFO("Signaling child sideload process to exit.\n");
1502 struct stat st;
1503 // Calling stat() on this magic filename signals the minadbd
1504 // subprocess to shut down.
1505 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1506 int status;
1507 LOGINFO("Waiting for child sideload process to exit.\n");
1508 waitpid(sideload_child_pid, &status, 0);
1509 }
1510
1511 TWFunc::Toggle_MTP(mtp_was_enabled);
1512 reinject_after_flash();
1513 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001514 }
that3f7b1ac2014-12-30 11:30:13 +01001515 return 0;
1516}
1517
1518int GUIAction::adbsideloadcancel(std::string arg)
1519{
that3f7b1ac2014-12-30 11:30:13 +01001520 struct stat st;
1521 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1522 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001523 LOGINFO("Signaling child sideload process to exit.\n");
1524 // Calling stat() on this magic filename signals the minadbd
1525 // subprocess to shut down.
1526 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1527 if (!sideload_child_pid) {
1528 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001529 return 0;
1530 }
thatcc8ddca2015-01-03 01:59:36 +01001531 ::sleep(1);
1532 LOGINFO("Killing child sideload process.\n");
1533 kill(sideload_child_pid, SIGTERM);
1534 int status;
1535 LOGINFO("Waiting for child sideload process to exit.\n");
1536 waitpid(sideload_child_pid, &status, 0);
1537 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001538 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1539 return 0;
1540}
1541
1542int GUIAction::openrecoveryscript(std::string arg)
1543{
1544 operation_start("OpenRecoveryScript");
1545 if (simulate) {
1546 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001547 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001548 } else {
thatc6085482015-01-09 22:12:43 +01001549 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1550 // that we converted to ORS commands during boot in recovery.cpp.
1551 // Run those first.
1552 int reboot = 0;
1553 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1554 gui_print("Processing AOSP recovery commands...\n");
1555 if (OpenRecoveryScript::run_script_file() == 0) {
1556 reboot = 1;
1557 }
that3f7b1ac2014-12-30 11:30:13 +01001558 }
thatc6085482015-01-09 22:12:43 +01001559 // Check for the ORS file in /cache and attempt to run those commands.
1560 if (OpenRecoveryScript::check_for_script_file()) {
1561 gui_print("Processing OpenRecoveryScript file...\n");
1562 if (OpenRecoveryScript::run_script_file() == 0) {
1563 reboot = 1;
1564 }
1565 }
1566 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001567 // Disable stock recovery reflashing
1568 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001569 usleep(2000000); // Sleep for 2 seconds before rebooting
1570 TWFunc::tw_reboot(rb_system);
1571 } else {
1572 DataManager::SetValue("tw_page_done", 1);
1573 }
1574 operation_end(1);
1575 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001576 }
1577 return 0;
1578}
1579
1580int GUIAction::installsu(std::string arg)
1581{
1582 int op_status = 0;
1583
1584 operation_start("Install SuperSU");
1585 if (simulate) {
1586 simulate_progress_bar();
1587 } else {
1588 if (!TWFunc::Install_SuperSU())
1589 op_status = 1;
1590 }
1591
1592 operation_end(op_status);
1593 return 0;
1594}
1595
1596int GUIAction::fixsu(std::string arg)
1597{
1598 int op_status = 0;
1599
1600 operation_start("Fixing Superuser Permissions");
1601 if (simulate) {
1602 simulate_progress_bar();
1603 } else {
1604 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1605 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1606 }
1607
1608 operation_end(op_status);
1609 return 0;
1610}
1611
1612int GUIAction::decrypt_backup(std::string arg)
1613{
1614 int op_status = 0;
1615
1616 operation_start("Try Restore Decrypt");
1617 if (simulate) {
1618 simulate_progress_bar();
1619 } else {
1620 string Restore_Path, Filename, Password;
1621 DataManager::GetValue("tw_restore", Restore_Path);
1622 Restore_Path += "/";
1623 DataManager::GetValue("tw_restore_password", Password);
1624 TWFunc::SetPerformanceMode(true);
1625 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1626 op_status = 0; // success
1627 else
1628 op_status = 1; // fail
1629 TWFunc::SetPerformanceMode(false);
1630 }
1631
1632 operation_end(op_status);
1633 return 0;
1634}
1635
1636int GUIAction::repair(std::string arg)
1637{
1638 int op_status = 0;
1639
1640 operation_start("Repair Partition");
1641 if (simulate) {
1642 simulate_progress_bar();
1643 } else {
1644 string part_path;
1645 DataManager::GetValue("tw_partition_mount_point", part_path);
1646 if (PartitionManager.Repair_By_Path(part_path, true)) {
1647 op_status = 0; // success
1648 } else {
1649 LOGERR("Error repairing file system.\n");
1650 op_status = 1; // fail
1651 }
1652 }
1653
1654 operation_end(op_status);
1655 return 0;
1656}
1657
Ethan Yonkera2719152015-05-28 09:44:41 -05001658int GUIAction::resize(std::string arg)
1659{
1660 int op_status = 0;
1661
1662 operation_start("Resize Partition");
1663 if (simulate) {
1664 simulate_progress_bar();
1665 } else {
1666 string part_path;
1667 DataManager::GetValue("tw_partition_mount_point", part_path);
1668 if (PartitionManager.Resize_By_Path(part_path, true)) {
1669 op_status = 0; // success
1670 } else {
1671 LOGERR("Error resizing file system.\n");
1672 op_status = 1; // fail
1673 }
1674 }
1675
1676 operation_end(op_status);
1677 return 0;
1678}
1679
that3f7b1ac2014-12-30 11:30:13 +01001680int GUIAction::changefilesystem(std::string arg)
1681{
1682 int op_status = 0;
1683
1684 operation_start("Change File System");
1685 if (simulate) {
1686 simulate_progress_bar();
1687 } else {
1688 string part_path, file_system;
1689 DataManager::GetValue("tw_partition_mount_point", part_path);
1690 DataManager::GetValue("tw_action_new_file_system", file_system);
1691 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1692 op_status = 0; // success
1693 } else {
1694 LOGERR("Error changing file system.\n");
1695 op_status = 1; // fail
1696 }
1697 }
1698 PartitionManager.Update_System_Details();
1699 operation_end(op_status);
1700 return 0;
1701}
1702
1703int GUIAction::startmtp(std::string arg)
1704{
1705 int op_status = 0;
1706
1707 operation_start("Start MTP");
1708 if (PartitionManager.Enable_MTP())
1709 op_status = 0; // success
1710 else
1711 op_status = 1; // fail
1712
1713 operation_end(op_status);
1714 return 0;
1715}
1716
1717int GUIAction::stopmtp(std::string arg)
1718{
1719 int op_status = 0;
1720
1721 operation_start("Stop MTP");
1722 if (PartitionManager.Disable_MTP())
1723 op_status = 0; // success
1724 else
1725 op_status = 1; // fail
1726
1727 operation_end(op_status);
1728 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001729}
1730
Ethan Yonker96af84a2015-01-05 14:58:36 -06001731int GUIAction::flashimage(std::string arg)
1732{
1733 int op_status = 0;
1734
1735 operation_start("Flash Image");
1736 string path, filename, full_filename;
1737 DataManager::GetValue("tw_zip_location", path);
1738 DataManager::GetValue("tw_file", filename);
1739 full_filename = path + "/" + filename;
1740 if (PartitionManager.Flash_Image(full_filename))
1741 op_status = 0; // success
1742 else
1743 op_status = 1; // fail
1744
1745 operation_end(op_status);
1746 return 0;
1747}
1748
Dees_Troy51a0e822012-09-05 15:24:24 -04001749int GUIAction::getKeyByName(std::string key)
1750{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001751 if (key == "home") return KEY_HOME;
1752 else if (key == "menu") return KEY_MENU;
1753 else if (key == "back") return KEY_BACK;
1754 else if (key == "search") return KEY_SEARCH;
1755 else if (key == "voldown") return KEY_VOLUMEDOWN;
1756 else if (key == "volup") return KEY_VOLUMEUP;
1757 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001758 int ret_val;
1759 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1760 if (!ret_val)
1761 return KEY_POWER;
1762 else
1763 return ret_val;
1764 }
1765
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001766 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001767}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001768
1769int GUIAction::checkpartitionlifetimewrites(std::string arg)
1770{
1771 int op_status = 0;
1772 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1773
1774 operation_start("Check Partition Lifetime Writes");
1775 if (sys) {
1776 if (sys->Check_Lifetime_Writes() != 0)
1777 DataManager::SetValue("tw_lifetime_writes", 1);
1778 else
1779 DataManager::SetValue("tw_lifetime_writes", 0);
1780 op_status = 0; // success
1781 } else {
1782 DataManager::SetValue("tw_lifetime_writes", 1);
1783 op_status = 1; // fail
1784 }
1785
1786 operation_end(op_status);
1787 return 0;
1788}
1789
1790int GUIAction::mountsystemtoggle(std::string arg)
1791{
1792 int op_status = 0;
1793 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
1794
1795 operation_start("Toggle System Mount");
1796 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1797 op_status = 1; // fail
1798 } else {
1799 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1800 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001801 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001802 DataManager::SetValue("tw_mount_system_ro", 0);
1803 Part->Change_Mount_Read_Only(false);
1804 } else {
1805 DataManager::SetValue("tw_mount_system_ro", 1);
1806 Part->Change_Mount_Read_Only(true);
1807 }
1808 if (remount_system) {
1809 Part->Mount(true);
1810 }
1811 op_status = 0; // success
1812 } else {
1813 op_status = 1; // fail
1814 }
1815 }
1816
1817 operation_end(op_status);
1818 return 0;
1819}