blob: 3e27dbb44313ced091473b012760f0c96b406954 [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"
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};
55
56#include "rapidxml.hpp"
57#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050058#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Dees_Troy51a0e822012-09-05 15:24:24 -040060void curtainClose(void);
61
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
66static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010067pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
111 if(m_thread_running) {
112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100201
202 // remember actions that run in the caller thread
203 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
204 setActionsRunningInCallerThread.insert(it->first);
205
206 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100207 ADD_ACTION(flash);
208 ADD_ACTION(wipe);
209 ADD_ACTION(refreshsizes);
210 ADD_ACTION(nandroid);
211 ADD_ACTION(fixpermissions);
212 ADD_ACTION(dd);
213 ADD_ACTION(partitionsd);
214 ADD_ACTION(installhtcdumlock);
215 ADD_ACTION(htcdumlockrestoreboot);
216 ADD_ACTION(htcdumlockreflashrecovery);
217 ADD_ACTION(cmd);
218 ADD_ACTION(terminalcommand);
219 ADD_ACTION(reinjecttwrp);
220 ADD_ACTION(decrypt);
221 ADD_ACTION(adbsideload);
222 ADD_ACTION(openrecoveryscript);
223 ADD_ACTION(installsu);
224 ADD_ACTION(decrypt_backup);
225 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500226 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100227 ADD_ACTION(changefilesystem);
228 ADD_ACTION(flashimage);
that3f7b1ac2014-12-30 11:30:13 +0100229 }
230
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600232 actions = FindNode(node, "actions");
233 if (actions) child = FindNode(actions, "action");
234 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 while (child)
239 {
240 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 attr = child->first_attribute("function");
243 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 action.mFunction = attr->value();
246 action.mArg = child->value();
247 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 child = child->next_sibling("action");
250 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600253 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 if (child)
255 {
256 attr = child->first_attribute("key");
257 if (attr)
258 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100259 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
260 for(size_t i = 0; i < keys.size(); ++i)
261 {
262 const int key = getKeyByName(keys[i]);
263 mKeys[key] = false;
264 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 }
266 else
267 {
268 attr = child->first_attribute("x");
269 if (!attr) return;
270 mActionX = atol(attr->value());
271 attr = child->first_attribute("y");
272 if (!attr) return;
273 mActionY = atol(attr->value());
274 attr = child->first_attribute("w");
275 if (!attr) return;
276 mActionW = atol(attr->value());
277 attr = child->first_attribute("h");
278 if (!attr) return;
279 mActionH = atol(attr->value());
280 }
281 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400282}
283
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500284int GUIAction::NotifyTouch(TOUCH_STATE state __unused, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400285{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 if (state == TOUCH_RELEASE)
287 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}
291
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100292int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400293{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100294 if (mKeys.empty())
295 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100297 std::map<int, bool>::iterator itr = mKeys.find(key);
298 if(itr == mKeys.end())
299 return 0;
300
301 bool prevState = itr->second;
302 itr->second = down;
303
304 // If there is only one key for this action, wait for key up so it
305 // doesn't trigger with multi-key actions.
306 // Else, check if all buttons are pressed, then consume their release events
307 // so they don't trigger one-button actions and reset mKeys pressed status
308 if(mKeys.size() == 1) {
309 if(!down && prevState)
310 doActions();
311 } else if(down) {
312 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
313 if(!itr->second)
314 return 0;
315 }
316
317 // Passed, all req buttons are pressed, reset them and consume release events
318 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
319 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
320 kb->ConsumeKeyRelease(itr->first);
321 itr->second = false;
322 }
323
324 doActions();
325 }
326
Dees_Troy51a0e822012-09-05 15:24:24 -0400327 return 0;
328}
329
Vojtech Bocek07220562014-02-08 02:05:33 +0100330int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400331{
Vojtech Bocek07220562014-02-08 02:05:33 +0100332 GUIObject::NotifyVarChange(varName, value);
333
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100334 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100336 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400338
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342void GUIAction::simulate_progress_bar(void)
343{
Ethan Yonker74db1572015-10-28 12:44:49 -0500344 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 for (int i = 0; i < 5; i++)
346 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500347 if (PartitionManager.stop_backup.get_value()) {
348 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -0500349 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -0500350 DataManager::SetValue("ui_progress", 0);
351 PartitionManager.stop_backup.set_value(0);
352 return;
353 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 usleep(500000);
355 DataManager::SetValue("ui_progress", i * 20);
356 }
357}
358
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600359int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400360{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200361 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362
363 DataManager::SetValue("ui_progress", 0);
364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 if (filename.empty())
366 {
367 LOGERR("No file specified.\n");
368 return -1;
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400372 return -1;
373
Dees_Troy51a0e822012-09-05 15:24:24 -0400374 if (simulate) {
375 simulate_progress_bar();
376 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400377 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
379 // Now, check if we need to ensure TWRP remains installed...
380 struct stat st;
381 if (stat("/sbin/installTwrp", &st) == 0)
382 {
383 DataManager::SetValue("tw_operation", "Configuring TWRP");
384 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500385 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200386 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400387 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500388 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400389 }
390 }
391 }
392
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 // Done
394 DataManager::SetValue("ui_progress", 100);
395 DataManager::SetValue("ui_progress", 0);
396 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397}
398
that73a52952015-01-28 23:07:34 +0100399GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100400{
that73a52952015-01-28 23:07:34 +0100401 string func = gui_parse_text(action.mFunction);
402 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
403 if (needsThread) {
404 if (func == "cancelbackup")
405 return THREAD_CANCEL;
406 else
407 return THREAD_ACTION;
408 }
409 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100410}
411
Dees_Troy51a0e822012-09-05 15:24:24 -0400412int GUIAction::doActions()
413{
that3f7b1ac2014-12-30 11:30:13 +0100414 if (mActions.size() < 1)
415 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416
that73a52952015-01-28 23:07:34 +0100417 // Determine in which thread to run the actions.
418 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
419 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100420 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100421 for (it = mActions.begin(); it != mActions.end(); ++it) {
422 ThreadType tt = getThreadType(*it);
423 if (tt == THREAD_NONE)
424 continue;
425 if (threadType == THREAD_NONE)
426 threadType = tt;
427 else if (threadType != tt) {
428 LOGERR("Can't mix normal and cancel actions in the same list.\n"
429 "Running the whole batch in the cancel thread.\n");
430 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100431 break;
432 }
that7d3b54f2015-01-09 22:52:51 +0100433 }
that73a52952015-01-28 23:07:34 +0100434
435 // Now run the actions in the desired thread.
436 switch (threadType) {
437 case THREAD_ACTION:
438 action_thread.threadActions(this);
439 break;
440
441 case THREAD_CANCEL:
442 cancel_thread.threadActions(this);
443 break;
444
445 default: {
446 // no iterators here because theme reloading might kill our object
447 const size_t cnt = mActions.size();
448 for (size_t i = 0; i < cnt; ++i)
449 doAction(mActions[i]);
450 }
thatc6085482015-01-09 22:12:43 +0100451 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400454}
455
that3f7b1ac2014-12-30 11:30:13 +0100456int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400457{
that3f7b1ac2014-12-30 11:30:13 +0100458 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
that3f7b1ac2014-12-30 11:30:13 +0100460 std::string function = gui_parse_text(action.mFunction);
461 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462
that3f7b1ac2014-12-30 11:30:13 +0100463 // find function and execute it
464 mapFunc::const_iterator funcitr = mf.find(function);
465 if (funcitr != mf.end())
466 return (this->*funcitr->second)(arg);
467
468 LOGERR("Unknown action '%s'\n", function.c_str());
469 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400470}
471
472void GUIAction::operation_start(const string operation_name)
473{
that3f7b1ac2014-12-30 11:30:13 +0100474 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000475 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 DataManager::SetValue(TW_ACTION_BUSY, 1);
477 DataManager::SetValue("ui_progress", 0);
478 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600480 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481}
482
that3f7b1ac2014-12-30 11:30:13 +0100483void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400484{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000485 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 DataManager::SetValue("ui_progress", 100);
488 if (simulate) {
489 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
490 if (simulate_fail != 0)
491 DataManager::SetValue("tw_operation_status", 1);
492 else
493 DataManager::SetValue("tw_operation_status", 0);
494 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500495 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500497 }
498 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 }
502 DataManager::SetValue("tw_operation_state", 1);
503 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500504 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000505 time(&Stop);
506 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600507 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100508 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509}
510
that3f7b1ac2014-12-30 11:30:13 +0100511int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400512{
that3f7b1ac2014-12-30 11:30:13 +0100513 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400514
that3f7b1ac2014-12-30 11:30:13 +0100515 sync();
516 DataManager::SetValue("tw_gui_done", 1);
517 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400518
that3f7b1ac2014-12-30 11:30:13 +0100519 return 0;
520}
Dees_Troy51a0e822012-09-05 15:24:24 -0400521
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500522int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100523{
524 PageManager::SelectPackage("TWRP");
525 gui_changePage("main");
526 return 0;
527}
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
that3f7b1ac2014-12-30 11:30:13 +0100529int GUIAction::key(std::string arg)
530{
531 const int key = getKeyByName(arg);
532 PageManager::NotifyKey(key, true);
533 PageManager::NotifyKey(key, false);
534 return 0;
535}
536
537int GUIAction::page(std::string arg)
538{
539 std::string page_name = gui_parse_text(arg);
540 return gui_changePage(page_name);
541}
542
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500543int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100544{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500545 PageManager::RequestReload();
546 // The actual reload is handled in pages.cpp in PageManager::RunReload()
547 // The reload will occur on the next Update or Render call and will
548 // be performed in the rendoer thread instead of the action thread
549 // to prevent crashing which could occur when we start deleting
550 // GUI resources in the action thread while we attempt to render
551 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100552 return 0;
553}
Dees_Troy51a0e822012-09-05 15:24:24 -0400554
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500555int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100556{
557 string Restore_Name;
558 DataManager::GetValue("tw_restore", Restore_Name);
559 PartitionManager.Set_Restore_Files(Restore_Name);
560 return 0;
561}
562
563int GUIAction::set(std::string arg)
564{
565 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200566 {
that3f7b1ac2014-12-30 11:30:13 +0100567 string varName = arg.substr(0, arg.find('='));
568 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400569
that3f7b1ac2014-12-30 11:30:13 +0100570 DataManager::GetValue(value, value);
571 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200572 }
that3f7b1ac2014-12-30 11:30:13 +0100573 else
574 DataManager::SetValue(arg, "1");
575 return 0;
576}
Dees_Troy51a0e822012-09-05 15:24:24 -0400577
that3f7b1ac2014-12-30 11:30:13 +0100578int GUIAction::clear(std::string arg)
579{
580 DataManager::SetValue(arg, "0");
581 return 0;
582}
Dees_Troy51a0e822012-09-05 15:24:24 -0400583
that3f7b1ac2014-12-30 11:30:13 +0100584int GUIAction::mount(std::string arg)
585{
586 if (arg == "usb") {
587 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400588 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100589 PartitionManager.usb_storage_enable();
590 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500591 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100592 } else if (!simulate) {
593 PartitionManager.Mount_By_Path(arg, true);
594 PartitionManager.Add_MTP_Storage(arg);
595 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500596 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100597 return 0;
598}
599
600int GUIAction::unmount(std::string arg)
601{
602 if (arg == "usb") {
603 if (!simulate)
604 PartitionManager.usb_storage_disable();
605 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100607 DataManager::SetValue(TW_ACTION_BUSY, 0);
608 } else if (!simulate) {
609 PartitionManager.UnMount_By_Path(arg, true);
610 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500611 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100612 return 0;
613}
614
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500615int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100616{
617 operation_start("Restore Defaults");
618 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500619 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100620 else {
621 DataManager::ResetDefaults();
622 PartitionManager.Update_System_Details();
623 PartitionManager.Mount_Current_Storage(true);
624 }
625 operation_end(0);
626 return 0;
627}
628
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500629int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100630{
631 operation_start("Copy Log");
632 if (!simulate)
633 {
634 string dst;
635 PartitionManager.Mount_Current_Storage(true);
636 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
637 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
638 tw_set_default_metadata(dst.c_str());
639 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100641 } else
642 simulate_progress_bar();
643 operation_end(0);
644 return 0;
645}
646
647
648int GUIAction::compute(std::string arg)
649{
650 if (arg.find("+") != string::npos)
651 {
652 string varName = arg.substr(0, arg.find('+'));
653 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
654 int amount_to_add = atoi(string_to_add.c_str());
655 int value;
656
657 DataManager::GetValue(varName, value);
658 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 return 0;
660 }
that3f7b1ac2014-12-30 11:30:13 +0100661 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 {
that3f7b1ac2014-12-30 11:30:13 +0100663 string varName = arg.substr(0, arg.find('-'));
664 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
665 int amount_to_subtract = atoi(string_to_subtract.c_str());
666 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400667
that3f7b1ac2014-12-30 11:30:13 +0100668 DataManager::GetValue(varName, value);
669 value -= amount_to_subtract;
670 if (value <= 0)
671 value = 0;
672 DataManager::SetValue(varName, value);
673 return 0;
674 }
675 if (arg.find("*") != string::npos)
676 {
677 string varName = arg.substr(0, arg.find('*'));
678 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
679 int multiply_by = atoi(multiply_by_str.c_str());
680 int value;
681
682 DataManager::GetValue(varName, value);
683 DataManager::SetValue(varName, value*multiply_by);
684 return 0;
685 }
686 if (arg.find("/") != string::npos)
687 {
688 string varName = arg.substr(0, arg.find('/'));
689 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
690 int divide_by = atoi(divide_by_str.c_str());
691 int value;
692
693 if(divide_by != 0)
694 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400695 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100696 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 }
698 return 0;
699 }
that3f7b1ac2014-12-30 11:30:13 +0100700 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
701 return -1;
702}
Dees_Troy51a0e822012-09-05 15:24:24 -0400703
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500704int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100705{
706 string SelectedZone;
707 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
708 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
709 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
710
711 int dst;
712 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
713
714 string offset;
715 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
716
717 string NewTimeZone = Zone;
718 if (offset != "0")
719 NewTimeZone += ":" + offset;
720
721 if (dst != 0)
722 NewTimeZone += DSTZone;
723
724 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
725 DataManager::update_tz_environment_variables();
726 return 0;
727}
728
729int GUIAction::overlay(std::string arg)
730{
731 return gui_changeOverlay(arg);
732}
733
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500734int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100735{
736 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500737 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 return 0;
739 }
that3f7b1ac2014-12-30 11:30:13 +0100740 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
741 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
742 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100744 }
745 return 0;
746}
747
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500748int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100749{
750 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500751 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100753 } else {
754 zip_queue_index--;
755 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400756 }
that3f7b1ac2014-12-30 11:30:13 +0100757 return 0;
758}
Dees_Troy51a0e822012-09-05 15:24:24 -0400759
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500760int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100761{
762 zip_queue_index = 0;
763 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
764 return 0;
765}
Dees_Troy51a0e822012-09-05 15:24:24 -0400766
that3f7b1ac2014-12-30 11:30:13 +0100767int GUIAction::sleep(std::string arg)
768{
769 operation_start("Sleep");
770 usleep(atoi(arg.c_str()));
771 operation_end(0);
772 return 0;
773}
Dees Troyb21cc642013-09-10 17:36:41 +0000774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500775int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100776{
777 operation_start("AppendDateToBackupName");
778 string Backup_Name;
779 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
780 Backup_Name += TWFunc::Get_Current_Date();
781 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
782 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
783 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
784 operation_end(0);
785 return 0;
786}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500787
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500788int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100789{
790 operation_start("GenerateBackupName");
791 TWFunc::Auto_Generate_Backup_Name();
792 operation_end(0);
793 return 0;
794}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500795
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500796int GUIAction::checkpartitionlist(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100797{
798 string Wipe_List, wipe_path;
799 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500800
that3f7b1ac2014-12-30 11:30:13 +0100801 DataManager::GetValue("tw_wipe_list", Wipe_List);
802 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
803 if (!Wipe_List.empty()) {
804 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
805 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
806 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
807 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
808 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
809 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 } else {
that3f7b1ac2014-12-30 11:30:13 +0100811 count++;
812 }
813 start_pos = end_pos + 1;
814 end_pos = Wipe_List.find(";", start_pos);
815 }
816 DataManager::SetValue("tw_check_partition_list", count);
817 } else {
818 DataManager::SetValue("tw_check_partition_list", 0);
819 }
820 return 0;
821}
Dees_Troy51a0e822012-09-05 15:24:24 -0400822
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500823int GUIAction::getpartitiondetails(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100824{
825 string Wipe_List, wipe_path;
826 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400827
that3f7b1ac2014-12-30 11:30:13 +0100828 DataManager::GetValue("tw_wipe_list", Wipe_List);
829 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
830 if (!Wipe_List.empty()) {
831 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
832 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
833 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
834 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
835 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
836 // Do nothing
837 } else {
838 DataManager::SetValue("tw_partition_path", wipe_path);
839 break;
840 }
841 start_pos = end_pos + 1;
842 end_pos = Wipe_List.find(";", start_pos);
843 }
844 if (!wipe_path.empty()) {
845 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
846 if (Part) {
847 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500848
that3f7b1ac2014-12-30 11:30:13 +0100849 DataManager::SetValue("tw_partition_name", Part->Display_Name);
850 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
851 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
852 DataManager::SetValue("tw_partition_size", Part->Size / mb);
853 DataManager::SetValue("tw_partition_used", Part->Used / mb);
854 DataManager::SetValue("tw_partition_free", Part->Free / mb);
855 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
856 DataManager::SetValue("tw_partition_removable", Part->Removable);
857 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
858
859 if (Part->Can_Repair())
860 DataManager::SetValue("tw_partition_can_repair", 1);
861 else
862 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500863 if (Part->Can_Resize())
864 DataManager::SetValue("tw_partition_can_resize", 1);
865 else
866 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600867 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100868 DataManager::SetValue("tw_partition_vfat", 1);
869 else
870 DataManager::SetValue("tw_partition_vfat", 0);
871 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
872 DataManager::SetValue("tw_partition_exfat", 1);
873 else
874 DataManager::SetValue("tw_partition_exfat", 0);
875 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
876 DataManager::SetValue("tw_partition_f2fs", 1);
877 else
878 DataManager::SetValue("tw_partition_f2fs", 0);
879 if (TWFunc::Path_Exists("/sbin/mke2fs"))
880 DataManager::SetValue("tw_partition_ext", 1);
881 else
882 DataManager::SetValue("tw_partition_ext", 0);
883 return 0;
884 } else {
885 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
886 }
887 }
888 }
889 DataManager::SetValue("tw_partition_name", "");
890 DataManager::SetValue("tw_partition_file_system", "");
891 return 0;
892}
893
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500894int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100895{
896 time_t tm;
897 char path[256];
898 int path_len;
899 uid_t uid = -1;
900 gid_t gid = -1;
901
902 struct passwd *pwd = getpwnam("media_rw");
903 if(pwd) {
904 uid = pwd->pw_uid;
905 gid = pwd->pw_gid;
906 }
907
908 const std::string storage = DataManager::GetCurrentStoragePath();
909 if(PartitionManager.Is_Mounted_By_Path(storage)) {
910 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
911 } else {
912 strcpy(path, "/tmp/");
913 }
914
915 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
916 return 0;
917
918 tm = time(NULL);
919 path_len = strlen(path);
920
921 // Screenshot_2014-01-01-18-21-38.png
922 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
923
924 int res = gr_save_screenshot(path);
925 if(res == 0) {
926 chmod(path, 0666);
927 chown(path, uid, gid);
928
Ethan Yonker74db1572015-10-28 12:44:49 -0500929 gui_msg(Msg("screenshot_saved=Screenshot was saved to %s")(path));
that3f7b1ac2014-12-30 11:30:13 +0100930
931 // blink to notify that the screenshow was taken
932 gr_color(255, 255, 255, 255);
933 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
934 gr_flip();
935 gui_forceRender();
936 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500937 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100938 }
939 return 0;
940}
941
942int GUIAction::setbrightness(std::string arg)
943{
944 return TWFunc::Set_Brightness(arg);
945}
946
947int GUIAction::fileexists(std::string arg)
948{
949 struct stat st;
950 string newpath = arg + "/.";
951
952 operation_start("FileExists");
953 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
954 operation_end(0);
955 else
956 operation_end(1);
957 return 0;
958}
959
thatcc8ddca2015-01-03 01:59:36 +0100960void GUIAction::reinject_after_flash()
961{
962 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500963 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100964 if (simulate) {
965 simulate_progress_bar();
966 } else {
967 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
968 if (Boot == NULL || Boot->Current_File_System != "emmc")
969 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
970 else {
971 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
972 TWFunc::Exec_Cmd(injectcmd);
973 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500974 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100975 }
976 }
977}
978
that3f7b1ac2014-12-30 11:30:13 +0100979int GUIAction::flash(std::string arg)
980{
981 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600982 // We're going to jump to this page first, like a loading page
983 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100984 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200985 string zip_path = zip_queue[i];
986 size_t slashpos = zip_path.find_last_of('/');
987 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100988 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200989 DataManager::SetValue("tw_filename", zip_path);
990 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100991 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
992
993 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +0200994 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100995 TWFunc::SetPerformanceMode(false);
996 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500997 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +0100998 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600999 break;
that3f7b1ac2014-12-30 11:30:13 +01001000 }
1001 }
1002 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001003
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001004 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001005 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001006 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001007 }
that3f7b1ac2014-12-30 11:30:13 +01001008
thatcc8ddca2015-01-03 01:59:36 +01001009 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001010 PartitionManager.Update_System_Details();
1011 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001012 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001013 return 0;
1014}
1015
1016int GUIAction::wipe(std::string arg)
1017{
1018 operation_start("Format");
1019 DataManager::SetValue("tw_partition", arg);
1020
1021 int ret_val = false;
1022
1023 if (simulate) {
1024 simulate_progress_bar();
1025 } else {
1026 if (arg == "data")
1027 ret_val = PartitionManager.Factory_Reset();
1028 else if (arg == "battery")
1029 ret_val = PartitionManager.Wipe_Battery_Stats();
1030 else if (arg == "rotate")
1031 ret_val = PartitionManager.Wipe_Rotate_Data();
1032 else if (arg == "dalvik")
1033 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1034 else if (arg == "DATAMEDIA") {
1035 ret_val = PartitionManager.Format_Data();
1036 } else if (arg == "INTERNAL") {
1037 int has_datamedia, dual_storage;
1038
1039 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1040 if (has_datamedia) {
1041 ret_val = PartitionManager.Wipe_Media_From_Data();
1042 } else {
1043 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1044 }
1045 } else if (arg == "EXTERNAL") {
1046 string External_Path;
1047
1048 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1049 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1050 } else if (arg == "ANDROIDSECURE") {
1051 ret_val = PartitionManager.Wipe_Android_Secure();
1052 } else if (arg == "LIST") {
1053 string Wipe_List, wipe_path;
1054 bool skip = false;
1055 ret_val = true;
1056 TWPartition* wipe_part = NULL;
1057
1058 DataManager::GetValue("tw_wipe_list", Wipe_List);
1059 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1060 if (!Wipe_List.empty()) {
1061 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1062 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1063 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1064 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1065 if (wipe_path == "/and-sec") {
1066 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001067 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001068 ret_val = false;
1069 break;
1070 } else {
1071 skip = true;
1072 }
1073 } else if (wipe_path == "DALVIK") {
1074 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001075 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001076 ret_val = false;
1077 break;
1078 } else {
1079 skip = true;
1080 }
1081 } else if (wipe_path == "INTERNAL") {
1082 if (!PartitionManager.Wipe_Media_From_Data()) {
1083 ret_val = false;
1084 break;
1085 } else {
1086 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001087 }
1088 }
that3f7b1ac2014-12-30 11:30:13 +01001089 if (!skip) {
1090 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001091 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001092 ret_val = false;
1093 break;
1094 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1095 arg = wipe_path;
1096 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001097 } else {
that3f7b1ac2014-12-30 11:30:13 +01001098 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001099 }
that3f7b1ac2014-12-30 11:30:13 +01001100 start_pos = end_pos + 1;
1101 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001102 }
1103 }
that3f7b1ac2014-12-30 11:30:13 +01001104 } else
1105 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001106#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001107 if (arg == DataManager::GetSettingsStoragePath()) {
1108 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1109 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001110
that3f7b1ac2014-12-30 11:30:13 +01001111 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1112 LOGINFO("Making TWRP folder and saving settings.\n");
1113 Storage_Path += "/TWRP";
1114 mkdir(Storage_Path.c_str(), 0777);
1115 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001116 } else {
that3f7b1ac2014-12-30 11:30:13 +01001117 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1118 }
1119 }
1120#endif
1121 }
1122 PartitionManager.Update_System_Details();
1123 if (ret_val)
1124 ret_val = 0; // 0 is success
1125 else
1126 ret_val = 1; // 1 is failure
1127 operation_end(ret_val);
1128 return 0;
1129}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001130
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001131int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001132{
1133 operation_start("Refreshing Sizes");
1134 if (simulate) {
1135 simulate_progress_bar();
1136 } else
1137 PartitionManager.Update_System_Details();
1138 operation_end(0);
1139 return 0;
1140}
1141
1142int GUIAction::nandroid(std::string arg)
1143{
that3f7b1ac2014-12-30 11:30:13 +01001144 if (simulate) {
that73a52952015-01-28 23:07:34 +01001145 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001146 DataManager::SetValue("tw_partition", "Simulation");
1147 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001148 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001149 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001150 operation_start("Nandroid");
1151 int ret = 0;
1152
that3f7b1ac2014-12-30 11:30:13 +01001153 if (arg == "backup") {
1154 string Backup_Name;
1155 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001156 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1157 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001158 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1159 ret = PartitionManager.Run_Backup();
1160 }
1161 else {
1162 operation_end(1);
1163 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001164 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001165 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001166 } else if (arg == "restore") {
1167 string Restore_Name;
1168 DataManager::GetValue("tw_restore", Restore_Name);
1169 ret = PartitionManager.Run_Restore(Restore_Name);
1170 } else {
1171 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001172 return -1;
1173 }
1174 DataManager::SetValue("tw_encrypt_backup", 0);
1175 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001176 if (ret == false)
1177 ret = 1; // 1 for failure
1178 else
1179 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001180 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001181 }
1182 else {
1183 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001184 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001185 ret = 0;
1186 }
that73a52952015-01-28 23:07:34 +01001187 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001188 return ret;
1189 }
1190 return 0;
1191}
1192
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001193int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001194 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 }
1197 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 int op_status = PartitionManager.Cancel_Backup();
1199 if (op_status != 0)
1200 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 }
1202
1203 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001204}
Dees_Troy51a0e822012-09-05 15:24:24 -04001205
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001206int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001207{
that73a52952015-01-28 23:07:34 +01001208 int op_status = 0;
1209
that3f7b1ac2014-12-30 11:30:13 +01001210 operation_start("Fix Permissions");
1211 LOGINFO("fix permissions started!\n");
1212 if (simulate) {
1213 simulate_progress_bar();
1214 } else {
that73a52952015-01-28 23:07:34 +01001215 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001216 if (op_status != 0)
1217 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001218 }
that73a52952015-01-28 23:07:34 +01001219 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001220 return 0;
1221}
1222
1223int GUIAction::dd(std::string arg)
1224{
1225 operation_start("imaging");
1226
1227 if (simulate) {
1228 simulate_progress_bar();
1229 } else {
1230 string cmd = "dd " + arg;
1231 TWFunc::Exec_Cmd(cmd);
1232 }
1233 operation_end(0);
1234 return 0;
1235}
1236
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001237int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001238{
1239 operation_start("Partition SD Card");
1240 int ret_val = 0;
1241
1242 if (simulate) {
1243 simulate_progress_bar();
1244 } else {
1245 int allow_partition;
1246 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1247 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001248 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001249 } else {
1250 if (!PartitionManager.Partition_SDCard())
1251 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001252 }
that3f7b1ac2014-12-30 11:30:13 +01001253 }
1254 operation_end(ret_val);
1255 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001256
that3f7b1ac2014-12-30 11:30:13 +01001257}
Dees_Troy51a0e822012-09-05 15:24:24 -04001258
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001259int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001260{
1261 operation_start("Install HTC Dumlock");
1262 if (simulate) {
1263 simulate_progress_bar();
1264 } else
1265 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001266
that3f7b1ac2014-12-30 11:30:13 +01001267 operation_end(0);
1268 return 0;
1269}
Dees_Troy51a0e822012-09-05 15:24:24 -04001270
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001271int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001272{
1273 operation_start("HTC Dumlock Restore Boot");
1274 if (simulate) {
1275 simulate_progress_bar();
1276 } else
1277 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001278
that3f7b1ac2014-12-30 11:30:13 +01001279 operation_end(0);
1280 return 0;
1281}
Dees_Troy51a0e822012-09-05 15:24:24 -04001282
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001283int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001284{
1285 operation_start("HTC Dumlock Reflash Recovery");
1286 if (simulate) {
1287 simulate_progress_bar();
1288 } else
1289 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001290
that3f7b1ac2014-12-30 11:30:13 +01001291 operation_end(0);
1292 return 0;
1293}
Dees_Troy51a0e822012-09-05 15:24:24 -04001294
that3f7b1ac2014-12-30 11:30:13 +01001295int GUIAction::cmd(std::string arg)
1296{
1297 int op_status = 0;
1298
1299 operation_start("Command");
1300 LOGINFO("Running command: '%s'\n", arg.c_str());
1301 if (simulate) {
1302 simulate_progress_bar();
1303 } else {
1304 op_status = TWFunc::Exec_Cmd(arg);
1305 if (op_status != 0)
1306 op_status = 1;
1307 }
1308
1309 operation_end(op_status);
1310 return 0;
1311}
1312
1313int GUIAction::terminalcommand(std::string arg)
1314{
1315 int op_status = 0;
1316 string cmdpath, command;
1317
1318 DataManager::GetValue("tw_terminal_location", cmdpath);
1319 operation_start("CommandOutput");
1320 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1321 if (simulate) {
1322 simulate_progress_bar();
1323 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001324 } else if (arg == "exit") {
1325 LOGINFO("Exiting terminal\n");
1326 operation_end(op_status);
1327 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001328 } else {
1329 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1330 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001331 DataManager::SetValue("tw_terminal_state", 1);
1332 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001333 FILE* fp;
1334 char line[512];
1335
1336 fp = popen(command.c_str(), "r");
1337 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001338 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001339 } else {
1340 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1341 struct timeval timeout;
1342 fd_set fdset;
1343
1344 while(keep_going)
1345 {
1346 FD_ZERO(&fdset);
1347 FD_SET(fd, &fdset);
1348 timeout.tv_sec = 0;
1349 timeout.tv_usec = 400000;
1350 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1351 if (has_data == 0) {
1352 // Timeout reached
1353 DataManager::GetValue("tw_terminal_state", check);
1354 if (check == 0) {
1355 keep_going = 0;
1356 }
1357 } else if (has_data < 0) {
1358 // End of execution
1359 keep_going = 0;
1360 } else {
1361 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001362 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001363 gui_print("%s", line); // Display output
1364 else
1365 keep_going = 0; // Done executing
1366 }
1367 }
1368 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001369 }
thatc6085482015-01-09 22:12:43 +01001370 DataManager::SetValue("tw_operation_status", 0);
1371 DataManager::SetValue("tw_operation_state", 1);
1372 DataManager::SetValue("tw_terminal_state", 0);
1373 DataManager::SetValue("tw_background_thread_running", 0);
1374 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001375 }
1376 return 0;
1377}
1378
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001379int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001380{
1381 int op_status = 0;
1382
1383 LOGINFO("Sending kill command...\n");
1384 operation_start("KillCommand");
1385 DataManager::SetValue("tw_operation_status", 0);
1386 DataManager::SetValue("tw_operation_state", 1);
1387 DataManager::SetValue("tw_terminal_state", 0);
1388 DataManager::SetValue("tw_background_thread_running", 0);
1389 DataManager::SetValue(TW_ACTION_BUSY, 0);
1390 return 0;
1391}
1392
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001393int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001394{
1395 int op_status = 0;
1396 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001397 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001398 if (simulate) {
1399 simulate_progress_bar();
1400 } else {
1401 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001402 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001403 }
1404
1405 operation_end(op_status);
1406 return 0;
1407}
1408
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001409int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001410{
1411 int op_status = 0;
1412
1413 operation_start("CheckBackupName");
1414 if (simulate) {
1415 simulate_progress_bar();
1416 } else {
1417 op_status = PartitionManager.Check_Backup_Name(true);
1418 if (op_status != 0)
1419 op_status = 1;
1420 }
1421
1422 operation_end(op_status);
1423 return 0;
1424}
1425
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001426int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001427{
1428 int op_status = 0;
1429
1430 operation_start("Decrypt");
1431 if (simulate) {
1432 simulate_progress_bar();
1433 } else {
1434 string Password;
1435 DataManager::GetValue("tw_crypto_password", Password);
1436 op_status = PartitionManager.Decrypt_Device(Password);
1437 if (op_status != 0)
1438 op_status = 1;
1439 else {
that3f7b1ac2014-12-30 11:30:13 +01001440
1441 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1442
Ethan Yonkercf50da52015-01-12 21:59:07 -06001443 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001444
Ethan Yonkercf50da52015-01-12 21:59:07 -06001445 // Check for a custom theme and load it if exists
1446 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1447 if (has_datamedia != 0) {
1448 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001449 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001450 } else {
1451 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001452 }
1453 }
1454 }
1455 }
1456
1457 operation_end(op_status);
1458 return 0;
1459}
1460
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001461int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001462{
1463 operation_start("Sideload");
1464 if (simulate) {
1465 simulate_progress_bar();
1466 operation_end(0);
1467 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001468 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001469 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1470
1471 // wait for the adb connection
1472 int ret = apply_from_adb("/", &sideload_child_pid);
1473 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1474
1475 if (ret != 0) {
1476 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001477 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001478 ret = 1; // failure
1479 } else {
1480 int wipe_cache = 0;
1481 int wipe_dalvik = 0;
1482 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1483
1484 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1485 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1486 PartitionManager.Wipe_By_Path("/cache");
1487 if (wipe_dalvik)
1488 PartitionManager.Wipe_Dalvik_Cache();
1489 } else {
1490 ret = 1; // failure
1491 }
thatcc8ddca2015-01-03 01:59:36 +01001492 }
thatc6085482015-01-09 22:12:43 +01001493 if (sideload_child_pid) {
1494 LOGINFO("Signaling child sideload process to exit.\n");
1495 struct stat st;
1496 // Calling stat() on this magic filename signals the minadbd
1497 // subprocess to shut down.
1498 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1499 int status;
1500 LOGINFO("Waiting for child sideload process to exit.\n");
1501 waitpid(sideload_child_pid, &status, 0);
1502 }
Dees Troy28a85d22015-04-28 02:03:16 +00001503 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001504 TWFunc::Toggle_MTP(mtp_was_enabled);
1505 reinject_after_flash();
1506 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001507 }
that3f7b1ac2014-12-30 11:30:13 +01001508 return 0;
1509}
1510
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001511int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001512{
that3f7b1ac2014-12-30 11:30:13 +01001513 struct stat st;
1514 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001515 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001516 LOGINFO("Signaling child sideload process to exit.\n");
1517 // Calling stat() on this magic filename signals the minadbd
1518 // subprocess to shut down.
1519 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1520 if (!sideload_child_pid) {
1521 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001522 return 0;
1523 }
thatcc8ddca2015-01-03 01:59:36 +01001524 ::sleep(1);
1525 LOGINFO("Killing child sideload process.\n");
1526 kill(sideload_child_pid, SIGTERM);
1527 int status;
1528 LOGINFO("Waiting for child sideload process to exit.\n");
1529 waitpid(sideload_child_pid, &status, 0);
1530 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001531 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1532 return 0;
1533}
1534
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001535int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001536{
Ethan Yonkere1abe612015-06-09 11:09:32 -05001537 int op_status = 1;
1538
that3f7b1ac2014-12-30 11:30:13 +01001539 operation_start("OpenRecoveryScript");
1540 if (simulate) {
1541 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001542 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001543 } else {
thatc6085482015-01-09 22:12:43 +01001544 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1545 // that we converted to ORS commands during boot in recovery.cpp.
1546 // Run those first.
1547 int reboot = 0;
1548 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001549 gui_msg("running_recovery_commands=Running Recovery Commands");
thatc6085482015-01-09 22:12:43 +01001550 if (OpenRecoveryScript::run_script_file() == 0) {
1551 reboot = 1;
Matt Mower85161af2015-08-08 10:00:50 -05001552 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001553 }
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()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001557 gui_msg("running_ors=Running OpenRecoveryScript");
thatc6085482015-01-09 22:12:43 +01001558 if (OpenRecoveryScript::run_script_file() == 0) {
1559 reboot = 1;
Ethan Yonkere1abe612015-06-09 11:09:32 -05001560 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001561 }
1562 }
1563 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001564 // Disable stock recovery reflashing
1565 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001566 usleep(2000000); // Sleep for 2 seconds before rebooting
1567 TWFunc::tw_reboot(rb_system);
Ethan Yonkere1abe612015-06-09 11:09:32 -05001568 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
thatc6085482015-01-09 22:12:43 +01001569 } else {
1570 DataManager::SetValue("tw_page_done", 1);
1571 }
Ethan Yonkere1abe612015-06-09 11:09:32 -05001572 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001573 }
1574 return 0;
1575}
1576
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001577int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001578{
1579 int op_status = 0;
1580
1581 operation_start("Install SuperSU");
1582 if (simulate) {
1583 simulate_progress_bar();
1584 } else {
1585 if (!TWFunc::Install_SuperSU())
1586 op_status = 1;
1587 }
1588
1589 operation_end(op_status);
1590 return 0;
1591}
1592
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001593int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001594{
1595 int op_status = 0;
1596
1597 operation_start("Fixing Superuser Permissions");
1598 if (simulate) {
1599 simulate_progress_bar();
1600 } else {
1601 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1602 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1603 }
1604
1605 operation_end(op_status);
1606 return 0;
1607}
1608
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001609int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001610{
1611 int op_status = 0;
1612
1613 operation_start("Try Restore Decrypt");
1614 if (simulate) {
1615 simulate_progress_bar();
1616 } else {
1617 string Restore_Path, Filename, Password;
1618 DataManager::GetValue("tw_restore", Restore_Path);
1619 Restore_Path += "/";
1620 DataManager::GetValue("tw_restore_password", Password);
1621 TWFunc::SetPerformanceMode(true);
1622 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1623 op_status = 0; // success
1624 else
1625 op_status = 1; // fail
1626 TWFunc::SetPerformanceMode(false);
1627 }
1628
1629 operation_end(op_status);
1630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
1635 int op_status = 0;
1636
1637 operation_start("Repair Partition");
1638 if (simulate) {
1639 simulate_progress_bar();
1640 } else {
1641 string part_path;
1642 DataManager::GetValue("tw_partition_mount_point", part_path);
1643 if (PartitionManager.Repair_By_Path(part_path, true)) {
1644 op_status = 0; // success
1645 } else {
that3f7b1ac2014-12-30 11:30:13 +01001646 op_status = 1; // fail
1647 }
1648 }
1649
1650 operation_end(op_status);
1651 return 0;
1652}
1653
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001654int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001655{
1656 int op_status = 0;
1657
1658 operation_start("Resize Partition");
1659 if (simulate) {
1660 simulate_progress_bar();
1661 } else {
1662 string part_path;
1663 DataManager::GetValue("tw_partition_mount_point", part_path);
1664 if (PartitionManager.Resize_By_Path(part_path, true)) {
1665 op_status = 0; // success
1666 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001667 op_status = 1; // fail
1668 }
1669 }
1670
1671 operation_end(op_status);
1672 return 0;
1673}
1674
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001675int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001676{
1677 int op_status = 0;
1678
1679 operation_start("Change File System");
1680 if (simulate) {
1681 simulate_progress_bar();
1682 } else {
1683 string part_path, file_system;
1684 DataManager::GetValue("tw_partition_mount_point", part_path);
1685 DataManager::GetValue("tw_action_new_file_system", file_system);
1686 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1687 op_status = 0; // success
1688 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001689 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001690 op_status = 1; // fail
1691 }
1692 }
1693 PartitionManager.Update_System_Details();
1694 operation_end(op_status);
1695 return 0;
1696}
1697
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001698int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001699{
1700 int op_status = 0;
1701
1702 operation_start("Start MTP");
1703 if (PartitionManager.Enable_MTP())
1704 op_status = 0; // success
1705 else
1706 op_status = 1; // fail
1707
1708 operation_end(op_status);
1709 return 0;
1710}
1711
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001712int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001713{
1714 int op_status = 0;
1715
1716 operation_start("Stop MTP");
1717 if (PartitionManager.Disable_MTP())
1718 op_status = 0; // success
1719 else
1720 op_status = 1; // fail
1721
1722 operation_end(op_status);
1723 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001724}
1725
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001726int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001727{
1728 int op_status = 0;
1729
1730 operation_start("Flash Image");
1731 string path, filename, full_filename;
1732 DataManager::GetValue("tw_zip_location", path);
1733 DataManager::GetValue("tw_file", filename);
1734 full_filename = path + "/" + filename;
1735 if (PartitionManager.Flash_Image(full_filename))
1736 op_status = 0; // success
1737 else
1738 op_status = 1; // fail
1739
1740 operation_end(op_status);
1741 return 0;
1742}
1743
Dees_Troy51a0e822012-09-05 15:24:24 -04001744int GUIAction::getKeyByName(std::string key)
1745{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001746 if (key == "home") return KEY_HOME;
1747 else if (key == "menu") return KEY_MENU;
1748 else if (key == "back") return KEY_BACK;
1749 else if (key == "search") return KEY_SEARCH;
1750 else if (key == "voldown") return KEY_VOLUMEDOWN;
1751 else if (key == "volup") return KEY_VOLUMEUP;
1752 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001753 int ret_val;
1754 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1755 if (!ret_val)
1756 return KEY_POWER;
1757 else
1758 return ret_val;
1759 }
1760
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001761 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001762}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001763
1764int GUIAction::checkpartitionlifetimewrites(std::string arg)
1765{
1766 int op_status = 0;
1767 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1768
1769 operation_start("Check Partition Lifetime Writes");
1770 if (sys) {
1771 if (sys->Check_Lifetime_Writes() != 0)
1772 DataManager::SetValue("tw_lifetime_writes", 1);
1773 else
1774 DataManager::SetValue("tw_lifetime_writes", 0);
1775 op_status = 0; // success
1776 } else {
1777 DataManager::SetValue("tw_lifetime_writes", 1);
1778 op_status = 1; // fail
1779 }
1780
1781 operation_end(op_status);
1782 return 0;
1783}
1784
1785int GUIAction::mountsystemtoggle(std::string arg)
1786{
1787 int op_status = 0;
1788 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001789 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001790
1791 operation_start("Toggle System Mount");
1792 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1793 op_status = 1; // fail
1794 } else {
1795 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1796 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001797 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001798 DataManager::SetValue("tw_mount_system_ro", 0);
1799 Part->Change_Mount_Read_Only(false);
1800 } else {
1801 DataManager::SetValue("tw_mount_system_ro", 1);
1802 Part->Change_Mount_Read_Only(true);
1803 }
1804 if (remount_system) {
1805 Part->Mount(true);
1806 }
1807 op_status = 0; // success
1808 } else {
1809 op_status = 1; // fail
1810 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001811 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1812 if (Part) {
1813 if (arg == "0") {
1814 Part->Change_Mount_Read_Only(false);
1815 } else {
1816 Part->Change_Mount_Read_Only(true);
1817 }
1818 if (remount_vendor) {
1819 Part->Mount(true);
1820 }
1821 op_status = 0; // success
1822 } else {
1823 op_status = 1; // fail
1824 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001825 }
1826
1827 operation_end(op_status);
1828 return 0;
1829}
Ethan Yonker74db1572015-10-28 12:44:49 -05001830
1831int GUIAction::setlanguage(std::string arg __unused)
1832{
1833 int op_status = 0;
1834
1835 operation_start("Set Language");
1836 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1837 PageManager::RequestReload();
1838 op_status = 0; // success
1839
1840 operation_end(op_status);
1841 return 0;
1842}