blob: 56336f4088da341b9e1b4b43d9ac90576bda87b8 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060053#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
that3f7b1ac2014-12-30 11:30:13 +010061GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010062std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010063static string zip_queue[10];
64static int zip_queue_index;
65static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010067
that73a52952015-01-28 23:07:34 +010068static void *ActionThread_work_wrapper(void *data);
69
70class ActionThread
71{
72public:
73 ActionThread();
74 ~ActionThread();
75
76 void threadActions(GUIAction *act);
77 void run(void *data);
78private:
79 friend void *ActionThread_work_wrapper(void*);
80 struct ThreadData
81 {
82 ActionThread *this_;
83 GUIAction *act;
84 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
85 };
86
87 pthread_t m_thread;
88 bool m_thread_running;
89 pthread_mutex_t m_act_lock;
90};
91
92static ActionThread action_thread; // for all kinds of longer running actions
93static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010094
95static void *ActionThread_work_wrapper(void *data)
96{
that73a52952015-01-28 23:07:34 +010097 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010098 return NULL;
99}
100
101ActionThread::ActionThread()
102{
103 m_thread_running = false;
104 pthread_mutex_init(&m_act_lock, NULL);
105}
106
107ActionThread::~ActionThread()
108{
109 pthread_mutex_lock(&m_act_lock);
110 if(m_thread_running) {
111 pthread_mutex_unlock(&m_act_lock);
112 pthread_join(m_thread, NULL);
113 } else {
114 pthread_mutex_unlock(&m_act_lock);
115 }
116 pthread_mutex_destroy(&m_act_lock);
117}
118
that7d3b54f2015-01-09 22:52:51 +0100119void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100120{
121 pthread_mutex_lock(&m_act_lock);
122 if (m_thread_running) {
123 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100124 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
125 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100126 } else {
127 m_thread_running = true;
128 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100129 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100130 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
131 }
132}
133
134void ActionThread::run(void *data)
135{
136 ThreadData *d = (ThreadData*)data;
137 GUIAction* act = d->act;
138
139 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100140 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100141 act->doAction(*it);
142
143 pthread_mutex_lock(&m_act_lock);
144 m_thread_running = false;
145 pthread_mutex_unlock(&m_act_lock);
146 delete d;
147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 xml_node<>* child;
153 xml_node<>* actions;
154 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
that3f7b1ac2014-12-30 11:30:13 +0100158 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100159#define ADD_ACTION(n) mf[#n] = &GUIAction::n
160#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100161 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100162 ADD_ACTION(reboot);
163 ADD_ACTION(home);
164 ADD_ACTION(key);
165 ADD_ACTION(page);
166 ADD_ACTION(reload);
167 ADD_ACTION(readBackup);
168 ADD_ACTION(set);
169 ADD_ACTION(clear);
170 ADD_ACTION(mount);
171 ADD_ACTION(unmount);
172 ADD_ACTION_EX("umount", unmount);
173 ADD_ACTION(restoredefaultsettings);
174 ADD_ACTION(copylog);
175 ADD_ACTION(compute);
176 ADD_ACTION_EX("addsubtract", compute);
177 ADD_ACTION(setguitimezone);
178 ADD_ACTION(overlay);
179 ADD_ACTION(queuezip);
180 ADD_ACTION(cancelzip);
181 ADD_ACTION(queueclear);
182 ADD_ACTION(sleep);
183 ADD_ACTION(appenddatetobackupname);
184 ADD_ACTION(generatebackupname);
185 ADD_ACTION(checkpartitionlist);
186 ADD_ACTION(getpartitiondetails);
187 ADD_ACTION(screenshot);
188 ADD_ACTION(setbrightness);
189 ADD_ACTION(fileexists);
190 ADD_ACTION(killterminal);
191 ADD_ACTION(checkbackupname);
192 ADD_ACTION(adbsideloadcancel);
193 ADD_ACTION(fixsu);
194 ADD_ACTION(startmtp);
195 ADD_ACTION(stopmtp);
196 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500197 ADD_ACTION(checkpartitionlifetimewrites);
198 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500199 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100200
201 // remember actions that run in the caller thread
202 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
203 setActionsRunningInCallerThread.insert(it->first);
204
205 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100206 ADD_ACTION(flash);
207 ADD_ACTION(wipe);
208 ADD_ACTION(refreshsizes);
209 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600210 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100211 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);
that10ae24f2015-12-26 20:53:51 +0100229 ADD_ACTION(twcmd);
that3f7b1ac2014-12-30 11:30:13 +0100230 }
231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600233 actions = FindNode(node, "actions");
234 if (actions) child = FindNode(actions, "action");
235 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 while (child)
240 {
241 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 attr = child->first_attribute("function");
244 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 action.mFunction = attr->value();
247 action.mArg = child->value();
248 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 child = child->next_sibling("action");
251 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600254 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (child)
256 {
257 attr = child->first_attribute("key");
258 if (attr)
259 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100260 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
261 for(size_t i = 0; i < keys.size(); ++i)
262 {
263 const int key = getKeyByName(keys[i]);
264 mKeys[key] = false;
265 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 }
267 else
268 {
269 attr = child->first_attribute("x");
270 if (!attr) return;
271 mActionX = atol(attr->value());
272 attr = child->first_attribute("y");
273 if (!attr) return;
274 mActionY = atol(attr->value());
275 attr = child->first_attribute("w");
276 if (!attr) return;
277 mActionW = atol(attr->value());
278 attr = child->first_attribute("h");
279 if (!attr) return;
280 mActionH = atol(attr->value());
281 }
282 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400283}
284
Matt Mower25036b72016-06-24 12:30:18 -0500285int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400286{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 if (state == TOUCH_RELEASE)
288 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100293int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400294{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100295 std::map<int, bool>::iterator itr = mKeys.find(key);
296 if(itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100297 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100298
299 bool prevState = itr->second;
300 itr->second = down;
301
302 // If there is only one key for this action, wait for key up so it
303 // doesn't trigger with multi-key actions.
304 // Else, check if all buttons are pressed, then consume their release events
305 // so they don't trigger one-button actions and reset mKeys pressed status
306 if(mKeys.size() == 1) {
thatd4725ca2016-01-19 00:15:21 +0100307 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100308 doActions();
thatd4725ca2016-01-19 00:15:21 +0100309 return 0;
310 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100311 } else if(down) {
312 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
313 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100314 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100315 }
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();
thatd4725ca2016-01-19 00:15:21 +0100325 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100326 }
327
thatd4725ca2016-01-19 00:15:21 +0100328 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400329}
330
Vojtech Bocek07220562014-02-08 02:05:33 +0100331int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400332{
Vojtech Bocek07220562014-02-08 02:05:33 +0100333 GUIObject::NotifyVarChange(varName, value);
334
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100337 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
343void GUIAction::simulate_progress_bar(void)
344{
Ethan Yonker74db1572015-10-28 12:44:49 -0500345 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 for (int i = 0; i < 5; i++)
347 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500348 if (PartitionManager.stop_backup.get_value()) {
349 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600350 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500351 DataManager::SetValue("ui_progress", 0);
352 PartitionManager.stop_backup.set_value(0);
353 return;
354 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 usleep(500000);
356 DataManager::SetValue("ui_progress", i * 20);
357 }
358}
359
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600360int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400361{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200362 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400363
364 DataManager::SetValue("ui_progress", 0);
365
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 if (filename.empty())
367 {
368 LOGERR("No file specified.\n");
369 return -1;
370 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Ethan Yonker9598c072016-01-15 21:54:34 -0600372 if (!TWFunc::Path_Exists(filename)) {
373 if (!PartitionManager.Mount_By_Path(filename, true)) {
374 return -1;
375 }
376 if (!TWFunc::Path_Exists(filename)) {
377 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
378 return -1;
379 }
380 }
Dees_Troy657c3092012-09-10 20:32:10 -0400381
Dees_Troy51a0e822012-09-05 15:24:24 -0400382 if (simulate) {
383 simulate_progress_bar();
384 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400385 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400386
387 // Now, check if we need to ensure TWRP remains installed...
388 struct stat st;
389 if (stat("/sbin/installTwrp", &st) == 0)
390 {
391 DataManager::SetValue("tw_operation", "Configuring TWRP");
392 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500393 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200394 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500396 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 }
398 }
399 }
400
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 // Done
402 DataManager::SetValue("ui_progress", 100);
403 DataManager::SetValue("ui_progress", 0);
404 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400405}
406
that73a52952015-01-28 23:07:34 +0100407GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100408{
that73a52952015-01-28 23:07:34 +0100409 string func = gui_parse_text(action.mFunction);
410 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
411 if (needsThread) {
412 if (func == "cancelbackup")
413 return THREAD_CANCEL;
414 else
415 return THREAD_ACTION;
416 }
417 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100418}
419
Dees_Troy51a0e822012-09-05 15:24:24 -0400420int GUIAction::doActions()
421{
that3f7b1ac2014-12-30 11:30:13 +0100422 if (mActions.size() < 1)
423 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424
that73a52952015-01-28 23:07:34 +0100425 // Determine in which thread to run the actions.
426 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
427 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100428 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100429 for (it = mActions.begin(); it != mActions.end(); ++it) {
430 ThreadType tt = getThreadType(*it);
431 if (tt == THREAD_NONE)
432 continue;
433 if (threadType == THREAD_NONE)
434 threadType = tt;
435 else if (threadType != tt) {
436 LOGERR("Can't mix normal and cancel actions in the same list.\n"
437 "Running the whole batch in the cancel thread.\n");
438 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100439 break;
440 }
that7d3b54f2015-01-09 22:52:51 +0100441 }
that73a52952015-01-28 23:07:34 +0100442
443 // Now run the actions in the desired thread.
444 switch (threadType) {
445 case THREAD_ACTION:
446 action_thread.threadActions(this);
447 break;
448
449 case THREAD_CANCEL:
450 cancel_thread.threadActions(this);
451 break;
452
453 default: {
454 // no iterators here because theme reloading might kill our object
455 const size_t cnt = mActions.size();
456 for (size_t i = 0; i < cnt; ++i)
457 doAction(mActions[i]);
458 }
thatc6085482015-01-09 22:12:43 +0100459 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200461 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400462}
463
that3f7b1ac2014-12-30 11:30:13 +0100464int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400465{
that3f7b1ac2014-12-30 11:30:13 +0100466 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
that3f7b1ac2014-12-30 11:30:13 +0100468 std::string function = gui_parse_text(action.mFunction);
469 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400470
that3f7b1ac2014-12-30 11:30:13 +0100471 // find function and execute it
472 mapFunc::const_iterator funcitr = mf.find(function);
473 if (funcitr != mf.end())
474 return (this->*funcitr->second)(arg);
475
476 LOGERR("Unknown action '%s'\n", function.c_str());
477 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400478}
479
480void GUIAction::operation_start(const string operation_name)
481{
that3f7b1ac2014-12-30 11:30:13 +0100482 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000483 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 DataManager::SetValue(TW_ACTION_BUSY, 1);
485 DataManager::SetValue("ui_progress", 0);
486 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600488 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489}
490
that3f7b1ac2014-12-30 11:30:13 +0100491void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400492{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000493 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 DataManager::SetValue("ui_progress", 100);
496 if (simulate) {
497 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
498 if (simulate_fail != 0)
499 DataManager::SetValue("tw_operation_status", 1);
500 else
501 DataManager::SetValue("tw_operation_status", 0);
502 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500503 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500505 }
506 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400507 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500508 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 }
510 DataManager::SetValue("tw_operation_state", 1);
511 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500512 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200513 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000514 time(&Stop);
515 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600516 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100517 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400518}
519
that3f7b1ac2014-12-30 11:30:13 +0100520int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400521{
that3f7b1ac2014-12-30 11:30:13 +0100522 sync();
523 DataManager::SetValue("tw_gui_done", 1);
524 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400525
that3f7b1ac2014-12-30 11:30:13 +0100526 return 0;
527}
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500529int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100530{
that3f7b1ac2014-12-30 11:30:13 +0100531 gui_changePage("main");
532 return 0;
533}
Dees_Troy51a0e822012-09-05 15:24:24 -0400534
that3f7b1ac2014-12-30 11:30:13 +0100535int GUIAction::key(std::string arg)
536{
537 const int key = getKeyByName(arg);
538 PageManager::NotifyKey(key, true);
539 PageManager::NotifyKey(key, false);
540 return 0;
541}
542
543int GUIAction::page(std::string arg)
544{
LuK133762326f42015-09-30 19:57:46 +0200545 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100546 std::string page_name = gui_parse_text(arg);
547 return gui_changePage(page_name);
548}
549
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500550int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100551{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500552 PageManager::RequestReload();
553 // The actual reload is handled in pages.cpp in PageManager::RunReload()
554 // The reload will occur on the next Update or Render call and will
555 // be performed in the rendoer thread instead of the action thread
556 // to prevent crashing which could occur when we start deleting
557 // GUI resources in the action thread while we attempt to render
558 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100559 return 0;
560}
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500562int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100563{
564 string Restore_Name;
565 DataManager::GetValue("tw_restore", Restore_Name);
566 PartitionManager.Set_Restore_Files(Restore_Name);
567 return 0;
568}
569
570int GUIAction::set(std::string arg)
571{
572 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573 {
that3f7b1ac2014-12-30 11:30:13 +0100574 string varName = arg.substr(0, arg.find('='));
575 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400576
that3f7b1ac2014-12-30 11:30:13 +0100577 DataManager::GetValue(value, value);
578 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200579 }
that3f7b1ac2014-12-30 11:30:13 +0100580 else
581 DataManager::SetValue(arg, "1");
582 return 0;
583}
Dees_Troy51a0e822012-09-05 15:24:24 -0400584
that3f7b1ac2014-12-30 11:30:13 +0100585int GUIAction::clear(std::string arg)
586{
587 DataManager::SetValue(arg, "0");
588 return 0;
589}
Dees_Troy51a0e822012-09-05 15:24:24 -0400590
that3f7b1ac2014-12-30 11:30:13 +0100591int GUIAction::mount(std::string arg)
592{
593 if (arg == "usb") {
594 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100596 PartitionManager.usb_storage_enable();
597 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500598 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100599 } else if (!simulate) {
600 PartitionManager.Mount_By_Path(arg, true);
601 PartitionManager.Add_MTP_Storage(arg);
602 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500603 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100604 return 0;
605}
606
607int GUIAction::unmount(std::string arg)
608{
609 if (arg == "usb") {
610 if (!simulate)
611 PartitionManager.usb_storage_disable();
612 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500613 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100614 DataManager::SetValue(TW_ACTION_BUSY, 0);
615 } else if (!simulate) {
616 PartitionManager.UnMount_By_Path(arg, true);
617 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100619 return 0;
620}
621
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500622int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100623{
624 operation_start("Restore Defaults");
625 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500626 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100627 else {
628 DataManager::ResetDefaults();
629 PartitionManager.Update_System_Details();
630 PartitionManager.Mount_Current_Storage(true);
631 }
632 operation_end(0);
633 return 0;
634}
635
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500636int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100637{
638 operation_start("Copy Log");
639 if (!simulate)
640 {
641 string dst;
642 PartitionManager.Mount_Current_Storage(true);
643 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
644 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
645 tw_set_default_metadata(dst.c_str());
646 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500647 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100648 } else
649 simulate_progress_bar();
650 operation_end(0);
651 return 0;
652}
653
654
655int GUIAction::compute(std::string arg)
656{
657 if (arg.find("+") != string::npos)
658 {
659 string varName = arg.substr(0, arg.find('+'));
660 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
661 int amount_to_add = atoi(string_to_add.c_str());
662 int value;
663
664 DataManager::GetValue(varName, value);
665 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400666 return 0;
667 }
that3f7b1ac2014-12-30 11:30:13 +0100668 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 {
that3f7b1ac2014-12-30 11:30:13 +0100670 string varName = arg.substr(0, arg.find('-'));
671 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
672 int amount_to_subtract = atoi(string_to_subtract.c_str());
673 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400674
that3f7b1ac2014-12-30 11:30:13 +0100675 DataManager::GetValue(varName, value);
676 value -= amount_to_subtract;
677 if (value <= 0)
678 value = 0;
679 DataManager::SetValue(varName, value);
680 return 0;
681 }
682 if (arg.find("*") != string::npos)
683 {
684 string varName = arg.substr(0, arg.find('*'));
685 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
686 int multiply_by = atoi(multiply_by_str.c_str());
687 int value;
688
689 DataManager::GetValue(varName, value);
690 DataManager::SetValue(varName, value*multiply_by);
691 return 0;
692 }
693 if (arg.find("/") != string::npos)
694 {
695 string varName = arg.substr(0, arg.find('/'));
696 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
697 int divide_by = atoi(divide_by_str.c_str());
698 int value;
699
700 if(divide_by != 0)
701 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100703 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 }
705 return 0;
706 }
that3f7b1ac2014-12-30 11:30:13 +0100707 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
708 return -1;
709}
Dees_Troy51a0e822012-09-05 15:24:24 -0400710
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500711int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100712{
713 string SelectedZone;
714 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
715 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
716 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
717
718 int dst;
719 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
720
721 string offset;
722 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
723
724 string NewTimeZone = Zone;
725 if (offset != "0")
726 NewTimeZone += ":" + offset;
727
728 if (dst != 0)
729 NewTimeZone += DSTZone;
730
731 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
732 DataManager::update_tz_environment_variables();
733 return 0;
734}
735
736int GUIAction::overlay(std::string arg)
737{
738 return gui_changeOverlay(arg);
739}
740
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500741int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100742{
743 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500744 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 return 0;
746 }
that3f7b1ac2014-12-30 11:30:13 +0100747 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
748 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
749 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100751 }
752 return 0;
753}
754
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500755int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100756{
757 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500758 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400759 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100760 } else {
761 zip_queue_index--;
762 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 }
that3f7b1ac2014-12-30 11:30:13 +0100764 return 0;
765}
Dees_Troy51a0e822012-09-05 15:24:24 -0400766
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500767int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100768{
769 zip_queue_index = 0;
770 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
771 return 0;
772}
Dees_Troy51a0e822012-09-05 15:24:24 -0400773
that3f7b1ac2014-12-30 11:30:13 +0100774int GUIAction::sleep(std::string arg)
775{
776 operation_start("Sleep");
777 usleep(atoi(arg.c_str()));
778 operation_end(0);
779 return 0;
780}
Dees Troyb21cc642013-09-10 17:36:41 +0000781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500782int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100783{
784 operation_start("AppendDateToBackupName");
785 string Backup_Name;
786 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
787 Backup_Name += TWFunc::Get_Current_Date();
788 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
789 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
790 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500791 PageManager::NotifyKey(KEY_END, true);
792 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100793 operation_end(0);
794 return 0;
795}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500796
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500797int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100798{
799 operation_start("GenerateBackupName");
800 TWFunc::Auto_Generate_Backup_Name();
801 operation_end(0);
802 return 0;
803}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500804
Ethan Yonker483e9f42016-01-11 22:21:18 -0600805int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100806{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600807 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100808 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500809
Ethan Yonker483e9f42016-01-11 22:21:18 -0600810 if (arg.empty())
811 arg = "tw_wipe_list";
812 DataManager::GetValue(arg, List);
813 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
814 if (!List.empty()) {
815 size_t start_pos = 0, end_pos = List.find(";", start_pos);
816 while (end_pos != string::npos && start_pos < List.size()) {
817 part_path = List.substr(start_pos, end_pos - start_pos);
818 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
819 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100820 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 } else {
that3f7b1ac2014-12-30 11:30:13 +0100822 count++;
823 }
824 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600825 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100826 }
827 DataManager::SetValue("tw_check_partition_list", count);
828 } else {
829 DataManager::SetValue("tw_check_partition_list", 0);
830 }
831 return 0;
832}
Dees_Troy51a0e822012-09-05 15:24:24 -0400833
Ethan Yonker483e9f42016-01-11 22:21:18 -0600834int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100835{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600836 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100837 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400838
Ethan Yonker483e9f42016-01-11 22:21:18 -0600839 if (arg.empty())
840 arg = "tw_wipe_list";
841 DataManager::GetValue(arg, List);
842 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
843 if (!List.empty()) {
844 size_t start_pos = 0, end_pos = List.find(";", start_pos);
845 part_path = List;
846 while (end_pos != string::npos && start_pos < List.size()) {
847 part_path = List.substr(start_pos, end_pos - start_pos);
848 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
849 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100850 // Do nothing
851 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100853 break;
854 }
855 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600856 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100857 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600858 if (!part_path.empty()) {
859 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100860 if (Part) {
861 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500862
that3f7b1ac2014-12-30 11:30:13 +0100863 DataManager::SetValue("tw_partition_name", Part->Display_Name);
864 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
865 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
866 DataManager::SetValue("tw_partition_size", Part->Size / mb);
867 DataManager::SetValue("tw_partition_used", Part->Used / mb);
868 DataManager::SetValue("tw_partition_free", Part->Free / mb);
869 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
870 DataManager::SetValue("tw_partition_removable", Part->Removable);
871 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
872
873 if (Part->Can_Repair())
874 DataManager::SetValue("tw_partition_can_repair", 1);
875 else
876 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500877 if (Part->Can_Resize())
878 DataManager::SetValue("tw_partition_can_resize", 1);
879 else
880 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600881 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100882 DataManager::SetValue("tw_partition_vfat", 1);
883 else
884 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600885 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100886 DataManager::SetValue("tw_partition_exfat", 1);
887 else
888 DataManager::SetValue("tw_partition_exfat", 0);
889 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
890 DataManager::SetValue("tw_partition_f2fs", 1);
891 else
892 DataManager::SetValue("tw_partition_f2fs", 0);
893 if (TWFunc::Path_Exists("/sbin/mke2fs"))
894 DataManager::SetValue("tw_partition_ext", 1);
895 else
896 DataManager::SetValue("tw_partition_ext", 0);
897 return 0;
898 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600899 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100900 }
901 }
902 }
903 DataManager::SetValue("tw_partition_name", "");
904 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600905 // Set this to 0 to prevent trying to partition this device, just in case
906 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100907 return 0;
908}
909
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500910int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100911{
912 time_t tm;
913 char path[256];
914 int path_len;
915 uid_t uid = -1;
916 gid_t gid = -1;
917
918 struct passwd *pwd = getpwnam("media_rw");
919 if(pwd) {
920 uid = pwd->pw_uid;
921 gid = pwd->pw_gid;
922 }
923
924 const std::string storage = DataManager::GetCurrentStoragePath();
925 if(PartitionManager.Is_Mounted_By_Path(storage)) {
926 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
927 } else {
928 strcpy(path, "/tmp/");
929 }
930
Xuefer579e9072016-01-22 13:35:50 +0800931 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100932 return 0;
933
934 tm = time(NULL);
935 path_len = strlen(path);
936
937 // Screenshot_2014-01-01-18-21-38.png
938 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
939
940 int res = gr_save_screenshot(path);
941 if(res == 0) {
942 chmod(path, 0666);
943 chown(path, uid, gid);
944
that677b13f2015-12-26 23:57:31 +0100945 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100946
947 // blink to notify that the screenshow was taken
948 gr_color(255, 255, 255, 255);
949 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
950 gr_flip();
951 gui_forceRender();
952 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500953 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100954 }
955 return 0;
956}
957
958int GUIAction::setbrightness(std::string arg)
959{
960 return TWFunc::Set_Brightness(arg);
961}
962
963int GUIAction::fileexists(std::string arg)
964{
965 struct stat st;
966 string newpath = arg + "/.";
967
968 operation_start("FileExists");
969 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
970 operation_end(0);
971 else
972 operation_end(1);
973 return 0;
974}
975
thatcc8ddca2015-01-03 01:59:36 +0100976void GUIAction::reinject_after_flash()
977{
978 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500979 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100980 if (simulate) {
981 simulate_progress_bar();
982 } else {
983 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
984 if (Boot == NULL || Boot->Current_File_System != "emmc")
985 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
986 else {
987 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
988 TWFunc::Exec_Cmd(injectcmd);
989 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500990 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100991 }
992 }
993}
994
that3f7b1ac2014-12-30 11:30:13 +0100995int GUIAction::flash(std::string arg)
996{
997 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600998 // We're going to jump to this page first, like a loading page
999 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001000 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001001 string zip_path = zip_queue[i];
1002 size_t slashpos = zip_path.find_last_of('/');
1003 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001004 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001005 DataManager::SetValue("tw_filename", zip_path);
1006 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001007 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1008
1009 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001010 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001011 TWFunc::SetPerformanceMode(false);
1012 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001013 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001014 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001015 break;
that3f7b1ac2014-12-30 11:30:13 +01001016 }
1017 }
1018 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001019
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001020 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001021 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001022 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001023 }
that3f7b1ac2014-12-30 11:30:13 +01001024
thatcc8ddca2015-01-03 01:59:36 +01001025 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001026 PartitionManager.Update_System_Details();
bigbiffa869fc72016-03-01 19:40:36 -05001027 if (DataManager::GetIntValue("tw_install_reboot") > 0 && ret_val == 0) {
1028 gui_msg("install_reboot=Rebooting in 5 seconds");
1029 usleep(5000000);
1030 TWFunc::tw_reboot(rb_system);
1031 usleep(5000000); // another sleep while we wait for the reboot to occur
1032 }
that3f7b1ac2014-12-30 11:30:13 +01001033 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001034 // This needs to be after the operation_end call so we change pages before we change variables that we display on the screen
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001035 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001036 return 0;
1037}
1038
1039int GUIAction::wipe(std::string arg)
1040{
1041 operation_start("Format");
1042 DataManager::SetValue("tw_partition", arg);
1043
1044 int ret_val = false;
1045
1046 if (simulate) {
1047 simulate_progress_bar();
1048 } else {
1049 if (arg == "data")
1050 ret_val = PartitionManager.Factory_Reset();
1051 else if (arg == "battery")
1052 ret_val = PartitionManager.Wipe_Battery_Stats();
1053 else if (arg == "rotate")
1054 ret_val = PartitionManager.Wipe_Rotate_Data();
1055 else if (arg == "dalvik")
1056 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1057 else if (arg == "DATAMEDIA") {
1058 ret_val = PartitionManager.Format_Data();
1059 } else if (arg == "INTERNAL") {
1060 int has_datamedia, dual_storage;
1061
1062 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1063 if (has_datamedia) {
1064 ret_val = PartitionManager.Wipe_Media_From_Data();
1065 } else {
1066 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1067 }
1068 } else if (arg == "EXTERNAL") {
1069 string External_Path;
1070
1071 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1072 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1073 } else if (arg == "ANDROIDSECURE") {
1074 ret_val = PartitionManager.Wipe_Android_Secure();
1075 } else if (arg == "LIST") {
1076 string Wipe_List, wipe_path;
1077 bool skip = false;
1078 ret_val = true;
1079 TWPartition* wipe_part = NULL;
1080
1081 DataManager::GetValue("tw_wipe_list", Wipe_List);
1082 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1083 if (!Wipe_List.empty()) {
1084 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1085 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1086 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1087 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1088 if (wipe_path == "/and-sec") {
1089 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001090 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001091 ret_val = false;
1092 break;
1093 } else {
1094 skip = true;
1095 }
1096 } else if (wipe_path == "DALVIK") {
1097 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001098 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001099 ret_val = false;
1100 break;
1101 } else {
1102 skip = true;
1103 }
1104 } else if (wipe_path == "INTERNAL") {
1105 if (!PartitionManager.Wipe_Media_From_Data()) {
1106 ret_val = false;
1107 break;
1108 } else {
1109 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001110 }
1111 }
that3f7b1ac2014-12-30 11:30:13 +01001112 if (!skip) {
1113 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001114 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001115 ret_val = false;
1116 break;
1117 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1118 arg = wipe_path;
1119 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001120 } else {
that3f7b1ac2014-12-30 11:30:13 +01001121 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001122 }
that3f7b1ac2014-12-30 11:30:13 +01001123 start_pos = end_pos + 1;
1124 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 }
1126 }
that3f7b1ac2014-12-30 11:30:13 +01001127 } else
1128 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001129#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001130 if (arg == DataManager::GetSettingsStoragePath()) {
1131 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1132 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001133
that3f7b1ac2014-12-30 11:30:13 +01001134 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1135 LOGINFO("Making TWRP folder and saving settings.\n");
1136 Storage_Path += "/TWRP";
1137 mkdir(Storage_Path.c_str(), 0777);
1138 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001139 } else {
that3f7b1ac2014-12-30 11:30:13 +01001140 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1141 }
1142 }
1143#endif
1144 }
1145 PartitionManager.Update_System_Details();
1146 if (ret_val)
1147 ret_val = 0; // 0 is success
1148 else
1149 ret_val = 1; // 1 is failure
1150 operation_end(ret_val);
1151 return 0;
1152}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001153
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001154int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001155{
1156 operation_start("Refreshing Sizes");
1157 if (simulate) {
1158 simulate_progress_bar();
1159 } else
1160 PartitionManager.Update_System_Details();
1161 operation_end(0);
1162 return 0;
1163}
1164
1165int GUIAction::nandroid(std::string arg)
1166{
that3f7b1ac2014-12-30 11:30:13 +01001167 if (simulate) {
that73a52952015-01-28 23:07:34 +01001168 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001169 DataManager::SetValue("tw_partition", "Simulation");
1170 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001171 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001172 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001173 operation_start("Nandroid");
1174 int ret = 0;
1175
that3f7b1ac2014-12-30 11:30:13 +01001176 if (arg == "backup") {
1177 string Backup_Name;
1178 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001179 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1180 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001181 ret = PartitionManager.Run_Backup(false);
1182 } else {
that3f7b1ac2014-12-30 11:30:13 +01001183 operation_end(1);
1184 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001185 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001186 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001187 } else if (arg == "restore") {
1188 string Restore_Name;
1189 DataManager::GetValue("tw_restore", Restore_Name);
1190 ret = PartitionManager.Run_Restore(Restore_Name);
1191 } else {
1192 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001193 return -1;
1194 }
1195 DataManager::SetValue("tw_encrypt_backup", 0);
1196 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001197 if (ret == false)
1198 ret = 1; // 1 for failure
1199 else
1200 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001202 }
1203 else {
1204 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001205 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001206 ret = 0;
1207 }
that73a52952015-01-28 23:07:34 +01001208 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001209 return ret;
1210 }
1211 return 0;
1212}
1213
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001214int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001215 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001216 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001217 }
1218 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001219 int op_status = PartitionManager.Cancel_Backup();
1220 if (op_status != 0)
1221 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001222 }
1223
1224 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001225}
Dees_Troy51a0e822012-09-05 15:24:24 -04001226
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001227int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001228{
that73a52952015-01-28 23:07:34 +01001229 int op_status = 0;
1230
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001231 operation_start("Fix Contexts");
1232 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001233 if (simulate) {
1234 simulate_progress_bar();
1235 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001236 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001237 if (op_status != 0)
1238 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001239 }
that73a52952015-01-28 23:07:34 +01001240 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001241 return 0;
1242}
1243
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001244int GUIAction::fixpermissions(std::string arg)
1245{
1246 return fixcontexts(arg);
1247}
1248
that3f7b1ac2014-12-30 11:30:13 +01001249int GUIAction::dd(std::string arg)
1250{
1251 operation_start("imaging");
1252
1253 if (simulate) {
1254 simulate_progress_bar();
1255 } else {
1256 string cmd = "dd " + arg;
1257 TWFunc::Exec_Cmd(cmd);
1258 }
1259 operation_end(0);
1260 return 0;
1261}
1262
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001263int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001264{
1265 operation_start("Partition SD Card");
1266 int ret_val = 0;
1267
1268 if (simulate) {
1269 simulate_progress_bar();
1270 } else {
1271 int allow_partition;
1272 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1273 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001274 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001275 } else {
1276 if (!PartitionManager.Partition_SDCard())
1277 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001278 }
that3f7b1ac2014-12-30 11:30:13 +01001279 }
1280 operation_end(ret_val);
1281 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001282
that3f7b1ac2014-12-30 11:30:13 +01001283}
Dees_Troy51a0e822012-09-05 15:24:24 -04001284
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001285int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001286{
1287 operation_start("Install HTC Dumlock");
1288 if (simulate) {
1289 simulate_progress_bar();
1290 } else
1291 TWFunc::install_htc_dumlock();
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001297int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001298{
1299 operation_start("HTC Dumlock Restore Boot");
1300 if (simulate) {
1301 simulate_progress_bar();
1302 } else
1303 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001304
that3f7b1ac2014-12-30 11:30:13 +01001305 operation_end(0);
1306 return 0;
1307}
Dees_Troy51a0e822012-09-05 15:24:24 -04001308
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001309int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001310{
1311 operation_start("HTC Dumlock Reflash Recovery");
1312 if (simulate) {
1313 simulate_progress_bar();
1314 } else
1315 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001316
that3f7b1ac2014-12-30 11:30:13 +01001317 operation_end(0);
1318 return 0;
1319}
Dees_Troy51a0e822012-09-05 15:24:24 -04001320
that3f7b1ac2014-12-30 11:30:13 +01001321int GUIAction::cmd(std::string arg)
1322{
1323 int op_status = 0;
1324
1325 operation_start("Command");
1326 LOGINFO("Running command: '%s'\n", arg.c_str());
1327 if (simulate) {
1328 simulate_progress_bar();
1329 } else {
1330 op_status = TWFunc::Exec_Cmd(arg);
1331 if (op_status != 0)
1332 op_status = 1;
1333 }
1334
1335 operation_end(op_status);
1336 return 0;
1337}
1338
1339int GUIAction::terminalcommand(std::string arg)
1340{
1341 int op_status = 0;
1342 string cmdpath, command;
1343
1344 DataManager::GetValue("tw_terminal_location", cmdpath);
1345 operation_start("CommandOutput");
1346 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1347 if (simulate) {
1348 simulate_progress_bar();
1349 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001350 } else if (arg == "exit") {
1351 LOGINFO("Exiting terminal\n");
1352 operation_end(op_status);
1353 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001354 } else {
1355 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1356 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001357 DataManager::SetValue("tw_terminal_state", 1);
1358 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001359 FILE* fp;
1360 char line[512];
1361
1362 fp = popen(command.c_str(), "r");
1363 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001364 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001365 } else {
1366 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1367 struct timeval timeout;
1368 fd_set fdset;
1369
1370 while(keep_going)
1371 {
1372 FD_ZERO(&fdset);
1373 FD_SET(fd, &fdset);
1374 timeout.tv_sec = 0;
1375 timeout.tv_usec = 400000;
1376 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1377 if (has_data == 0) {
1378 // Timeout reached
1379 DataManager::GetValue("tw_terminal_state", check);
1380 if (check == 0) {
1381 keep_going = 0;
1382 }
1383 } else if (has_data < 0) {
1384 // End of execution
1385 keep_going = 0;
1386 } else {
1387 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001388 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001389 gui_print("%s", line); // Display output
1390 else
1391 keep_going = 0; // Done executing
1392 }
1393 }
1394 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001395 }
thatc6085482015-01-09 22:12:43 +01001396 DataManager::SetValue("tw_operation_status", 0);
1397 DataManager::SetValue("tw_operation_state", 1);
1398 DataManager::SetValue("tw_terminal_state", 0);
1399 DataManager::SetValue("tw_background_thread_running", 0);
1400 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001401 }
1402 return 0;
1403}
1404
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001405int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001406{
1407 int op_status = 0;
1408
1409 LOGINFO("Sending kill command...\n");
1410 operation_start("KillCommand");
1411 DataManager::SetValue("tw_operation_status", 0);
1412 DataManager::SetValue("tw_operation_state", 1);
1413 DataManager::SetValue("tw_terminal_state", 0);
1414 DataManager::SetValue("tw_background_thread_running", 0);
1415 DataManager::SetValue(TW_ACTION_BUSY, 0);
1416 return 0;
1417}
1418
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001419int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001420{
1421 int op_status = 0;
1422 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001423 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001424 if (simulate) {
1425 simulate_progress_bar();
1426 } else {
1427 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001428 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001429 }
1430
1431 operation_end(op_status);
1432 return 0;
1433}
1434
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001435int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001436{
1437 int op_status = 0;
1438
1439 operation_start("CheckBackupName");
1440 if (simulate) {
1441 simulate_progress_bar();
1442 } else {
1443 op_status = PartitionManager.Check_Backup_Name(true);
1444 if (op_status != 0)
1445 op_status = 1;
1446 }
1447
1448 operation_end(op_status);
1449 return 0;
1450}
1451
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001452int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001453{
1454 int op_status = 0;
1455
1456 operation_start("Decrypt");
1457 if (simulate) {
1458 simulate_progress_bar();
1459 } else {
1460 string Password;
1461 DataManager::GetValue("tw_crypto_password", Password);
1462 op_status = PartitionManager.Decrypt_Device(Password);
1463 if (op_status != 0)
1464 op_status = 1;
1465 else {
that3f7b1ac2014-12-30 11:30:13 +01001466
1467 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1468
Ethan Yonkercf50da52015-01-12 21:59:07 -06001469 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001470
Ethan Yonkercf50da52015-01-12 21:59:07 -06001471 // Check for a custom theme and load it if exists
1472 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1473 if (has_datamedia != 0) {
1474 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001475 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001476 } else {
1477 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001478 }
1479 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001480 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001481 }
1482 }
1483
1484 operation_end(op_status);
1485 return 0;
1486}
1487
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001488int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001489{
1490 operation_start("Sideload");
1491 if (simulate) {
1492 simulate_progress_bar();
1493 operation_end(0);
1494 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001496 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1497
1498 // wait for the adb connection
1499 int ret = apply_from_adb("/", &sideload_child_pid);
1500 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1501
1502 if (ret != 0) {
1503 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001504 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001505 ret = 1; // failure
1506 } else {
1507 int wipe_cache = 0;
1508 int wipe_dalvik = 0;
1509 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1510
1511 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1512 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1513 PartitionManager.Wipe_By_Path("/cache");
1514 if (wipe_dalvik)
1515 PartitionManager.Wipe_Dalvik_Cache();
1516 } else {
1517 ret = 1; // failure
1518 }
thatcc8ddca2015-01-03 01:59:36 +01001519 }
thatc6085482015-01-09 22:12:43 +01001520 if (sideload_child_pid) {
1521 LOGINFO("Signaling child sideload process to exit.\n");
1522 struct stat st;
1523 // Calling stat() on this magic filename signals the minadbd
1524 // subprocess to shut down.
1525 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1526 int status;
1527 LOGINFO("Waiting for child sideload process to exit.\n");
1528 waitpid(sideload_child_pid, &status, 0);
1529 }
Dees Troy28a85d22015-04-28 02:03:16 +00001530 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001531 TWFunc::Toggle_MTP(mtp_was_enabled);
1532 reinject_after_flash();
1533 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001534 }
that3f7b1ac2014-12-30 11:30:13 +01001535 return 0;
1536}
1537
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001538int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001539{
that3f7b1ac2014-12-30 11:30:13 +01001540 struct stat st;
1541 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001542 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001543 LOGINFO("Signaling child sideload process to exit.\n");
1544 // Calling stat() on this magic filename signals the minadbd
1545 // subprocess to shut down.
1546 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1547 if (!sideload_child_pid) {
1548 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001549 return 0;
1550 }
thatcc8ddca2015-01-03 01:59:36 +01001551 ::sleep(1);
1552 LOGINFO("Killing child sideload process.\n");
1553 kill(sideload_child_pid, SIGTERM);
1554 int status;
1555 LOGINFO("Waiting for child sideload process to exit.\n");
1556 waitpid(sideload_child_pid, &status, 0);
1557 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001558 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1559 return 0;
1560}
1561
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001562int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001563{
1564 operation_start("OpenRecoveryScript");
1565 if (simulate) {
1566 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001567 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001568 } else {
that10ae24f2015-12-26 20:53:51 +01001569 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001570 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001571 }
1572 return 0;
1573}
1574
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001575int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001576{
1577 int op_status = 0;
1578
1579 operation_start("Install SuperSU");
1580 if (simulate) {
1581 simulate_progress_bar();
1582 } else {
1583 if (!TWFunc::Install_SuperSU())
1584 op_status = 1;
1585 }
1586
1587 operation_end(op_status);
1588 return 0;
1589}
1590
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001591int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001592{
1593 int op_status = 0;
1594
1595 operation_start("Fixing Superuser Permissions");
1596 if (simulate) {
1597 simulate_progress_bar();
1598 } else {
1599 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1600 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1601 }
1602
1603 operation_end(op_status);
1604 return 0;
1605}
1606
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001607int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001608{
1609 int op_status = 0;
1610
1611 operation_start("Try Restore Decrypt");
1612 if (simulate) {
1613 simulate_progress_bar();
1614 } else {
1615 string Restore_Path, Filename, Password;
1616 DataManager::GetValue("tw_restore", Restore_Path);
1617 Restore_Path += "/";
1618 DataManager::GetValue("tw_restore_password", Password);
1619 TWFunc::SetPerformanceMode(true);
1620 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1621 op_status = 0; // success
1622 else
1623 op_status = 1; // fail
1624 TWFunc::SetPerformanceMode(false);
1625 }
1626
1627 operation_end(op_status);
1628 return 0;
1629}
1630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001631int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001632{
1633 int op_status = 0;
1634
1635 operation_start("Repair Partition");
1636 if (simulate) {
1637 simulate_progress_bar();
1638 } else {
1639 string part_path;
1640 DataManager::GetValue("tw_partition_mount_point", part_path);
1641 if (PartitionManager.Repair_By_Path(part_path, true)) {
1642 op_status = 0; // success
1643 } else {
that3f7b1ac2014-12-30 11:30:13 +01001644 op_status = 1; // fail
1645 }
1646 }
1647
1648 operation_end(op_status);
1649 return 0;
1650}
1651
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001652int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001653{
1654 int op_status = 0;
1655
1656 operation_start("Resize Partition");
1657 if (simulate) {
1658 simulate_progress_bar();
1659 } else {
1660 string part_path;
1661 DataManager::GetValue("tw_partition_mount_point", part_path);
1662 if (PartitionManager.Resize_By_Path(part_path, true)) {
1663 op_status = 0; // success
1664 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001665 op_status = 1; // fail
1666 }
1667 }
1668
1669 operation_end(op_status);
1670 return 0;
1671}
1672
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001673int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001674{
1675 int op_status = 0;
1676
1677 operation_start("Change File System");
1678 if (simulate) {
1679 simulate_progress_bar();
1680 } else {
1681 string part_path, file_system;
1682 DataManager::GetValue("tw_partition_mount_point", part_path);
1683 DataManager::GetValue("tw_action_new_file_system", file_system);
1684 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1685 op_status = 0; // success
1686 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001687 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001688 op_status = 1; // fail
1689 }
1690 }
1691 PartitionManager.Update_System_Details();
1692 operation_end(op_status);
1693 return 0;
1694}
1695
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001696int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001697{
1698 int op_status = 0;
1699
1700 operation_start("Start MTP");
1701 if (PartitionManager.Enable_MTP())
1702 op_status = 0; // success
1703 else
1704 op_status = 1; // fail
1705
1706 operation_end(op_status);
1707 return 0;
1708}
1709
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001710int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001711{
1712 int op_status = 0;
1713
1714 operation_start("Stop MTP");
1715 if (PartitionManager.Disable_MTP())
1716 op_status = 0; // success
1717 else
1718 op_status = 1; // fail
1719
1720 operation_end(op_status);
1721 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001722}
1723
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001724int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001725{
1726 int op_status = 0;
1727
bigbiffce8f83c2015-12-12 18:30:21 -05001728 PartitionSettings part_settings;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001729 operation_start("Flash Image");
bigbiffce8f83c2015-12-12 18:30:21 -05001730 DataManager::GetValue("tw_zip_location", part_settings.Restore_Name);
1731 DataManager::GetValue("tw_file", part_settings.Backup_FileName);
1732 unsigned long long total_bytes = TWFunc::Get_File_Size(part_settings.Restore_Name + "/" + part_settings.Backup_FileName);
1733 ProgressTracking progress(total_bytes);
1734 part_settings.progress = &progress;
1735 part_settings.adbbackup = false;
1736 part_settings.PM_Method = PM_RESTORE;
1737 if (PartitionManager.Flash_Image(&part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001738 op_status = 0; // success
1739 else
1740 op_status = 1; // fail
1741
1742 operation_end(op_status);
1743 return 0;
1744}
1745
that10ae24f2015-12-26 20:53:51 +01001746int GUIAction::twcmd(std::string arg)
1747{
1748 operation_start("TWRP CLI Command");
1749 if (simulate)
1750 simulate_progress_bar();
1751 else
1752 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1753 operation_end(0);
1754 return 0;
1755}
1756
Dees_Troy51a0e822012-09-05 15:24:24 -04001757int GUIAction::getKeyByName(std::string key)
1758{
that8834a0f2016-01-05 23:29:30 +01001759 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001760 else if (key == "menu") return KEY_MENU;
1761 else if (key == "back") return KEY_BACK;
1762 else if (key == "search") return KEY_SEARCH;
1763 else if (key == "voldown") return KEY_VOLUMEDOWN;
1764 else if (key == "volup") return KEY_VOLUMEUP;
1765 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001766 int ret_val;
1767 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1768 if (!ret_val)
1769 return KEY_POWER;
1770 else
1771 return ret_val;
1772 }
1773
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001774 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001775}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001776
1777int GUIAction::checkpartitionlifetimewrites(std::string arg)
1778{
1779 int op_status = 0;
1780 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1781
1782 operation_start("Check Partition Lifetime Writes");
1783 if (sys) {
1784 if (sys->Check_Lifetime_Writes() != 0)
1785 DataManager::SetValue("tw_lifetime_writes", 1);
1786 else
1787 DataManager::SetValue("tw_lifetime_writes", 0);
1788 op_status = 0; // success
1789 } else {
1790 DataManager::SetValue("tw_lifetime_writes", 1);
1791 op_status = 1; // fail
1792 }
1793
1794 operation_end(op_status);
1795 return 0;
1796}
1797
1798int GUIAction::mountsystemtoggle(std::string arg)
1799{
1800 int op_status = 0;
1801 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001802 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001803
1804 operation_start("Toggle System Mount");
1805 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1806 op_status = 1; // fail
1807 } else {
1808 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1809 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001810 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001811 DataManager::SetValue("tw_mount_system_ro", 0);
1812 Part->Change_Mount_Read_Only(false);
1813 } else {
1814 DataManager::SetValue("tw_mount_system_ro", 1);
1815 Part->Change_Mount_Read_Only(true);
1816 }
1817 if (remount_system) {
1818 Part->Mount(true);
1819 }
1820 op_status = 0; // success
1821 } else {
1822 op_status = 1; // fail
1823 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001824 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1825 if (Part) {
1826 if (arg == "0") {
1827 Part->Change_Mount_Read_Only(false);
1828 } else {
1829 Part->Change_Mount_Read_Only(true);
1830 }
1831 if (remount_vendor) {
1832 Part->Mount(true);
1833 }
1834 op_status = 0; // success
1835 } else {
1836 op_status = 1; // fail
1837 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001838 }
1839
1840 operation_end(op_status);
1841 return 0;
1842}
Ethan Yonker74db1572015-10-28 12:44:49 -05001843
1844int GUIAction::setlanguage(std::string arg __unused)
1845{
1846 int op_status = 0;
1847
1848 operation_start("Set Language");
1849 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1850 PageManager::RequestReload();
1851 op_status = 0; // success
1852
1853 operation_end(op_status);
1854 return 0;
1855}