blob: b471aafe76b67dcbc699d73a815bdfc5ab8bd922 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060053#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Dees_Troy51a0e822012-09-05 15:24:24 -040061void curtainClose(void);
62
that3f7b1ac2014-12-30 11:30:13 +010063GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010064std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010065static string zip_queue[10];
66static int zip_queue_index;
67static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010068pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010069
that73a52952015-01-28 23:07:34 +010070static void *ActionThread_work_wrapper(void *data);
71
72class ActionThread
73{
74public:
75 ActionThread();
76 ~ActionThread();
77
78 void threadActions(GUIAction *act);
79 void run(void *data);
80private:
81 friend void *ActionThread_work_wrapper(void*);
82 struct ThreadData
83 {
84 ActionThread *this_;
85 GUIAction *act;
86 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
87 };
88
89 pthread_t m_thread;
90 bool m_thread_running;
91 pthread_mutex_t m_act_lock;
92};
93
94static ActionThread action_thread; // for all kinds of longer running actions
95static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010096
97static void *ActionThread_work_wrapper(void *data)
98{
that73a52952015-01-28 23:07:34 +010099 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +0100100 return NULL;
101}
102
103ActionThread::ActionThread()
104{
105 m_thread_running = false;
106 pthread_mutex_init(&m_act_lock, NULL);
107}
108
109ActionThread::~ActionThread()
110{
111 pthread_mutex_lock(&m_act_lock);
112 if(m_thread_running) {
113 pthread_mutex_unlock(&m_act_lock);
114 pthread_join(m_thread, NULL);
115 } else {
116 pthread_mutex_unlock(&m_act_lock);
117 }
118 pthread_mutex_destroy(&m_act_lock);
119}
120
that7d3b54f2015-01-09 22:52:51 +0100121void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100122{
123 pthread_mutex_lock(&m_act_lock);
124 if (m_thread_running) {
125 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100126 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
127 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100128 } else {
129 m_thread_running = true;
130 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100131 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100132 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
133 }
134}
135
136void ActionThread::run(void *data)
137{
138 ThreadData *d = (ThreadData*)data;
139 GUIAction* act = d->act;
140
141 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100142 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100143 act->doAction(*it);
144
145 pthread_mutex_lock(&m_act_lock);
146 m_thread_running = false;
147 pthread_mutex_unlock(&m_act_lock);
148 delete d;
149}
150
Dees_Troy51a0e822012-09-05 15:24:24 -0400151GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100152 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400153{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 xml_node<>* child;
155 xml_node<>* actions;
156 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
that3f7b1ac2014-12-30 11:30:13 +0100160 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100161#define ADD_ACTION(n) mf[#n] = &GUIAction::n
162#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100163 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100164 ADD_ACTION(reboot);
165 ADD_ACTION(home);
166 ADD_ACTION(key);
167 ADD_ACTION(page);
168 ADD_ACTION(reload);
169 ADD_ACTION(readBackup);
170 ADD_ACTION(set);
171 ADD_ACTION(clear);
172 ADD_ACTION(mount);
173 ADD_ACTION(unmount);
174 ADD_ACTION_EX("umount", unmount);
175 ADD_ACTION(restoredefaultsettings);
176 ADD_ACTION(copylog);
177 ADD_ACTION(compute);
178 ADD_ACTION_EX("addsubtract", compute);
179 ADD_ACTION(setguitimezone);
180 ADD_ACTION(overlay);
181 ADD_ACTION(queuezip);
182 ADD_ACTION(cancelzip);
183 ADD_ACTION(queueclear);
184 ADD_ACTION(sleep);
185 ADD_ACTION(appenddatetobackupname);
186 ADD_ACTION(generatebackupname);
187 ADD_ACTION(checkpartitionlist);
188 ADD_ACTION(getpartitiondetails);
189 ADD_ACTION(screenshot);
190 ADD_ACTION(setbrightness);
191 ADD_ACTION(fileexists);
192 ADD_ACTION(killterminal);
193 ADD_ACTION(checkbackupname);
194 ADD_ACTION(adbsideloadcancel);
195 ADD_ACTION(fixsu);
196 ADD_ACTION(startmtp);
197 ADD_ACTION(stopmtp);
198 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500199 ADD_ACTION(checkpartitionlifetimewrites);
200 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500201 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100202
203 // remember actions that run in the caller thread
204 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
205 setActionsRunningInCallerThread.insert(it->first);
206
207 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100208 ADD_ACTION(flash);
209 ADD_ACTION(wipe);
210 ADD_ACTION(refreshsizes);
211 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600212 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100213 ADD_ACTION(fixpermissions);
214 ADD_ACTION(dd);
215 ADD_ACTION(partitionsd);
216 ADD_ACTION(installhtcdumlock);
217 ADD_ACTION(htcdumlockrestoreboot);
218 ADD_ACTION(htcdumlockreflashrecovery);
219 ADD_ACTION(cmd);
220 ADD_ACTION(terminalcommand);
221 ADD_ACTION(reinjecttwrp);
222 ADD_ACTION(decrypt);
223 ADD_ACTION(adbsideload);
224 ADD_ACTION(openrecoveryscript);
225 ADD_ACTION(installsu);
226 ADD_ACTION(decrypt_backup);
227 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500228 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100229 ADD_ACTION(changefilesystem);
230 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100231 ADD_ACTION(twcmd);
that3f7b1ac2014-12-30 11:30:13 +0100232 }
233
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600235 actions = FindNode(node, "actions");
236 if (actions) child = FindNode(actions, "action");
237 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 while (child)
242 {
243 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 attr = child->first_attribute("function");
246 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 action.mFunction = attr->value();
249 action.mArg = child->value();
250 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 child = child->next_sibling("action");
253 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600256 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 if (child)
258 {
259 attr = child->first_attribute("key");
260 if (attr)
261 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100262 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
263 for(size_t i = 0; i < keys.size(); ++i)
264 {
265 const int key = getKeyByName(keys[i]);
266 mKeys[key] = false;
267 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 }
269 else
270 {
271 attr = child->first_attribute("x");
272 if (!attr) return;
273 mActionX = atol(attr->value());
274 attr = child->first_attribute("y");
275 if (!attr) return;
276 mActionY = atol(attr->value());
277 attr = child->first_attribute("w");
278 if (!attr) return;
279 mActionW = atol(attr->value());
280 attr = child->first_attribute("h");
281 if (!attr) return;
282 mActionH = atol(attr->value());
283 }
284 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400285}
286
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500287int GUIAction::NotifyTouch(TOUCH_STATE state __unused, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400288{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 if (state == TOUCH_RELEASE)
290 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400291
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293}
294
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100295int GUIAction::NotifyKey(int key, bool down)
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())
that8834a0f2016-01-05 23:29:30 +0100299 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100300
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) {
thatd4725ca2016-01-19 00:15:21 +0100309 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310 doActions();
thatd4725ca2016-01-19 00:15:21 +0100311 return 0;
312 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313 } else if(down) {
314 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
315 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100316 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100317 }
318
319 // Passed, all req buttons are pressed, reset them and consume release events
320 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
321 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
322 kb->ConsumeKeyRelease(itr->first);
323 itr->second = false;
324 }
325
326 doActions();
thatd4725ca2016-01-19 00:15:21 +0100327 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100328 }
329
thatd4725ca2016-01-19 00:15:21 +0100330 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400331}
332
Vojtech Bocek07220562014-02-08 02:05:33 +0100333int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400334{
Vojtech Bocek07220562014-02-08 02:05:33 +0100335 GUIObject::NotifyVarChange(varName, value);
336
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100337 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100339 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400343}
344
345void GUIAction::simulate_progress_bar(void)
346{
Ethan Yonker74db1572015-10-28 12:44:49 -0500347 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400348 for (int i = 0; i < 5; i++)
349 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500350 if (PartitionManager.stop_backup.get_value()) {
351 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600352 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500353 DataManager::SetValue("ui_progress", 0);
354 PartitionManager.stop_backup.set_value(0);
355 return;
356 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 usleep(500000);
358 DataManager::SetValue("ui_progress", i * 20);
359 }
360}
361
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600362int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400363{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
366 DataManager::SetValue("ui_progress", 0);
367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 if (filename.empty())
369 {
370 LOGERR("No file specified.\n");
371 return -1;
372 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400373
Ethan Yonker9598c072016-01-15 21:54:34 -0600374 if (!TWFunc::Path_Exists(filename)) {
375 if (!PartitionManager.Mount_By_Path(filename, true)) {
376 return -1;
377 }
378 if (!TWFunc::Path_Exists(filename)) {
379 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
380 return -1;
381 }
382 }
Dees_Troy657c3092012-09-10 20:32:10 -0400383
Dees_Troy51a0e822012-09-05 15:24:24 -0400384 if (simulate) {
385 simulate_progress_bar();
386 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400387 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400388
389 // Now, check if we need to ensure TWRP remains installed...
390 struct stat st;
391 if (stat("/sbin/installTwrp", &st) == 0)
392 {
393 DataManager::SetValue("tw_operation", "Configuring TWRP");
394 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500395 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200396 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500398 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 }
400 }
401 }
402
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 // Done
404 DataManager::SetValue("ui_progress", 100);
405 DataManager::SetValue("ui_progress", 0);
406 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407}
408
that73a52952015-01-28 23:07:34 +0100409GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100410{
that73a52952015-01-28 23:07:34 +0100411 string func = gui_parse_text(action.mFunction);
412 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
413 if (needsThread) {
414 if (func == "cancelbackup")
415 return THREAD_CANCEL;
416 else
417 return THREAD_ACTION;
418 }
419 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100420}
421
Dees_Troy51a0e822012-09-05 15:24:24 -0400422int GUIAction::doActions()
423{
that3f7b1ac2014-12-30 11:30:13 +0100424 if (mActions.size() < 1)
425 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426
that73a52952015-01-28 23:07:34 +0100427 // Determine in which thread to run the actions.
428 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
429 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100430 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100431 for (it = mActions.begin(); it != mActions.end(); ++it) {
432 ThreadType tt = getThreadType(*it);
433 if (tt == THREAD_NONE)
434 continue;
435 if (threadType == THREAD_NONE)
436 threadType = tt;
437 else if (threadType != tt) {
438 LOGERR("Can't mix normal and cancel actions in the same list.\n"
439 "Running the whole batch in the cancel thread.\n");
440 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100441 break;
442 }
that7d3b54f2015-01-09 22:52:51 +0100443 }
that73a52952015-01-28 23:07:34 +0100444
445 // Now run the actions in the desired thread.
446 switch (threadType) {
447 case THREAD_ACTION:
448 action_thread.threadActions(this);
449 break;
450
451 case THREAD_CANCEL:
452 cancel_thread.threadActions(this);
453 break;
454
455 default: {
456 // no iterators here because theme reloading might kill our object
457 const size_t cnt = mActions.size();
458 for (size_t i = 0; i < cnt; ++i)
459 doAction(mActions[i]);
460 }
thatc6085482015-01-09 22:12:43 +0100461 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400462
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200463 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400464}
465
that3f7b1ac2014-12-30 11:30:13 +0100466int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400467{
that3f7b1ac2014-12-30 11:30:13 +0100468 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400469
that3f7b1ac2014-12-30 11:30:13 +0100470 std::string function = gui_parse_text(action.mFunction);
471 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400472
that3f7b1ac2014-12-30 11:30:13 +0100473 // find function and execute it
474 mapFunc::const_iterator funcitr = mf.find(function);
475 if (funcitr != mf.end())
476 return (this->*funcitr->second)(arg);
477
478 LOGERR("Unknown action '%s'\n", function.c_str());
479 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
482void GUIAction::operation_start(const string operation_name)
483{
that3f7b1ac2014-12-30 11:30:13 +0100484 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000485 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 DataManager::SetValue(TW_ACTION_BUSY, 1);
487 DataManager::SetValue("ui_progress", 0);
488 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600490 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491}
492
that3f7b1ac2014-12-30 11:30:13 +0100493void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400494{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000495 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue("ui_progress", 100);
498 if (simulate) {
499 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
500 if (simulate_fail != 0)
501 DataManager::SetValue("tw_operation_status", 1);
502 else
503 DataManager::SetValue("tw_operation_status", 0);
504 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500505 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 }
508 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500510 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 }
512 DataManager::SetValue("tw_operation_state", 1);
513 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500514 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200515 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000516 time(&Stop);
517 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600518 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100519 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400520}
521
that3f7b1ac2014-12-30 11:30:13 +0100522int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400523{
that3f7b1ac2014-12-30 11:30:13 +0100524 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400525
that3f7b1ac2014-12-30 11:30:13 +0100526 sync();
527 DataManager::SetValue("tw_gui_done", 1);
528 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
that3f7b1ac2014-12-30 11:30:13 +0100530 return 0;
531}
Dees_Troy51a0e822012-09-05 15:24:24 -0400532
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500533int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100534{
that3f7b1ac2014-12-30 11:30:13 +0100535 gui_changePage("main");
536 return 0;
537}
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
that3f7b1ac2014-12-30 11:30:13 +0100539int GUIAction::key(std::string arg)
540{
541 const int key = getKeyByName(arg);
542 PageManager::NotifyKey(key, true);
543 PageManager::NotifyKey(key, false);
544 return 0;
545}
546
547int GUIAction::page(std::string arg)
548{
LuK133762326f42015-09-30 19:57:46 +0200549 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100550 std::string page_name = gui_parse_text(arg);
551 return gui_changePage(page_name);
552}
553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500556 PageManager::RequestReload();
557 // The actual reload is handled in pages.cpp in PageManager::RunReload()
558 // The reload will occur on the next Update or Render call and will
559 // be performed in the rendoer thread instead of the action thread
560 // to prevent crashing which could occur when we start deleting
561 // GUI resources in the action thread while we attempt to render
562 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100563 return 0;
564}
Dees_Troy51a0e822012-09-05 15:24:24 -0400565
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500566int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100567{
568 string Restore_Name;
569 DataManager::GetValue("tw_restore", Restore_Name);
570 PartitionManager.Set_Restore_Files(Restore_Name);
571 return 0;
572}
573
574int GUIAction::set(std::string arg)
575{
576 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200577 {
that3f7b1ac2014-12-30 11:30:13 +0100578 string varName = arg.substr(0, arg.find('='));
579 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400580
that3f7b1ac2014-12-30 11:30:13 +0100581 DataManager::GetValue(value, value);
582 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 }
that3f7b1ac2014-12-30 11:30:13 +0100584 else
585 DataManager::SetValue(arg, "1");
586 return 0;
587}
Dees_Troy51a0e822012-09-05 15:24:24 -0400588
that3f7b1ac2014-12-30 11:30:13 +0100589int GUIAction::clear(std::string arg)
590{
591 DataManager::SetValue(arg, "0");
592 return 0;
593}
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
that3f7b1ac2014-12-30 11:30:13 +0100595int GUIAction::mount(std::string arg)
596{
597 if (arg == "usb") {
598 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100600 PartitionManager.usb_storage_enable();
601 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500602 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100603 } else if (!simulate) {
604 PartitionManager.Mount_By_Path(arg, true);
605 PartitionManager.Add_MTP_Storage(arg);
606 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500607 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100608 return 0;
609}
610
611int GUIAction::unmount(std::string arg)
612{
613 if (arg == "usb") {
614 if (!simulate)
615 PartitionManager.usb_storage_disable();
616 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100618 DataManager::SetValue(TW_ACTION_BUSY, 0);
619 } else if (!simulate) {
620 PartitionManager.UnMount_By_Path(arg, true);
621 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500622 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100623 return 0;
624}
625
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500626int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100627{
628 operation_start("Restore Defaults");
629 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500630 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100631 else {
632 DataManager::ResetDefaults();
633 PartitionManager.Update_System_Details();
634 PartitionManager.Mount_Current_Storage(true);
635 }
636 operation_end(0);
637 return 0;
638}
639
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500640int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100641{
642 operation_start("Copy Log");
643 if (!simulate)
644 {
645 string dst;
646 PartitionManager.Mount_Current_Storage(true);
647 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
648 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
649 tw_set_default_metadata(dst.c_str());
650 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500651 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100652 } else
653 simulate_progress_bar();
654 operation_end(0);
655 return 0;
656}
657
658
659int GUIAction::compute(std::string arg)
660{
661 if (arg.find("+") != string::npos)
662 {
663 string varName = arg.substr(0, arg.find('+'));
664 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
665 int amount_to_add = atoi(string_to_add.c_str());
666 int value;
667
668 DataManager::GetValue(varName, value);
669 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 return 0;
671 }
that3f7b1ac2014-12-30 11:30:13 +0100672 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 {
that3f7b1ac2014-12-30 11:30:13 +0100674 string varName = arg.substr(0, arg.find('-'));
675 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
676 int amount_to_subtract = atoi(string_to_subtract.c_str());
677 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400678
that3f7b1ac2014-12-30 11:30:13 +0100679 DataManager::GetValue(varName, value);
680 value -= amount_to_subtract;
681 if (value <= 0)
682 value = 0;
683 DataManager::SetValue(varName, value);
684 return 0;
685 }
686 if (arg.find("*") != string::npos)
687 {
688 string varName = arg.substr(0, arg.find('*'));
689 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
690 int multiply_by = atoi(multiply_by_str.c_str());
691 int value;
692
693 DataManager::GetValue(varName, value);
694 DataManager::SetValue(varName, value*multiply_by);
695 return 0;
696 }
697 if (arg.find("/") != string::npos)
698 {
699 string varName = arg.substr(0, arg.find('/'));
700 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
701 int divide_by = atoi(divide_by_str.c_str());
702 int value;
703
704 if(divide_by != 0)
705 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400706 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100707 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 }
709 return 0;
710 }
that3f7b1ac2014-12-30 11:30:13 +0100711 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
712 return -1;
713}
Dees_Troy51a0e822012-09-05 15:24:24 -0400714
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500715int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100716{
717 string SelectedZone;
718 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
719 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
720 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
721
722 int dst;
723 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
724
725 string offset;
726 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
727
728 string NewTimeZone = Zone;
729 if (offset != "0")
730 NewTimeZone += ":" + offset;
731
732 if (dst != 0)
733 NewTimeZone += DSTZone;
734
735 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
736 DataManager::update_tz_environment_variables();
737 return 0;
738}
739
740int GUIAction::overlay(std::string arg)
741{
742 return gui_changeOverlay(arg);
743}
744
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500745int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100746{
747 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500748 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 return 0;
750 }
that3f7b1ac2014-12-30 11:30:13 +0100751 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
752 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
753 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400754 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100755 }
756 return 0;
757}
758
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500759int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100760{
761 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500762 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100764 } else {
765 zip_queue_index--;
766 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 }
that3f7b1ac2014-12-30 11:30:13 +0100768 return 0;
769}
Dees_Troy51a0e822012-09-05 15:24:24 -0400770
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500771int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100772{
773 zip_queue_index = 0;
774 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
775 return 0;
776}
Dees_Troy51a0e822012-09-05 15:24:24 -0400777
that3f7b1ac2014-12-30 11:30:13 +0100778int GUIAction::sleep(std::string arg)
779{
780 operation_start("Sleep");
781 usleep(atoi(arg.c_str()));
782 operation_end(0);
783 return 0;
784}
Dees Troyb21cc642013-09-10 17:36:41 +0000785
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500786int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100787{
788 operation_start("AppendDateToBackupName");
789 string Backup_Name;
790 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
791 Backup_Name += TWFunc::Get_Current_Date();
792 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
793 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
794 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500795 PageManager::NotifyKey(KEY_END, true);
796 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100797 operation_end(0);
798 return 0;
799}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500800
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500801int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100802{
803 operation_start("GenerateBackupName");
804 TWFunc::Auto_Generate_Backup_Name();
805 operation_end(0);
806 return 0;
807}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500808
Ethan Yonker483e9f42016-01-11 22:21:18 -0600809int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100810{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600811 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100812 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500813
Ethan Yonker483e9f42016-01-11 22:21:18 -0600814 if (arg.empty())
815 arg = "tw_wipe_list";
816 DataManager::GetValue(arg, List);
817 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
818 if (!List.empty()) {
819 size_t start_pos = 0, end_pos = List.find(";", start_pos);
820 while (end_pos != string::npos && start_pos < List.size()) {
821 part_path = List.substr(start_pos, end_pos - start_pos);
822 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
823 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100824 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400825 } else {
that3f7b1ac2014-12-30 11:30:13 +0100826 count++;
827 }
828 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600829 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100830 }
831 DataManager::SetValue("tw_check_partition_list", count);
832 } else {
833 DataManager::SetValue("tw_check_partition_list", 0);
834 }
835 return 0;
836}
Dees_Troy51a0e822012-09-05 15:24:24 -0400837
Ethan Yonker483e9f42016-01-11 22:21:18 -0600838int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100839{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600840 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100841 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400842
Ethan Yonker483e9f42016-01-11 22:21:18 -0600843 if (arg.empty())
844 arg = "tw_wipe_list";
845 DataManager::GetValue(arg, List);
846 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
847 if (!List.empty()) {
848 size_t start_pos = 0, end_pos = List.find(";", start_pos);
849 part_path = List;
850 while (end_pos != string::npos && start_pos < List.size()) {
851 part_path = List.substr(start_pos, end_pos - start_pos);
852 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
853 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100854 // Do nothing
855 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600856 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100857 break;
858 }
859 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600860 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100861 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862 if (!part_path.empty()) {
863 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100864 if (Part) {
865 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500866
that3f7b1ac2014-12-30 11:30:13 +0100867 DataManager::SetValue("tw_partition_name", Part->Display_Name);
868 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
869 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
870 DataManager::SetValue("tw_partition_size", Part->Size / mb);
871 DataManager::SetValue("tw_partition_used", Part->Used / mb);
872 DataManager::SetValue("tw_partition_free", Part->Free / mb);
873 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
874 DataManager::SetValue("tw_partition_removable", Part->Removable);
875 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
876
877 if (Part->Can_Repair())
878 DataManager::SetValue("tw_partition_can_repair", 1);
879 else
880 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500881 if (Part->Can_Resize())
882 DataManager::SetValue("tw_partition_can_resize", 1);
883 else
884 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600885 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100886 DataManager::SetValue("tw_partition_vfat", 1);
887 else
888 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600889 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100890 DataManager::SetValue("tw_partition_exfat", 1);
891 else
892 DataManager::SetValue("tw_partition_exfat", 0);
893 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
894 DataManager::SetValue("tw_partition_f2fs", 1);
895 else
896 DataManager::SetValue("tw_partition_f2fs", 0);
897 if (TWFunc::Path_Exists("/sbin/mke2fs"))
898 DataManager::SetValue("tw_partition_ext", 1);
899 else
900 DataManager::SetValue("tw_partition_ext", 0);
901 return 0;
902 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600903 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100904 }
905 }
906 }
907 DataManager::SetValue("tw_partition_name", "");
908 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600909 // Set this to 0 to prevent trying to partition this device, just in case
910 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100911 return 0;
912}
913
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500914int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100915{
916 time_t tm;
917 char path[256];
918 int path_len;
919 uid_t uid = -1;
920 gid_t gid = -1;
921
922 struct passwd *pwd = getpwnam("media_rw");
923 if(pwd) {
924 uid = pwd->pw_uid;
925 gid = pwd->pw_gid;
926 }
927
928 const std::string storage = DataManager::GetCurrentStoragePath();
929 if(PartitionManager.Is_Mounted_By_Path(storage)) {
930 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
931 } else {
932 strcpy(path, "/tmp/");
933 }
934
Xuefer579e9072016-01-22 13:35:50 +0800935 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100936 return 0;
937
938 tm = time(NULL);
939 path_len = strlen(path);
940
941 // Screenshot_2014-01-01-18-21-38.png
942 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
943
944 int res = gr_save_screenshot(path);
945 if(res == 0) {
946 chmod(path, 0666);
947 chown(path, uid, gid);
948
that677b13f2015-12-26 23:57:31 +0100949 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100950
951 // blink to notify that the screenshow was taken
952 gr_color(255, 255, 255, 255);
953 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
954 gr_flip();
955 gui_forceRender();
956 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500957 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100958 }
959 return 0;
960}
961
962int GUIAction::setbrightness(std::string arg)
963{
964 return TWFunc::Set_Brightness(arg);
965}
966
967int GUIAction::fileexists(std::string arg)
968{
969 struct stat st;
970 string newpath = arg + "/.";
971
972 operation_start("FileExists");
973 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
974 operation_end(0);
975 else
976 operation_end(1);
977 return 0;
978}
979
thatcc8ddca2015-01-03 01:59:36 +0100980void GUIAction::reinject_after_flash()
981{
982 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500983 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100984 if (simulate) {
985 simulate_progress_bar();
986 } else {
987 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
988 if (Boot == NULL || Boot->Current_File_System != "emmc")
989 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
990 else {
991 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
992 TWFunc::Exec_Cmd(injectcmd);
993 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500994 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +0100995 }
996 }
997}
998
that3f7b1ac2014-12-30 11:30:13 +0100999int GUIAction::flash(std::string arg)
1000{
1001 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001002 // We're going to jump to this page first, like a loading page
1003 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001004 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001005 string zip_path = zip_queue[i];
1006 size_t slashpos = zip_path.find_last_of('/');
1007 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001008 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001009 DataManager::SetValue("tw_filename", zip_path);
1010 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001011 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1012
1013 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001014 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001015 TWFunc::SetPerformanceMode(false);
1016 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001017 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001018 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001019 break;
that3f7b1ac2014-12-30 11:30:13 +01001020 }
1021 }
1022 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001023
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001024 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001025 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001026 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001027 }
that3f7b1ac2014-12-30 11:30:13 +01001028
thatcc8ddca2015-01-03 01:59:36 +01001029 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001030 PartitionManager.Update_System_Details();
bigbiffa869fc72016-03-01 19:40:36 -05001031 if (DataManager::GetIntValue("tw_install_reboot") > 0 && ret_val == 0) {
1032 gui_msg("install_reboot=Rebooting in 5 seconds");
1033 usleep(5000000);
1034 TWFunc::tw_reboot(rb_system);
1035 usleep(5000000); // another sleep while we wait for the reboot to occur
1036 }
that3f7b1ac2014-12-30 11:30:13 +01001037 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001038 // 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 -06001039 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001040 return 0;
1041}
1042
1043int GUIAction::wipe(std::string arg)
1044{
1045 operation_start("Format");
1046 DataManager::SetValue("tw_partition", arg);
1047
1048 int ret_val = false;
1049
1050 if (simulate) {
1051 simulate_progress_bar();
1052 } else {
1053 if (arg == "data")
1054 ret_val = PartitionManager.Factory_Reset();
1055 else if (arg == "battery")
1056 ret_val = PartitionManager.Wipe_Battery_Stats();
1057 else if (arg == "rotate")
1058 ret_val = PartitionManager.Wipe_Rotate_Data();
1059 else if (arg == "dalvik")
1060 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1061 else if (arg == "DATAMEDIA") {
1062 ret_val = PartitionManager.Format_Data();
1063 } else if (arg == "INTERNAL") {
1064 int has_datamedia, dual_storage;
1065
1066 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1067 if (has_datamedia) {
1068 ret_val = PartitionManager.Wipe_Media_From_Data();
1069 } else {
1070 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1071 }
1072 } else if (arg == "EXTERNAL") {
1073 string External_Path;
1074
1075 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1076 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1077 } else if (arg == "ANDROIDSECURE") {
1078 ret_val = PartitionManager.Wipe_Android_Secure();
1079 } else if (arg == "LIST") {
1080 string Wipe_List, wipe_path;
1081 bool skip = false;
1082 ret_val = true;
1083 TWPartition* wipe_part = NULL;
1084
1085 DataManager::GetValue("tw_wipe_list", Wipe_List);
1086 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1087 if (!Wipe_List.empty()) {
1088 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1089 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1090 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1091 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1092 if (wipe_path == "/and-sec") {
1093 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001094 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001095 ret_val = false;
1096 break;
1097 } else {
1098 skip = true;
1099 }
1100 } else if (wipe_path == "DALVIK") {
1101 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001102 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001103 ret_val = false;
1104 break;
1105 } else {
1106 skip = true;
1107 }
1108 } else if (wipe_path == "INTERNAL") {
1109 if (!PartitionManager.Wipe_Media_From_Data()) {
1110 ret_val = false;
1111 break;
1112 } else {
1113 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001114 }
1115 }
that3f7b1ac2014-12-30 11:30:13 +01001116 if (!skip) {
1117 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001118 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001119 ret_val = false;
1120 break;
1121 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1122 arg = wipe_path;
1123 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001124 } else {
that3f7b1ac2014-12-30 11:30:13 +01001125 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001126 }
that3f7b1ac2014-12-30 11:30:13 +01001127 start_pos = end_pos + 1;
1128 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001129 }
1130 }
that3f7b1ac2014-12-30 11:30:13 +01001131 } else
1132 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001133#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001134 if (arg == DataManager::GetSettingsStoragePath()) {
1135 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1136 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001137
that3f7b1ac2014-12-30 11:30:13 +01001138 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1139 LOGINFO("Making TWRP folder and saving settings.\n");
1140 Storage_Path += "/TWRP";
1141 mkdir(Storage_Path.c_str(), 0777);
1142 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001143 } else {
that3f7b1ac2014-12-30 11:30:13 +01001144 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1145 }
1146 }
1147#endif
1148 }
1149 PartitionManager.Update_System_Details();
1150 if (ret_val)
1151 ret_val = 0; // 0 is success
1152 else
1153 ret_val = 1; // 1 is failure
1154 operation_end(ret_val);
1155 return 0;
1156}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001157
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001158int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001159{
1160 operation_start("Refreshing Sizes");
1161 if (simulate) {
1162 simulate_progress_bar();
1163 } else
1164 PartitionManager.Update_System_Details();
1165 operation_end(0);
1166 return 0;
1167}
1168
1169int GUIAction::nandroid(std::string arg)
1170{
that3f7b1ac2014-12-30 11:30:13 +01001171 if (simulate) {
that73a52952015-01-28 23:07:34 +01001172 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001173 DataManager::SetValue("tw_partition", "Simulation");
1174 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001175 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001176 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001177 operation_start("Nandroid");
1178 int ret = 0;
1179
that3f7b1ac2014-12-30 11:30:13 +01001180 if (arg == "backup") {
1181 string Backup_Name;
1182 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001183 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1184 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 -05001185 ret = PartitionManager.Run_Backup(false);
1186 } else {
that3f7b1ac2014-12-30 11:30:13 +01001187 operation_end(1);
1188 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001189 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001190 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001191 } else if (arg == "restore") {
1192 string Restore_Name;
1193 DataManager::GetValue("tw_restore", Restore_Name);
1194 ret = PartitionManager.Run_Restore(Restore_Name);
1195 } else {
1196 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001197 return -1;
1198 }
1199 DataManager::SetValue("tw_encrypt_backup", 0);
1200 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001201 if (ret == false)
1202 ret = 1; // 1 for failure
1203 else
1204 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001205 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001206 }
1207 else {
1208 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001209 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001210 ret = 0;
1211 }
that73a52952015-01-28 23:07:34 +01001212 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001213 return ret;
1214 }
1215 return 0;
1216}
1217
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001218int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001219 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001220 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001221 }
1222 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001223 int op_status = PartitionManager.Cancel_Backup();
1224 if (op_status != 0)
1225 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001226 }
1227
1228 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001229}
Dees_Troy51a0e822012-09-05 15:24:24 -04001230
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001231int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001232{
that73a52952015-01-28 23:07:34 +01001233 int op_status = 0;
1234
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001235 operation_start("Fix Contexts");
1236 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001237 if (simulate) {
1238 simulate_progress_bar();
1239 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001240 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001241 if (op_status != 0)
1242 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001243 }
that73a52952015-01-28 23:07:34 +01001244 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001245 return 0;
1246}
1247
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001248int GUIAction::fixpermissions(std::string arg)
1249{
1250 return fixcontexts(arg);
1251}
1252
that3f7b1ac2014-12-30 11:30:13 +01001253int GUIAction::dd(std::string arg)
1254{
1255 operation_start("imaging");
1256
1257 if (simulate) {
1258 simulate_progress_bar();
1259 } else {
1260 string cmd = "dd " + arg;
1261 TWFunc::Exec_Cmd(cmd);
1262 }
1263 operation_end(0);
1264 return 0;
1265}
1266
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001267int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001268{
1269 operation_start("Partition SD Card");
1270 int ret_val = 0;
1271
1272 if (simulate) {
1273 simulate_progress_bar();
1274 } else {
1275 int allow_partition;
1276 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1277 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001278 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001279 } else {
1280 if (!PartitionManager.Partition_SDCard())
1281 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001282 }
that3f7b1ac2014-12-30 11:30:13 +01001283 }
1284 operation_end(ret_val);
1285 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001286
that3f7b1ac2014-12-30 11:30:13 +01001287}
Dees_Troy51a0e822012-09-05 15:24:24 -04001288
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001289int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001290{
1291 operation_start("Install HTC Dumlock");
1292 if (simulate) {
1293 simulate_progress_bar();
1294 } else
1295 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
that3f7b1ac2014-12-30 11:30:13 +01001297 operation_end(0);
1298 return 0;
1299}
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001301int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001302{
1303 operation_start("HTC Dumlock Restore Boot");
1304 if (simulate) {
1305 simulate_progress_bar();
1306 } else
1307 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001308
that3f7b1ac2014-12-30 11:30:13 +01001309 operation_end(0);
1310 return 0;
1311}
Dees_Troy51a0e822012-09-05 15:24:24 -04001312
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001313int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001314{
1315 operation_start("HTC Dumlock Reflash Recovery");
1316 if (simulate) {
1317 simulate_progress_bar();
1318 } else
1319 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001320
that3f7b1ac2014-12-30 11:30:13 +01001321 operation_end(0);
1322 return 0;
1323}
Dees_Troy51a0e822012-09-05 15:24:24 -04001324
that3f7b1ac2014-12-30 11:30:13 +01001325int GUIAction::cmd(std::string arg)
1326{
1327 int op_status = 0;
1328
1329 operation_start("Command");
1330 LOGINFO("Running command: '%s'\n", arg.c_str());
1331 if (simulate) {
1332 simulate_progress_bar();
1333 } else {
1334 op_status = TWFunc::Exec_Cmd(arg);
1335 if (op_status != 0)
1336 op_status = 1;
1337 }
1338
1339 operation_end(op_status);
1340 return 0;
1341}
1342
1343int GUIAction::terminalcommand(std::string arg)
1344{
1345 int op_status = 0;
1346 string cmdpath, command;
1347
1348 DataManager::GetValue("tw_terminal_location", cmdpath);
1349 operation_start("CommandOutput");
1350 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1351 if (simulate) {
1352 simulate_progress_bar();
1353 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001354 } else if (arg == "exit") {
1355 LOGINFO("Exiting terminal\n");
1356 operation_end(op_status);
1357 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001358 } else {
1359 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1360 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001361 DataManager::SetValue("tw_terminal_state", 1);
1362 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001363 FILE* fp;
1364 char line[512];
1365
1366 fp = popen(command.c_str(), "r");
1367 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001368 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001369 } else {
1370 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1371 struct timeval timeout;
1372 fd_set fdset;
1373
1374 while(keep_going)
1375 {
1376 FD_ZERO(&fdset);
1377 FD_SET(fd, &fdset);
1378 timeout.tv_sec = 0;
1379 timeout.tv_usec = 400000;
1380 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1381 if (has_data == 0) {
1382 // Timeout reached
1383 DataManager::GetValue("tw_terminal_state", check);
1384 if (check == 0) {
1385 keep_going = 0;
1386 }
1387 } else if (has_data < 0) {
1388 // End of execution
1389 keep_going = 0;
1390 } else {
1391 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001392 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001393 gui_print("%s", line); // Display output
1394 else
1395 keep_going = 0; // Done executing
1396 }
1397 }
1398 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001399 }
thatc6085482015-01-09 22:12:43 +01001400 DataManager::SetValue("tw_operation_status", 0);
1401 DataManager::SetValue("tw_operation_state", 1);
1402 DataManager::SetValue("tw_terminal_state", 0);
1403 DataManager::SetValue("tw_background_thread_running", 0);
1404 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001405 }
1406 return 0;
1407}
1408
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001409int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001410{
1411 int op_status = 0;
1412
1413 LOGINFO("Sending kill command...\n");
1414 operation_start("KillCommand");
1415 DataManager::SetValue("tw_operation_status", 0);
1416 DataManager::SetValue("tw_operation_state", 1);
1417 DataManager::SetValue("tw_terminal_state", 0);
1418 DataManager::SetValue("tw_background_thread_running", 0);
1419 DataManager::SetValue(TW_ACTION_BUSY, 0);
1420 return 0;
1421}
1422
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001423int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001424{
1425 int op_status = 0;
1426 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001427 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001428 if (simulate) {
1429 simulate_progress_bar();
1430 } else {
1431 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001432 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001433 }
1434
1435 operation_end(op_status);
1436 return 0;
1437}
1438
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001439int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001440{
1441 int op_status = 0;
1442
1443 operation_start("CheckBackupName");
1444 if (simulate) {
1445 simulate_progress_bar();
1446 } else {
1447 op_status = PartitionManager.Check_Backup_Name(true);
1448 if (op_status != 0)
1449 op_status = 1;
1450 }
1451
1452 operation_end(op_status);
1453 return 0;
1454}
1455
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001456int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001457{
1458 int op_status = 0;
1459
1460 operation_start("Decrypt");
1461 if (simulate) {
1462 simulate_progress_bar();
1463 } else {
1464 string Password;
1465 DataManager::GetValue("tw_crypto_password", Password);
1466 op_status = PartitionManager.Decrypt_Device(Password);
1467 if (op_status != 0)
1468 op_status = 1;
1469 else {
that3f7b1ac2014-12-30 11:30:13 +01001470
1471 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1472
Ethan Yonkercf50da52015-01-12 21:59:07 -06001473 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001474
Ethan Yonkercf50da52015-01-12 21:59:07 -06001475 // Check for a custom theme and load it if exists
1476 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1477 if (has_datamedia != 0) {
1478 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001479 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001480 } else {
1481 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001482 }
1483 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001484 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001485 }
1486 }
1487
1488 operation_end(op_status);
1489 return 0;
1490}
1491
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001492int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001493{
1494 operation_start("Sideload");
1495 if (simulate) {
1496 simulate_progress_bar();
1497 operation_end(0);
1498 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001499 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001500 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1501
1502 // wait for the adb connection
1503 int ret = apply_from_adb("/", &sideload_child_pid);
1504 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1505
1506 if (ret != 0) {
1507 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001508 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001509 ret = 1; // failure
1510 } else {
1511 int wipe_cache = 0;
1512 int wipe_dalvik = 0;
1513 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1514
1515 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1516 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1517 PartitionManager.Wipe_By_Path("/cache");
1518 if (wipe_dalvik)
1519 PartitionManager.Wipe_Dalvik_Cache();
1520 } else {
1521 ret = 1; // failure
1522 }
thatcc8ddca2015-01-03 01:59:36 +01001523 }
thatc6085482015-01-09 22:12:43 +01001524 if (sideload_child_pid) {
1525 LOGINFO("Signaling child sideload process to exit.\n");
1526 struct stat st;
1527 // Calling stat() on this magic filename signals the minadbd
1528 // subprocess to shut down.
1529 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1530 int status;
1531 LOGINFO("Waiting for child sideload process to exit.\n");
1532 waitpid(sideload_child_pid, &status, 0);
1533 }
Dees Troy28a85d22015-04-28 02:03:16 +00001534 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001535 TWFunc::Toggle_MTP(mtp_was_enabled);
1536 reinject_after_flash();
1537 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001538 }
that3f7b1ac2014-12-30 11:30:13 +01001539 return 0;
1540}
1541
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001542int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001543{
that3f7b1ac2014-12-30 11:30:13 +01001544 struct stat st;
1545 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001546 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001547 LOGINFO("Signaling child sideload process to exit.\n");
1548 // Calling stat() on this magic filename signals the minadbd
1549 // subprocess to shut down.
1550 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1551 if (!sideload_child_pid) {
1552 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001553 return 0;
1554 }
thatcc8ddca2015-01-03 01:59:36 +01001555 ::sleep(1);
1556 LOGINFO("Killing child sideload process.\n");
1557 kill(sideload_child_pid, SIGTERM);
1558 int status;
1559 LOGINFO("Waiting for child sideload process to exit.\n");
1560 waitpid(sideload_child_pid, &status, 0);
1561 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001562 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1563 return 0;
1564}
1565
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001566int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001567{
1568 operation_start("OpenRecoveryScript");
1569 if (simulate) {
1570 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001571 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001572 } else {
that10ae24f2015-12-26 20:53:51 +01001573 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
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
bigbiffce8f83c2015-12-12 18:30:21 -05001732 PartitionSettings part_settings;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001733 operation_start("Flash Image");
bigbiffce8f83c2015-12-12 18:30:21 -05001734 DataManager::GetValue("tw_zip_location", part_settings.Restore_Name);
1735 DataManager::GetValue("tw_file", part_settings.Backup_FileName);
1736 unsigned long long total_bytes = TWFunc::Get_File_Size(part_settings.Restore_Name + "/" + part_settings.Backup_FileName);
1737 ProgressTracking progress(total_bytes);
1738 part_settings.progress = &progress;
1739 part_settings.adbbackup = false;
1740 part_settings.PM_Method = PM_RESTORE;
1741 if (PartitionManager.Flash_Image(&part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001742 op_status = 0; // success
1743 else
1744 op_status = 1; // fail
1745
1746 operation_end(op_status);
1747 return 0;
1748}
1749
that10ae24f2015-12-26 20:53:51 +01001750int GUIAction::twcmd(std::string arg)
1751{
1752 operation_start("TWRP CLI Command");
1753 if (simulate)
1754 simulate_progress_bar();
1755 else
1756 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1757 operation_end(0);
1758 return 0;
1759}
1760
Dees_Troy51a0e822012-09-05 15:24:24 -04001761int GUIAction::getKeyByName(std::string key)
1762{
that8834a0f2016-01-05 23:29:30 +01001763 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001764 else if (key == "menu") return KEY_MENU;
1765 else if (key == "back") return KEY_BACK;
1766 else if (key == "search") return KEY_SEARCH;
1767 else if (key == "voldown") return KEY_VOLUMEDOWN;
1768 else if (key == "volup") return KEY_VOLUMEUP;
1769 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001770 int ret_val;
1771 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1772 if (!ret_val)
1773 return KEY_POWER;
1774 else
1775 return ret_val;
1776 }
1777
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001778 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001779}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001780
1781int GUIAction::checkpartitionlifetimewrites(std::string arg)
1782{
1783 int op_status = 0;
1784 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1785
1786 operation_start("Check Partition Lifetime Writes");
1787 if (sys) {
1788 if (sys->Check_Lifetime_Writes() != 0)
1789 DataManager::SetValue("tw_lifetime_writes", 1);
1790 else
1791 DataManager::SetValue("tw_lifetime_writes", 0);
1792 op_status = 0; // success
1793 } else {
1794 DataManager::SetValue("tw_lifetime_writes", 1);
1795 op_status = 1; // fail
1796 }
1797
1798 operation_end(op_status);
1799 return 0;
1800}
1801
1802int GUIAction::mountsystemtoggle(std::string arg)
1803{
1804 int op_status = 0;
1805 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001806 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001807
1808 operation_start("Toggle System Mount");
1809 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1810 op_status = 1; // fail
1811 } else {
1812 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1813 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001814 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001815 DataManager::SetValue("tw_mount_system_ro", 0);
1816 Part->Change_Mount_Read_Only(false);
1817 } else {
1818 DataManager::SetValue("tw_mount_system_ro", 1);
1819 Part->Change_Mount_Read_Only(true);
1820 }
1821 if (remount_system) {
1822 Part->Mount(true);
1823 }
1824 op_status = 0; // success
1825 } else {
1826 op_status = 1; // fail
1827 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001828 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1829 if (Part) {
1830 if (arg == "0") {
1831 Part->Change_Mount_Read_Only(false);
1832 } else {
1833 Part->Change_Mount_Read_Only(true);
1834 }
1835 if (remount_vendor) {
1836 Part->Mount(true);
1837 }
1838 op_status = 0; // success
1839 } else {
1840 op_status = 1; // fail
1841 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001842 }
1843
1844 operation_end(op_status);
1845 return 0;
1846}
Ethan Yonker74db1572015-10-28 12:44:49 -05001847
1848int GUIAction::setlanguage(std::string arg __unused)
1849{
1850 int op_status = 0;
1851
1852 operation_start("Set Language");
1853 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1854 PageManager::RequestReload();
1855 op_status = 0; // success
1856
1857 operation_end(op_status);
1858 return 0;
1859}