blob: b7458459837703def84ae4e33232876dfdcf5c3b [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++) {
thatc7b631b2015-06-01 23:36:57 +0200998 string zip_path = zip_queue[i];
999 size_t slashpos = zip_path.find_last_of('/');
1000 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001001 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001002 DataManager::SetValue("tw_filename", zip_path);
1003 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001004 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1005
1006 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001007 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001008 TWFunc::SetPerformanceMode(false);
1009 if (ret_val != 0) {
thatc7b631b2015-06-01 23:36:57 +02001010 gui_print("Error flashing zip '%s'\n", zip_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001011 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001012 break;
that3f7b1ac2014-12-30 11:30:13 +01001013 }
1014 }
1015 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001016
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001017 if (wipe_cache) {
1018 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001019 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001020 }
that3f7b1ac2014-12-30 11:30:13 +01001021
thatcc8ddca2015-01-03 01:59:36 +01001022 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001023 PartitionManager.Update_System_Details();
1024 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001025 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001026 return 0;
1027}
1028
1029int GUIAction::wipe(std::string arg)
1030{
1031 operation_start("Format");
1032 DataManager::SetValue("tw_partition", arg);
1033
1034 int ret_val = false;
1035
1036 if (simulate) {
1037 simulate_progress_bar();
1038 } else {
1039 if (arg == "data")
1040 ret_val = PartitionManager.Factory_Reset();
1041 else if (arg == "battery")
1042 ret_val = PartitionManager.Wipe_Battery_Stats();
1043 else if (arg == "rotate")
1044 ret_val = PartitionManager.Wipe_Rotate_Data();
1045 else if (arg == "dalvik")
1046 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1047 else if (arg == "DATAMEDIA") {
1048 ret_val = PartitionManager.Format_Data();
1049 } else if (arg == "INTERNAL") {
1050 int has_datamedia, dual_storage;
1051
1052 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1053 if (has_datamedia) {
1054 ret_val = PartitionManager.Wipe_Media_From_Data();
1055 } else {
1056 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1057 }
1058 } else if (arg == "EXTERNAL") {
1059 string External_Path;
1060
1061 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1062 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1063 } else if (arg == "ANDROIDSECURE") {
1064 ret_val = PartitionManager.Wipe_Android_Secure();
1065 } else if (arg == "LIST") {
1066 string Wipe_List, wipe_path;
1067 bool skip = false;
1068 ret_val = true;
1069 TWPartition* wipe_part = NULL;
1070
1071 DataManager::GetValue("tw_wipe_list", Wipe_List);
1072 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1073 if (!Wipe_List.empty()) {
1074 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1075 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1076 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1077 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1078 if (wipe_path == "/and-sec") {
1079 if (!PartitionManager.Wipe_Android_Secure()) {
1080 LOGERR("Unable to wipe android secure\n");
1081 ret_val = false;
1082 break;
1083 } else {
1084 skip = true;
1085 }
1086 } else if (wipe_path == "DALVIK") {
1087 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1088 LOGERR("Failed to wipe dalvik\n");
1089 ret_val = false;
1090 break;
1091 } else {
1092 skip = true;
1093 }
1094 } else if (wipe_path == "INTERNAL") {
1095 if (!PartitionManager.Wipe_Media_From_Data()) {
1096 ret_val = false;
1097 break;
1098 } else {
1099 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001100 }
1101 }
that3f7b1ac2014-12-30 11:30:13 +01001102 if (!skip) {
1103 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1104 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1105 ret_val = false;
1106 break;
1107 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1108 arg = wipe_path;
1109 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 } else {
that3f7b1ac2014-12-30 11:30:13 +01001111 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001112 }
that3f7b1ac2014-12-30 11:30:13 +01001113 start_pos = end_pos + 1;
1114 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 }
1116 }
that3f7b1ac2014-12-30 11:30:13 +01001117 } else
1118 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001119#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001120 if (arg == DataManager::GetSettingsStoragePath()) {
1121 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1122 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001123
that3f7b1ac2014-12-30 11:30:13 +01001124 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1125 LOGINFO("Making TWRP folder and saving settings.\n");
1126 Storage_Path += "/TWRP";
1127 mkdir(Storage_Path.c_str(), 0777);
1128 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001129 } else {
that3f7b1ac2014-12-30 11:30:13 +01001130 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1131 }
1132 }
1133#endif
1134 }
1135 PartitionManager.Update_System_Details();
1136 if (ret_val)
1137 ret_val = 0; // 0 is success
1138 else
1139 ret_val = 1; // 1 is failure
1140 operation_end(ret_val);
1141 return 0;
1142}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001143
that3f7b1ac2014-12-30 11:30:13 +01001144int GUIAction::refreshsizes(std::string arg)
1145{
1146 operation_start("Refreshing Sizes");
1147 if (simulate) {
1148 simulate_progress_bar();
1149 } else
1150 PartitionManager.Update_System_Details();
1151 operation_end(0);
1152 return 0;
1153}
1154
1155int GUIAction::nandroid(std::string arg)
1156{
that3f7b1ac2014-12-30 11:30:13 +01001157 if (simulate) {
that73a52952015-01-28 23:07:34 +01001158 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001159 DataManager::SetValue("tw_partition", "Simulation");
1160 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001161 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001162 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001163 operation_start("Nandroid");
1164 int ret = 0;
1165
that3f7b1ac2014-12-30 11:30:13 +01001166 if (arg == "backup") {
1167 string Backup_Name;
1168 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1169 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1170 ret = PartitionManager.Run_Backup();
1171 }
1172 else {
1173 operation_end(1);
1174 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001175 }
1176 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1177 } else if (arg == "restore") {
1178 string Restore_Name;
1179 DataManager::GetValue("tw_restore", Restore_Name);
1180 ret = PartitionManager.Run_Restore(Restore_Name);
1181 } else {
1182 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001183 return -1;
1184 }
1185 DataManager::SetValue("tw_encrypt_backup", 0);
1186 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001187 if (ret == false)
1188 ret = 1; // 1 for failure
1189 else
1190 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001192 }
1193 else {
1194 DataManager::SetValue("tw_cancel_backup", 1);
1195 gui_print("Backup Canceled.\n");
1196 ret = 0;
1197 }
that73a52952015-01-28 23:07:34 +01001198 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001199 return ret;
1200 }
1201 return 0;
1202}
1203
1204int GUIAction::cancelbackup(std::string arg) {
1205 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001206 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001207 }
1208 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001209 int op_status = PartitionManager.Cancel_Backup();
1210 if (op_status != 0)
1211 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001212 }
1213
1214 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001215}
Dees_Troy51a0e822012-09-05 15:24:24 -04001216
that3f7b1ac2014-12-30 11:30:13 +01001217int GUIAction::fixpermissions(std::string arg)
1218{
that73a52952015-01-28 23:07:34 +01001219 int op_status = 0;
1220
that3f7b1ac2014-12-30 11:30:13 +01001221 operation_start("Fix Permissions");
1222 LOGINFO("fix permissions started!\n");
1223 if (simulate) {
1224 simulate_progress_bar();
1225 } else {
that73a52952015-01-28 23:07:34 +01001226 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001227 if (op_status != 0)
1228 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001229 }
that73a52952015-01-28 23:07:34 +01001230 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001231 return 0;
1232}
1233
1234int GUIAction::dd(std::string arg)
1235{
1236 operation_start("imaging");
1237
1238 if (simulate) {
1239 simulate_progress_bar();
1240 } else {
1241 string cmd = "dd " + arg;
1242 TWFunc::Exec_Cmd(cmd);
1243 }
1244 operation_end(0);
1245 return 0;
1246}
1247
1248int GUIAction::partitionsd(std::string arg)
1249{
1250 operation_start("Partition SD Card");
1251 int ret_val = 0;
1252
1253 if (simulate) {
1254 simulate_progress_bar();
1255 } else {
1256 int allow_partition;
1257 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1258 if (allow_partition == 0) {
1259 gui_print("This device does not have a real SD Card!\nAborting!\n");
1260 } else {
1261 if (!PartitionManager.Partition_SDCard())
1262 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001263 }
that3f7b1ac2014-12-30 11:30:13 +01001264 }
1265 operation_end(ret_val);
1266 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001267
that3f7b1ac2014-12-30 11:30:13 +01001268}
Dees_Troy51a0e822012-09-05 15:24:24 -04001269
that3f7b1ac2014-12-30 11:30:13 +01001270int GUIAction::installhtcdumlock(std::string arg)
1271{
1272 operation_start("Install HTC Dumlock");
1273 if (simulate) {
1274 simulate_progress_bar();
1275 } else
1276 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001277
that3f7b1ac2014-12-30 11:30:13 +01001278 operation_end(0);
1279 return 0;
1280}
Dees_Troy51a0e822012-09-05 15:24:24 -04001281
that3f7b1ac2014-12-30 11:30:13 +01001282int GUIAction::htcdumlockrestoreboot(std::string arg)
1283{
1284 operation_start("HTC Dumlock Restore Boot");
1285 if (simulate) {
1286 simulate_progress_bar();
1287 } else
1288 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001289
that3f7b1ac2014-12-30 11:30:13 +01001290 operation_end(0);
1291 return 0;
1292}
Dees_Troy51a0e822012-09-05 15:24:24 -04001293
that3f7b1ac2014-12-30 11:30:13 +01001294int GUIAction::htcdumlockreflashrecovery(std::string arg)
1295{
1296 operation_start("HTC Dumlock Reflash Recovery");
1297 if (simulate) {
1298 simulate_progress_bar();
1299 } else
1300 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001301
that3f7b1ac2014-12-30 11:30:13 +01001302 operation_end(0);
1303 return 0;
1304}
Dees_Troy51a0e822012-09-05 15:24:24 -04001305
that3f7b1ac2014-12-30 11:30:13 +01001306int GUIAction::cmd(std::string arg)
1307{
1308 int op_status = 0;
1309
1310 operation_start("Command");
1311 LOGINFO("Running command: '%s'\n", arg.c_str());
1312 if (simulate) {
1313 simulate_progress_bar();
1314 } else {
1315 op_status = TWFunc::Exec_Cmd(arg);
1316 if (op_status != 0)
1317 op_status = 1;
1318 }
1319
1320 operation_end(op_status);
1321 return 0;
1322}
1323
1324int GUIAction::terminalcommand(std::string arg)
1325{
1326 int op_status = 0;
1327 string cmdpath, command;
1328
1329 DataManager::GetValue("tw_terminal_location", cmdpath);
1330 operation_start("CommandOutput");
1331 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1332 if (simulate) {
1333 simulate_progress_bar();
1334 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001335 } else if (arg == "exit") {
1336 LOGINFO("Exiting terminal\n");
1337 operation_end(op_status);
1338 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001339 } else {
1340 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1341 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001342 DataManager::SetValue("tw_terminal_state", 1);
1343 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001344 FILE* fp;
1345 char line[512];
1346
1347 fp = popen(command.c_str(), "r");
1348 if (fp == NULL) {
1349 LOGERR("Error opening command to run.\n");
1350 } else {
1351 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1352 struct timeval timeout;
1353 fd_set fdset;
1354
1355 while(keep_going)
1356 {
1357 FD_ZERO(&fdset);
1358 FD_SET(fd, &fdset);
1359 timeout.tv_sec = 0;
1360 timeout.tv_usec = 400000;
1361 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1362 if (has_data == 0) {
1363 // Timeout reached
1364 DataManager::GetValue("tw_terminal_state", check);
1365 if (check == 0) {
1366 keep_going = 0;
1367 }
1368 } else if (has_data < 0) {
1369 // End of execution
1370 keep_going = 0;
1371 } else {
1372 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001373 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001374 gui_print("%s", line); // Display output
1375 else
1376 keep_going = 0; // Done executing
1377 }
1378 }
1379 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001380 }
thatc6085482015-01-09 22:12:43 +01001381 DataManager::SetValue("tw_operation_status", 0);
1382 DataManager::SetValue("tw_operation_state", 1);
1383 DataManager::SetValue("tw_terminal_state", 0);
1384 DataManager::SetValue("tw_background_thread_running", 0);
1385 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001386 }
1387 return 0;
1388}
1389
1390int GUIAction::killterminal(std::string arg)
1391{
1392 int op_status = 0;
1393
1394 LOGINFO("Sending kill command...\n");
1395 operation_start("KillCommand");
1396 DataManager::SetValue("tw_operation_status", 0);
1397 DataManager::SetValue("tw_operation_state", 1);
1398 DataManager::SetValue("tw_terminal_state", 0);
1399 DataManager::SetValue("tw_background_thread_running", 0);
1400 DataManager::SetValue(TW_ACTION_BUSY, 0);
1401 return 0;
1402}
1403
1404int GUIAction::reinjecttwrp(std::string arg)
1405{
1406 int op_status = 0;
1407 operation_start("ReinjectTWRP");
1408 gui_print("Injecting TWRP into boot image...\n");
1409 if (simulate) {
1410 simulate_progress_bar();
1411 } else {
1412 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1413 gui_print("TWRP injection complete.\n");
1414 }
1415
1416 operation_end(op_status);
1417 return 0;
1418}
1419
1420int GUIAction::checkbackupname(std::string arg)
1421{
1422 int op_status = 0;
1423
1424 operation_start("CheckBackupName");
1425 if (simulate) {
1426 simulate_progress_bar();
1427 } else {
1428 op_status = PartitionManager.Check_Backup_Name(true);
1429 if (op_status != 0)
1430 op_status = 1;
1431 }
1432
1433 operation_end(op_status);
1434 return 0;
1435}
1436
1437int GUIAction::decrypt(std::string arg)
1438{
1439 int op_status = 0;
1440
1441 operation_start("Decrypt");
1442 if (simulate) {
1443 simulate_progress_bar();
1444 } else {
1445 string Password;
1446 DataManager::GetValue("tw_crypto_password", Password);
1447 op_status = PartitionManager.Decrypt_Device(Password);
1448 if (op_status != 0)
1449 op_status = 1;
1450 else {
that3f7b1ac2014-12-30 11:30:13 +01001451
1452 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1453
Ethan Yonkercf50da52015-01-12 21:59:07 -06001454 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001455
Ethan Yonkercf50da52015-01-12 21:59:07 -06001456 // Check for a custom theme and load it if exists
1457 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1458 if (has_datamedia != 0) {
1459 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001460 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001461 } else {
1462 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001463 }
1464 }
1465 }
1466 }
1467
1468 operation_end(op_status);
1469 return 0;
1470}
1471
thatcc8ddca2015-01-03 01:59:36 +01001472int GUIAction::adbsideload(std::string arg)
1473{
1474 operation_start("Sideload");
1475 if (simulate) {
1476 simulate_progress_bar();
1477 operation_end(0);
1478 } else {
thatc6085482015-01-09 22:12:43 +01001479 gui_print("Starting ADB sideload feature...\n");
1480 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1481
1482 // wait for the adb connection
1483 int ret = apply_from_adb("/", &sideload_child_pid);
1484 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1485
1486 if (ret != 0) {
1487 if (ret == -2)
1488 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1489 ret = 1; // failure
1490 } else {
1491 int wipe_cache = 0;
1492 int wipe_dalvik = 0;
1493 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1494
1495 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1496 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1497 PartitionManager.Wipe_By_Path("/cache");
1498 if (wipe_dalvik)
1499 PartitionManager.Wipe_Dalvik_Cache();
1500 } else {
1501 ret = 1; // failure
1502 }
thatcc8ddca2015-01-03 01:59:36 +01001503 }
thatc6085482015-01-09 22:12:43 +01001504 if (sideload_child_pid) {
1505 LOGINFO("Signaling child sideload process to exit.\n");
1506 struct stat st;
1507 // Calling stat() on this magic filename signals the minadbd
1508 // subprocess to shut down.
1509 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1510 int status;
1511 LOGINFO("Waiting for child sideload process to exit.\n");
1512 waitpid(sideload_child_pid, &status, 0);
1513 }
Dees Troy28a85d22015-04-28 02:03:16 +00001514 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001515 TWFunc::Toggle_MTP(mtp_was_enabled);
1516 reinject_after_flash();
1517 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001518 }
that3f7b1ac2014-12-30 11:30:13 +01001519 return 0;
1520}
1521
1522int GUIAction::adbsideloadcancel(std::string arg)
1523{
that3f7b1ac2014-12-30 11:30:13 +01001524 struct stat st;
1525 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1526 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001527 LOGINFO("Signaling child sideload process to exit.\n");
1528 // Calling stat() on this magic filename signals the minadbd
1529 // subprocess to shut down.
1530 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1531 if (!sideload_child_pid) {
1532 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001533 return 0;
1534 }
thatcc8ddca2015-01-03 01:59:36 +01001535 ::sleep(1);
1536 LOGINFO("Killing child sideload process.\n");
1537 kill(sideload_child_pid, SIGTERM);
1538 int status;
1539 LOGINFO("Waiting for child sideload process to exit.\n");
1540 waitpid(sideload_child_pid, &status, 0);
1541 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001542 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1543 return 0;
1544}
1545
1546int GUIAction::openrecoveryscript(std::string arg)
1547{
Ethan Yonkere1abe612015-06-09 11:09:32 -05001548 int op_status = 1;
1549
that3f7b1ac2014-12-30 11:30:13 +01001550 operation_start("OpenRecoveryScript");
1551 if (simulate) {
1552 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001553 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001554 } else {
thatc6085482015-01-09 22:12:43 +01001555 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1556 // that we converted to ORS commands during boot in recovery.cpp.
1557 // Run those first.
1558 int reboot = 0;
1559 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1560 gui_print("Processing AOSP recovery commands...\n");
1561 if (OpenRecoveryScript::run_script_file() == 0) {
1562 reboot = 1;
Matt Mower85161af2015-08-08 10:00:50 -05001563 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001564 }
that3f7b1ac2014-12-30 11:30:13 +01001565 }
thatc6085482015-01-09 22:12:43 +01001566 // Check for the ORS file in /cache and attempt to run those commands.
1567 if (OpenRecoveryScript::check_for_script_file()) {
1568 gui_print("Processing OpenRecoveryScript file...\n");
1569 if (OpenRecoveryScript::run_script_file() == 0) {
1570 reboot = 1;
Ethan Yonkere1abe612015-06-09 11:09:32 -05001571 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001572 }
1573 }
1574 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001575 // Disable stock recovery reflashing
1576 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001577 usleep(2000000); // Sleep for 2 seconds before rebooting
1578 TWFunc::tw_reboot(rb_system);
Ethan Yonkere1abe612015-06-09 11:09:32 -05001579 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
thatc6085482015-01-09 22:12:43 +01001580 } else {
1581 DataManager::SetValue("tw_page_done", 1);
1582 }
Ethan Yonkere1abe612015-06-09 11:09:32 -05001583 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001584 }
1585 return 0;
1586}
1587
1588int GUIAction::installsu(std::string arg)
1589{
1590 int op_status = 0;
1591
1592 operation_start("Install SuperSU");
1593 if (simulate) {
1594 simulate_progress_bar();
1595 } else {
1596 if (!TWFunc::Install_SuperSU())
1597 op_status = 1;
1598 }
1599
1600 operation_end(op_status);
1601 return 0;
1602}
1603
1604int GUIAction::fixsu(std::string arg)
1605{
1606 int op_status = 0;
1607
1608 operation_start("Fixing Superuser Permissions");
1609 if (simulate) {
1610 simulate_progress_bar();
1611 } else {
1612 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1613 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1614 }
1615
1616 operation_end(op_status);
1617 return 0;
1618}
1619
1620int GUIAction::decrypt_backup(std::string arg)
1621{
1622 int op_status = 0;
1623
1624 operation_start("Try Restore Decrypt");
1625 if (simulate) {
1626 simulate_progress_bar();
1627 } else {
1628 string Restore_Path, Filename, Password;
1629 DataManager::GetValue("tw_restore", Restore_Path);
1630 Restore_Path += "/";
1631 DataManager::GetValue("tw_restore_password", Password);
1632 TWFunc::SetPerformanceMode(true);
1633 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1634 op_status = 0; // success
1635 else
1636 op_status = 1; // fail
1637 TWFunc::SetPerformanceMode(false);
1638 }
1639
1640 operation_end(op_status);
1641 return 0;
1642}
1643
1644int GUIAction::repair(std::string arg)
1645{
1646 int op_status = 0;
1647
1648 operation_start("Repair Partition");
1649 if (simulate) {
1650 simulate_progress_bar();
1651 } else {
1652 string part_path;
1653 DataManager::GetValue("tw_partition_mount_point", part_path);
1654 if (PartitionManager.Repair_By_Path(part_path, true)) {
1655 op_status = 0; // success
1656 } else {
1657 LOGERR("Error repairing file system.\n");
1658 op_status = 1; // fail
1659 }
1660 }
1661
1662 operation_end(op_status);
1663 return 0;
1664}
1665
Ethan Yonkera2719152015-05-28 09:44:41 -05001666int GUIAction::resize(std::string arg)
1667{
1668 int op_status = 0;
1669
1670 operation_start("Resize Partition");
1671 if (simulate) {
1672 simulate_progress_bar();
1673 } else {
1674 string part_path;
1675 DataManager::GetValue("tw_partition_mount_point", part_path);
1676 if (PartitionManager.Resize_By_Path(part_path, true)) {
1677 op_status = 0; // success
1678 } else {
1679 LOGERR("Error resizing file system.\n");
1680 op_status = 1; // fail
1681 }
1682 }
1683
1684 operation_end(op_status);
1685 return 0;
1686}
1687
that3f7b1ac2014-12-30 11:30:13 +01001688int GUIAction::changefilesystem(std::string arg)
1689{
1690 int op_status = 0;
1691
1692 operation_start("Change File System");
1693 if (simulate) {
1694 simulate_progress_bar();
1695 } else {
1696 string part_path, file_system;
1697 DataManager::GetValue("tw_partition_mount_point", part_path);
1698 DataManager::GetValue("tw_action_new_file_system", file_system);
1699 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1700 op_status = 0; // success
1701 } else {
1702 LOGERR("Error changing file system.\n");
1703 op_status = 1; // fail
1704 }
1705 }
1706 PartitionManager.Update_System_Details();
1707 operation_end(op_status);
1708 return 0;
1709}
1710
1711int GUIAction::startmtp(std::string arg)
1712{
1713 int op_status = 0;
1714
1715 operation_start("Start MTP");
1716 if (PartitionManager.Enable_MTP())
1717 op_status = 0; // success
1718 else
1719 op_status = 1; // fail
1720
1721 operation_end(op_status);
1722 return 0;
1723}
1724
1725int GUIAction::stopmtp(std::string arg)
1726{
1727 int op_status = 0;
1728
1729 operation_start("Stop MTP");
1730 if (PartitionManager.Disable_MTP())
1731 op_status = 0; // success
1732 else
1733 op_status = 1; // fail
1734
1735 operation_end(op_status);
1736 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001737}
1738
Ethan Yonker96af84a2015-01-05 14:58:36 -06001739int GUIAction::flashimage(std::string arg)
1740{
1741 int op_status = 0;
1742
1743 operation_start("Flash Image");
1744 string path, filename, full_filename;
1745 DataManager::GetValue("tw_zip_location", path);
1746 DataManager::GetValue("tw_file", filename);
1747 full_filename = path + "/" + filename;
1748 if (PartitionManager.Flash_Image(full_filename))
1749 op_status = 0; // success
1750 else
1751 op_status = 1; // fail
1752
1753 operation_end(op_status);
1754 return 0;
1755}
1756
Dees_Troy51a0e822012-09-05 15:24:24 -04001757int GUIAction::getKeyByName(std::string key)
1758{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001759 if (key == "home") return KEY_HOME;
1760 else if (key == "menu") return KEY_MENU;
1761 else if (key == "back") return KEY_BACK;
1762 else if (key == "search") return KEY_SEARCH;
1763 else if (key == "voldown") return KEY_VOLUMEDOWN;
1764 else if (key == "volup") return KEY_VOLUMEUP;
1765 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001766 int ret_val;
1767 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1768 if (!ret_val)
1769 return KEY_POWER;
1770 else
1771 return ret_val;
1772 }
1773
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001774 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001775}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001776
1777int GUIAction::checkpartitionlifetimewrites(std::string arg)
1778{
1779 int op_status = 0;
1780 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1781
1782 operation_start("Check Partition Lifetime Writes");
1783 if (sys) {
1784 if (sys->Check_Lifetime_Writes() != 0)
1785 DataManager::SetValue("tw_lifetime_writes", 1);
1786 else
1787 DataManager::SetValue("tw_lifetime_writes", 0);
1788 op_status = 0; // success
1789 } else {
1790 DataManager::SetValue("tw_lifetime_writes", 1);
1791 op_status = 1; // fail
1792 }
1793
1794 operation_end(op_status);
1795 return 0;
1796}
1797
1798int GUIAction::mountsystemtoggle(std::string arg)
1799{
1800 int op_status = 0;
1801 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
1802
1803 operation_start("Toggle System Mount");
1804 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1805 op_status = 1; // fail
1806 } else {
1807 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1808 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001809 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001810 DataManager::SetValue("tw_mount_system_ro", 0);
1811 Part->Change_Mount_Read_Only(false);
1812 } else {
1813 DataManager::SetValue("tw_mount_system_ro", 1);
1814 Part->Change_Mount_Read_Only(true);
1815 }
1816 if (remount_system) {
1817 Part->Mount(true);
1818 }
1819 op_status = 0; // success
1820 } else {
1821 op_status = 1; // fail
1822 }
1823 }
1824
1825 operation_end(op_status);
1826 return 0;
1827}