blob: 20944dac772e3e30973bbcd2e64b45162bea514a [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();
LuK133762326f42015-09-30 19:57:46 +0200505 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000506 time(&Stop);
507 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600508 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100509 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400510}
511
that3f7b1ac2014-12-30 11:30:13 +0100512int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400513{
that3f7b1ac2014-12-30 11:30:13 +0100514 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400515
that3f7b1ac2014-12-30 11:30:13 +0100516 sync();
517 DataManager::SetValue("tw_gui_done", 1);
518 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400519
that3f7b1ac2014-12-30 11:30:13 +0100520 return 0;
521}
Dees_Troy51a0e822012-09-05 15:24:24 -0400522
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500523int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100524{
525 PageManager::SelectPackage("TWRP");
526 gui_changePage("main");
527 return 0;
528}
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
that3f7b1ac2014-12-30 11:30:13 +0100530int GUIAction::key(std::string arg)
531{
532 const int key = getKeyByName(arg);
533 PageManager::NotifyKey(key, true);
534 PageManager::NotifyKey(key, false);
535 return 0;
536}
537
538int GUIAction::page(std::string arg)
539{
LuK133762326f42015-09-30 19:57:46 +0200540 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100541 std::string page_name = gui_parse_text(arg);
542 return gui_changePage(page_name);
543}
544
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500545int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100546{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500547 PageManager::RequestReload();
548 // The actual reload is handled in pages.cpp in PageManager::RunReload()
549 // The reload will occur on the next Update or Render call and will
550 // be performed in the rendoer thread instead of the action thread
551 // to prevent crashing which could occur when we start deleting
552 // GUI resources in the action thread while we attempt to render
553 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100554 return 0;
555}
Dees_Troy51a0e822012-09-05 15:24:24 -0400556
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500557int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100558{
559 string Restore_Name;
560 DataManager::GetValue("tw_restore", Restore_Name);
561 PartitionManager.Set_Restore_Files(Restore_Name);
562 return 0;
563}
564
565int GUIAction::set(std::string arg)
566{
567 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200568 {
that3f7b1ac2014-12-30 11:30:13 +0100569 string varName = arg.substr(0, arg.find('='));
570 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400571
that3f7b1ac2014-12-30 11:30:13 +0100572 DataManager::GetValue(value, value);
573 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200574 }
that3f7b1ac2014-12-30 11:30:13 +0100575 else
576 DataManager::SetValue(arg, "1");
577 return 0;
578}
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
that3f7b1ac2014-12-30 11:30:13 +0100580int GUIAction::clear(std::string arg)
581{
582 DataManager::SetValue(arg, "0");
583 return 0;
584}
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
that3f7b1ac2014-12-30 11:30:13 +0100586int GUIAction::mount(std::string arg)
587{
588 if (arg == "usb") {
589 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400590 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100591 PartitionManager.usb_storage_enable();
592 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500593 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100594 } else if (!simulate) {
595 PartitionManager.Mount_By_Path(arg, true);
596 PartitionManager.Add_MTP_Storage(arg);
597 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500598 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100599 return 0;
600}
601
602int GUIAction::unmount(std::string arg)
603{
604 if (arg == "usb") {
605 if (!simulate)
606 PartitionManager.usb_storage_disable();
607 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500608 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100609 DataManager::SetValue(TW_ACTION_BUSY, 0);
610 } else if (!simulate) {
611 PartitionManager.UnMount_By_Path(arg, true);
612 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500613 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100614 return 0;
615}
616
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500617int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100618{
619 operation_start("Restore Defaults");
620 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500621 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100622 else {
623 DataManager::ResetDefaults();
624 PartitionManager.Update_System_Details();
625 PartitionManager.Mount_Current_Storage(true);
626 }
627 operation_end(0);
628 return 0;
629}
630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500631int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100632{
633 operation_start("Copy Log");
634 if (!simulate)
635 {
636 string dst;
637 PartitionManager.Mount_Current_Storage(true);
638 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
639 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
640 tw_set_default_metadata(dst.c_str());
641 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500642 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100643 } else
644 simulate_progress_bar();
645 operation_end(0);
646 return 0;
647}
648
649
650int GUIAction::compute(std::string arg)
651{
652 if (arg.find("+") != string::npos)
653 {
654 string varName = arg.substr(0, arg.find('+'));
655 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
656 int amount_to_add = atoi(string_to_add.c_str());
657 int value;
658
659 DataManager::GetValue(varName, value);
660 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 return 0;
662 }
that3f7b1ac2014-12-30 11:30:13 +0100663 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 {
that3f7b1ac2014-12-30 11:30:13 +0100665 string varName = arg.substr(0, arg.find('-'));
666 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
667 int amount_to_subtract = atoi(string_to_subtract.c_str());
668 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400669
that3f7b1ac2014-12-30 11:30:13 +0100670 DataManager::GetValue(varName, value);
671 value -= amount_to_subtract;
672 if (value <= 0)
673 value = 0;
674 DataManager::SetValue(varName, value);
675 return 0;
676 }
677 if (arg.find("*") != string::npos)
678 {
679 string varName = arg.substr(0, arg.find('*'));
680 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
681 int multiply_by = atoi(multiply_by_str.c_str());
682 int value;
683
684 DataManager::GetValue(varName, value);
685 DataManager::SetValue(varName, value*multiply_by);
686 return 0;
687 }
688 if (arg.find("/") != string::npos)
689 {
690 string varName = arg.substr(0, arg.find('/'));
691 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
692 int divide_by = atoi(divide_by_str.c_str());
693 int value;
694
695 if(divide_by != 0)
696 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100698 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 }
700 return 0;
701 }
that3f7b1ac2014-12-30 11:30:13 +0100702 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
703 return -1;
704}
Dees_Troy51a0e822012-09-05 15:24:24 -0400705
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500706int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100707{
708 string SelectedZone;
709 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
710 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
711 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
712
713 int dst;
714 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
715
716 string offset;
717 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
718
719 string NewTimeZone = Zone;
720 if (offset != "0")
721 NewTimeZone += ":" + offset;
722
723 if (dst != 0)
724 NewTimeZone += DSTZone;
725
726 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
727 DataManager::update_tz_environment_variables();
728 return 0;
729}
730
731int GUIAction::overlay(std::string arg)
732{
733 return gui_changeOverlay(arg);
734}
735
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500736int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100737{
738 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500739 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 return 0;
741 }
that3f7b1ac2014-12-30 11:30:13 +0100742 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
743 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
744 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100746 }
747 return 0;
748}
749
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500750int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100751{
752 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400754 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100755 } else {
756 zip_queue_index--;
757 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 }
that3f7b1ac2014-12-30 11:30:13 +0100759 return 0;
760}
Dees_Troy51a0e822012-09-05 15:24:24 -0400761
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500762int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100763{
764 zip_queue_index = 0;
765 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
766 return 0;
767}
Dees_Troy51a0e822012-09-05 15:24:24 -0400768
that3f7b1ac2014-12-30 11:30:13 +0100769int GUIAction::sleep(std::string arg)
770{
771 operation_start("Sleep");
772 usleep(atoi(arg.c_str()));
773 operation_end(0);
774 return 0;
775}
Dees Troyb21cc642013-09-10 17:36:41 +0000776
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500777int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100778{
779 operation_start("AppendDateToBackupName");
780 string Backup_Name;
781 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
782 Backup_Name += TWFunc::Get_Current_Date();
783 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
784 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
785 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
786 operation_end(0);
787 return 0;
788}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500789
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500790int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100791{
792 operation_start("GenerateBackupName");
793 TWFunc::Auto_Generate_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::checkpartitionlist(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100799{
800 string Wipe_List, wipe_path;
801 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500802
that3f7b1ac2014-12-30 11:30:13 +0100803 DataManager::GetValue("tw_wipe_list", Wipe_List);
804 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
805 if (!Wipe_List.empty()) {
806 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
807 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
808 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
809 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
810 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
811 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400812 } else {
that3f7b1ac2014-12-30 11:30:13 +0100813 count++;
814 }
815 start_pos = end_pos + 1;
816 end_pos = Wipe_List.find(";", start_pos);
817 }
818 DataManager::SetValue("tw_check_partition_list", count);
819 } else {
820 DataManager::SetValue("tw_check_partition_list", 0);
821 }
822 return 0;
823}
Dees_Troy51a0e822012-09-05 15:24:24 -0400824
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500825int GUIAction::getpartitiondetails(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100826{
827 string Wipe_List, wipe_path;
828 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829
that3f7b1ac2014-12-30 11:30:13 +0100830 DataManager::GetValue("tw_wipe_list", Wipe_List);
831 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
832 if (!Wipe_List.empty()) {
833 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
834 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
835 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
836 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
837 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
838 // Do nothing
839 } else {
840 DataManager::SetValue("tw_partition_path", wipe_path);
841 break;
842 }
843 start_pos = end_pos + 1;
844 end_pos = Wipe_List.find(";", start_pos);
845 }
846 if (!wipe_path.empty()) {
847 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
848 if (Part) {
849 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500850
that3f7b1ac2014-12-30 11:30:13 +0100851 DataManager::SetValue("tw_partition_name", Part->Display_Name);
852 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
853 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
854 DataManager::SetValue("tw_partition_size", Part->Size / mb);
855 DataManager::SetValue("tw_partition_used", Part->Used / mb);
856 DataManager::SetValue("tw_partition_free", Part->Free / mb);
857 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
858 DataManager::SetValue("tw_partition_removable", Part->Removable);
859 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
860
861 if (Part->Can_Repair())
862 DataManager::SetValue("tw_partition_can_repair", 1);
863 else
864 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500865 if (Part->Can_Resize())
866 DataManager::SetValue("tw_partition_can_resize", 1);
867 else
868 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600869 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100870 DataManager::SetValue("tw_partition_vfat", 1);
871 else
872 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600873 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100874 DataManager::SetValue("tw_partition_exfat", 1);
875 else
876 DataManager::SetValue("tw_partition_exfat", 0);
877 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
878 DataManager::SetValue("tw_partition_f2fs", 1);
879 else
880 DataManager::SetValue("tw_partition_f2fs", 0);
881 if (TWFunc::Path_Exists("/sbin/mke2fs"))
882 DataManager::SetValue("tw_partition_ext", 1);
883 else
884 DataManager::SetValue("tw_partition_ext", 0);
885 return 0;
886 } else {
887 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
888 }
889 }
890 }
891 DataManager::SetValue("tw_partition_name", "");
892 DataManager::SetValue("tw_partition_file_system", "");
893 return 0;
894}
895
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500896int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100897{
898 time_t tm;
899 char path[256];
900 int path_len;
901 uid_t uid = -1;
902 gid_t gid = -1;
903
904 struct passwd *pwd = getpwnam("media_rw");
905 if(pwd) {
906 uid = pwd->pw_uid;
907 gid = pwd->pw_gid;
908 }
909
910 const std::string storage = DataManager::GetCurrentStoragePath();
911 if(PartitionManager.Is_Mounted_By_Path(storage)) {
912 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
913 } else {
914 strcpy(path, "/tmp/");
915 }
916
917 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
918 return 0;
919
920 tm = time(NULL);
921 path_len = strlen(path);
922
923 // Screenshot_2014-01-01-18-21-38.png
924 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
925
926 int res = gr_save_screenshot(path);
927 if(res == 0) {
928 chmod(path, 0666);
929 chown(path, uid, gid);
930
Ethan Yonker74db1572015-10-28 12:44:49 -0500931 gui_msg(Msg("screenshot_saved=Screenshot was saved to %s")(path));
that3f7b1ac2014-12-30 11:30:13 +0100932
933 // blink to notify that the screenshow was taken
934 gr_color(255, 255, 255, 255);
935 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
936 gr_flip();
937 gui_forceRender();
938 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500939 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100940 }
941 return 0;
942}
943
944int GUIAction::setbrightness(std::string arg)
945{
946 return TWFunc::Set_Brightness(arg);
947}
948
949int GUIAction::fileexists(std::string arg)
950{
951 struct stat st;
952 string newpath = arg + "/.";
953
954 operation_start("FileExists");
955 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
956 operation_end(0);
957 else
958 operation_end(1);
959 return 0;
960}
961
thatcc8ddca2015-01-03 01:59:36 +0100962void GUIAction::reinject_after_flash()
963{
964 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500965 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100966 if (simulate) {
967 simulate_progress_bar();
968 } else {
969 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
970 if (Boot == NULL || Boot->Current_File_System != "emmc")
971 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
972 else {
973 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
974 TWFunc::Exec_Cmd(injectcmd);
975 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500976 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100977 }
978 }
979}
980
that3f7b1ac2014-12-30 11:30:13 +0100981int GUIAction::flash(std::string arg)
982{
983 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600984 // We're going to jump to this page first, like a loading page
985 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100986 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200987 string zip_path = zip_queue[i];
988 size_t slashpos = zip_path.find_last_of('/');
989 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100990 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200991 DataManager::SetValue("tw_filename", zip_path);
992 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100993 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
994
995 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +0200996 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100997 TWFunc::SetPerformanceMode(false);
998 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500999 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001000 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001001 break;
that3f7b1ac2014-12-30 11:30:13 +01001002 }
1003 }
1004 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001005
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001006 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001007 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001008 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001009 }
that3f7b1ac2014-12-30 11:30:13 +01001010
thatcc8ddca2015-01-03 01:59:36 +01001011 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001012 PartitionManager.Update_System_Details();
1013 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001014 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001015 return 0;
1016}
1017
1018int GUIAction::wipe(std::string arg)
1019{
1020 operation_start("Format");
1021 DataManager::SetValue("tw_partition", arg);
1022
1023 int ret_val = false;
1024
1025 if (simulate) {
1026 simulate_progress_bar();
1027 } else {
1028 if (arg == "data")
1029 ret_val = PartitionManager.Factory_Reset();
1030 else if (arg == "battery")
1031 ret_val = PartitionManager.Wipe_Battery_Stats();
1032 else if (arg == "rotate")
1033 ret_val = PartitionManager.Wipe_Rotate_Data();
1034 else if (arg == "dalvik")
1035 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1036 else if (arg == "DATAMEDIA") {
1037 ret_val = PartitionManager.Format_Data();
1038 } else if (arg == "INTERNAL") {
1039 int has_datamedia, dual_storage;
1040
1041 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1042 if (has_datamedia) {
1043 ret_val = PartitionManager.Wipe_Media_From_Data();
1044 } else {
1045 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1046 }
1047 } else if (arg == "EXTERNAL") {
1048 string External_Path;
1049
1050 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1051 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1052 } else if (arg == "ANDROIDSECURE") {
1053 ret_val = PartitionManager.Wipe_Android_Secure();
1054 } else if (arg == "LIST") {
1055 string Wipe_List, wipe_path;
1056 bool skip = false;
1057 ret_val = true;
1058 TWPartition* wipe_part = NULL;
1059
1060 DataManager::GetValue("tw_wipe_list", Wipe_List);
1061 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1062 if (!Wipe_List.empty()) {
1063 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1064 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1065 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1066 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1067 if (wipe_path == "/and-sec") {
1068 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001069 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001070 ret_val = false;
1071 break;
1072 } else {
1073 skip = true;
1074 }
1075 } else if (wipe_path == "DALVIK") {
1076 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001077 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001078 ret_val = false;
1079 break;
1080 } else {
1081 skip = true;
1082 }
1083 } else if (wipe_path == "INTERNAL") {
1084 if (!PartitionManager.Wipe_Media_From_Data()) {
1085 ret_val = false;
1086 break;
1087 } else {
1088 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001089 }
1090 }
that3f7b1ac2014-12-30 11:30:13 +01001091 if (!skip) {
1092 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001093 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001094 ret_val = false;
1095 break;
1096 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1097 arg = wipe_path;
1098 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001099 } else {
that3f7b1ac2014-12-30 11:30:13 +01001100 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 }
that3f7b1ac2014-12-30 11:30:13 +01001102 start_pos = end_pos + 1;
1103 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001104 }
1105 }
that3f7b1ac2014-12-30 11:30:13 +01001106 } else
1107 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001108#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001109 if (arg == DataManager::GetSettingsStoragePath()) {
1110 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1111 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001112
that3f7b1ac2014-12-30 11:30:13 +01001113 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1114 LOGINFO("Making TWRP folder and saving settings.\n");
1115 Storage_Path += "/TWRP";
1116 mkdir(Storage_Path.c_str(), 0777);
1117 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 } else {
that3f7b1ac2014-12-30 11:30:13 +01001119 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1120 }
1121 }
1122#endif
1123 }
1124 PartitionManager.Update_System_Details();
1125 if (ret_val)
1126 ret_val = 0; // 0 is success
1127 else
1128 ret_val = 1; // 1 is failure
1129 operation_end(ret_val);
1130 return 0;
1131}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001132
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001133int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001134{
1135 operation_start("Refreshing Sizes");
1136 if (simulate) {
1137 simulate_progress_bar();
1138 } else
1139 PartitionManager.Update_System_Details();
1140 operation_end(0);
1141 return 0;
1142}
1143
1144int GUIAction::nandroid(std::string arg)
1145{
that3f7b1ac2014-12-30 11:30:13 +01001146 if (simulate) {
that73a52952015-01-28 23:07:34 +01001147 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001148 DataManager::SetValue("tw_partition", "Simulation");
1149 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001150 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001151 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001152 operation_start("Nandroid");
1153 int ret = 0;
1154
that3f7b1ac2014-12-30 11:30:13 +01001155 if (arg == "backup") {
1156 string Backup_Name;
1157 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -05001158 string auto_gen = gui_lookup("auto_gen", "(Auto Generate)");
1159 string curr_date = gui_lookup("curr_date", "(Current Date)");
that3f7b1ac2014-12-30 11:30:13 +01001160 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1161 ret = PartitionManager.Run_Backup();
1162 }
1163 else {
1164 operation_end(1);
1165 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001166 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001167 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001168 } else if (arg == "restore") {
1169 string Restore_Name;
1170 DataManager::GetValue("tw_restore", Restore_Name);
1171 ret = PartitionManager.Run_Restore(Restore_Name);
1172 } else {
1173 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001174 return -1;
1175 }
1176 DataManager::SetValue("tw_encrypt_backup", 0);
1177 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001178 if (ret == false)
1179 ret = 1; // 1 for failure
1180 else
1181 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001182 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001183 }
1184 else {
1185 DataManager::SetValue("tw_cancel_backup", 1);
Ethan Yonker74db1572015-10-28 12:44:49 -05001186 gui_msg("backup_cancel=Backup Canceled.");
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 ret = 0;
1188 }
that73a52952015-01-28 23:07:34 +01001189 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001190 return ret;
1191 }
1192 return 0;
1193}
1194
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001195int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 }
1199 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001200 int op_status = PartitionManager.Cancel_Backup();
1201 if (op_status != 0)
1202 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001203 }
1204
1205 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001206}
Dees_Troy51a0e822012-09-05 15:24:24 -04001207
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001208int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001209{
that73a52952015-01-28 23:07:34 +01001210 int op_status = 0;
1211
that3f7b1ac2014-12-30 11:30:13 +01001212 operation_start("Fix Permissions");
1213 LOGINFO("fix permissions started!\n");
1214 if (simulate) {
1215 simulate_progress_bar();
1216 } else {
that73a52952015-01-28 23:07:34 +01001217 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001218 if (op_status != 0)
1219 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001220 }
that73a52952015-01-28 23:07:34 +01001221 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001222 return 0;
1223}
1224
1225int GUIAction::dd(std::string arg)
1226{
1227 operation_start("imaging");
1228
1229 if (simulate) {
1230 simulate_progress_bar();
1231 } else {
1232 string cmd = "dd " + arg;
1233 TWFunc::Exec_Cmd(cmd);
1234 }
1235 operation_end(0);
1236 return 0;
1237}
1238
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001239int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001240{
1241 operation_start("Partition SD Card");
1242 int ret_val = 0;
1243
1244 if (simulate) {
1245 simulate_progress_bar();
1246 } else {
1247 int allow_partition;
1248 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1249 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001250 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001251 } else {
1252 if (!PartitionManager.Partition_SDCard())
1253 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001254 }
that3f7b1ac2014-12-30 11:30:13 +01001255 }
1256 operation_end(ret_val);
1257 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001258
that3f7b1ac2014-12-30 11:30:13 +01001259}
Dees_Troy51a0e822012-09-05 15:24:24 -04001260
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001261int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001262{
1263 operation_start("Install HTC Dumlock");
1264 if (simulate) {
1265 simulate_progress_bar();
1266 } else
1267 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001268
that3f7b1ac2014-12-30 11:30:13 +01001269 operation_end(0);
1270 return 0;
1271}
Dees_Troy51a0e822012-09-05 15:24:24 -04001272
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001273int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001274{
1275 operation_start("HTC Dumlock Restore Boot");
1276 if (simulate) {
1277 simulate_progress_bar();
1278 } else
1279 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001280
that3f7b1ac2014-12-30 11:30:13 +01001281 operation_end(0);
1282 return 0;
1283}
Dees_Troy51a0e822012-09-05 15:24:24 -04001284
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001285int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001286{
1287 operation_start("HTC Dumlock Reflash Recovery");
1288 if (simulate) {
1289 simulate_progress_bar();
1290 } else
1291 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001292
that3f7b1ac2014-12-30 11:30:13 +01001293 operation_end(0);
1294 return 0;
1295}
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
that3f7b1ac2014-12-30 11:30:13 +01001297int GUIAction::cmd(std::string arg)
1298{
1299 int op_status = 0;
1300
1301 operation_start("Command");
1302 LOGINFO("Running command: '%s'\n", arg.c_str());
1303 if (simulate) {
1304 simulate_progress_bar();
1305 } else {
1306 op_status = TWFunc::Exec_Cmd(arg);
1307 if (op_status != 0)
1308 op_status = 1;
1309 }
1310
1311 operation_end(op_status);
1312 return 0;
1313}
1314
1315int GUIAction::terminalcommand(std::string arg)
1316{
1317 int op_status = 0;
1318 string cmdpath, command;
1319
1320 DataManager::GetValue("tw_terminal_location", cmdpath);
1321 operation_start("CommandOutput");
1322 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1323 if (simulate) {
1324 simulate_progress_bar();
1325 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001326 } else if (arg == "exit") {
1327 LOGINFO("Exiting terminal\n");
1328 operation_end(op_status);
1329 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001330 } else {
1331 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1332 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001333 DataManager::SetValue("tw_terminal_state", 1);
1334 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001335 FILE* fp;
1336 char line[512];
1337
1338 fp = popen(command.c_str(), "r");
1339 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001340 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001341 } else {
1342 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1343 struct timeval timeout;
1344 fd_set fdset;
1345
1346 while(keep_going)
1347 {
1348 FD_ZERO(&fdset);
1349 FD_SET(fd, &fdset);
1350 timeout.tv_sec = 0;
1351 timeout.tv_usec = 400000;
1352 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1353 if (has_data == 0) {
1354 // Timeout reached
1355 DataManager::GetValue("tw_terminal_state", check);
1356 if (check == 0) {
1357 keep_going = 0;
1358 }
1359 } else if (has_data < 0) {
1360 // End of execution
1361 keep_going = 0;
1362 } else {
1363 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001364 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001365 gui_print("%s", line); // Display output
1366 else
1367 keep_going = 0; // Done executing
1368 }
1369 }
1370 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001371 }
thatc6085482015-01-09 22:12:43 +01001372 DataManager::SetValue("tw_operation_status", 0);
1373 DataManager::SetValue("tw_operation_state", 1);
1374 DataManager::SetValue("tw_terminal_state", 0);
1375 DataManager::SetValue("tw_background_thread_running", 0);
1376 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001377 }
1378 return 0;
1379}
1380
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001381int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001382{
1383 int op_status = 0;
1384
1385 LOGINFO("Sending kill command...\n");
1386 operation_start("KillCommand");
1387 DataManager::SetValue("tw_operation_status", 0);
1388 DataManager::SetValue("tw_operation_state", 1);
1389 DataManager::SetValue("tw_terminal_state", 0);
1390 DataManager::SetValue("tw_background_thread_running", 0);
1391 DataManager::SetValue(TW_ACTION_BUSY, 0);
1392 return 0;
1393}
1394
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001395int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001396{
1397 int op_status = 0;
1398 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001399 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001400 if (simulate) {
1401 simulate_progress_bar();
1402 } else {
1403 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001404 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001405 }
1406
1407 operation_end(op_status);
1408 return 0;
1409}
1410
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001411int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001412{
1413 int op_status = 0;
1414
1415 operation_start("CheckBackupName");
1416 if (simulate) {
1417 simulate_progress_bar();
1418 } else {
1419 op_status = PartitionManager.Check_Backup_Name(true);
1420 if (op_status != 0)
1421 op_status = 1;
1422 }
1423
1424 operation_end(op_status);
1425 return 0;
1426}
1427
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001428int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001429{
1430 int op_status = 0;
1431
1432 operation_start("Decrypt");
1433 if (simulate) {
1434 simulate_progress_bar();
1435 } else {
1436 string Password;
1437 DataManager::GetValue("tw_crypto_password", Password);
1438 op_status = PartitionManager.Decrypt_Device(Password);
1439 if (op_status != 0)
1440 op_status = 1;
1441 else {
that3f7b1ac2014-12-30 11:30:13 +01001442
1443 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1444
Ethan Yonkercf50da52015-01-12 21:59:07 -06001445 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001446
Ethan Yonkercf50da52015-01-12 21:59:07 -06001447 // Check for a custom theme and load it if exists
1448 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1449 if (has_datamedia != 0) {
1450 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001451 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001452 } else {
1453 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001454 }
1455 }
1456 }
1457 }
1458
1459 operation_end(op_status);
1460 return 0;
1461}
1462
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001463int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001464{
1465 operation_start("Sideload");
1466 if (simulate) {
1467 simulate_progress_bar();
1468 operation_end(0);
1469 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001471 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1472
1473 // wait for the adb connection
1474 int ret = apply_from_adb("/", &sideload_child_pid);
1475 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1476
1477 if (ret != 0) {
1478 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001480 ret = 1; // failure
1481 } else {
1482 int wipe_cache = 0;
1483 int wipe_dalvik = 0;
1484 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1485
1486 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1487 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1488 PartitionManager.Wipe_By_Path("/cache");
1489 if (wipe_dalvik)
1490 PartitionManager.Wipe_Dalvik_Cache();
1491 } else {
1492 ret = 1; // failure
1493 }
thatcc8ddca2015-01-03 01:59:36 +01001494 }
thatc6085482015-01-09 22:12:43 +01001495 if (sideload_child_pid) {
1496 LOGINFO("Signaling child sideload process to exit.\n");
1497 struct stat st;
1498 // Calling stat() on this magic filename signals the minadbd
1499 // subprocess to shut down.
1500 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1501 int status;
1502 LOGINFO("Waiting for child sideload process to exit.\n");
1503 waitpid(sideload_child_pid, &status, 0);
1504 }
Dees Troy28a85d22015-04-28 02:03:16 +00001505 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001506 TWFunc::Toggle_MTP(mtp_was_enabled);
1507 reinject_after_flash();
1508 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001509 }
that3f7b1ac2014-12-30 11:30:13 +01001510 return 0;
1511}
1512
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001513int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001514{
that3f7b1ac2014-12-30 11:30:13 +01001515 struct stat st;
1516 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001517 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001518 LOGINFO("Signaling child sideload process to exit.\n");
1519 // Calling stat() on this magic filename signals the minadbd
1520 // subprocess to shut down.
1521 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1522 if (!sideload_child_pid) {
1523 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001524 return 0;
1525 }
thatcc8ddca2015-01-03 01:59:36 +01001526 ::sleep(1);
1527 LOGINFO("Killing child sideload process.\n");
1528 kill(sideload_child_pid, SIGTERM);
1529 int status;
1530 LOGINFO("Waiting for child sideload process to exit.\n");
1531 waitpid(sideload_child_pid, &status, 0);
1532 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001533 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1534 return 0;
1535}
1536
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001537int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001538{
Ethan Yonkere1abe612015-06-09 11:09:32 -05001539 int op_status = 1;
1540
that3f7b1ac2014-12-30 11:30:13 +01001541 operation_start("OpenRecoveryScript");
1542 if (simulate) {
1543 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001544 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001545 } else {
thatc6085482015-01-09 22:12:43 +01001546 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1547 // that we converted to ORS commands during boot in recovery.cpp.
1548 // Run those first.
1549 int reboot = 0;
1550 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001551 gui_msg("running_recovery_commands=Running Recovery Commands");
thatc6085482015-01-09 22:12:43 +01001552 if (OpenRecoveryScript::run_script_file() == 0) {
1553 reboot = 1;
Matt Mower85161af2015-08-08 10:00:50 -05001554 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001555 }
that3f7b1ac2014-12-30 11:30:13 +01001556 }
thatc6085482015-01-09 22:12:43 +01001557 // Check for the ORS file in /cache and attempt to run those commands.
1558 if (OpenRecoveryScript::check_for_script_file()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_msg("running_ors=Running OpenRecoveryScript");
thatc6085482015-01-09 22:12:43 +01001560 if (OpenRecoveryScript::run_script_file() == 0) {
1561 reboot = 1;
Ethan Yonkere1abe612015-06-09 11:09:32 -05001562 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001563 }
1564 }
1565 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001566 // Disable stock recovery reflashing
1567 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001568 usleep(2000000); // Sleep for 2 seconds before rebooting
1569 TWFunc::tw_reboot(rb_system);
Ethan Yonkere1abe612015-06-09 11:09:32 -05001570 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
thatc6085482015-01-09 22:12:43 +01001571 } else {
1572 DataManager::SetValue("tw_page_done", 1);
1573 }
Ethan Yonkere1abe612015-06-09 11:09:32 -05001574 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001575 }
1576 return 0;
1577}
1578
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001579int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001580{
1581 int op_status = 0;
1582
1583 operation_start("Install SuperSU");
1584 if (simulate) {
1585 simulate_progress_bar();
1586 } else {
1587 if (!TWFunc::Install_SuperSU())
1588 op_status = 1;
1589 }
1590
1591 operation_end(op_status);
1592 return 0;
1593}
1594
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001595int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001596{
1597 int op_status = 0;
1598
1599 operation_start("Fixing Superuser Permissions");
1600 if (simulate) {
1601 simulate_progress_bar();
1602 } else {
1603 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1604 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1605 }
1606
1607 operation_end(op_status);
1608 return 0;
1609}
1610
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001611int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001612{
1613 int op_status = 0;
1614
1615 operation_start("Try Restore Decrypt");
1616 if (simulate) {
1617 simulate_progress_bar();
1618 } else {
1619 string Restore_Path, Filename, Password;
1620 DataManager::GetValue("tw_restore", Restore_Path);
1621 Restore_Path += "/";
1622 DataManager::GetValue("tw_restore_password", Password);
1623 TWFunc::SetPerformanceMode(true);
1624 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1625 op_status = 0; // success
1626 else
1627 op_status = 1; // fail
1628 TWFunc::SetPerformanceMode(false);
1629 }
1630
1631 operation_end(op_status);
1632 return 0;
1633}
1634
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001635int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001636{
1637 int op_status = 0;
1638
1639 operation_start("Repair Partition");
1640 if (simulate) {
1641 simulate_progress_bar();
1642 } else {
1643 string part_path;
1644 DataManager::GetValue("tw_partition_mount_point", part_path);
1645 if (PartitionManager.Repair_By_Path(part_path, true)) {
1646 op_status = 0; // success
1647 } else {
that3f7b1ac2014-12-30 11:30:13 +01001648 op_status = 1; // fail
1649 }
1650 }
1651
1652 operation_end(op_status);
1653 return 0;
1654}
1655
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001656int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001657{
1658 int op_status = 0;
1659
1660 operation_start("Resize Partition");
1661 if (simulate) {
1662 simulate_progress_bar();
1663 } else {
1664 string part_path;
1665 DataManager::GetValue("tw_partition_mount_point", part_path);
1666 if (PartitionManager.Resize_By_Path(part_path, true)) {
1667 op_status = 0; // success
1668 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001669 op_status = 1; // fail
1670 }
1671 }
1672
1673 operation_end(op_status);
1674 return 0;
1675}
1676
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001677int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001678{
1679 int op_status = 0;
1680
1681 operation_start("Change File System");
1682 if (simulate) {
1683 simulate_progress_bar();
1684 } else {
1685 string part_path, file_system;
1686 DataManager::GetValue("tw_partition_mount_point", part_path);
1687 DataManager::GetValue("tw_action_new_file_system", file_system);
1688 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1689 op_status = 0; // success
1690 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001691 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001692 op_status = 1; // fail
1693 }
1694 }
1695 PartitionManager.Update_System_Details();
1696 operation_end(op_status);
1697 return 0;
1698}
1699
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001700int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001701{
1702 int op_status = 0;
1703
1704 operation_start("Start MTP");
1705 if (PartitionManager.Enable_MTP())
1706 op_status = 0; // success
1707 else
1708 op_status = 1; // fail
1709
1710 operation_end(op_status);
1711 return 0;
1712}
1713
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001714int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001715{
1716 int op_status = 0;
1717
1718 operation_start("Stop MTP");
1719 if (PartitionManager.Disable_MTP())
1720 op_status = 0; // success
1721 else
1722 op_status = 1; // fail
1723
1724 operation_end(op_status);
1725 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001726}
1727
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001728int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001729{
1730 int op_status = 0;
1731
1732 operation_start("Flash Image");
1733 string path, filename, full_filename;
1734 DataManager::GetValue("tw_zip_location", path);
1735 DataManager::GetValue("tw_file", filename);
1736 full_filename = path + "/" + filename;
1737 if (PartitionManager.Flash_Image(full_filename))
1738 op_status = 0; // success
1739 else
1740 op_status = 1; // fail
1741
1742 operation_end(op_status);
1743 return 0;
1744}
1745
Dees_Troy51a0e822012-09-05 15:24:24 -04001746int GUIAction::getKeyByName(std::string key)
1747{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001748 if (key == "home") return KEY_HOME;
1749 else if (key == "menu") return KEY_MENU;
1750 else if (key == "back") return KEY_BACK;
1751 else if (key == "search") return KEY_SEARCH;
1752 else if (key == "voldown") return KEY_VOLUMEDOWN;
1753 else if (key == "volup") return KEY_VOLUMEUP;
1754 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001755 int ret_val;
1756 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1757 if (!ret_val)
1758 return KEY_POWER;
1759 else
1760 return ret_val;
1761 }
1762
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001763 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001764}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001765
1766int GUIAction::checkpartitionlifetimewrites(std::string arg)
1767{
1768 int op_status = 0;
1769 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1770
1771 operation_start("Check Partition Lifetime Writes");
1772 if (sys) {
1773 if (sys->Check_Lifetime_Writes() != 0)
1774 DataManager::SetValue("tw_lifetime_writes", 1);
1775 else
1776 DataManager::SetValue("tw_lifetime_writes", 0);
1777 op_status = 0; // success
1778 } else {
1779 DataManager::SetValue("tw_lifetime_writes", 1);
1780 op_status = 1; // fail
1781 }
1782
1783 operation_end(op_status);
1784 return 0;
1785}
1786
1787int GUIAction::mountsystemtoggle(std::string arg)
1788{
1789 int op_status = 0;
1790 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001791 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001792
1793 operation_start("Toggle System Mount");
1794 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1795 op_status = 1; // fail
1796 } else {
1797 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1798 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001799 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001800 DataManager::SetValue("tw_mount_system_ro", 0);
1801 Part->Change_Mount_Read_Only(false);
1802 } else {
1803 DataManager::SetValue("tw_mount_system_ro", 1);
1804 Part->Change_Mount_Read_Only(true);
1805 }
1806 if (remount_system) {
1807 Part->Mount(true);
1808 }
1809 op_status = 0; // success
1810 } else {
1811 op_status = 1; // fail
1812 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001813 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1814 if (Part) {
1815 if (arg == "0") {
1816 Part->Change_Mount_Read_Only(false);
1817 } else {
1818 Part->Change_Mount_Read_Only(true);
1819 }
1820 if (remount_vendor) {
1821 Part->Mount(true);
1822 }
1823 op_status = 0; // success
1824 } else {
1825 op_status = 1; // fail
1826 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001827 }
1828
1829 operation_end(op_status);
1830 return 0;
1831}
Ethan Yonker74db1572015-10-28 12:44:49 -05001832
1833int GUIAction::setlanguage(std::string arg __unused)
1834{
1835 int op_status = 0;
1836
1837 operation_start("Set Language");
1838 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1839 PageManager::RequestReload();
1840 op_status = 0; // success
1841
1842 operation_end(op_status);
1843 return 0;
1844}