blob: 7ecd0b46a5c372dc0046e5180d84ab30fb13b1e8 [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);
226 ADD_ACTION(changefilesystem);
227 ADD_ACTION(flashimage);
that3f7b1ac2014-12-30 11:30:13 +0100228 }
229
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600231 actions = FindNode(node, "actions");
232 if (actions) child = FindNode(actions, "action");
233 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 while (child)
238 {
239 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 attr = child->first_attribute("function");
242 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 action.mFunction = attr->value();
245 action.mArg = child->value();
246 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 child = child->next_sibling("action");
249 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600252 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (child)
254 {
255 attr = child->first_attribute("key");
256 if (attr)
257 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100258 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
259 for(size_t i = 0; i < keys.size(); ++i)
260 {
261 const int key = getKeyByName(keys[i]);
262 mKeys[key] = false;
263 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 }
265 else
266 {
267 attr = child->first_attribute("x");
268 if (!attr) return;
269 mActionX = atol(attr->value());
270 attr = child->first_attribute("y");
271 if (!attr) return;
272 mActionY = atol(attr->value());
273 attr = child->first_attribute("w");
274 if (!attr) return;
275 mActionW = atol(attr->value());
276 attr = child->first_attribute("h");
277 if (!attr) return;
278 mActionH = atol(attr->value());
279 }
280 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400281}
282
283int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
284{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 if (state == TOUCH_RELEASE)
286 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289}
290
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100291int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400292{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100293 if (mKeys.empty())
294 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100296 std::map<int, bool>::iterator itr = mKeys.find(key);
297 if(itr == mKeys.end())
298 return 0;
299
300 bool prevState = itr->second;
301 itr->second = down;
302
303 // If there is only one key for this action, wait for key up so it
304 // doesn't trigger with multi-key actions.
305 // Else, check if all buttons are pressed, then consume their release events
306 // so they don't trigger one-button actions and reset mKeys pressed status
307 if(mKeys.size() == 1) {
308 if(!down && prevState)
309 doActions();
310 } else if(down) {
311 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
312 if(!itr->second)
313 return 0;
314 }
315
316 // Passed, all req buttons are pressed, reset them and consume release events
317 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
318 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
319 kb->ConsumeKeyRelease(itr->first);
320 itr->second = false;
321 }
322
323 doActions();
324 }
325
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 return 0;
327}
328
Vojtech Bocek07220562014-02-08 02:05:33 +0100329int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400330{
Vojtech Bocek07220562014-02-08 02:05:33 +0100331 GUIObject::NotifyVarChange(varName, value);
332
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100333 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100335 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400337
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341void GUIAction::simulate_progress_bar(void)
342{
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400344 for (int i = 0; i < 5; i++)
345 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500346 if (PartitionManager.stop_backup.get_value()) {
347 DataManager::SetValue("tw_cancel_backup", 1);
348 gui_print("Backup Canceled.\n");
349 DataManager::SetValue("ui_progress", 0);
350 PartitionManager.stop_backup.set_value(0);
351 return;
352 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400353 usleep(500000);
354 DataManager::SetValue("ui_progress", i * 20);
355 }
356}
357
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600358int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400359{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361
362 DataManager::SetValue("ui_progress", 0);
363
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 if (filename.empty())
365 {
366 LOGERR("No file specified.\n");
367 return -1;
368 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400371 return -1;
372
Dees_Troy51a0e822012-09-05 15:24:24 -0400373 if (simulate) {
374 simulate_progress_bar();
375 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400376 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400377
378 // Now, check if we need to ensure TWRP remains installed...
379 struct stat st;
380 if (stat("/sbin/installTwrp", &st) == 0)
381 {
382 DataManager::SetValue("tw_operation", "Configuring TWRP");
383 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000384 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200385 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000387 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400388 }
389 }
390 }
391
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 // Done
393 DataManager::SetValue("ui_progress", 100);
394 DataManager::SetValue("ui_progress", 0);
395 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
that73a52952015-01-28 23:07:34 +0100398GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100399{
that73a52952015-01-28 23:07:34 +0100400 string func = gui_parse_text(action.mFunction);
401 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
402 if (needsThread) {
403 if (func == "cancelbackup")
404 return THREAD_CANCEL;
405 else
406 return THREAD_ACTION;
407 }
408 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100409}
410
Dees_Troy51a0e822012-09-05 15:24:24 -0400411int GUIAction::doActions()
412{
that3f7b1ac2014-12-30 11:30:13 +0100413 if (mActions.size() < 1)
414 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415
that73a52952015-01-28 23:07:34 +0100416 // Determine in which thread to run the actions.
417 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
418 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100419 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100420 for (it = mActions.begin(); it != mActions.end(); ++it) {
421 ThreadType tt = getThreadType(*it);
422 if (tt == THREAD_NONE)
423 continue;
424 if (threadType == THREAD_NONE)
425 threadType = tt;
426 else if (threadType != tt) {
427 LOGERR("Can't mix normal and cancel actions in the same list.\n"
428 "Running the whole batch in the cancel thread.\n");
429 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100430 break;
431 }
that7d3b54f2015-01-09 22:52:51 +0100432 }
that73a52952015-01-28 23:07:34 +0100433
434 // Now run the actions in the desired thread.
435 switch (threadType) {
436 case THREAD_ACTION:
437 action_thread.threadActions(this);
438 break;
439
440 case THREAD_CANCEL:
441 cancel_thread.threadActions(this);
442 break;
443
444 default: {
445 // no iterators here because theme reloading might kill our object
446 const size_t cnt = mActions.size();
447 for (size_t i = 0; i < cnt; ++i)
448 doAction(mActions[i]);
449 }
thatc6085482015-01-09 22:12:43 +0100450 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400453}
454
that3f7b1ac2014-12-30 11:30:13 +0100455int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400456{
that3f7b1ac2014-12-30 11:30:13 +0100457 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
that3f7b1ac2014-12-30 11:30:13 +0100459 std::string function = gui_parse_text(action.mFunction);
460 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
that3f7b1ac2014-12-30 11:30:13 +0100462 // find function and execute it
463 mapFunc::const_iterator funcitr = mf.find(function);
464 if (funcitr != mf.end())
465 return (this->*funcitr->second)(arg);
466
467 LOGERR("Unknown action '%s'\n", function.c_str());
468 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400469}
470
471void GUIAction::operation_start(const string operation_name)
472{
that3f7b1ac2014-12-30 11:30:13 +0100473 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000474 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 DataManager::SetValue(TW_ACTION_BUSY, 1);
476 DataManager::SetValue("ui_progress", 0);
477 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600479 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
that3f7b1ac2014-12-30 11:30:13 +0100482void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400483{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000484 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 DataManager::SetValue("ui_progress", 100);
487 if (simulate) {
488 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
489 if (simulate_fail != 0)
490 DataManager::SetValue("tw_operation_status", 1);
491 else
492 DataManager::SetValue("tw_operation_status", 0);
493 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500494 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500496 }
497 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500499 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 }
501 DataManager::SetValue("tw_operation_state", 1);
502 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500503 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000504 time(&Stop);
505 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600506 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100507 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508}
509
that3f7b1ac2014-12-30 11:30:13 +0100510int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400511{
that3f7b1ac2014-12-30 11:30:13 +0100512 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400513
that3f7b1ac2014-12-30 11:30:13 +0100514 sync();
515 DataManager::SetValue("tw_gui_done", 1);
516 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400517
that3f7b1ac2014-12-30 11:30:13 +0100518 return 0;
519}
Dees_Troy51a0e822012-09-05 15:24:24 -0400520
that3f7b1ac2014-12-30 11:30:13 +0100521int GUIAction::home(std::string arg)
522{
523 PageManager::SelectPackage("TWRP");
524 gui_changePage("main");
525 return 0;
526}
Dees_Troy51a0e822012-09-05 15:24:24 -0400527
that3f7b1ac2014-12-30 11:30:13 +0100528int GUIAction::key(std::string arg)
529{
530 const int key = getKeyByName(arg);
531 PageManager::NotifyKey(key, true);
532 PageManager::NotifyKey(key, false);
533 return 0;
534}
535
536int GUIAction::page(std::string arg)
537{
538 std::string page_name = gui_parse_text(arg);
539 return gui_changePage(page_name);
540}
541
542int GUIAction::reload(std::string arg)
543{
544 int check = 0, ret_val = 0;
545 std::string theme_path;
546
that3f7b1ac2014-12-30 11:30:13 +0100547 theme_path = DataManager::GetSettingsStoragePath();
548 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
549 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
550 check = 1;
551 }
552
553 theme_path += "/TWRP/theme/ui.zip";
554 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600555 {
that3f7b1ac2014-12-30 11:30:13 +0100556 // Loading the custom theme failed - try loading the stock theme
557 LOGINFO("Attempting to reload stock theme...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000558 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400559 {
that3f7b1ac2014-12-30 11:30:13 +0100560 LOGERR("Failed to load base packages.\n");
561 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 }
that3f7b1ac2014-12-30 11:30:13 +0100564 return 0;
565}
Dees_Troy51a0e822012-09-05 15:24:24 -0400566
that3f7b1ac2014-12-30 11:30:13 +0100567int GUIAction::readBackup(std::string arg)
568{
569 string Restore_Name;
570 DataManager::GetValue("tw_restore", Restore_Name);
571 PartitionManager.Set_Restore_Files(Restore_Name);
572 return 0;
573}
574
575int GUIAction::set(std::string arg)
576{
577 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 {
that3f7b1ac2014-12-30 11:30:13 +0100579 string varName = arg.substr(0, arg.find('='));
580 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
that3f7b1ac2014-12-30 11:30:13 +0100582 DataManager::GetValue(value, value);
583 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200584 }
that3f7b1ac2014-12-30 11:30:13 +0100585 else
586 DataManager::SetValue(arg, "1");
587 return 0;
588}
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
that3f7b1ac2014-12-30 11:30:13 +0100590int GUIAction::clear(std::string arg)
591{
592 DataManager::SetValue(arg, "0");
593 return 0;
594}
Dees_Troy51a0e822012-09-05 15:24:24 -0400595
that3f7b1ac2014-12-30 11:30:13 +0100596int GUIAction::mount(std::string arg)
597{
598 if (arg == "usb") {
599 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100601 PartitionManager.usb_storage_enable();
602 else
603 gui_print("Simulating actions...\n");
604 } else if (!simulate) {
605 PartitionManager.Mount_By_Path(arg, true);
606 PartitionManager.Add_MTP_Storage(arg);
607 } else
608 gui_print("Simulating actions...\n");
609 return 0;
610}
611
612int GUIAction::unmount(std::string arg)
613{
614 if (arg == "usb") {
615 if (!simulate)
616 PartitionManager.usb_storage_disable();
617 else
618 gui_print("Simulating actions...\n");
619 DataManager::SetValue(TW_ACTION_BUSY, 0);
620 } else if (!simulate) {
621 PartitionManager.UnMount_By_Path(arg, true);
622 } else
623 gui_print("Simulating actions...\n");
624 return 0;
625}
626
627int GUIAction::restoredefaultsettings(std::string arg)
628{
629 operation_start("Restore Defaults");
630 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
631 gui_print("Simulating actions...\n");
632 else {
633 DataManager::ResetDefaults();
634 PartitionManager.Update_System_Details();
635 PartitionManager.Mount_Current_Storage(true);
636 }
637 operation_end(0);
638 return 0;
639}
640
641int GUIAction::copylog(std::string arg)
642{
643 operation_start("Copy Log");
644 if (!simulate)
645 {
646 string dst;
647 PartitionManager.Mount_Current_Storage(true);
648 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
649 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
650 tw_set_default_metadata(dst.c_str());
651 sync();
652 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
653 } else
654 simulate_progress_bar();
655 operation_end(0);
656 return 0;
657}
658
659
660int GUIAction::compute(std::string arg)
661{
662 if (arg.find("+") != string::npos)
663 {
664 string varName = arg.substr(0, arg.find('+'));
665 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
666 int amount_to_add = atoi(string_to_add.c_str());
667 int value;
668
669 DataManager::GetValue(varName, value);
670 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 return 0;
672 }
that3f7b1ac2014-12-30 11:30:13 +0100673 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 {
that3f7b1ac2014-12-30 11:30:13 +0100675 string varName = arg.substr(0, arg.find('-'));
676 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
677 int amount_to_subtract = atoi(string_to_subtract.c_str());
678 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400679
that3f7b1ac2014-12-30 11:30:13 +0100680 DataManager::GetValue(varName, value);
681 value -= amount_to_subtract;
682 if (value <= 0)
683 value = 0;
684 DataManager::SetValue(varName, value);
685 return 0;
686 }
687 if (arg.find("*") != string::npos)
688 {
689 string varName = arg.substr(0, arg.find('*'));
690 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
691 int multiply_by = atoi(multiply_by_str.c_str());
692 int value;
693
694 DataManager::GetValue(varName, value);
695 DataManager::SetValue(varName, value*multiply_by);
696 return 0;
697 }
698 if (arg.find("/") != string::npos)
699 {
700 string varName = arg.substr(0, arg.find('/'));
701 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
702 int divide_by = atoi(divide_by_str.c_str());
703 int value;
704
705 if(divide_by != 0)
706 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100708 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 }
710 return 0;
711 }
that3f7b1ac2014-12-30 11:30:13 +0100712 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
713 return -1;
714}
Dees_Troy51a0e822012-09-05 15:24:24 -0400715
that3f7b1ac2014-12-30 11:30:13 +0100716int GUIAction::setguitimezone(std::string arg)
717{
718 string SelectedZone;
719 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
720 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
721 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
722
723 int dst;
724 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
725
726 string offset;
727 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
728
729 string NewTimeZone = Zone;
730 if (offset != "0")
731 NewTimeZone += ":" + offset;
732
733 if (dst != 0)
734 NewTimeZone += DSTZone;
735
736 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
737 DataManager::update_tz_environment_variables();
738 return 0;
739}
740
741int GUIAction::overlay(std::string arg)
742{
743 return gui_changeOverlay(arg);
744}
745
746int GUIAction::queuezip(std::string arg)
747{
748 if (zip_queue_index >= 10) {
749 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 return 0;
751 }
that3f7b1ac2014-12-30 11:30:13 +0100752 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
753 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
754 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100756 }
757 return 0;
758}
759
760int GUIAction::cancelzip(std::string arg)
761{
762 if (zip_queue_index <= 0) {
763 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100765 } else {
766 zip_queue_index--;
767 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 }
that3f7b1ac2014-12-30 11:30:13 +0100769 return 0;
770}
Dees_Troy51a0e822012-09-05 15:24:24 -0400771
that3f7b1ac2014-12-30 11:30:13 +0100772int GUIAction::queueclear(std::string arg)
773{
774 zip_queue_index = 0;
775 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
776 return 0;
777}
Dees_Troy51a0e822012-09-05 15:24:24 -0400778
that3f7b1ac2014-12-30 11:30:13 +0100779int GUIAction::sleep(std::string arg)
780{
781 operation_start("Sleep");
782 usleep(atoi(arg.c_str()));
783 operation_end(0);
784 return 0;
785}
Dees Troyb21cc642013-09-10 17:36:41 +0000786
that3f7b1ac2014-12-30 11:30:13 +0100787int GUIAction::appenddatetobackupname(std::string arg)
788{
789 operation_start("AppendDateToBackupName");
790 string Backup_Name;
791 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
792 Backup_Name += TWFunc::Get_Current_Date();
793 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
794 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
795 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
796 operation_end(0);
797 return 0;
798}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500799
that3f7b1ac2014-12-30 11:30:13 +0100800int GUIAction::generatebackupname(std::string arg)
801{
802 operation_start("GenerateBackupName");
803 TWFunc::Auto_Generate_Backup_Name();
804 operation_end(0);
805 return 0;
806}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500807
that3f7b1ac2014-12-30 11:30:13 +0100808int GUIAction::checkpartitionlist(std::string arg)
809{
810 string Wipe_List, wipe_path;
811 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500812
that3f7b1ac2014-12-30 11:30:13 +0100813 DataManager::GetValue("tw_wipe_list", Wipe_List);
814 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
815 if (!Wipe_List.empty()) {
816 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
817 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
818 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
819 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
820 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
821 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400822 } else {
that3f7b1ac2014-12-30 11:30:13 +0100823 count++;
824 }
825 start_pos = end_pos + 1;
826 end_pos = Wipe_List.find(";", start_pos);
827 }
828 DataManager::SetValue("tw_check_partition_list", count);
829 } else {
830 DataManager::SetValue("tw_check_partition_list", 0);
831 }
832 return 0;
833}
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
that3f7b1ac2014-12-30 11:30:13 +0100835int GUIAction::getpartitiondetails(std::string arg)
836{
837 string Wipe_List, wipe_path;
838 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
that3f7b1ac2014-12-30 11:30:13 +0100840 DataManager::GetValue("tw_wipe_list", Wipe_List);
841 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
842 if (!Wipe_List.empty()) {
843 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
844 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
845 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
846 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
847 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
848 // Do nothing
849 } else {
850 DataManager::SetValue("tw_partition_path", wipe_path);
851 break;
852 }
853 start_pos = end_pos + 1;
854 end_pos = Wipe_List.find(";", start_pos);
855 }
856 if (!wipe_path.empty()) {
857 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
858 if (Part) {
859 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500860
that3f7b1ac2014-12-30 11:30:13 +0100861 DataManager::SetValue("tw_partition_name", Part->Display_Name);
862 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
863 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
864 DataManager::SetValue("tw_partition_size", Part->Size / mb);
865 DataManager::SetValue("tw_partition_used", Part->Used / mb);
866 DataManager::SetValue("tw_partition_free", Part->Free / mb);
867 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
868 DataManager::SetValue("tw_partition_removable", Part->Removable);
869 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
870
871 if (Part->Can_Repair())
872 DataManager::SetValue("tw_partition_can_repair", 1);
873 else
874 DataManager::SetValue("tw_partition_can_repair", 0);
875 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
876 DataManager::SetValue("tw_partition_vfat", 1);
877 else
878 DataManager::SetValue("tw_partition_vfat", 0);
879 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
880 DataManager::SetValue("tw_partition_exfat", 1);
881 else
882 DataManager::SetValue("tw_partition_exfat", 0);
883 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
884 DataManager::SetValue("tw_partition_f2fs", 1);
885 else
886 DataManager::SetValue("tw_partition_f2fs", 0);
887 if (TWFunc::Path_Exists("/sbin/mke2fs"))
888 DataManager::SetValue("tw_partition_ext", 1);
889 else
890 DataManager::SetValue("tw_partition_ext", 0);
891 return 0;
892 } else {
893 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
894 }
895 }
896 }
897 DataManager::SetValue("tw_partition_name", "");
898 DataManager::SetValue("tw_partition_file_system", "");
899 return 0;
900}
901
902int GUIAction::screenshot(std::string arg)
903{
904 time_t tm;
905 char path[256];
906 int path_len;
907 uid_t uid = -1;
908 gid_t gid = -1;
909
910 struct passwd *pwd = getpwnam("media_rw");
911 if(pwd) {
912 uid = pwd->pw_uid;
913 gid = pwd->pw_gid;
914 }
915
916 const std::string storage = DataManager::GetCurrentStoragePath();
917 if(PartitionManager.Is_Mounted_By_Path(storage)) {
918 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
919 } else {
920 strcpy(path, "/tmp/");
921 }
922
923 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
924 return 0;
925
926 tm = time(NULL);
927 path_len = strlen(path);
928
929 // Screenshot_2014-01-01-18-21-38.png
930 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
931
932 int res = gr_save_screenshot(path);
933 if(res == 0) {
934 chmod(path, 0666);
935 chown(path, uid, gid);
936
937 gui_print("Screenshot was saved to %s\n", path);
938
939 // blink to notify that the screenshow was taken
940 gr_color(255, 255, 255, 255);
941 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
942 gr_flip();
943 gui_forceRender();
944 } else {
945 LOGERR("Failed to take a screenshot!\n");
946 }
947 return 0;
948}
949
950int GUIAction::setbrightness(std::string arg)
951{
952 return TWFunc::Set_Brightness(arg);
953}
954
955int GUIAction::fileexists(std::string arg)
956{
957 struct stat st;
958 string newpath = arg + "/.";
959
960 operation_start("FileExists");
961 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
962 operation_end(0);
963 else
964 operation_end(1);
965 return 0;
966}
967
thatcc8ddca2015-01-03 01:59:36 +0100968void GUIAction::reinject_after_flash()
969{
970 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
thatcc8ddca2015-01-03 01:59:36 +0100971 gui_print("Injecting TWRP into boot image...\n");
972 if (simulate) {
973 simulate_progress_bar();
974 } else {
975 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
976 if (Boot == NULL || Boot->Current_File_System != "emmc")
977 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
978 else {
979 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
980 TWFunc::Exec_Cmd(injectcmd);
981 }
982 gui_print("TWRP injection complete.\n");
983 }
984 }
985}
986
that3f7b1ac2014-12-30 11:30:13 +0100987int GUIAction::flash(std::string arg)
988{
989 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600990 // We're going to jump to this page first, like a loading page
991 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100992 for (i=0; i<zip_queue_index; i++) {
993 operation_start("Flashing");
994 DataManager::SetValue("tw_filename", zip_queue[i]);
995 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
996
997 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600998 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100999 TWFunc::SetPerformanceMode(false);
1000 if (ret_val != 0) {
1001 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +01001002 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001003 break;
that3f7b1ac2014-12-30 11:30:13 +01001004 }
1005 }
1006 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001007
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001008 if (wipe_cache) {
1009 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001010 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001011 }
that3f7b1ac2014-12-30 11:30:13 +01001012
thatcc8ddca2015-01-03 01:59:36 +01001013 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001014 PartitionManager.Update_System_Details();
1015 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001016 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001017 return 0;
1018}
1019
1020int GUIAction::wipe(std::string arg)
1021{
1022 operation_start("Format");
1023 DataManager::SetValue("tw_partition", arg);
1024
1025 int ret_val = false;
1026
1027 if (simulate) {
1028 simulate_progress_bar();
1029 } else {
1030 if (arg == "data")
1031 ret_val = PartitionManager.Factory_Reset();
1032 else if (arg == "battery")
1033 ret_val = PartitionManager.Wipe_Battery_Stats();
1034 else if (arg == "rotate")
1035 ret_val = PartitionManager.Wipe_Rotate_Data();
1036 else if (arg == "dalvik")
1037 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1038 else if (arg == "DATAMEDIA") {
1039 ret_val = PartitionManager.Format_Data();
1040 } else if (arg == "INTERNAL") {
1041 int has_datamedia, dual_storage;
1042
1043 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1044 if (has_datamedia) {
1045 ret_val = PartitionManager.Wipe_Media_From_Data();
1046 } else {
1047 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1048 }
1049 } else if (arg == "EXTERNAL") {
1050 string External_Path;
1051
1052 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1053 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1054 } else if (arg == "ANDROIDSECURE") {
1055 ret_val = PartitionManager.Wipe_Android_Secure();
1056 } else if (arg == "LIST") {
1057 string Wipe_List, wipe_path;
1058 bool skip = false;
1059 ret_val = true;
1060 TWPartition* wipe_part = NULL;
1061
1062 DataManager::GetValue("tw_wipe_list", Wipe_List);
1063 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1064 if (!Wipe_List.empty()) {
1065 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1066 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1067 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1068 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1069 if (wipe_path == "/and-sec") {
1070 if (!PartitionManager.Wipe_Android_Secure()) {
1071 LOGERR("Unable to wipe android secure\n");
1072 ret_val = false;
1073 break;
1074 } else {
1075 skip = true;
1076 }
1077 } else if (wipe_path == "DALVIK") {
1078 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1079 LOGERR("Failed to wipe dalvik\n");
1080 ret_val = false;
1081 break;
1082 } else {
1083 skip = true;
1084 }
1085 } else if (wipe_path == "INTERNAL") {
1086 if (!PartitionManager.Wipe_Media_From_Data()) {
1087 ret_val = false;
1088 break;
1089 } else {
1090 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001091 }
1092 }
that3f7b1ac2014-12-30 11:30:13 +01001093 if (!skip) {
1094 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1095 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1096 ret_val = false;
1097 break;
1098 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1099 arg = wipe_path;
1100 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 } else {
that3f7b1ac2014-12-30 11:30:13 +01001102 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001103 }
that3f7b1ac2014-12-30 11:30:13 +01001104 start_pos = end_pos + 1;
1105 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001106 }
1107 }
that3f7b1ac2014-12-30 11:30:13 +01001108 } else
1109 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001110#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001111 if (arg == DataManager::GetSettingsStoragePath()) {
1112 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1113 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001114
that3f7b1ac2014-12-30 11:30:13 +01001115 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1116 LOGINFO("Making TWRP folder and saving settings.\n");
1117 Storage_Path += "/TWRP";
1118 mkdir(Storage_Path.c_str(), 0777);
1119 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001120 } else {
that3f7b1ac2014-12-30 11:30:13 +01001121 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1122 }
1123 }
1124#endif
1125 }
1126 PartitionManager.Update_System_Details();
1127 if (ret_val)
1128 ret_val = 0; // 0 is success
1129 else
1130 ret_val = 1; // 1 is failure
1131 operation_end(ret_val);
1132 return 0;
1133}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001134
that3f7b1ac2014-12-30 11:30:13 +01001135int GUIAction::refreshsizes(std::string arg)
1136{
1137 operation_start("Refreshing Sizes");
1138 if (simulate) {
1139 simulate_progress_bar();
1140 } else
1141 PartitionManager.Update_System_Details();
1142 operation_end(0);
1143 return 0;
1144}
1145
1146int GUIAction::nandroid(std::string arg)
1147{
that3f7b1ac2014-12-30 11:30:13 +01001148 if (simulate) {
that73a52952015-01-28 23:07:34 +01001149 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001150 DataManager::SetValue("tw_partition", "Simulation");
1151 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001152 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001153 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001154 operation_start("Nandroid");
1155 int ret = 0;
1156
that3f7b1ac2014-12-30 11:30:13 +01001157 if (arg == "backup") {
1158 string Backup_Name;
1159 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1160 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1161 ret = PartitionManager.Run_Backup();
1162 }
1163 else {
1164 operation_end(1);
1165 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001166 }
1167 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1168 } else if (arg == "restore") {
1169 string Restore_Name;
1170 DataManager::GetValue("tw_restore", Restore_Name);
1171 ret = PartitionManager.Run_Restore(Restore_Name);
1172 } else {
1173 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001174 return -1;
1175 }
1176 DataManager::SetValue("tw_encrypt_backup", 0);
1177 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001178 if (ret == false)
1179 ret = 1; // 1 for failure
1180 else
1181 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001182 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001183 }
1184 else {
1185 DataManager::SetValue("tw_cancel_backup", 1);
1186 gui_print("Backup Canceled.\n");
1187 ret = 0;
1188 }
that73a52952015-01-28 23:07:34 +01001189 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001190 return ret;
1191 }
1192 return 0;
1193}
1194
1195int GUIAction::cancelbackup(std::string arg) {
1196 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 }
1199 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001200 int op_status = PartitionManager.Cancel_Backup();
1201 if (op_status != 0)
1202 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001203 }
1204
1205 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001206}
Dees_Troy51a0e822012-09-05 15:24:24 -04001207
that3f7b1ac2014-12-30 11:30:13 +01001208int GUIAction::fixpermissions(std::string arg)
1209{
that73a52952015-01-28 23:07:34 +01001210 int op_status = 0;
1211
that3f7b1ac2014-12-30 11:30:13 +01001212 operation_start("Fix Permissions");
1213 LOGINFO("fix permissions started!\n");
1214 if (simulate) {
1215 simulate_progress_bar();
1216 } else {
that73a52952015-01-28 23:07:34 +01001217 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001218 if (op_status != 0)
1219 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001220 }
that73a52952015-01-28 23:07:34 +01001221 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001222 return 0;
1223}
1224
1225int GUIAction::dd(std::string arg)
1226{
1227 operation_start("imaging");
1228
1229 if (simulate) {
1230 simulate_progress_bar();
1231 } else {
1232 string cmd = "dd " + arg;
1233 TWFunc::Exec_Cmd(cmd);
1234 }
1235 operation_end(0);
1236 return 0;
1237}
1238
1239int GUIAction::partitionsd(std::string arg)
1240{
1241 operation_start("Partition SD Card");
1242 int ret_val = 0;
1243
1244 if (simulate) {
1245 simulate_progress_bar();
1246 } else {
1247 int allow_partition;
1248 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1249 if (allow_partition == 0) {
1250 gui_print("This device does not have a real SD Card!\nAborting!\n");
1251 } else {
1252 if (!PartitionManager.Partition_SDCard())
1253 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001254 }
that3f7b1ac2014-12-30 11:30:13 +01001255 }
1256 operation_end(ret_val);
1257 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001258
that3f7b1ac2014-12-30 11:30:13 +01001259}
Dees_Troy51a0e822012-09-05 15:24:24 -04001260
that3f7b1ac2014-12-30 11:30:13 +01001261int GUIAction::installhtcdumlock(std::string arg)
1262{
1263 operation_start("Install HTC Dumlock");
1264 if (simulate) {
1265 simulate_progress_bar();
1266 } else
1267 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001268
that3f7b1ac2014-12-30 11:30:13 +01001269 operation_end(0);
1270 return 0;
1271}
Dees_Troy51a0e822012-09-05 15:24:24 -04001272
that3f7b1ac2014-12-30 11:30:13 +01001273int GUIAction::htcdumlockrestoreboot(std::string arg)
1274{
1275 operation_start("HTC Dumlock Restore Boot");
1276 if (simulate) {
1277 simulate_progress_bar();
1278 } else
1279 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001280
that3f7b1ac2014-12-30 11:30:13 +01001281 operation_end(0);
1282 return 0;
1283}
Dees_Troy51a0e822012-09-05 15:24:24 -04001284
that3f7b1ac2014-12-30 11:30:13 +01001285int GUIAction::htcdumlockreflashrecovery(std::string arg)
1286{
1287 operation_start("HTC Dumlock Reflash Recovery");
1288 if (simulate) {
1289 simulate_progress_bar();
1290 } else
1291 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001292
that3f7b1ac2014-12-30 11:30:13 +01001293 operation_end(0);
1294 return 0;
1295}
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
that3f7b1ac2014-12-30 11:30:13 +01001297int GUIAction::cmd(std::string arg)
1298{
1299 int op_status = 0;
1300
1301 operation_start("Command");
1302 LOGINFO("Running command: '%s'\n", arg.c_str());
1303 if (simulate) {
1304 simulate_progress_bar();
1305 } else {
1306 op_status = TWFunc::Exec_Cmd(arg);
1307 if (op_status != 0)
1308 op_status = 1;
1309 }
1310
1311 operation_end(op_status);
1312 return 0;
1313}
1314
1315int GUIAction::terminalcommand(std::string arg)
1316{
1317 int op_status = 0;
1318 string cmdpath, command;
1319
1320 DataManager::GetValue("tw_terminal_location", cmdpath);
1321 operation_start("CommandOutput");
1322 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1323 if (simulate) {
1324 simulate_progress_bar();
1325 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001326 } else if (arg == "exit") {
1327 LOGINFO("Exiting terminal\n");
1328 operation_end(op_status);
1329 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001330 } else {
1331 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1332 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001333 DataManager::SetValue("tw_terminal_state", 1);
1334 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001335 FILE* fp;
1336 char line[512];
1337
1338 fp = popen(command.c_str(), "r");
1339 if (fp == NULL) {
1340 LOGERR("Error opening command to run.\n");
1341 } else {
1342 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1343 struct timeval timeout;
1344 fd_set fdset;
1345
1346 while(keep_going)
1347 {
1348 FD_ZERO(&fdset);
1349 FD_SET(fd, &fdset);
1350 timeout.tv_sec = 0;
1351 timeout.tv_usec = 400000;
1352 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1353 if (has_data == 0) {
1354 // Timeout reached
1355 DataManager::GetValue("tw_terminal_state", check);
1356 if (check == 0) {
1357 keep_going = 0;
1358 }
1359 } else if (has_data < 0) {
1360 // End of execution
1361 keep_going = 0;
1362 } else {
1363 // Try to read output
1364 memset(line, 0, sizeof(line));
xiaolue738da52015-02-22 20:49:35 +08001365 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001366 gui_print("%s", line); // Display output
1367 else
1368 keep_going = 0; // Done executing
1369 }
1370 }
1371 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001372 }
thatc6085482015-01-09 22:12:43 +01001373 DataManager::SetValue("tw_operation_status", 0);
1374 DataManager::SetValue("tw_operation_state", 1);
1375 DataManager::SetValue("tw_terminal_state", 0);
1376 DataManager::SetValue("tw_background_thread_running", 0);
1377 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001378 }
1379 return 0;
1380}
1381
1382int GUIAction::killterminal(std::string arg)
1383{
1384 int op_status = 0;
1385
1386 LOGINFO("Sending kill command...\n");
1387 operation_start("KillCommand");
1388 DataManager::SetValue("tw_operation_status", 0);
1389 DataManager::SetValue("tw_operation_state", 1);
1390 DataManager::SetValue("tw_terminal_state", 0);
1391 DataManager::SetValue("tw_background_thread_running", 0);
1392 DataManager::SetValue(TW_ACTION_BUSY, 0);
1393 return 0;
1394}
1395
1396int GUIAction::reinjecttwrp(std::string arg)
1397{
1398 int op_status = 0;
1399 operation_start("ReinjectTWRP");
1400 gui_print("Injecting TWRP into boot image...\n");
1401 if (simulate) {
1402 simulate_progress_bar();
1403 } else {
1404 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1405 gui_print("TWRP injection complete.\n");
1406 }
1407
1408 operation_end(op_status);
1409 return 0;
1410}
1411
1412int GUIAction::checkbackupname(std::string arg)
1413{
1414 int op_status = 0;
1415
1416 operation_start("CheckBackupName");
1417 if (simulate) {
1418 simulate_progress_bar();
1419 } else {
1420 op_status = PartitionManager.Check_Backup_Name(true);
1421 if (op_status != 0)
1422 op_status = 1;
1423 }
1424
1425 operation_end(op_status);
1426 return 0;
1427}
1428
1429int GUIAction::decrypt(std::string arg)
1430{
1431 int op_status = 0;
1432
1433 operation_start("Decrypt");
1434 if (simulate) {
1435 simulate_progress_bar();
1436 } else {
1437 string Password;
1438 DataManager::GetValue("tw_crypto_password", Password);
1439 op_status = PartitionManager.Decrypt_Device(Password);
1440 if (op_status != 0)
1441 op_status = 1;
1442 else {
that3f7b1ac2014-12-30 11:30:13 +01001443
1444 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1445
Ethan Yonkercf50da52015-01-12 21:59:07 -06001446 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001447
Ethan Yonkercf50da52015-01-12 21:59:07 -06001448 // Check for a custom theme and load it if exists
1449 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1450 if (has_datamedia != 0) {
1451 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001452 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001453 } else {
1454 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001455 }
1456 }
1457 }
1458 }
1459
1460 operation_end(op_status);
1461 return 0;
1462}
1463
thatcc8ddca2015-01-03 01:59:36 +01001464int GUIAction::adbsideload(std::string arg)
1465{
1466 operation_start("Sideload");
1467 if (simulate) {
1468 simulate_progress_bar();
1469 operation_end(0);
1470 } else {
thatc6085482015-01-09 22:12:43 +01001471 gui_print("Starting ADB sideload feature...\n");
1472 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1473
1474 // wait for the adb connection
1475 int ret = apply_from_adb("/", &sideload_child_pid);
1476 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1477
1478 if (ret != 0) {
1479 if (ret == -2)
1480 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1481 ret = 1; // failure
1482 } else {
1483 int wipe_cache = 0;
1484 int wipe_dalvik = 0;
1485 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1486
1487 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1488 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1489 PartitionManager.Wipe_By_Path("/cache");
1490 if (wipe_dalvik)
1491 PartitionManager.Wipe_Dalvik_Cache();
1492 } else {
1493 ret = 1; // failure
1494 }
thatcc8ddca2015-01-03 01:59:36 +01001495 }
thatc6085482015-01-09 22:12:43 +01001496 if (sideload_child_pid) {
1497 LOGINFO("Signaling child sideload process to exit.\n");
1498 struct stat st;
1499 // Calling stat() on this magic filename signals the minadbd
1500 // subprocess to shut down.
1501 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1502 int status;
1503 LOGINFO("Waiting for child sideload process to exit.\n");
1504 waitpid(sideload_child_pid, &status, 0);
1505 }
1506
1507 TWFunc::Toggle_MTP(mtp_was_enabled);
1508 reinject_after_flash();
1509 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001510 }
that3f7b1ac2014-12-30 11:30:13 +01001511 return 0;
1512}
1513
1514int GUIAction::adbsideloadcancel(std::string arg)
1515{
that3f7b1ac2014-12-30 11:30:13 +01001516 struct stat st;
1517 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1518 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001519 LOGINFO("Signaling child sideload process to exit.\n");
1520 // Calling stat() on this magic filename signals the minadbd
1521 // subprocess to shut down.
1522 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1523 if (!sideload_child_pid) {
1524 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001525 return 0;
1526 }
thatcc8ddca2015-01-03 01:59:36 +01001527 ::sleep(1);
1528 LOGINFO("Killing child sideload process.\n");
1529 kill(sideload_child_pid, SIGTERM);
1530 int status;
1531 LOGINFO("Waiting for child sideload process to exit.\n");
1532 waitpid(sideload_child_pid, &status, 0);
1533 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001534 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1535 return 0;
1536}
1537
1538int GUIAction::openrecoveryscript(std::string arg)
1539{
1540 operation_start("OpenRecoveryScript");
1541 if (simulate) {
1542 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001543 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001544 } else {
thatc6085482015-01-09 22:12:43 +01001545 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1546 // that we converted to ORS commands during boot in recovery.cpp.
1547 // Run those first.
1548 int reboot = 0;
1549 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1550 gui_print("Processing AOSP recovery commands...\n");
1551 if (OpenRecoveryScript::run_script_file() == 0) {
1552 reboot = 1;
1553 }
that3f7b1ac2014-12-30 11:30:13 +01001554 }
thatc6085482015-01-09 22:12:43 +01001555 // Check for the ORS file in /cache and attempt to run those commands.
1556 if (OpenRecoveryScript::check_for_script_file()) {
1557 gui_print("Processing OpenRecoveryScript file...\n");
1558 if (OpenRecoveryScript::run_script_file() == 0) {
1559 reboot = 1;
1560 }
1561 }
1562 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001563 // Disable stock recovery reflashing
1564 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001565 usleep(2000000); // Sleep for 2 seconds before rebooting
1566 TWFunc::tw_reboot(rb_system);
1567 } else {
1568 DataManager::SetValue("tw_page_done", 1);
1569 }
1570 operation_end(1);
1571 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001572 }
1573 return 0;
1574}
1575
1576int GUIAction::installsu(std::string arg)
1577{
1578 int op_status = 0;
1579
1580 operation_start("Install SuperSU");
1581 if (simulate) {
1582 simulate_progress_bar();
1583 } else {
1584 if (!TWFunc::Install_SuperSU())
1585 op_status = 1;
1586 }
1587
1588 operation_end(op_status);
1589 return 0;
1590}
1591
1592int GUIAction::fixsu(std::string arg)
1593{
1594 int op_status = 0;
1595
1596 operation_start("Fixing Superuser Permissions");
1597 if (simulate) {
1598 simulate_progress_bar();
1599 } else {
1600 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1601 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1602 }
1603
1604 operation_end(op_status);
1605 return 0;
1606}
1607
1608int GUIAction::decrypt_backup(std::string arg)
1609{
1610 int op_status = 0;
1611
1612 operation_start("Try Restore Decrypt");
1613 if (simulate) {
1614 simulate_progress_bar();
1615 } else {
1616 string Restore_Path, Filename, Password;
1617 DataManager::GetValue("tw_restore", Restore_Path);
1618 Restore_Path += "/";
1619 DataManager::GetValue("tw_restore_password", Password);
1620 TWFunc::SetPerformanceMode(true);
1621 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1622 op_status = 0; // success
1623 else
1624 op_status = 1; // fail
1625 TWFunc::SetPerformanceMode(false);
1626 }
1627
1628 operation_end(op_status);
1629 return 0;
1630}
1631
1632int GUIAction::repair(std::string arg)
1633{
1634 int op_status = 0;
1635
1636 operation_start("Repair Partition");
1637 if (simulate) {
1638 simulate_progress_bar();
1639 } else {
1640 string part_path;
1641 DataManager::GetValue("tw_partition_mount_point", part_path);
1642 if (PartitionManager.Repair_By_Path(part_path, true)) {
1643 op_status = 0; // success
1644 } else {
1645 LOGERR("Error repairing file system.\n");
1646 op_status = 1; // fail
1647 }
1648 }
1649
1650 operation_end(op_status);
1651 return 0;
1652}
1653
1654int GUIAction::changefilesystem(std::string arg)
1655{
1656 int op_status = 0;
1657
1658 operation_start("Change File System");
1659 if (simulate) {
1660 simulate_progress_bar();
1661 } else {
1662 string part_path, file_system;
1663 DataManager::GetValue("tw_partition_mount_point", part_path);
1664 DataManager::GetValue("tw_action_new_file_system", file_system);
1665 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1666 op_status = 0; // success
1667 } else {
1668 LOGERR("Error changing file system.\n");
1669 op_status = 1; // fail
1670 }
1671 }
1672 PartitionManager.Update_System_Details();
1673 operation_end(op_status);
1674 return 0;
1675}
1676
1677int GUIAction::startmtp(std::string arg)
1678{
1679 int op_status = 0;
1680
1681 operation_start("Start MTP");
1682 if (PartitionManager.Enable_MTP())
1683 op_status = 0; // success
1684 else
1685 op_status = 1; // fail
1686
1687 operation_end(op_status);
1688 return 0;
1689}
1690
1691int GUIAction::stopmtp(std::string arg)
1692{
1693 int op_status = 0;
1694
1695 operation_start("Stop MTP");
1696 if (PartitionManager.Disable_MTP())
1697 op_status = 0; // success
1698 else
1699 op_status = 1; // fail
1700
1701 operation_end(op_status);
1702 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001703}
1704
Ethan Yonker96af84a2015-01-05 14:58:36 -06001705int GUIAction::flashimage(std::string arg)
1706{
1707 int op_status = 0;
1708
1709 operation_start("Flash Image");
1710 string path, filename, full_filename;
1711 DataManager::GetValue("tw_zip_location", path);
1712 DataManager::GetValue("tw_file", filename);
1713 full_filename = path + "/" + filename;
1714 if (PartitionManager.Flash_Image(full_filename))
1715 op_status = 0; // success
1716 else
1717 op_status = 1; // fail
1718
1719 operation_end(op_status);
1720 return 0;
1721}
1722
Dees_Troy51a0e822012-09-05 15:24:24 -04001723int GUIAction::getKeyByName(std::string key)
1724{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001725 if (key == "home") return KEY_HOME;
1726 else if (key == "menu") return KEY_MENU;
1727 else if (key == "back") return KEY_BACK;
1728 else if (key == "search") return KEY_SEARCH;
1729 else if (key == "voldown") return KEY_VOLUMEDOWN;
1730 else if (key == "volup") return KEY_VOLUMEUP;
1731 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001732 int ret_val;
1733 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1734 if (!ret_val)
1735 return KEY_POWER;
1736 else
1737 return ret_val;
1738 }
1739
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001740 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001741}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001742
1743int GUIAction::checkpartitionlifetimewrites(std::string arg)
1744{
1745 int op_status = 0;
1746 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1747
1748 operation_start("Check Partition Lifetime Writes");
1749 if (sys) {
1750 if (sys->Check_Lifetime_Writes() != 0)
1751 DataManager::SetValue("tw_lifetime_writes", 1);
1752 else
1753 DataManager::SetValue("tw_lifetime_writes", 0);
1754 op_status = 0; // success
1755 } else {
1756 DataManager::SetValue("tw_lifetime_writes", 1);
1757 op_status = 1; // fail
1758 }
1759
1760 operation_end(op_status);
1761 return 0;
1762}
1763
1764int GUIAction::mountsystemtoggle(std::string arg)
1765{
1766 int op_status = 0;
1767 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
1768
1769 operation_start("Toggle System Mount");
1770 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1771 op_status = 1; // fail
1772 } else {
1773 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1774 if (Part) {
1775 if (DataManager::GetIntValue("tw_mount_system_ro")) {
1776 DataManager::SetValue("tw_mount_system_ro", 0);
1777 Part->Change_Mount_Read_Only(false);
1778 } else {
1779 DataManager::SetValue("tw_mount_system_ro", 1);
1780 Part->Change_Mount_Read_Only(true);
1781 }
1782 if (remount_system) {
1783 Part->Mount(true);
1784 }
1785 op_status = 0; // success
1786 } else {
1787 op_status = 1; // fail
1788 }
1789 }
1790
1791 operation_end(op_status);
1792 return 0;
1793}