blob: 240db8f2419f3b0c32d3efd76cc8fb48096269f7 [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"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.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"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060053#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
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);
Ethan Yonker74db1572015-10-28 12:44:49 -0500201 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100202
203 // remember actions that run in the caller thread
204 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
205 setActionsRunningInCallerThread.insert(it->first);
206
207 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100208 ADD_ACTION(flash);
209 ADD_ACTION(wipe);
210 ADD_ACTION(refreshsizes);
211 ADD_ACTION(nandroid);
212 ADD_ACTION(fixpermissions);
213 ADD_ACTION(dd);
214 ADD_ACTION(partitionsd);
215 ADD_ACTION(installhtcdumlock);
216 ADD_ACTION(htcdumlockrestoreboot);
217 ADD_ACTION(htcdumlockreflashrecovery);
218 ADD_ACTION(cmd);
219 ADD_ACTION(terminalcommand);
220 ADD_ACTION(reinjecttwrp);
221 ADD_ACTION(decrypt);
222 ADD_ACTION(adbsideload);
223 ADD_ACTION(openrecoveryscript);
224 ADD_ACTION(installsu);
225 ADD_ACTION(decrypt_backup);
226 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500227 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100228 ADD_ACTION(changefilesystem);
229 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100230 ADD_ACTION(twcmd);
that3f7b1ac2014-12-30 11:30:13 +0100231 }
232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200233 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600234 actions = FindNode(node, "actions");
235 if (actions) child = FindNode(actions, "action");
236 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 while (child)
241 {
242 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 attr = child->first_attribute("function");
245 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 action.mFunction = attr->value();
248 action.mArg = child->value();
249 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 child = child->next_sibling("action");
252 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600255 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (child)
257 {
258 attr = child->first_attribute("key");
259 if (attr)
260 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100261 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
262 for(size_t i = 0; i < keys.size(); ++i)
263 {
264 const int key = getKeyByName(keys[i]);
265 mKeys[key] = false;
266 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 }
268 else
269 {
270 attr = child->first_attribute("x");
271 if (!attr) return;
272 mActionX = atol(attr->value());
273 attr = child->first_attribute("y");
274 if (!attr) return;
275 mActionY = atol(attr->value());
276 attr = child->first_attribute("w");
277 if (!attr) return;
278 mActionW = atol(attr->value());
279 attr = child->first_attribute("h");
280 if (!attr) return;
281 mActionH = atol(attr->value());
282 }
283 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400284}
285
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500286int GUIAction::NotifyTouch(TOUCH_STATE state __unused, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400287{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 if (state == TOUCH_RELEASE)
289 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400290
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100294int GUIAction::NotifyKey(int key, bool down)
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())
that8834a0f2016-01-05 23:29:30 +0100298 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100299
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) {
thatd4725ca2016-01-19 00:15:21 +0100308 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100309 doActions();
thatd4725ca2016-01-19 00:15:21 +0100310 return 0;
311 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312 } else if(down) {
313 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
314 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100315 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100316 }
317
318 // Passed, all req buttons are pressed, reset them and consume release events
319 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
320 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
321 kb->ConsumeKeyRelease(itr->first);
322 itr->second = false;
323 }
324
325 doActions();
thatd4725ca2016-01-19 00:15:21 +0100326 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100327 }
328
thatd4725ca2016-01-19 00:15:21 +0100329 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400330}
331
Vojtech Bocek07220562014-02-08 02:05:33 +0100332int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400333{
Vojtech Bocek07220562014-02-08 02:05:33 +0100334 GUIObject::NotifyVarChange(varName, value);
335
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100336 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100338 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400340
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200341 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
344void GUIAction::simulate_progress_bar(void)
345{
Ethan Yonker74db1572015-10-28 12:44:49 -0500346 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400347 for (int i = 0; i < 5; i++)
348 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500349 if (PartitionManager.stop_backup.get_value()) {
350 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600351 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500352 DataManager::SetValue("ui_progress", 0);
353 PartitionManager.stop_backup.set_value(0);
354 return;
355 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 usleep(500000);
357 DataManager::SetValue("ui_progress", i * 20);
358 }
359}
360
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600361int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400362{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
365 DataManager::SetValue("ui_progress", 0);
366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 if (filename.empty())
368 {
369 LOGERR("No file specified.\n");
370 return -1;
371 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
Ethan Yonker9598c072016-01-15 21:54:34 -0600373 if (!TWFunc::Path_Exists(filename)) {
374 if (!PartitionManager.Mount_By_Path(filename, true)) {
375 return -1;
376 }
377 if (!TWFunc::Path_Exists(filename)) {
378 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
379 return -1;
380 }
381 }
Dees_Troy657c3092012-09-10 20:32:10 -0400382
Dees_Troy51a0e822012-09-05 15:24:24 -0400383 if (simulate) {
384 simulate_progress_bar();
385 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400386 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400387
388 // Now, check if we need to ensure TWRP remains installed...
389 struct stat st;
390 if (stat("/sbin/installTwrp", &st) == 0)
391 {
392 DataManager::SetValue("tw_operation", "Configuring TWRP");
393 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500394 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200395 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400396 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500397 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 }
399 }
400 }
401
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 // Done
403 DataManager::SetValue("ui_progress", 100);
404 DataManager::SetValue("ui_progress", 0);
405 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400406}
407
that73a52952015-01-28 23:07:34 +0100408GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100409{
that73a52952015-01-28 23:07:34 +0100410 string func = gui_parse_text(action.mFunction);
411 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
412 if (needsThread) {
413 if (func == "cancelbackup")
414 return THREAD_CANCEL;
415 else
416 return THREAD_ACTION;
417 }
418 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100419}
420
Dees_Troy51a0e822012-09-05 15:24:24 -0400421int GUIAction::doActions()
422{
that3f7b1ac2014-12-30 11:30:13 +0100423 if (mActions.size() < 1)
424 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425
that73a52952015-01-28 23:07:34 +0100426 // Determine in which thread to run the actions.
427 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
428 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100429 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100430 for (it = mActions.begin(); it != mActions.end(); ++it) {
431 ThreadType tt = getThreadType(*it);
432 if (tt == THREAD_NONE)
433 continue;
434 if (threadType == THREAD_NONE)
435 threadType = tt;
436 else if (threadType != tt) {
437 LOGERR("Can't mix normal and cancel actions in the same list.\n"
438 "Running the whole batch in the cancel thread.\n");
439 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100440 break;
441 }
that7d3b54f2015-01-09 22:52:51 +0100442 }
that73a52952015-01-28 23:07:34 +0100443
444 // Now run the actions in the desired thread.
445 switch (threadType) {
446 case THREAD_ACTION:
447 action_thread.threadActions(this);
448 break;
449
450 case THREAD_CANCEL:
451 cancel_thread.threadActions(this);
452 break;
453
454 default: {
455 // no iterators here because theme reloading might kill our object
456 const size_t cnt = mActions.size();
457 for (size_t i = 0; i < cnt; ++i)
458 doAction(mActions[i]);
459 }
thatc6085482015-01-09 22:12:43 +0100460 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200462 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400463}
464
that3f7b1ac2014-12-30 11:30:13 +0100465int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400466{
that3f7b1ac2014-12-30 11:30:13 +0100467 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
that3f7b1ac2014-12-30 11:30:13 +0100469 std::string function = gui_parse_text(action.mFunction);
470 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
that3f7b1ac2014-12-30 11:30:13 +0100472 // find function and execute it
473 mapFunc::const_iterator funcitr = mf.find(function);
474 if (funcitr != mf.end())
475 return (this->*funcitr->second)(arg);
476
477 LOGERR("Unknown action '%s'\n", function.c_str());
478 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
481void GUIAction::operation_start(const string operation_name)
482{
that3f7b1ac2014-12-30 11:30:13 +0100483 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000484 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 DataManager::SetValue(TW_ACTION_BUSY, 1);
486 DataManager::SetValue("ui_progress", 0);
487 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600489 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400490}
491
that3f7b1ac2014-12-30 11:30:13 +0100492void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400493{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000494 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue("ui_progress", 100);
497 if (simulate) {
498 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
499 if (simulate_fail != 0)
500 DataManager::SetValue("tw_operation_status", 1);
501 else
502 DataManager::SetValue("tw_operation_status", 0);
503 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500504 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500506 }
507 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 }
511 DataManager::SetValue("tw_operation_state", 1);
512 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500513 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200514 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000515 time(&Stop);
516 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600517 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100518 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400519}
520
that3f7b1ac2014-12-30 11:30:13 +0100521int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400522{
that3f7b1ac2014-12-30 11:30:13 +0100523 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400524
that3f7b1ac2014-12-30 11:30:13 +0100525 sync();
526 DataManager::SetValue("tw_gui_done", 1);
527 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
that3f7b1ac2014-12-30 11:30:13 +0100529 return 0;
530}
Dees_Troy51a0e822012-09-05 15:24:24 -0400531
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500532int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100533{
that3f7b1ac2014-12-30 11:30:13 +0100534 gui_changePage("main");
535 return 0;
536}
Dees_Troy51a0e822012-09-05 15:24:24 -0400537
that3f7b1ac2014-12-30 11:30:13 +0100538int GUIAction::key(std::string arg)
539{
540 const int key = getKeyByName(arg);
541 PageManager::NotifyKey(key, true);
542 PageManager::NotifyKey(key, false);
543 return 0;
544}
545
546int GUIAction::page(std::string arg)
547{
LuK133762326f42015-09-30 19:57:46 +0200548 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100549 std::string page_name = gui_parse_text(arg);
550 return gui_changePage(page_name);
551}
552
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500553int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100554{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500555 PageManager::RequestReload();
556 // The actual reload is handled in pages.cpp in PageManager::RunReload()
557 // The reload will occur on the next Update or Render call and will
558 // be performed in the rendoer thread instead of the action thread
559 // to prevent crashing which could occur when we start deleting
560 // GUI resources in the action thread while we attempt to render
561 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100562 return 0;
563}
Dees_Troy51a0e822012-09-05 15:24:24 -0400564
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500565int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100566{
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
Ethan Yonker74db1572015-10-28 12:44:49 -0500601 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100602 } else if (!simulate) {
603 PartitionManager.Mount_By_Path(arg, true);
604 PartitionManager.Add_MTP_Storage(arg);
605 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100607 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
Ethan Yonker74db1572015-10-28 12:44:49 -0500616 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100617 DataManager::SetValue(TW_ACTION_BUSY, 0);
618 } else if (!simulate) {
619 PartitionManager.UnMount_By_Path(arg, true);
620 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500621 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100622 return 0;
623}
624
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500625int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100626{
627 operation_start("Restore Defaults");
628 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500629 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100630 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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500639int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100640{
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();
Ethan Yonker74db1572015-10-28 12:44:49 -0500650 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100651 } 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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500714int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100715{
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500744int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100745{
746 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500747 gui_msg("max_queue=Maximum zip queue reached!");
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500758int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100759{
760 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500761 gui_msg("min_queue=Minimum zip queue reached!");
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500770int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100771{
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500785int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100786{
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500798int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100799{
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
Ethan Yonker483e9f42016-01-11 22:21:18 -0600806int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100807{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600808 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100809 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500810
Ethan Yonker483e9f42016-01-11 22:21:18 -0600811 if (arg.empty())
812 arg = "tw_wipe_list";
813 DataManager::GetValue(arg, List);
814 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
815 if (!List.empty()) {
816 size_t start_pos = 0, end_pos = List.find(";", start_pos);
817 while (end_pos != string::npos && start_pos < List.size()) {
818 part_path = List.substr(start_pos, end_pos - start_pos);
819 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
820 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100821 // 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;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600826 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100827 }
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
Ethan Yonker483e9f42016-01-11 22:21:18 -0600835int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100836{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600837 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100838 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
Ethan Yonker483e9f42016-01-11 22:21:18 -0600840 if (arg.empty())
841 arg = "tw_wipe_list";
842 DataManager::GetValue(arg, List);
843 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
844 if (!List.empty()) {
845 size_t start_pos = 0, end_pos = List.find(";", start_pos);
846 part_path = List;
847 while (end_pos != string::npos && start_pos < List.size()) {
848 part_path = List.substr(start_pos, end_pos - start_pos);
849 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
850 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100851 // Do nothing
852 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600853 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100854 break;
855 }
856 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600857 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100858 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600859 if (!part_path.empty()) {
860 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100861 if (Part) {
862 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500863
that3f7b1ac2014-12-30 11:30:13 +0100864 DataManager::SetValue("tw_partition_name", Part->Display_Name);
865 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
866 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
867 DataManager::SetValue("tw_partition_size", Part->Size / mb);
868 DataManager::SetValue("tw_partition_used", Part->Used / mb);
869 DataManager::SetValue("tw_partition_free", Part->Free / mb);
870 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
871 DataManager::SetValue("tw_partition_removable", Part->Removable);
872 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
873
874 if (Part->Can_Repair())
875 DataManager::SetValue("tw_partition_can_repair", 1);
876 else
877 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500878 if (Part->Can_Resize())
879 DataManager::SetValue("tw_partition_can_resize", 1);
880 else
881 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600882 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100883 DataManager::SetValue("tw_partition_vfat", 1);
884 else
885 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600886 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100887 DataManager::SetValue("tw_partition_exfat", 1);
888 else
889 DataManager::SetValue("tw_partition_exfat", 0);
890 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
891 DataManager::SetValue("tw_partition_f2fs", 1);
892 else
893 DataManager::SetValue("tw_partition_f2fs", 0);
894 if (TWFunc::Path_Exists("/sbin/mke2fs"))
895 DataManager::SetValue("tw_partition_ext", 1);
896 else
897 DataManager::SetValue("tw_partition_ext", 0);
898 return 0;
899 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100901 }
902 }
903 }
904 DataManager::SetValue("tw_partition_name", "");
905 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600906 // Set this to 0 to prevent trying to partition this device, just in case
907 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100908 return 0;
909}
910
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500911int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100912{
913 time_t tm;
914 char path[256];
915 int path_len;
916 uid_t uid = -1;
917 gid_t gid = -1;
918
919 struct passwd *pwd = getpwnam("media_rw");
920 if(pwd) {
921 uid = pwd->pw_uid;
922 gid = pwd->pw_gid;
923 }
924
925 const std::string storage = DataManager::GetCurrentStoragePath();
926 if(PartitionManager.Is_Mounted_By_Path(storage)) {
927 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
928 } else {
929 strcpy(path, "/tmp/");
930 }
931
Xuefer579e9072016-01-22 13:35:50 +0800932 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100933 return 0;
934
935 tm = time(NULL);
936 path_len = strlen(path);
937
938 // Screenshot_2014-01-01-18-21-38.png
939 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
940
941 int res = gr_save_screenshot(path);
942 if(res == 0) {
943 chmod(path, 0666);
944 chown(path, uid, gid);
945
that677b13f2015-12-26 23:57:31 +0100946 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100947
948 // blink to notify that the screenshow was taken
949 gr_color(255, 255, 255, 255);
950 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
951 gr_flip();
952 gui_forceRender();
953 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500954 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100955 }
956 return 0;
957}
958
959int GUIAction::setbrightness(std::string arg)
960{
961 return TWFunc::Set_Brightness(arg);
962}
963
964int GUIAction::fileexists(std::string arg)
965{
966 struct stat st;
967 string newpath = arg + "/.";
968
969 operation_start("FileExists");
970 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
971 operation_end(0);
972 else
973 operation_end(1);
974 return 0;
975}
976
thatcc8ddca2015-01-03 01:59:36 +0100977void GUIAction::reinject_after_flash()
978{
979 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500980 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100981 if (simulate) {
982 simulate_progress_bar();
983 } else {
984 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
985 if (Boot == NULL || Boot->Current_File_System != "emmc")
986 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
987 else {
988 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
989 TWFunc::Exec_Cmd(injectcmd);
990 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500991 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100992 }
993 }
994}
995
that3f7b1ac2014-12-30 11:30:13 +0100996int GUIAction::flash(std::string arg)
997{
998 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600999 // We're going to jump to this page first, like a loading page
1000 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001001 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001002 string zip_path = zip_queue[i];
1003 size_t slashpos = zip_path.find_last_of('/');
1004 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001005 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001006 DataManager::SetValue("tw_filename", zip_path);
1007 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001008 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1009
1010 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001011 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001012 TWFunc::SetPerformanceMode(false);
1013 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001014 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001015 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001016 break;
that3f7b1ac2014-12-30 11:30:13 +01001017 }
1018 }
1019 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001020
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001021 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001022 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001023 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001024 }
that3f7b1ac2014-12-30 11:30:13 +01001025
thatcc8ddca2015-01-03 01:59:36 +01001026 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001027 PartitionManager.Update_System_Details();
1028 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001029 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001030 return 0;
1031}
1032
1033int GUIAction::wipe(std::string arg)
1034{
1035 operation_start("Format");
1036 DataManager::SetValue("tw_partition", arg);
1037
1038 int ret_val = false;
1039
1040 if (simulate) {
1041 simulate_progress_bar();
1042 } else {
1043 if (arg == "data")
1044 ret_val = PartitionManager.Factory_Reset();
1045 else if (arg == "battery")
1046 ret_val = PartitionManager.Wipe_Battery_Stats();
1047 else if (arg == "rotate")
1048 ret_val = PartitionManager.Wipe_Rotate_Data();
1049 else if (arg == "dalvik")
1050 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1051 else if (arg == "DATAMEDIA") {
1052 ret_val = PartitionManager.Format_Data();
1053 } else if (arg == "INTERNAL") {
1054 int has_datamedia, dual_storage;
1055
1056 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1057 if (has_datamedia) {
1058 ret_val = PartitionManager.Wipe_Media_From_Data();
1059 } else {
1060 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1061 }
1062 } else if (arg == "EXTERNAL") {
1063 string External_Path;
1064
1065 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1066 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1067 } else if (arg == "ANDROIDSECURE") {
1068 ret_val = PartitionManager.Wipe_Android_Secure();
1069 } else if (arg == "LIST") {
1070 string Wipe_List, wipe_path;
1071 bool skip = false;
1072 ret_val = true;
1073 TWPartition* wipe_part = NULL;
1074
1075 DataManager::GetValue("tw_wipe_list", Wipe_List);
1076 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1077 if (!Wipe_List.empty()) {
1078 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1079 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1080 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1081 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1082 if (wipe_path == "/and-sec") {
1083 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001084 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001085 ret_val = false;
1086 break;
1087 } else {
1088 skip = true;
1089 }
1090 } else if (wipe_path == "DALVIK") {
1091 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001092 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001093 ret_val = false;
1094 break;
1095 } else {
1096 skip = true;
1097 }
1098 } else if (wipe_path == "INTERNAL") {
1099 if (!PartitionManager.Wipe_Media_From_Data()) {
1100 ret_val = false;
1101 break;
1102 } else {
1103 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001104 }
1105 }
that3f7b1ac2014-12-30 11:30:13 +01001106 if (!skip) {
1107 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001108 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001109 ret_val = false;
1110 break;
1111 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1112 arg = wipe_path;
1113 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001114 } else {
that3f7b1ac2014-12-30 11:30:13 +01001115 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001116 }
that3f7b1ac2014-12-30 11:30:13 +01001117 start_pos = end_pos + 1;
1118 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001119 }
1120 }
that3f7b1ac2014-12-30 11:30:13 +01001121 } else
1122 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001123#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001124 if (arg == DataManager::GetSettingsStoragePath()) {
1125 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1126 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001127
that3f7b1ac2014-12-30 11:30:13 +01001128 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1129 LOGINFO("Making TWRP folder and saving settings.\n");
1130 Storage_Path += "/TWRP";
1131 mkdir(Storage_Path.c_str(), 0777);
1132 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001133 } else {
that3f7b1ac2014-12-30 11:30:13 +01001134 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1135 }
1136 }
1137#endif
1138 }
1139 PartitionManager.Update_System_Details();
1140 if (ret_val)
1141 ret_val = 0; // 0 is success
1142 else
1143 ret_val = 1; // 1 is failure
1144 operation_end(ret_val);
1145 return 0;
1146}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001147
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001148int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001149{
1150 operation_start("Refreshing Sizes");
1151 if (simulate) {
1152 simulate_progress_bar();
1153 } else
1154 PartitionManager.Update_System_Details();
1155 operation_end(0);
1156 return 0;
1157}
1158
1159int GUIAction::nandroid(std::string arg)
1160{
that3f7b1ac2014-12-30 11:30:13 +01001161 if (simulate) {
that73a52952015-01-28 23:07:34 +01001162 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001163 DataManager::SetValue("tw_partition", "Simulation");
1164 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001165 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001166 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001167 operation_start("Nandroid");
1168 int ret = 0;
1169
that3f7b1ac2014-12-30 11:30:13 +01001170 if (arg == "backup") {
1171 string Backup_Name;
1172 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001173 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1174 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
that3f7b1ac2014-12-30 11:30:13 +01001175 ret = PartitionManager.Run_Backup();
1176 }
1177 else {
1178 operation_end(1);
1179 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001180 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001181 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001182 } else if (arg == "restore") {
1183 string Restore_Name;
1184 DataManager::GetValue("tw_restore", Restore_Name);
1185 ret = PartitionManager.Run_Restore(Restore_Name);
1186 } else {
1187 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001188 return -1;
1189 }
1190 DataManager::SetValue("tw_encrypt_backup", 0);
1191 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001192 if (ret == false)
1193 ret = 1; // 1 for failure
1194 else
1195 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 }
1198 else {
1199 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001200 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 ret = 0;
1202 }
that73a52952015-01-28 23:07:34 +01001203 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001204 return ret;
1205 }
1206 return 0;
1207}
1208
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001209int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001210 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001211 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001212 }
1213 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001214 int op_status = PartitionManager.Cancel_Backup();
1215 if (op_status != 0)
1216 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001217 }
1218
1219 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001220}
Dees_Troy51a0e822012-09-05 15:24:24 -04001221
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001222int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001223{
that73a52952015-01-28 23:07:34 +01001224 int op_status = 0;
1225
that3f7b1ac2014-12-30 11:30:13 +01001226 operation_start("Fix Permissions");
1227 LOGINFO("fix permissions started!\n");
1228 if (simulate) {
1229 simulate_progress_bar();
1230 } else {
that73a52952015-01-28 23:07:34 +01001231 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001232 if (op_status != 0)
1233 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001234 }
that73a52952015-01-28 23:07:34 +01001235 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001236 return 0;
1237}
1238
1239int GUIAction::dd(std::string arg)
1240{
1241 operation_start("imaging");
1242
1243 if (simulate) {
1244 simulate_progress_bar();
1245 } else {
1246 string cmd = "dd " + arg;
1247 TWFunc::Exec_Cmd(cmd);
1248 }
1249 operation_end(0);
1250 return 0;
1251}
1252
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001253int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001254{
1255 operation_start("Partition SD Card");
1256 int ret_val = 0;
1257
1258 if (simulate) {
1259 simulate_progress_bar();
1260 } else {
1261 int allow_partition;
1262 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1263 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001264 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001265 } else {
1266 if (!PartitionManager.Partition_SDCard())
1267 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001268 }
that3f7b1ac2014-12-30 11:30:13 +01001269 }
1270 operation_end(ret_val);
1271 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001272
that3f7b1ac2014-12-30 11:30:13 +01001273}
Dees_Troy51a0e822012-09-05 15:24:24 -04001274
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001275int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001276{
1277 operation_start("Install HTC Dumlock");
1278 if (simulate) {
1279 simulate_progress_bar();
1280 } else
1281 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001282
that3f7b1ac2014-12-30 11:30:13 +01001283 operation_end(0);
1284 return 0;
1285}
Dees_Troy51a0e822012-09-05 15:24:24 -04001286
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001287int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001288{
1289 operation_start("HTC Dumlock Restore Boot");
1290 if (simulate) {
1291 simulate_progress_bar();
1292 } else
1293 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001294
that3f7b1ac2014-12-30 11:30:13 +01001295 operation_end(0);
1296 return 0;
1297}
Dees_Troy51a0e822012-09-05 15:24:24 -04001298
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001299int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001300{
1301 operation_start("HTC Dumlock Reflash Recovery");
1302 if (simulate) {
1303 simulate_progress_bar();
1304 } else
1305 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001306
that3f7b1ac2014-12-30 11:30:13 +01001307 operation_end(0);
1308 return 0;
1309}
Dees_Troy51a0e822012-09-05 15:24:24 -04001310
that3f7b1ac2014-12-30 11:30:13 +01001311int GUIAction::cmd(std::string arg)
1312{
1313 int op_status = 0;
1314
1315 operation_start("Command");
1316 LOGINFO("Running command: '%s'\n", arg.c_str());
1317 if (simulate) {
1318 simulate_progress_bar();
1319 } else {
1320 op_status = TWFunc::Exec_Cmd(arg);
1321 if (op_status != 0)
1322 op_status = 1;
1323 }
1324
1325 operation_end(op_status);
1326 return 0;
1327}
1328
1329int GUIAction::terminalcommand(std::string arg)
1330{
1331 int op_status = 0;
1332 string cmdpath, command;
1333
1334 DataManager::GetValue("tw_terminal_location", cmdpath);
1335 operation_start("CommandOutput");
1336 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1337 if (simulate) {
1338 simulate_progress_bar();
1339 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001340 } else if (arg == "exit") {
1341 LOGINFO("Exiting terminal\n");
1342 operation_end(op_status);
1343 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001344 } else {
1345 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1346 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001347 DataManager::SetValue("tw_terminal_state", 1);
1348 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001349 FILE* fp;
1350 char line[512];
1351
1352 fp = popen(command.c_str(), "r");
1353 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001354 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001355 } else {
1356 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1357 struct timeval timeout;
1358 fd_set fdset;
1359
1360 while(keep_going)
1361 {
1362 FD_ZERO(&fdset);
1363 FD_SET(fd, &fdset);
1364 timeout.tv_sec = 0;
1365 timeout.tv_usec = 400000;
1366 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1367 if (has_data == 0) {
1368 // Timeout reached
1369 DataManager::GetValue("tw_terminal_state", check);
1370 if (check == 0) {
1371 keep_going = 0;
1372 }
1373 } else if (has_data < 0) {
1374 // End of execution
1375 keep_going = 0;
1376 } else {
1377 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001378 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001379 gui_print("%s", line); // Display output
1380 else
1381 keep_going = 0; // Done executing
1382 }
1383 }
1384 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001385 }
thatc6085482015-01-09 22:12:43 +01001386 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);
that3f7b1ac2014-12-30 11:30:13 +01001391 }
1392 return 0;
1393}
1394
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001395int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001396{
1397 int op_status = 0;
1398
1399 LOGINFO("Sending kill command...\n");
1400 operation_start("KillCommand");
1401 DataManager::SetValue("tw_operation_status", 0);
1402 DataManager::SetValue("tw_operation_state", 1);
1403 DataManager::SetValue("tw_terminal_state", 0);
1404 DataManager::SetValue("tw_background_thread_running", 0);
1405 DataManager::SetValue(TW_ACTION_BUSY, 0);
1406 return 0;
1407}
1408
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001409int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001410{
1411 int op_status = 0;
1412 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001413 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001414 if (simulate) {
1415 simulate_progress_bar();
1416 } else {
1417 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001418 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001419 }
1420
1421 operation_end(op_status);
1422 return 0;
1423}
1424
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001425int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001426{
1427 int op_status = 0;
1428
1429 operation_start("CheckBackupName");
1430 if (simulate) {
1431 simulate_progress_bar();
1432 } else {
1433 op_status = PartitionManager.Check_Backup_Name(true);
1434 if (op_status != 0)
1435 op_status = 1;
1436 }
1437
1438 operation_end(op_status);
1439 return 0;
1440}
1441
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001442int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001443{
1444 int op_status = 0;
1445
1446 operation_start("Decrypt");
1447 if (simulate) {
1448 simulate_progress_bar();
1449 } else {
1450 string Password;
1451 DataManager::GetValue("tw_crypto_password", Password);
1452 op_status = PartitionManager.Decrypt_Device(Password);
1453 if (op_status != 0)
1454 op_status = 1;
1455 else {
that3f7b1ac2014-12-30 11:30:13 +01001456
1457 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1458
Ethan Yonkercf50da52015-01-12 21:59:07 -06001459 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001460
Ethan Yonkercf50da52015-01-12 21:59:07 -06001461 // Check for a custom theme and load it if exists
1462 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1463 if (has_datamedia != 0) {
1464 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001465 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001466 } else {
1467 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001468 }
1469 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001470 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001471 }
1472 }
1473
1474 operation_end(op_status);
1475 return 0;
1476}
1477
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001478int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001479{
1480 operation_start("Sideload");
1481 if (simulate) {
1482 simulate_progress_bar();
1483 operation_end(0);
1484 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001485 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001486 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1487
1488 // wait for the adb connection
1489 int ret = apply_from_adb("/", &sideload_child_pid);
1490 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1491
1492 if (ret != 0) {
1493 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001494 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001495 ret = 1; // failure
1496 } else {
1497 int wipe_cache = 0;
1498 int wipe_dalvik = 0;
1499 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1500
1501 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1502 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1503 PartitionManager.Wipe_By_Path("/cache");
1504 if (wipe_dalvik)
1505 PartitionManager.Wipe_Dalvik_Cache();
1506 } else {
1507 ret = 1; // failure
1508 }
thatcc8ddca2015-01-03 01:59:36 +01001509 }
thatc6085482015-01-09 22:12:43 +01001510 if (sideload_child_pid) {
1511 LOGINFO("Signaling child sideload process to exit.\n");
1512 struct stat st;
1513 // Calling stat() on this magic filename signals the minadbd
1514 // subprocess to shut down.
1515 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1516 int status;
1517 LOGINFO("Waiting for child sideload process to exit.\n");
1518 waitpid(sideload_child_pid, &status, 0);
1519 }
Dees Troy28a85d22015-04-28 02:03:16 +00001520 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001521 TWFunc::Toggle_MTP(mtp_was_enabled);
1522 reinject_after_flash();
1523 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001524 }
that3f7b1ac2014-12-30 11:30:13 +01001525 return 0;
1526}
1527
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001528int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001529{
that3f7b1ac2014-12-30 11:30:13 +01001530 struct stat st;
1531 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001532 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001533 LOGINFO("Signaling child sideload process to exit.\n");
1534 // Calling stat() on this magic filename signals the minadbd
1535 // subprocess to shut down.
1536 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1537 if (!sideload_child_pid) {
1538 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001539 return 0;
1540 }
thatcc8ddca2015-01-03 01:59:36 +01001541 ::sleep(1);
1542 LOGINFO("Killing child sideload process.\n");
1543 kill(sideload_child_pid, SIGTERM);
1544 int status;
1545 LOGINFO("Waiting for child sideload process to exit.\n");
1546 waitpid(sideload_child_pid, &status, 0);
1547 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001548 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1549 return 0;
1550}
1551
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001552int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001553{
1554 operation_start("OpenRecoveryScript");
1555 if (simulate) {
1556 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001557 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001558 } else {
that10ae24f2015-12-26 20:53:51 +01001559 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001560 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001561 }
1562 return 0;
1563}
1564
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001565int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001566{
1567 int op_status = 0;
1568
1569 operation_start("Install SuperSU");
1570 if (simulate) {
1571 simulate_progress_bar();
1572 } else {
1573 if (!TWFunc::Install_SuperSU())
1574 op_status = 1;
1575 }
1576
1577 operation_end(op_status);
1578 return 0;
1579}
1580
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001581int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001582{
1583 int op_status = 0;
1584
1585 operation_start("Fixing Superuser Permissions");
1586 if (simulate) {
1587 simulate_progress_bar();
1588 } else {
1589 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1590 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1591 }
1592
1593 operation_end(op_status);
1594 return 0;
1595}
1596
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001597int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001598{
1599 int op_status = 0;
1600
1601 operation_start("Try Restore Decrypt");
1602 if (simulate) {
1603 simulate_progress_bar();
1604 } else {
1605 string Restore_Path, Filename, Password;
1606 DataManager::GetValue("tw_restore", Restore_Path);
1607 Restore_Path += "/";
1608 DataManager::GetValue("tw_restore_password", Password);
1609 TWFunc::SetPerformanceMode(true);
1610 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1611 op_status = 0; // success
1612 else
1613 op_status = 1; // fail
1614 TWFunc::SetPerformanceMode(false);
1615 }
1616
1617 operation_end(op_status);
1618 return 0;
1619}
1620
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001621int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001622{
1623 int op_status = 0;
1624
1625 operation_start("Repair Partition");
1626 if (simulate) {
1627 simulate_progress_bar();
1628 } else {
1629 string part_path;
1630 DataManager::GetValue("tw_partition_mount_point", part_path);
1631 if (PartitionManager.Repair_By_Path(part_path, true)) {
1632 op_status = 0; // success
1633 } else {
that3f7b1ac2014-12-30 11:30:13 +01001634 op_status = 1; // fail
1635 }
1636 }
1637
1638 operation_end(op_status);
1639 return 0;
1640}
1641
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001642int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001643{
1644 int op_status = 0;
1645
1646 operation_start("Resize Partition");
1647 if (simulate) {
1648 simulate_progress_bar();
1649 } else {
1650 string part_path;
1651 DataManager::GetValue("tw_partition_mount_point", part_path);
1652 if (PartitionManager.Resize_By_Path(part_path, true)) {
1653 op_status = 0; // success
1654 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001655 op_status = 1; // fail
1656 }
1657 }
1658
1659 operation_end(op_status);
1660 return 0;
1661}
1662
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001663int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001664{
1665 int op_status = 0;
1666
1667 operation_start("Change File System");
1668 if (simulate) {
1669 simulate_progress_bar();
1670 } else {
1671 string part_path, file_system;
1672 DataManager::GetValue("tw_partition_mount_point", part_path);
1673 DataManager::GetValue("tw_action_new_file_system", file_system);
1674 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1675 op_status = 0; // success
1676 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001677 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001678 op_status = 1; // fail
1679 }
1680 }
1681 PartitionManager.Update_System_Details();
1682 operation_end(op_status);
1683 return 0;
1684}
1685
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001686int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001687{
1688 int op_status = 0;
1689
1690 operation_start("Start MTP");
1691 if (PartitionManager.Enable_MTP())
1692 op_status = 0; // success
1693 else
1694 op_status = 1; // fail
1695
1696 operation_end(op_status);
1697 return 0;
1698}
1699
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001700int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001701{
1702 int op_status = 0;
1703
1704 operation_start("Stop MTP");
1705 if (PartitionManager.Disable_MTP())
1706 op_status = 0; // success
1707 else
1708 op_status = 1; // fail
1709
1710 operation_end(op_status);
1711 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001712}
1713
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001714int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001715{
1716 int op_status = 0;
1717
1718 operation_start("Flash Image");
1719 string path, filename, full_filename;
1720 DataManager::GetValue("tw_zip_location", path);
1721 DataManager::GetValue("tw_file", filename);
1722 full_filename = path + "/" + filename;
1723 if (PartitionManager.Flash_Image(full_filename))
1724 op_status = 0; // success
1725 else
1726 op_status = 1; // fail
1727
1728 operation_end(op_status);
1729 return 0;
1730}
1731
that10ae24f2015-12-26 20:53:51 +01001732int GUIAction::twcmd(std::string arg)
1733{
1734 operation_start("TWRP CLI Command");
1735 if (simulate)
1736 simulate_progress_bar();
1737 else
1738 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1739 operation_end(0);
1740 return 0;
1741}
1742
Dees_Troy51a0e822012-09-05 15:24:24 -04001743int GUIAction::getKeyByName(std::string key)
1744{
that8834a0f2016-01-05 23:29:30 +01001745 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001746 else if (key == "menu") return KEY_MENU;
1747 else if (key == "back") return KEY_BACK;
1748 else if (key == "search") return KEY_SEARCH;
1749 else if (key == "voldown") return KEY_VOLUMEDOWN;
1750 else if (key == "volup") return KEY_VOLUMEUP;
1751 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001752 int ret_val;
1753 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1754 if (!ret_val)
1755 return KEY_POWER;
1756 else
1757 return ret_val;
1758 }
1759
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001760 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001761}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001762
1763int GUIAction::checkpartitionlifetimewrites(std::string arg)
1764{
1765 int op_status = 0;
1766 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1767
1768 operation_start("Check Partition Lifetime Writes");
1769 if (sys) {
1770 if (sys->Check_Lifetime_Writes() != 0)
1771 DataManager::SetValue("tw_lifetime_writes", 1);
1772 else
1773 DataManager::SetValue("tw_lifetime_writes", 0);
1774 op_status = 0; // success
1775 } else {
1776 DataManager::SetValue("tw_lifetime_writes", 1);
1777 op_status = 1; // fail
1778 }
1779
1780 operation_end(op_status);
1781 return 0;
1782}
1783
1784int GUIAction::mountsystemtoggle(std::string arg)
1785{
1786 int op_status = 0;
1787 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001788 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001789
1790 operation_start("Toggle System Mount");
1791 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1792 op_status = 1; // fail
1793 } else {
1794 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1795 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001796 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001797 DataManager::SetValue("tw_mount_system_ro", 0);
1798 Part->Change_Mount_Read_Only(false);
1799 } else {
1800 DataManager::SetValue("tw_mount_system_ro", 1);
1801 Part->Change_Mount_Read_Only(true);
1802 }
1803 if (remount_system) {
1804 Part->Mount(true);
1805 }
1806 op_status = 0; // success
1807 } else {
1808 op_status = 1; // fail
1809 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001810 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1811 if (Part) {
1812 if (arg == "0") {
1813 Part->Change_Mount_Read_Only(false);
1814 } else {
1815 Part->Change_Mount_Read_Only(true);
1816 }
1817 if (remount_vendor) {
1818 Part->Mount(true);
1819 }
1820 op_status = 0; // success
1821 } else {
1822 op_status = 1; // fail
1823 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001824 }
1825
1826 operation_end(op_status);
1827 return 0;
1828}
Ethan Yonker74db1572015-10-28 12:44:49 -05001829
1830int GUIAction::setlanguage(std::string arg __unused)
1831{
1832 int op_status = 0;
1833
1834 operation_start("Set Language");
1835 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1836 PageManager::RequestReload();
1837 op_status = 0; // success
1838
1839 operation_end(op_status);
1840 return 0;
1841}