blob: 3425512474a4578239f028d1e0bbb51e15da914e [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);
thatc6085482015-01-09 22:12:43 +0100199
200 // remember actions that run in the caller thread
201 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
202 setActionsRunningInCallerThread.insert(it->first);
203
204 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100205 ADD_ACTION(flash);
206 ADD_ACTION(wipe);
207 ADD_ACTION(refreshsizes);
208 ADD_ACTION(nandroid);
209 ADD_ACTION(fixpermissions);
210 ADD_ACTION(dd);
211 ADD_ACTION(partitionsd);
212 ADD_ACTION(installhtcdumlock);
213 ADD_ACTION(htcdumlockrestoreboot);
214 ADD_ACTION(htcdumlockreflashrecovery);
215 ADD_ACTION(cmd);
216 ADD_ACTION(terminalcommand);
217 ADD_ACTION(reinjecttwrp);
218 ADD_ACTION(decrypt);
219 ADD_ACTION(adbsideload);
220 ADD_ACTION(openrecoveryscript);
221 ADD_ACTION(installsu);
222 ADD_ACTION(decrypt_backup);
223 ADD_ACTION(repair);
224 ADD_ACTION(changefilesystem);
225 ADD_ACTION(flashimage);
that3f7b1ac2014-12-30 11:30:13 +0100226 }
227
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600229 actions = FindNode(node, "actions");
230 if (actions) child = FindNode(actions, "action");
231 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200233 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 while (child)
236 {
237 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 attr = child->first_attribute("function");
240 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 action.mFunction = attr->value();
243 action.mArg = child->value();
244 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 child = child->next_sibling("action");
247 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600250 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 if (child)
252 {
253 attr = child->first_attribute("key");
254 if (attr)
255 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100256 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
257 for(size_t i = 0; i < keys.size(); ++i)
258 {
259 const int key = getKeyByName(keys[i]);
260 mKeys[key] = false;
261 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 }
263 else
264 {
265 attr = child->first_attribute("x");
266 if (!attr) return;
267 mActionX = atol(attr->value());
268 attr = child->first_attribute("y");
269 if (!attr) return;
270 mActionY = atol(attr->value());
271 attr = child->first_attribute("w");
272 if (!attr) return;
273 mActionW = atol(attr->value());
274 attr = child->first_attribute("h");
275 if (!attr) return;
276 mActionH = atol(attr->value());
277 }
278 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400279}
280
281int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
282{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200283 if (state == TOUCH_RELEASE)
284 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400287}
288
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100289int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400290{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100291 if (mKeys.empty())
292 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100294 std::map<int, bool>::iterator itr = mKeys.find(key);
295 if(itr == mKeys.end())
296 return 0;
297
298 bool prevState = itr->second;
299 itr->second = down;
300
301 // If there is only one key for this action, wait for key up so it
302 // doesn't trigger with multi-key actions.
303 // Else, check if all buttons are pressed, then consume their release events
304 // so they don't trigger one-button actions and reset mKeys pressed status
305 if(mKeys.size() == 1) {
306 if(!down && prevState)
307 doActions();
308 } else if(down) {
309 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
310 if(!itr->second)
311 return 0;
312 }
313
314 // Passed, all req buttons are pressed, reset them and consume release events
315 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
316 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
317 kb->ConsumeKeyRelease(itr->first);
318 itr->second = false;
319 }
320
321 doActions();
322 }
323
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 return 0;
325}
326
Vojtech Bocek07220562014-02-08 02:05:33 +0100327int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400328{
Vojtech Bocek07220562014-02-08 02:05:33 +0100329 GUIObject::NotifyVarChange(varName, value);
330
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100331 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200332 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100333 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400335
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337}
338
339void GUIAction::simulate_progress_bar(void)
340{
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400342 for (int i = 0; i < 5; i++)
343 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500344 if (PartitionManager.stop_backup.get_value()) {
345 DataManager::SetValue("tw_cancel_backup", 1);
346 gui_print("Backup Canceled.\n");
347 DataManager::SetValue("ui_progress", 0);
348 PartitionManager.stop_backup.set_value(0);
349 return;
350 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400351 usleep(500000);
352 DataManager::SetValue("ui_progress", i * 20);
353 }
354}
355
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600356int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400357{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200358 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
360 DataManager::SetValue("ui_progress", 0);
361
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200362 if (filename.empty())
363 {
364 LOGERR("No file specified.\n");
365 return -1;
366 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400369 return -1;
370
Dees_Troy51a0e822012-09-05 15:24:24 -0400371 if (simulate) {
372 simulate_progress_bar();
373 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400374 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
376 // Now, check if we need to ensure TWRP remains installed...
377 struct stat st;
378 if (stat("/sbin/installTwrp", &st) == 0)
379 {
380 DataManager::SetValue("tw_operation", "Configuring TWRP");
381 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000382 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200383 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400384 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000385 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 }
387 }
388 }
389
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200390 // Done
391 DataManager::SetValue("ui_progress", 100);
392 DataManager::SetValue("ui_progress", 0);
393 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
that73a52952015-01-28 23:07:34 +0100396GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100397{
that73a52952015-01-28 23:07:34 +0100398 string func = gui_parse_text(action.mFunction);
399 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
400 if (needsThread) {
401 if (func == "cancelbackup")
402 return THREAD_CANCEL;
403 else
404 return THREAD_ACTION;
405 }
406 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100407}
408
Dees_Troy51a0e822012-09-05 15:24:24 -0400409int GUIAction::doActions()
410{
that3f7b1ac2014-12-30 11:30:13 +0100411 if (mActions.size() < 1)
412 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413
that73a52952015-01-28 23:07:34 +0100414 // Determine in which thread to run the actions.
415 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
416 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100417 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100418 for (it = mActions.begin(); it != mActions.end(); ++it) {
419 ThreadType tt = getThreadType(*it);
420 if (tt == THREAD_NONE)
421 continue;
422 if (threadType == THREAD_NONE)
423 threadType = tt;
424 else if (threadType != tt) {
425 LOGERR("Can't mix normal and cancel actions in the same list.\n"
426 "Running the whole batch in the cancel thread.\n");
427 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100428 break;
429 }
that7d3b54f2015-01-09 22:52:51 +0100430 }
that73a52952015-01-28 23:07:34 +0100431
432 // Now run the actions in the desired thread.
433 switch (threadType) {
434 case THREAD_ACTION:
435 action_thread.threadActions(this);
436 break;
437
438 case THREAD_CANCEL:
439 cancel_thread.threadActions(this);
440 break;
441
442 default: {
443 // no iterators here because theme reloading might kill our object
444 const size_t cnt = mActions.size();
445 for (size_t i = 0; i < cnt; ++i)
446 doAction(mActions[i]);
447 }
thatc6085482015-01-09 22:12:43 +0100448 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200450 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400451}
452
that3f7b1ac2014-12-30 11:30:13 +0100453int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400454{
that3f7b1ac2014-12-30 11:30:13 +0100455 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400456
that3f7b1ac2014-12-30 11:30:13 +0100457 std::string function = gui_parse_text(action.mFunction);
458 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
that3f7b1ac2014-12-30 11:30:13 +0100460 // find function and execute it
461 mapFunc::const_iterator funcitr = mf.find(function);
462 if (funcitr != mf.end())
463 return (this->*funcitr->second)(arg);
464
465 LOGERR("Unknown action '%s'\n", function.c_str());
466 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400467}
468
469void GUIAction::operation_start(const string operation_name)
470{
that3f7b1ac2014-12-30 11:30:13 +0100471 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000472 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 DataManager::SetValue(TW_ACTION_BUSY, 1);
474 DataManager::SetValue("ui_progress", 0);
475 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600477 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478}
479
that3f7b1ac2014-12-30 11:30:13 +0100480void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400481{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000482 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 DataManager::SetValue("ui_progress", 100);
485 if (simulate) {
486 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
487 if (simulate_fail != 0)
488 DataManager::SetValue("tw_operation_status", 1);
489 else
490 DataManager::SetValue("tw_operation_status", 0);
491 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500492 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500494 }
495 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500497 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 }
499 DataManager::SetValue("tw_operation_state", 1);
500 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500501 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000502 time(&Stop);
503 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600504 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100505 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506}
507
that3f7b1ac2014-12-30 11:30:13 +0100508int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400509{
that3f7b1ac2014-12-30 11:30:13 +0100510 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400511
that3f7b1ac2014-12-30 11:30:13 +0100512 sync();
513 DataManager::SetValue("tw_gui_done", 1);
514 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515
that3f7b1ac2014-12-30 11:30:13 +0100516 return 0;
517}
Dees_Troy51a0e822012-09-05 15:24:24 -0400518
that3f7b1ac2014-12-30 11:30:13 +0100519int GUIAction::home(std::string arg)
520{
521 PageManager::SelectPackage("TWRP");
522 gui_changePage("main");
523 return 0;
524}
Dees_Troy51a0e822012-09-05 15:24:24 -0400525
that3f7b1ac2014-12-30 11:30:13 +0100526int GUIAction::key(std::string arg)
527{
528 const int key = getKeyByName(arg);
529 PageManager::NotifyKey(key, true);
530 PageManager::NotifyKey(key, false);
531 return 0;
532}
533
534int GUIAction::page(std::string arg)
535{
536 std::string page_name = gui_parse_text(arg);
537 return gui_changePage(page_name);
538}
539
540int GUIAction::reload(std::string arg)
541{
542 int check = 0, ret_val = 0;
543 std::string theme_path;
544
that3f7b1ac2014-12-30 11:30:13 +0100545 theme_path = DataManager::GetSettingsStoragePath();
546 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
547 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
548 check = 1;
549 }
550
551 theme_path += "/TWRP/theme/ui.zip";
552 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600553 {
that3f7b1ac2014-12-30 11:30:13 +0100554 // Loading the custom theme failed - try loading the stock theme
555 LOGINFO("Attempting to reload stock theme...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000556 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 {
that3f7b1ac2014-12-30 11:30:13 +0100558 LOGERR("Failed to load base packages.\n");
559 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 }
that3f7b1ac2014-12-30 11:30:13 +0100562 return 0;
563}
Dees_Troy51a0e822012-09-05 15:24:24 -0400564
that3f7b1ac2014-12-30 11:30:13 +0100565int GUIAction::readBackup(std::string arg)
566{
567 string Restore_Name;
568 DataManager::GetValue("tw_restore", Restore_Name);
569 PartitionManager.Set_Restore_Files(Restore_Name);
570 return 0;
571}
572
573int GUIAction::set(std::string arg)
574{
575 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200576 {
that3f7b1ac2014-12-30 11:30:13 +0100577 string varName = arg.substr(0, arg.find('='));
578 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
that3f7b1ac2014-12-30 11:30:13 +0100580 DataManager::GetValue(value, value);
581 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200582 }
that3f7b1ac2014-12-30 11:30:13 +0100583 else
584 DataManager::SetValue(arg, "1");
585 return 0;
586}
Dees_Troy51a0e822012-09-05 15:24:24 -0400587
that3f7b1ac2014-12-30 11:30:13 +0100588int GUIAction::clear(std::string arg)
589{
590 DataManager::SetValue(arg, "0");
591 return 0;
592}
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
that3f7b1ac2014-12-30 11:30:13 +0100594int GUIAction::mount(std::string arg)
595{
596 if (arg == "usb") {
597 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100599 PartitionManager.usb_storage_enable();
600 else
601 gui_print("Simulating actions...\n");
602 } else if (!simulate) {
603 PartitionManager.Mount_By_Path(arg, true);
604 PartitionManager.Add_MTP_Storage(arg);
605 } else
606 gui_print("Simulating actions...\n");
607 return 0;
608}
609
610int GUIAction::unmount(std::string arg)
611{
612 if (arg == "usb") {
613 if (!simulate)
614 PartitionManager.usb_storage_disable();
615 else
616 gui_print("Simulating actions...\n");
617 DataManager::SetValue(TW_ACTION_BUSY, 0);
618 } else if (!simulate) {
619 PartitionManager.UnMount_By_Path(arg, true);
620 } else
621 gui_print("Simulating actions...\n");
622 return 0;
623}
624
625int GUIAction::restoredefaultsettings(std::string arg)
626{
627 operation_start("Restore Defaults");
628 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
629 gui_print("Simulating actions...\n");
630 else {
631 DataManager::ResetDefaults();
632 PartitionManager.Update_System_Details();
633 PartitionManager.Mount_Current_Storage(true);
634 }
635 operation_end(0);
636 return 0;
637}
638
639int GUIAction::copylog(std::string arg)
640{
641 operation_start("Copy Log");
642 if (!simulate)
643 {
644 string dst;
645 PartitionManager.Mount_Current_Storage(true);
646 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
647 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
648 tw_set_default_metadata(dst.c_str());
649 sync();
650 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
651 } else
652 simulate_progress_bar();
653 operation_end(0);
654 return 0;
655}
656
657
658int GUIAction::compute(std::string arg)
659{
660 if (arg.find("+") != string::npos)
661 {
662 string varName = arg.substr(0, arg.find('+'));
663 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
664 int amount_to_add = atoi(string_to_add.c_str());
665 int value;
666
667 DataManager::GetValue(varName, value);
668 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 return 0;
670 }
that3f7b1ac2014-12-30 11:30:13 +0100671 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400672 {
that3f7b1ac2014-12-30 11:30:13 +0100673 string varName = arg.substr(0, arg.find('-'));
674 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
675 int amount_to_subtract = atoi(string_to_subtract.c_str());
676 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400677
that3f7b1ac2014-12-30 11:30:13 +0100678 DataManager::GetValue(varName, value);
679 value -= amount_to_subtract;
680 if (value <= 0)
681 value = 0;
682 DataManager::SetValue(varName, value);
683 return 0;
684 }
685 if (arg.find("*") != string::npos)
686 {
687 string varName = arg.substr(0, arg.find('*'));
688 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
689 int multiply_by = atoi(multiply_by_str.c_str());
690 int value;
691
692 DataManager::GetValue(varName, value);
693 DataManager::SetValue(varName, value*multiply_by);
694 return 0;
695 }
696 if (arg.find("/") != string::npos)
697 {
698 string varName = arg.substr(0, arg.find('/'));
699 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
700 int divide_by = atoi(divide_by_str.c_str());
701 int value;
702
703 if(divide_by != 0)
704 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100706 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 }
708 return 0;
709 }
that3f7b1ac2014-12-30 11:30:13 +0100710 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
711 return -1;
712}
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
that3f7b1ac2014-12-30 11:30:13 +0100714int GUIAction::setguitimezone(std::string arg)
715{
716 string SelectedZone;
717 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
718 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
719 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
720
721 int dst;
722 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
723
724 string offset;
725 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
726
727 string NewTimeZone = Zone;
728 if (offset != "0")
729 NewTimeZone += ":" + offset;
730
731 if (dst != 0)
732 NewTimeZone += DSTZone;
733
734 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
735 DataManager::update_tz_environment_variables();
736 return 0;
737}
738
739int GUIAction::overlay(std::string arg)
740{
741 return gui_changeOverlay(arg);
742}
743
744int GUIAction::queuezip(std::string arg)
745{
746 if (zip_queue_index >= 10) {
747 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400748 return 0;
749 }
that3f7b1ac2014-12-30 11:30:13 +0100750 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
751 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
752 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400753 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100754 }
755 return 0;
756}
757
758int GUIAction::cancelzip(std::string arg)
759{
760 if (zip_queue_index <= 0) {
761 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100763 } else {
764 zip_queue_index--;
765 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 }
that3f7b1ac2014-12-30 11:30:13 +0100767 return 0;
768}
Dees_Troy51a0e822012-09-05 15:24:24 -0400769
that3f7b1ac2014-12-30 11:30:13 +0100770int GUIAction::queueclear(std::string arg)
771{
772 zip_queue_index = 0;
773 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
774 return 0;
775}
Dees_Troy51a0e822012-09-05 15:24:24 -0400776
that3f7b1ac2014-12-30 11:30:13 +0100777int GUIAction::sleep(std::string arg)
778{
779 operation_start("Sleep");
780 usleep(atoi(arg.c_str()));
781 operation_end(0);
782 return 0;
783}
Dees Troyb21cc642013-09-10 17:36:41 +0000784
that3f7b1ac2014-12-30 11:30:13 +0100785int GUIAction::appenddatetobackupname(std::string arg)
786{
787 operation_start("AppendDateToBackupName");
788 string Backup_Name;
789 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
790 Backup_Name += TWFunc::Get_Current_Date();
791 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
792 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
793 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
794 operation_end(0);
795 return 0;
796}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500797
that3f7b1ac2014-12-30 11:30:13 +0100798int GUIAction::generatebackupname(std::string arg)
799{
800 operation_start("GenerateBackupName");
801 TWFunc::Auto_Generate_Backup_Name();
802 operation_end(0);
803 return 0;
804}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500805
that3f7b1ac2014-12-30 11:30:13 +0100806int GUIAction::checkpartitionlist(std::string arg)
807{
808 string Wipe_List, wipe_path;
809 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500810
that3f7b1ac2014-12-30 11:30:13 +0100811 DataManager::GetValue("tw_wipe_list", Wipe_List);
812 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
813 if (!Wipe_List.empty()) {
814 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
815 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
816 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
817 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
818 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
819 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 } else {
that3f7b1ac2014-12-30 11:30:13 +0100821 count++;
822 }
823 start_pos = end_pos + 1;
824 end_pos = Wipe_List.find(";", start_pos);
825 }
826 DataManager::SetValue("tw_check_partition_list", count);
827 } else {
828 DataManager::SetValue("tw_check_partition_list", 0);
829 }
830 return 0;
831}
Dees_Troy51a0e822012-09-05 15:24:24 -0400832
that3f7b1ac2014-12-30 11:30:13 +0100833int GUIAction::getpartitiondetails(std::string arg)
834{
835 string Wipe_List, wipe_path;
836 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400837
that3f7b1ac2014-12-30 11:30:13 +0100838 DataManager::GetValue("tw_wipe_list", Wipe_List);
839 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
840 if (!Wipe_List.empty()) {
841 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
842 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
843 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
844 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
845 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
846 // Do nothing
847 } else {
848 DataManager::SetValue("tw_partition_path", wipe_path);
849 break;
850 }
851 start_pos = end_pos + 1;
852 end_pos = Wipe_List.find(";", start_pos);
853 }
854 if (!wipe_path.empty()) {
855 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
856 if (Part) {
857 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500858
that3f7b1ac2014-12-30 11:30:13 +0100859 DataManager::SetValue("tw_partition_name", Part->Display_Name);
860 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
861 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
862 DataManager::SetValue("tw_partition_size", Part->Size / mb);
863 DataManager::SetValue("tw_partition_used", Part->Used / mb);
864 DataManager::SetValue("tw_partition_free", Part->Free / mb);
865 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
866 DataManager::SetValue("tw_partition_removable", Part->Removable);
867 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
868
869 if (Part->Can_Repair())
870 DataManager::SetValue("tw_partition_can_repair", 1);
871 else
872 DataManager::SetValue("tw_partition_can_repair", 0);
873 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
874 DataManager::SetValue("tw_partition_vfat", 1);
875 else
876 DataManager::SetValue("tw_partition_vfat", 0);
877 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
878 DataManager::SetValue("tw_partition_exfat", 1);
879 else
880 DataManager::SetValue("tw_partition_exfat", 0);
881 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
882 DataManager::SetValue("tw_partition_f2fs", 1);
883 else
884 DataManager::SetValue("tw_partition_f2fs", 0);
885 if (TWFunc::Path_Exists("/sbin/mke2fs"))
886 DataManager::SetValue("tw_partition_ext", 1);
887 else
888 DataManager::SetValue("tw_partition_ext", 0);
889 return 0;
890 } else {
891 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
892 }
893 }
894 }
895 DataManager::SetValue("tw_partition_name", "");
896 DataManager::SetValue("tw_partition_file_system", "");
897 return 0;
898}
899
900int GUIAction::screenshot(std::string arg)
901{
902 time_t tm;
903 char path[256];
904 int path_len;
905 uid_t uid = -1;
906 gid_t gid = -1;
907
908 struct passwd *pwd = getpwnam("media_rw");
909 if(pwd) {
910 uid = pwd->pw_uid;
911 gid = pwd->pw_gid;
912 }
913
914 const std::string storage = DataManager::GetCurrentStoragePath();
915 if(PartitionManager.Is_Mounted_By_Path(storage)) {
916 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
917 } else {
918 strcpy(path, "/tmp/");
919 }
920
921 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
922 return 0;
923
924 tm = time(NULL);
925 path_len = strlen(path);
926
927 // Screenshot_2014-01-01-18-21-38.png
928 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
929
930 int res = gr_save_screenshot(path);
931 if(res == 0) {
932 chmod(path, 0666);
933 chown(path, uid, gid);
934
935 gui_print("Screenshot was saved to %s\n", path);
936
937 // blink to notify that the screenshow was taken
938 gr_color(255, 255, 255, 255);
939 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
940 gr_flip();
941 gui_forceRender();
942 } else {
943 LOGERR("Failed to take a screenshot!\n");
944 }
945 return 0;
946}
947
948int GUIAction::setbrightness(std::string arg)
949{
950 return TWFunc::Set_Brightness(arg);
951}
952
953int GUIAction::fileexists(std::string arg)
954{
955 struct stat st;
956 string newpath = arg + "/.";
957
958 operation_start("FileExists");
959 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
960 operation_end(0);
961 else
962 operation_end(1);
963 return 0;
964}
965
thatcc8ddca2015-01-03 01:59:36 +0100966void GUIAction::reinject_after_flash()
967{
968 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
thatcc8ddca2015-01-03 01:59:36 +0100969 gui_print("Injecting TWRP into boot image...\n");
970 if (simulate) {
971 simulate_progress_bar();
972 } else {
973 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
974 if (Boot == NULL || Boot->Current_File_System != "emmc")
975 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
976 else {
977 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
978 TWFunc::Exec_Cmd(injectcmd);
979 }
980 gui_print("TWRP injection complete.\n");
981 }
982 }
983}
984
that3f7b1ac2014-12-30 11:30:13 +0100985int GUIAction::flash(std::string arg)
986{
987 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600988 // We're going to jump to this page first, like a loading page
989 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100990 for (i=0; i<zip_queue_index; i++) {
991 operation_start("Flashing");
992 DataManager::SetValue("tw_filename", zip_queue[i]);
993 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
994
995 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600996 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100997 TWFunc::SetPerformanceMode(false);
998 if (ret_val != 0) {
999 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +01001000 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001001 break;
that3f7b1ac2014-12-30 11:30:13 +01001002 }
1003 }
1004 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001005
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001006 if (wipe_cache) {
1007 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001008 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001009 }
that3f7b1ac2014-12-30 11:30:13 +01001010
thatcc8ddca2015-01-03 01:59:36 +01001011 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001012 PartitionManager.Update_System_Details();
1013 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001014 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001015 return 0;
1016}
1017
1018int GUIAction::wipe(std::string arg)
1019{
1020 operation_start("Format");
1021 DataManager::SetValue("tw_partition", arg);
1022
1023 int ret_val = false;
1024
1025 if (simulate) {
1026 simulate_progress_bar();
1027 } else {
1028 if (arg == "data")
1029 ret_val = PartitionManager.Factory_Reset();
1030 else if (arg == "battery")
1031 ret_val = PartitionManager.Wipe_Battery_Stats();
1032 else if (arg == "rotate")
1033 ret_val = PartitionManager.Wipe_Rotate_Data();
1034 else if (arg == "dalvik")
1035 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1036 else if (arg == "DATAMEDIA") {
1037 ret_val = PartitionManager.Format_Data();
1038 } else if (arg == "INTERNAL") {
1039 int has_datamedia, dual_storage;
1040
1041 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1042 if (has_datamedia) {
1043 ret_val = PartitionManager.Wipe_Media_From_Data();
1044 } else {
1045 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1046 }
1047 } else if (arg == "EXTERNAL") {
1048 string External_Path;
1049
1050 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1051 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1052 } else if (arg == "ANDROIDSECURE") {
1053 ret_val = PartitionManager.Wipe_Android_Secure();
1054 } else if (arg == "LIST") {
1055 string Wipe_List, wipe_path;
1056 bool skip = false;
1057 ret_val = true;
1058 TWPartition* wipe_part = NULL;
1059
1060 DataManager::GetValue("tw_wipe_list", Wipe_List);
1061 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1062 if (!Wipe_List.empty()) {
1063 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1064 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1065 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1066 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1067 if (wipe_path == "/and-sec") {
1068 if (!PartitionManager.Wipe_Android_Secure()) {
1069 LOGERR("Unable to wipe android secure\n");
1070 ret_val = false;
1071 break;
1072 } else {
1073 skip = true;
1074 }
1075 } else if (wipe_path == "DALVIK") {
1076 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1077 LOGERR("Failed to wipe dalvik\n");
1078 ret_val = false;
1079 break;
1080 } else {
1081 skip = true;
1082 }
1083 } else if (wipe_path == "INTERNAL") {
1084 if (!PartitionManager.Wipe_Media_From_Data()) {
1085 ret_val = false;
1086 break;
1087 } else {
1088 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001089 }
1090 }
that3f7b1ac2014-12-30 11:30:13 +01001091 if (!skip) {
1092 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1093 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1094 ret_val = false;
1095 break;
1096 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1097 arg = wipe_path;
1098 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001099 } else {
that3f7b1ac2014-12-30 11:30:13 +01001100 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 }
that3f7b1ac2014-12-30 11:30:13 +01001102 start_pos = end_pos + 1;
1103 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001104 }
1105 }
that3f7b1ac2014-12-30 11:30:13 +01001106 } else
1107 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001108#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001109 if (arg == DataManager::GetSettingsStoragePath()) {
1110 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1111 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001112
that3f7b1ac2014-12-30 11:30:13 +01001113 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1114 LOGINFO("Making TWRP folder and saving settings.\n");
1115 Storage_Path += "/TWRP";
1116 mkdir(Storage_Path.c_str(), 0777);
1117 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 } else {
that3f7b1ac2014-12-30 11:30:13 +01001119 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1120 }
1121 }
1122#endif
1123 }
1124 PartitionManager.Update_System_Details();
1125 if (ret_val)
1126 ret_val = 0; // 0 is success
1127 else
1128 ret_val = 1; // 1 is failure
1129 operation_end(ret_val);
1130 return 0;
1131}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001132
that3f7b1ac2014-12-30 11:30:13 +01001133int GUIAction::refreshsizes(std::string arg)
1134{
1135 operation_start("Refreshing Sizes");
1136 if (simulate) {
1137 simulate_progress_bar();
1138 } else
1139 PartitionManager.Update_System_Details();
1140 operation_end(0);
1141 return 0;
1142}
1143
1144int GUIAction::nandroid(std::string arg)
1145{
that3f7b1ac2014-12-30 11:30:13 +01001146 if (simulate) {
that73a52952015-01-28 23:07:34 +01001147 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001148 DataManager::SetValue("tw_partition", "Simulation");
1149 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001150 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001151 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001152 operation_start("Nandroid");
1153 int ret = 0;
1154
that3f7b1ac2014-12-30 11:30:13 +01001155 if (arg == "backup") {
1156 string Backup_Name;
1157 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1158 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1159 ret = PartitionManager.Run_Backup();
1160 }
1161 else {
1162 operation_end(1);
1163 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001164 }
1165 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1166 } else if (arg == "restore") {
1167 string Restore_Name;
1168 DataManager::GetValue("tw_restore", Restore_Name);
1169 ret = PartitionManager.Run_Restore(Restore_Name);
1170 } else {
1171 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001172 return -1;
1173 }
1174 DataManager::SetValue("tw_encrypt_backup", 0);
1175 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001176 if (ret == false)
1177 ret = 1; // 1 for failure
1178 else
1179 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001180 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001181 }
1182 else {
1183 DataManager::SetValue("tw_cancel_backup", 1);
1184 gui_print("Backup Canceled.\n");
1185 ret = 0;
1186 }
that73a52952015-01-28 23:07:34 +01001187 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001188 return ret;
1189 }
1190 return 0;
1191}
1192
1193int GUIAction::cancelbackup(std::string arg) {
1194 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 }
1197 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 int op_status = PartitionManager.Cancel_Backup();
1199 if (op_status != 0)
1200 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 }
1202
1203 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001204}
Dees_Troy51a0e822012-09-05 15:24:24 -04001205
that3f7b1ac2014-12-30 11:30:13 +01001206int GUIAction::fixpermissions(std::string arg)
1207{
that73a52952015-01-28 23:07:34 +01001208 int op_status = 0;
1209
that3f7b1ac2014-12-30 11:30:13 +01001210 operation_start("Fix Permissions");
1211 LOGINFO("fix permissions started!\n");
1212 if (simulate) {
1213 simulate_progress_bar();
1214 } else {
that73a52952015-01-28 23:07:34 +01001215 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001216 if (op_status != 0)
1217 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001218 }
that73a52952015-01-28 23:07:34 +01001219 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001220 return 0;
1221}
1222
1223int GUIAction::dd(std::string arg)
1224{
1225 operation_start("imaging");
1226
1227 if (simulate) {
1228 simulate_progress_bar();
1229 } else {
1230 string cmd = "dd " + arg;
1231 TWFunc::Exec_Cmd(cmd);
1232 }
1233 operation_end(0);
1234 return 0;
1235}
1236
1237int GUIAction::partitionsd(std::string arg)
1238{
1239 operation_start("Partition SD Card");
1240 int ret_val = 0;
1241
1242 if (simulate) {
1243 simulate_progress_bar();
1244 } else {
1245 int allow_partition;
1246 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1247 if (allow_partition == 0) {
1248 gui_print("This device does not have a real SD Card!\nAborting!\n");
1249 } else {
1250 if (!PartitionManager.Partition_SDCard())
1251 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001252 }
that3f7b1ac2014-12-30 11:30:13 +01001253 }
1254 operation_end(ret_val);
1255 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001256
that3f7b1ac2014-12-30 11:30:13 +01001257}
Dees_Troy51a0e822012-09-05 15:24:24 -04001258
that3f7b1ac2014-12-30 11:30:13 +01001259int GUIAction::installhtcdumlock(std::string arg)
1260{
1261 operation_start("Install HTC Dumlock");
1262 if (simulate) {
1263 simulate_progress_bar();
1264 } else
1265 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001266
that3f7b1ac2014-12-30 11:30:13 +01001267 operation_end(0);
1268 return 0;
1269}
Dees_Troy51a0e822012-09-05 15:24:24 -04001270
that3f7b1ac2014-12-30 11:30:13 +01001271int GUIAction::htcdumlockrestoreboot(std::string arg)
1272{
1273 operation_start("HTC Dumlock Restore Boot");
1274 if (simulate) {
1275 simulate_progress_bar();
1276 } else
1277 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001278
that3f7b1ac2014-12-30 11:30:13 +01001279 operation_end(0);
1280 return 0;
1281}
Dees_Troy51a0e822012-09-05 15:24:24 -04001282
that3f7b1ac2014-12-30 11:30:13 +01001283int GUIAction::htcdumlockreflashrecovery(std::string arg)
1284{
1285 operation_start("HTC Dumlock Reflash Recovery");
1286 if (simulate) {
1287 simulate_progress_bar();
1288 } else
1289 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001290
that3f7b1ac2014-12-30 11:30:13 +01001291 operation_end(0);
1292 return 0;
1293}
Dees_Troy51a0e822012-09-05 15:24:24 -04001294
that3f7b1ac2014-12-30 11:30:13 +01001295int GUIAction::cmd(std::string arg)
1296{
1297 int op_status = 0;
1298
1299 operation_start("Command");
1300 LOGINFO("Running command: '%s'\n", arg.c_str());
1301 if (simulate) {
1302 simulate_progress_bar();
1303 } else {
1304 op_status = TWFunc::Exec_Cmd(arg);
1305 if (op_status != 0)
1306 op_status = 1;
1307 }
1308
1309 operation_end(op_status);
1310 return 0;
1311}
1312
1313int GUIAction::terminalcommand(std::string arg)
1314{
1315 int op_status = 0;
1316 string cmdpath, command;
1317
1318 DataManager::GetValue("tw_terminal_location", cmdpath);
1319 operation_start("CommandOutput");
1320 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1321 if (simulate) {
1322 simulate_progress_bar();
1323 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001324 } else if (arg == "exit") {
1325 LOGINFO("Exiting terminal\n");
1326 operation_end(op_status);
1327 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001328 } else {
1329 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1330 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001331 DataManager::SetValue("tw_terminal_state", 1);
1332 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001333 FILE* fp;
1334 char line[512];
1335
1336 fp = popen(command.c_str(), "r");
1337 if (fp == NULL) {
1338 LOGERR("Error opening command to run.\n");
1339 } else {
1340 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1341 struct timeval timeout;
1342 fd_set fdset;
1343
1344 while(keep_going)
1345 {
1346 FD_ZERO(&fdset);
1347 FD_SET(fd, &fdset);
1348 timeout.tv_sec = 0;
1349 timeout.tv_usec = 400000;
1350 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1351 if (has_data == 0) {
1352 // Timeout reached
1353 DataManager::GetValue("tw_terminal_state", check);
1354 if (check == 0) {
1355 keep_going = 0;
1356 }
1357 } else if (has_data < 0) {
1358 // End of execution
1359 keep_going = 0;
1360 } else {
1361 // Try to read output
1362 memset(line, 0, sizeof(line));
xiaolue738da52015-02-22 20:49:35 +08001363 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001364 gui_print("%s", line); // Display output
1365 else
1366 keep_going = 0; // Done executing
1367 }
1368 }
1369 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001370 }
thatc6085482015-01-09 22:12:43 +01001371 DataManager::SetValue("tw_operation_status", 0);
1372 DataManager::SetValue("tw_operation_state", 1);
1373 DataManager::SetValue("tw_terminal_state", 0);
1374 DataManager::SetValue("tw_background_thread_running", 0);
1375 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001376 }
1377 return 0;
1378}
1379
1380int GUIAction::killterminal(std::string arg)
1381{
1382 int op_status = 0;
1383
1384 LOGINFO("Sending kill command...\n");
1385 operation_start("KillCommand");
1386 DataManager::SetValue("tw_operation_status", 0);
1387 DataManager::SetValue("tw_operation_state", 1);
1388 DataManager::SetValue("tw_terminal_state", 0);
1389 DataManager::SetValue("tw_background_thread_running", 0);
1390 DataManager::SetValue(TW_ACTION_BUSY, 0);
1391 return 0;
1392}
1393
1394int GUIAction::reinjecttwrp(std::string arg)
1395{
1396 int op_status = 0;
1397 operation_start("ReinjectTWRP");
1398 gui_print("Injecting TWRP into boot image...\n");
1399 if (simulate) {
1400 simulate_progress_bar();
1401 } else {
1402 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1403 gui_print("TWRP injection complete.\n");
1404 }
1405
1406 operation_end(op_status);
1407 return 0;
1408}
1409
1410int GUIAction::checkbackupname(std::string arg)
1411{
1412 int op_status = 0;
1413
1414 operation_start("CheckBackupName");
1415 if (simulate) {
1416 simulate_progress_bar();
1417 } else {
1418 op_status = PartitionManager.Check_Backup_Name(true);
1419 if (op_status != 0)
1420 op_status = 1;
1421 }
1422
1423 operation_end(op_status);
1424 return 0;
1425}
1426
1427int GUIAction::decrypt(std::string arg)
1428{
1429 int op_status = 0;
1430
1431 operation_start("Decrypt");
1432 if (simulate) {
1433 simulate_progress_bar();
1434 } else {
1435 string Password;
1436 DataManager::GetValue("tw_crypto_password", Password);
1437 op_status = PartitionManager.Decrypt_Device(Password);
1438 if (op_status != 0)
1439 op_status = 1;
1440 else {
that3f7b1ac2014-12-30 11:30:13 +01001441
1442 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1443
Ethan Yonkercf50da52015-01-12 21:59:07 -06001444 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001445
Ethan Yonkercf50da52015-01-12 21:59:07 -06001446 // Check for a custom theme and load it if exists
1447 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1448 if (has_datamedia != 0) {
1449 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001450 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001451 } else {
1452 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001453 }
1454 }
1455 }
1456 }
1457
1458 operation_end(op_status);
1459 return 0;
1460}
1461
thatcc8ddca2015-01-03 01:59:36 +01001462int GUIAction::adbsideload(std::string arg)
1463{
1464 operation_start("Sideload");
1465 if (simulate) {
1466 simulate_progress_bar();
1467 operation_end(0);
1468 } else {
thatc6085482015-01-09 22:12:43 +01001469 gui_print("Starting ADB sideload feature...\n");
1470 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1471
1472 // wait for the adb connection
1473 int ret = apply_from_adb("/", &sideload_child_pid);
1474 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1475
1476 if (ret != 0) {
1477 if (ret == -2)
1478 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1479 ret = 1; // failure
1480 } else {
1481 int wipe_cache = 0;
1482 int wipe_dalvik = 0;
1483 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1484
1485 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1486 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1487 PartitionManager.Wipe_By_Path("/cache");
1488 if (wipe_dalvik)
1489 PartitionManager.Wipe_Dalvik_Cache();
1490 } else {
1491 ret = 1; // failure
1492 }
thatcc8ddca2015-01-03 01:59:36 +01001493 }
thatc6085482015-01-09 22:12:43 +01001494 if (sideload_child_pid) {
1495 LOGINFO("Signaling child sideload process to exit.\n");
1496 struct stat st;
1497 // Calling stat() on this magic filename signals the minadbd
1498 // subprocess to shut down.
1499 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1500 int status;
1501 LOGINFO("Waiting for child sideload process to exit.\n");
1502 waitpid(sideload_child_pid, &status, 0);
1503 }
1504
1505 TWFunc::Toggle_MTP(mtp_was_enabled);
1506 reinject_after_flash();
1507 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001508 }
that3f7b1ac2014-12-30 11:30:13 +01001509 return 0;
1510}
1511
1512int GUIAction::adbsideloadcancel(std::string arg)
1513{
that3f7b1ac2014-12-30 11:30:13 +01001514 struct stat st;
1515 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1516 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001517 LOGINFO("Signaling child sideload process to exit.\n");
1518 // Calling stat() on this magic filename signals the minadbd
1519 // subprocess to shut down.
1520 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1521 if (!sideload_child_pid) {
1522 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001523 return 0;
1524 }
thatcc8ddca2015-01-03 01:59:36 +01001525 ::sleep(1);
1526 LOGINFO("Killing child sideload process.\n");
1527 kill(sideload_child_pid, SIGTERM);
1528 int status;
1529 LOGINFO("Waiting for child sideload process to exit.\n");
1530 waitpid(sideload_child_pid, &status, 0);
1531 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001532 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1533 return 0;
1534}
1535
1536int GUIAction::openrecoveryscript(std::string arg)
1537{
1538 operation_start("OpenRecoveryScript");
1539 if (simulate) {
1540 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001541 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001542 } else {
thatc6085482015-01-09 22:12:43 +01001543 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1544 // that we converted to ORS commands during boot in recovery.cpp.
1545 // Run those first.
1546 int reboot = 0;
1547 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1548 gui_print("Processing AOSP recovery commands...\n");
1549 if (OpenRecoveryScript::run_script_file() == 0) {
1550 reboot = 1;
1551 }
that3f7b1ac2014-12-30 11:30:13 +01001552 }
thatc6085482015-01-09 22:12:43 +01001553 // Check for the ORS file in /cache and attempt to run those commands.
1554 if (OpenRecoveryScript::check_for_script_file()) {
1555 gui_print("Processing OpenRecoveryScript file...\n");
1556 if (OpenRecoveryScript::run_script_file() == 0) {
1557 reboot = 1;
1558 }
1559 }
1560 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001561 // Disable stock recovery reflashing
1562 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001563 usleep(2000000); // Sleep for 2 seconds before rebooting
1564 TWFunc::tw_reboot(rb_system);
1565 } else {
1566 DataManager::SetValue("tw_page_done", 1);
1567 }
1568 operation_end(1);
1569 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001570 }
1571 return 0;
1572}
1573
1574int GUIAction::installsu(std::string arg)
1575{
1576 int op_status = 0;
1577
1578 operation_start("Install SuperSU");
1579 if (simulate) {
1580 simulate_progress_bar();
1581 } else {
1582 if (!TWFunc::Install_SuperSU())
1583 op_status = 1;
1584 }
1585
1586 operation_end(op_status);
1587 return 0;
1588}
1589
1590int GUIAction::fixsu(std::string arg)
1591{
1592 int op_status = 0;
1593
1594 operation_start("Fixing Superuser Permissions");
1595 if (simulate) {
1596 simulate_progress_bar();
1597 } else {
1598 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1599 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1600 }
1601
1602 operation_end(op_status);
1603 return 0;
1604}
1605
1606int GUIAction::decrypt_backup(std::string arg)
1607{
1608 int op_status = 0;
1609
1610 operation_start("Try Restore Decrypt");
1611 if (simulate) {
1612 simulate_progress_bar();
1613 } else {
1614 string Restore_Path, Filename, Password;
1615 DataManager::GetValue("tw_restore", Restore_Path);
1616 Restore_Path += "/";
1617 DataManager::GetValue("tw_restore_password", Password);
1618 TWFunc::SetPerformanceMode(true);
1619 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1620 op_status = 0; // success
1621 else
1622 op_status = 1; // fail
1623 TWFunc::SetPerformanceMode(false);
1624 }
1625
1626 operation_end(op_status);
1627 return 0;
1628}
1629
1630int GUIAction::repair(std::string arg)
1631{
1632 int op_status = 0;
1633
1634 operation_start("Repair Partition");
1635 if (simulate) {
1636 simulate_progress_bar();
1637 } else {
1638 string part_path;
1639 DataManager::GetValue("tw_partition_mount_point", part_path);
1640 if (PartitionManager.Repair_By_Path(part_path, true)) {
1641 op_status = 0; // success
1642 } else {
1643 LOGERR("Error repairing file system.\n");
1644 op_status = 1; // fail
1645 }
1646 }
1647
1648 operation_end(op_status);
1649 return 0;
1650}
1651
1652int GUIAction::changefilesystem(std::string arg)
1653{
1654 int op_status = 0;
1655
1656 operation_start("Change File System");
1657 if (simulate) {
1658 simulate_progress_bar();
1659 } else {
1660 string part_path, file_system;
1661 DataManager::GetValue("tw_partition_mount_point", part_path);
1662 DataManager::GetValue("tw_action_new_file_system", file_system);
1663 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1664 op_status = 0; // success
1665 } else {
1666 LOGERR("Error changing file system.\n");
1667 op_status = 1; // fail
1668 }
1669 }
1670 PartitionManager.Update_System_Details();
1671 operation_end(op_status);
1672 return 0;
1673}
1674
1675int GUIAction::startmtp(std::string arg)
1676{
1677 int op_status = 0;
1678
1679 operation_start("Start MTP");
1680 if (PartitionManager.Enable_MTP())
1681 op_status = 0; // success
1682 else
1683 op_status = 1; // fail
1684
1685 operation_end(op_status);
1686 return 0;
1687}
1688
1689int GUIAction::stopmtp(std::string arg)
1690{
1691 int op_status = 0;
1692
1693 operation_start("Stop MTP");
1694 if (PartitionManager.Disable_MTP())
1695 op_status = 0; // success
1696 else
1697 op_status = 1; // fail
1698
1699 operation_end(op_status);
1700 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001701}
1702
Ethan Yonker96af84a2015-01-05 14:58:36 -06001703int GUIAction::flashimage(std::string arg)
1704{
1705 int op_status = 0;
1706
1707 operation_start("Flash Image");
1708 string path, filename, full_filename;
1709 DataManager::GetValue("tw_zip_location", path);
1710 DataManager::GetValue("tw_file", filename);
1711 full_filename = path + "/" + filename;
1712 if (PartitionManager.Flash_Image(full_filename))
1713 op_status = 0; // success
1714 else
1715 op_status = 1; // fail
1716
1717 operation_end(op_status);
1718 return 0;
1719}
1720
Dees_Troy51a0e822012-09-05 15:24:24 -04001721int GUIAction::getKeyByName(std::string key)
1722{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001723 if (key == "home") return KEY_HOME;
1724 else if (key == "menu") return KEY_MENU;
1725 else if (key == "back") return KEY_BACK;
1726 else if (key == "search") return KEY_SEARCH;
1727 else if (key == "voldown") return KEY_VOLUMEDOWN;
1728 else if (key == "volup") return KEY_VOLUMEUP;
1729 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001730 int ret_val;
1731 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1732 if (!ret_val)
1733 return KEY_POWER;
1734 else
1735 return ret_val;
1736 }
1737
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001738 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001739}