blob: 494665c87c5010906ad02baee280b3deff3df6cd [file] [log] [blame]
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001/*
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>
Matt Mower0c88b842017-01-15 16:00:49 -060035#include <private/android_filesystem_config.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"
bigbiff56b02eb2020-07-06 20:24:34 -040041#include "../twrpRepacker.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040042#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040043
Dees_Troy43d8b002012-09-17 16:00:01 -040044#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010045#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050046#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050047#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010048
Dees_Troy51a0e822012-09-05 15:24:24 -040049extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000050#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060053#include "../adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
Ethan Yonkerf1179622016-08-25 15:32:21 -050055#include "../set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010056#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040057
58#include "rapidxml.hpp"
59#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050060#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040061
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
Noah Jacobson5a79f672019-04-28 00:10:07 -040067extern std::vector<users_struct> Users_List;
Mohd Faraz3c25ab32020-08-12 12:29:42 +000068extern GUITerminal* term;
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);
Matt Mowera8a89d12016-12-30 18:10:37 -0600112 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100113 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);
Matt Mower9a2a2052016-05-31 21:31:22 -0500185 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100186 ADD_ACTION(appenddatetobackupname);
187 ADD_ACTION(generatebackupname);
188 ADD_ACTION(checkpartitionlist);
189 ADD_ACTION(getpartitiondetails);
190 ADD_ACTION(screenshot);
191 ADD_ACTION(setbrightness);
192 ADD_ACTION(fileexists);
193 ADD_ACTION(killterminal);
194 ADD_ACTION(checkbackupname);
195 ADD_ACTION(adbsideloadcancel);
196 ADD_ACTION(fixsu);
197 ADD_ACTION(startmtp);
198 ADD_ACTION(stopmtp);
199 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500200 ADD_ACTION(checkpartitionlifetimewrites);
201 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500202 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600203 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600204 ADD_ACTION(togglebacklight);
Mohd Faraz3c25ab32020-08-12 12:29:42 +0000205 ADD_ACTION(changeterminal);
thatc6085482015-01-09 22:12:43 +0100206
207 // remember actions that run in the caller thread
208 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
209 setActionsRunningInCallerThread.insert(it->first);
210
211 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100212 ADD_ACTION(flash);
213 ADD_ACTION(wipe);
214 ADD_ACTION(refreshsizes);
215 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600216 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100217 ADD_ACTION(fixpermissions);
218 ADD_ACTION(dd);
219 ADD_ACTION(partitionsd);
220 ADD_ACTION(installhtcdumlock);
221 ADD_ACTION(htcdumlockrestoreboot);
222 ADD_ACTION(htcdumlockreflashrecovery);
223 ADD_ACTION(cmd);
224 ADD_ACTION(terminalcommand);
225 ADD_ACTION(reinjecttwrp);
226 ADD_ACTION(decrypt);
227 ADD_ACTION(adbsideload);
228 ADD_ACTION(openrecoveryscript);
229 ADD_ACTION(installsu);
230 ADD_ACTION(decrypt_backup);
231 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500232 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100233 ADD_ACTION(changefilesystem);
234 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100235 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600236 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600237 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500238 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600239 ADD_ACTION(repackimage);
240 ADD_ACTION(fixabrecoverybootloop);
epicX98053c32021-01-04 13:01:31 +0530241 ADD_ACTION(applycustomtwrpfolder);
Captain Throwback3b556102021-02-12 19:32:36 -0500242#ifndef TW_EXCLUDE_NANO
243 ADD_ACTION(editfile);
244#endif
that3f7b1ac2014-12-30 11:30:13 +0100245 }
246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600248 actions = FindNode(node, "actions");
249 if (actions) child = FindNode(actions, "action");
250 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 while (child)
255 {
256 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 attr = child->first_attribute("function");
259 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 action.mFunction = attr->value();
262 action.mArg = child->value();
263 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 child = child->next_sibling("action");
266 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600269 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 if (child)
271 {
272 attr = child->first_attribute("key");
273 if (attr)
274 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100275 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600276 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100277 {
278 const int key = getKeyByName(keys[i]);
279 mKeys[key] = false;
280 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 }
282 else
283 {
284 attr = child->first_attribute("x");
285 if (!attr) return;
286 mActionX = atol(attr->value());
287 attr = child->first_attribute("y");
288 if (!attr) return;
289 mActionY = atol(attr->value());
290 attr = child->first_attribute("w");
291 if (!attr) return;
292 mActionW = atol(attr->value());
293 attr = child->first_attribute("h");
294 if (!attr) return;
295 mActionH = atol(attr->value());
296 }
297 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
Matt Mower25036b72016-06-24 12:30:18 -0500300int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400301{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (state == TOUCH_RELEASE)
303 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400304
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400306}
307
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100308int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400309{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600311 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100312 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313
314 bool prevState = itr->second;
315 itr->second = down;
316
317 // If there is only one key for this action, wait for key up so it
318 // doesn't trigger with multi-key actions.
319 // Else, check if all buttons are pressed, then consume their release events
320 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600321 if (mKeys.size() == 1) {
322 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100323 doActions();
thatd4725ca2016-01-19 00:15:21 +0100324 return 0;
325 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600326 } else if (down) {
327 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
328 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100329 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100330 }
331
332 // Passed, all req buttons are pressed, reset them and consume release events
333 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600334 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 kb->ConsumeKeyRelease(itr->first);
336 itr->second = false;
337 }
338
339 doActions();
thatd4725ca2016-01-19 00:15:21 +0100340 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100341 }
342
thatd4725ca2016-01-19 00:15:21 +0100343 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
Vojtech Bocek07220562014-02-08 02:05:33 +0100346int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400347{
Vojtech Bocek07220562014-02-08 02:05:33 +0100348 GUIObject::NotifyVarChange(varName, value);
349
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100350 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200351 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600352 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200353 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400354
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200355 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356}
357
358void GUIAction::simulate_progress_bar(void)
359{
Ethan Yonker74db1572015-10-28 12:44:49 -0500360 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400361 for (int i = 0; i < 5; i++)
362 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500363 if (PartitionManager.stop_backup.get_value()) {
364 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600365 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500366 DataManager::SetValue("ui_progress", 0);
367 PartitionManager.stop_backup.set_value(0);
368 return;
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 usleep(500000);
371 DataManager::SetValue("ui_progress", i * 20);
372 }
373}
374
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600375int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400376{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200377 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
379 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100380 DataManager::SetValue("ui_portion_size", 0);
381 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 if (filename.empty())
384 {
385 LOGERR("No file specified.\n");
386 return -1;
387 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400388
Ethan Yonker9598c072016-01-15 21:54:34 -0600389 if (!TWFunc::Path_Exists(filename)) {
390 if (!PartitionManager.Mount_By_Path(filename, true)) {
391 return -1;
392 }
393 if (!TWFunc::Path_Exists(filename)) {
394 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
395 return -1;
396 }
397 }
Dees_Troy657c3092012-09-10 20:32:10 -0400398
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 if (simulate) {
400 simulate_progress_bar();
401 } else {
epicX40b7f7a2021-03-20 21:58:17 +0530402 ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR));
Dees_Troy51a0e822012-09-05 15:24:24 -0400403
404 // Now, check if we need to ensure TWRP remains installed...
405 struct stat st;
406 if (stat("/sbin/installTwrp", &st) == 0)
407 {
408 DataManager::SetValue("tw_operation", "Configuring TWRP");
409 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500410 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200411 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500413 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 }
415 }
416 }
417
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 // Done
419 DataManager::SetValue("ui_progress", 100);
420 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100421 DataManager::SetValue("ui_portion_size", 0);
422 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424}
425
that73a52952015-01-28 23:07:34 +0100426GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100427{
that73a52952015-01-28 23:07:34 +0100428 string func = gui_parse_text(action.mFunction);
429 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
430 if (needsThread) {
431 if (func == "cancelbackup")
432 return THREAD_CANCEL;
433 else
434 return THREAD_ACTION;
435 }
436 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100437}
438
Dees_Troy51a0e822012-09-05 15:24:24 -0400439int GUIAction::doActions()
440{
that3f7b1ac2014-12-30 11:30:13 +0100441 if (mActions.size() < 1)
442 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400443
that73a52952015-01-28 23:07:34 +0100444 // Determine in which thread to run the actions.
445 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
446 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100447 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100448 for (it = mActions.begin(); it != mActions.end(); ++it) {
449 ThreadType tt = getThreadType(*it);
450 if (tt == THREAD_NONE)
451 continue;
452 if (threadType == THREAD_NONE)
453 threadType = tt;
454 else if (threadType != tt) {
455 LOGERR("Can't mix normal and cancel actions in the same list.\n"
456 "Running the whole batch in the cancel thread.\n");
457 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100458 break;
459 }
that7d3b54f2015-01-09 22:52:51 +0100460 }
that73a52952015-01-28 23:07:34 +0100461
462 // Now run the actions in the desired thread.
463 switch (threadType) {
464 case THREAD_ACTION:
465 action_thread.threadActions(this);
466 break;
467
468 case THREAD_CANCEL:
469 cancel_thread.threadActions(this);
470 break;
471
472 default: {
473 // no iterators here because theme reloading might kill our object
474 const size_t cnt = mActions.size();
475 for (size_t i = 0; i < cnt; ++i)
476 doAction(mActions[i]);
477 }
thatc6085482015-01-09 22:12:43 +0100478 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400479
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481}
482
that3f7b1ac2014-12-30 11:30:13 +0100483int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400484{
that3f7b1ac2014-12-30 11:30:13 +0100485 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486
that3f7b1ac2014-12-30 11:30:13 +0100487 std::string function = gui_parse_text(action.mFunction);
488 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489
that3f7b1ac2014-12-30 11:30:13 +0100490 // find function and execute it
491 mapFunc::const_iterator funcitr = mf.find(function);
492 if (funcitr != mf.end())
493 return (this->*funcitr->second)(arg);
494
495 LOGERR("Unknown action '%s'\n", function.c_str());
496 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400497}
498
499void GUIAction::operation_start(const string operation_name)
500{
that3f7b1ac2014-12-30 11:30:13 +0100501 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000502 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 DataManager::SetValue(TW_ACTION_BUSY, 1);
504 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100505 DataManager::SetValue("ui_portion_size", 0);
506 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400507 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600509 DataManager::SetValue("tw_operation_status", 0);
bigbiff349ea552020-06-19 16:07:38 -0400510 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500511 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400512}
513
that3f7b1ac2014-12-30 11:30:13 +0100514void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400515{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000516 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 DataManager::SetValue("ui_progress", 100);
519 if (simulate) {
520 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
521 if (simulate_fail != 0)
522 DataManager::SetValue("tw_operation_status", 1);
523 else
524 DataManager::SetValue("tw_operation_status", 0);
525 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500526 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400527 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500528 }
529 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500531 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 }
533 DataManager::SetValue("tw_operation_state", 1);
534 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500535 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200536 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000537 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400538
539#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000540 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600541 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400542#endif
543
that3f7b1ac2014-12-30 11:30:13 +0100544 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400545}
546
that3f7b1ac2014-12-30 11:30:13 +0100547int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400548{
that3f7b1ac2014-12-30 11:30:13 +0100549 sync();
550 DataManager::SetValue("tw_gui_done", 1);
551 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400552
that3f7b1ac2014-12-30 11:30:13 +0100553 return 0;
554}
Dees_Troy51a0e822012-09-05 15:24:24 -0400555
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500556int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100557{
that3f7b1ac2014-12-30 11:30:13 +0100558 gui_changePage("main");
559 return 0;
560}
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
that3f7b1ac2014-12-30 11:30:13 +0100562int GUIAction::key(std::string arg)
563{
564 const int key = getKeyByName(arg);
565 PageManager::NotifyKey(key, true);
566 PageManager::NotifyKey(key, false);
567 return 0;
568}
569
570int GUIAction::page(std::string arg)
571{
LuK133762326f42015-09-30 19:57:46 +0200572 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100573 std::string page_name = gui_parse_text(arg);
574 return gui_changePage(page_name);
575}
576
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500577int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100578{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500579 PageManager::RequestReload();
580 // The actual reload is handled in pages.cpp in PageManager::RunReload()
581 // The reload will occur on the next Update or Render call and will
582 // be performed in the rendoer thread instead of the action thread
583 // to prevent crashing which could occur when we start deleting
584 // GUI resources in the action thread while we attempt to render
585 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100586 return 0;
587}
Dees_Troy51a0e822012-09-05 15:24:24 -0400588
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500589int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100590{
591 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400592
that3f7b1ac2014-12-30 11:30:13 +0100593 DataManager::GetValue("tw_restore", Restore_Name);
594 PartitionManager.Set_Restore_Files(Restore_Name);
595 return 0;
596}
597
598int GUIAction::set(std::string arg)
599{
600 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 {
that3f7b1ac2014-12-30 11:30:13 +0100602 string varName = arg.substr(0, arg.find('='));
603 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400604
that3f7b1ac2014-12-30 11:30:13 +0100605 DataManager::GetValue(value, value);
606 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607 }
that3f7b1ac2014-12-30 11:30:13 +0100608 else
609 DataManager::SetValue(arg, "1");
610 return 0;
611}
Dees_Troy51a0e822012-09-05 15:24:24 -0400612
that3f7b1ac2014-12-30 11:30:13 +0100613int GUIAction::clear(std::string arg)
614{
615 DataManager::SetValue(arg, "0");
616 return 0;
617}
Dees_Troy51a0e822012-09-05 15:24:24 -0400618
that3f7b1ac2014-12-30 11:30:13 +0100619int GUIAction::mount(std::string arg)
620{
621 if (arg == "usb") {
622 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100624 PartitionManager.usb_storage_enable();
625 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500626 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100627 } else if (!simulate) {
628 PartitionManager.Mount_By_Path(arg, true);
629 PartitionManager.Add_MTP_Storage(arg);
630 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500631 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100632 return 0;
633}
634
635int GUIAction::unmount(std::string arg)
636{
637 if (arg == "usb") {
638 if (!simulate)
639 PartitionManager.usb_storage_disable();
640 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500641 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100642 DataManager::SetValue(TW_ACTION_BUSY, 0);
643 } else if (!simulate) {
644 PartitionManager.UnMount_By_Path(arg, true);
645 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100647 return 0;
648}
649
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500650int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100651{
652 operation_start("Restore Defaults");
653 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500654 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100655 else {
656 DataManager::ResetDefaults();
657 PartitionManager.Update_System_Details();
658 PartitionManager.Mount_Current_Storage(true);
659 }
660 operation_end(0);
661 return 0;
662}
663
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500664int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100665{
666 operation_start("Copy Log");
667 if (!simulate)
668 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400669 string dst, curr_storage;
670 int copy_kernel_log = 0;
671
672 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100673 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400674 curr_storage = DataManager::GetCurrentStoragePath();
675 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100676 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
677 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400678 if (copy_kernel_log)
679 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100680 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400681 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100682 } else
683 simulate_progress_bar();
684 operation_end(0);
685 return 0;
686}
687
688
689int GUIAction::compute(std::string arg)
690{
691 if (arg.find("+") != string::npos)
692 {
693 string varName = arg.substr(0, arg.find('+'));
694 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
695 int amount_to_add = atoi(string_to_add.c_str());
696 int value;
697
698 DataManager::GetValue(varName, value);
699 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 return 0;
701 }
that3f7b1ac2014-12-30 11:30:13 +0100702 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 {
that3f7b1ac2014-12-30 11:30:13 +0100704 string varName = arg.substr(0, arg.find('-'));
705 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
706 int amount_to_subtract = atoi(string_to_subtract.c_str());
707 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400708
that3f7b1ac2014-12-30 11:30:13 +0100709 DataManager::GetValue(varName, value);
710 value -= amount_to_subtract;
711 if (value <= 0)
712 value = 0;
713 DataManager::SetValue(varName, value);
714 return 0;
715 }
716 if (arg.find("*") != string::npos)
717 {
718 string varName = arg.substr(0, arg.find('*'));
719 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
720 int multiply_by = atoi(multiply_by_str.c_str());
721 int value;
722
723 DataManager::GetValue(varName, value);
724 DataManager::SetValue(varName, value*multiply_by);
725 return 0;
726 }
727 if (arg.find("/") != string::npos)
728 {
729 string varName = arg.substr(0, arg.find('/'));
730 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
731 int divide_by = atoi(divide_by_str.c_str());
732 int value;
733
Matt Mowera8a89d12016-12-30 18:10:37 -0600734 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100735 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100737 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 }
739 return 0;
740 }
that3f7b1ac2014-12-30 11:30:13 +0100741 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
742 return -1;
743}
Dees_Troy51a0e822012-09-05 15:24:24 -0400744
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500745int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100746{
747 string SelectedZone;
748 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
749 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
750 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
751
752 int dst;
753 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
754
755 string offset;
756 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
757
758 string NewTimeZone = Zone;
759 if (offset != "0")
760 NewTimeZone += ":" + offset;
761
762 if (dst != 0)
763 NewTimeZone += DSTZone;
764
765 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
766 DataManager::update_tz_environment_variables();
767 return 0;
768}
769
770int GUIAction::overlay(std::string arg)
771{
772 return gui_changeOverlay(arg);
773}
774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500775int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100776{
777 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500778 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400779 return 0;
780 }
that3f7b1ac2014-12-30 11:30:13 +0100781 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
782 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
783 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100785 }
786 return 0;
787}
788
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500789int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100790{
791 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500792 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400793 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100794 } else {
795 zip_queue_index--;
796 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400797 }
that3f7b1ac2014-12-30 11:30:13 +0100798 return 0;
799}
Dees_Troy51a0e822012-09-05 15:24:24 -0400800
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500801int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100802{
803 zip_queue_index = 0;
804 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
805 return 0;
806}
Dees_Troy51a0e822012-09-05 15:24:24 -0400807
that3f7b1ac2014-12-30 11:30:13 +0100808int GUIAction::sleep(std::string arg)
809{
810 operation_start("Sleep");
811 usleep(atoi(arg.c_str()));
812 operation_end(0);
813 return 0;
814}
Dees Troyb21cc642013-09-10 17:36:41 +0000815
Matt Mower9a2a2052016-05-31 21:31:22 -0500816int GUIAction::sleepcounter(std::string arg)
817{
818 operation_start("SleepCounter");
819 // Ensure user notices countdown in case it needs to be cancelled
820 blankTimer.resetTimerAndUnblank();
821 int total = atoi(arg.c_str());
822 for (int t = total; t > 0; t--) {
823 int progress = (int)(((float)(total-t)/(float)total)*100.0);
824 DataManager::SetValue("ui_progress", progress);
825 ::sleep(1);
826 DataManager::SetValue("tw_sleep", t-1);
827 }
828 DataManager::SetValue("ui_progress", 100);
829 operation_end(0);
830 return 0;
831}
832
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500833int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100834{
835 operation_start("AppendDateToBackupName");
836 string Backup_Name;
837 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
838 Backup_Name += TWFunc::Get_Current_Date();
839 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
840 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
841 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500842 PageManager::NotifyKey(KEY_END, true);
843 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100844 operation_end(0);
845 return 0;
846}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500847
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500848int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100849{
850 operation_start("GenerateBackupName");
851 TWFunc::Auto_Generate_Backup_Name();
852 operation_end(0);
853 return 0;
854}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500855
Ethan Yonker483e9f42016-01-11 22:21:18 -0600856int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100857{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600858 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100859 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500860
Ethan Yonker483e9f42016-01-11 22:21:18 -0600861 if (arg.empty())
862 arg = "tw_wipe_list";
863 DataManager::GetValue(arg, List);
864 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
865 if (!List.empty()) {
866 size_t start_pos = 0, end_pos = List.find(";", start_pos);
867 while (end_pos != string::npos && start_pos < List.size()) {
868 part_path = List.substr(start_pos, end_pos - start_pos);
869 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
870 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100871 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400872 } else {
that3f7b1ac2014-12-30 11:30:13 +0100873 count++;
874 }
875 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600876 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100877 }
878 DataManager::SetValue("tw_check_partition_list", count);
879 } else {
880 DataManager::SetValue("tw_check_partition_list", 0);
881 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600882 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100883}
Dees_Troy51a0e822012-09-05 15:24:24 -0400884
Ethan Yonker483e9f42016-01-11 22:21:18 -0600885int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100886{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600887 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400888
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 if (arg.empty())
890 arg = "tw_wipe_list";
891 DataManager::GetValue(arg, List);
892 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
893 if (!List.empty()) {
894 size_t start_pos = 0, end_pos = List.find(";", start_pos);
895 part_path = List;
896 while (end_pos != string::npos && start_pos < List.size()) {
897 part_path = List.substr(start_pos, end_pos - start_pos);
898 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
899 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100900 // Do nothing
901 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600902 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100903 break;
904 }
905 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600906 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100907 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600908 if (!part_path.empty()) {
909 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100910 if (Part) {
911 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500912
that3f7b1ac2014-12-30 11:30:13 +0100913 DataManager::SetValue("tw_partition_name", Part->Display_Name);
914 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
915 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
916 DataManager::SetValue("tw_partition_size", Part->Size / mb);
917 DataManager::SetValue("tw_partition_used", Part->Used / mb);
918 DataManager::SetValue("tw_partition_free", Part->Free / mb);
919 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
920 DataManager::SetValue("tw_partition_removable", Part->Removable);
921 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
922
923 if (Part->Can_Repair())
924 DataManager::SetValue("tw_partition_can_repair", 1);
925 else
926 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500927 if (Part->Can_Resize())
928 DataManager::SetValue("tw_partition_can_resize", 1);
929 else
930 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600931 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100932 DataManager::SetValue("tw_partition_vfat", 1);
933 else
934 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600935 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100936 DataManager::SetValue("tw_partition_exfat", 1);
937 else
938 DataManager::SetValue("tw_partition_exfat", 0);
939 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
940 DataManager::SetValue("tw_partition_f2fs", 1);
941 else
942 DataManager::SetValue("tw_partition_f2fs", 0);
943 if (TWFunc::Path_Exists("/sbin/mke2fs"))
944 DataManager::SetValue("tw_partition_ext", 1);
945 else
946 DataManager::SetValue("tw_partition_ext", 0);
947 return 0;
948 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600949 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100950 }
951 }
952 }
953 DataManager::SetValue("tw_partition_name", "");
954 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600955 // Set this to 0 to prevent trying to partition this device, just in case
956 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100957 return 0;
958}
959
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500960int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100961{
962 time_t tm;
963 char path[256];
964 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600965 uid_t uid = AID_MEDIA_RW;
966 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100967
968 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600969 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100970 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
971 } else {
972 strcpy(path, "/tmp/");
973 }
974
Matt Mowera8a89d12016-12-30 18:10:37 -0600975 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100976 return 0;
977
978 tm = time(NULL);
979 path_len = strlen(path);
980
981 // Screenshot_2014-01-01-18-21-38.png
982 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
983
984 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600985 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100986 chmod(path, 0666);
987 chown(path, uid, gid);
988
that677b13f2015-12-26 23:57:31 +0100989 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100990
VDavid0032034a412019-10-18 22:43:20 +0200991 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100992 gr_color(255, 255, 255, 255);
993 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
994 gr_flip();
995 gui_forceRender();
996 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500997 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100998 }
999 return 0;
1000}
1001
1002int GUIAction::setbrightness(std::string arg)
1003{
1004 return TWFunc::Set_Brightness(arg);
1005}
1006
1007int GUIAction::fileexists(std::string arg)
1008{
1009 struct stat st;
1010 string newpath = arg + "/.";
1011
1012 operation_start("FileExists");
1013 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1014 operation_end(0);
1015 else
1016 operation_end(1);
1017 return 0;
1018}
1019
thatcc8ddca2015-01-03 01:59:36 +01001020void GUIAction::reinject_after_flash()
1021{
1022 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001023 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001024 if (simulate) {
1025 simulate_progress_bar();
1026 } else {
1027 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1028 if (Boot == NULL || Boot->Current_File_System != "emmc")
1029 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1030 else {
1031 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1032 TWFunc::Exec_Cmd(injectcmd);
1033 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001034 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001035 }
1036 }
1037}
1038
mauronofrio0ff59842019-10-26 19:47:55 +02001039int GUIAction::ozip_decrypt(string zip_path)
1040{
1041 if (!TWFunc::Path_Exists("/sbin/ozip_decrypt")) {
1042 return 1;
1043 }
1044 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1045 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1046 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1047 return 0;
1048}
1049
that3f7b1ac2014-12-30 11:30:13 +01001050int GUIAction::flash(std::string arg)
1051{
1052 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001053 // We're going to jump to this page first, like a loading page
1054 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001055 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001056 string zip_path = zip_queue[i];
1057 size_t slashpos = zip_path.find_last_of('/');
1058 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001059 operation_start("Flashing");
mauronofrio0ff59842019-10-26 19:47:55 +02001060 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1061 {
1062 if((ozip_decrypt(zip_path)) != 0)
1063 {
1064 LOGERR("Unable to find ozip_decrypt!");
1065 break;
1066 }
1067 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1068 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1069 if (!TWFunc::Path_Exists(zip_path)) {
1070 LOGERR("Unable to find decrypted zip");
1071 break;
1072 }
1073 }
thatc7b631b2015-06-01 23:36:57 +02001074 DataManager::SetValue("tw_filename", zip_path);
1075 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001076 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1077
1078 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001079 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001080 TWFunc::SetPerformanceMode(false);
1081 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001082 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001083 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001084 break;
that3f7b1ac2014-12-30 11:30:13 +01001085 }
1086 }
1087 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001088
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001089 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001090 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001091 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001092 }
that3f7b1ac2014-12-30 11:30:13 +01001093
thatcc8ddca2015-01-03 01:59:36 +01001094 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001095 PartitionManager.Update_System_Details();
1096 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001097 // 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 -06001098 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001099 return 0;
1100}
1101
1102int GUIAction::wipe(std::string arg)
1103{
1104 operation_start("Format");
1105 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001106 int ret_val = false;
1107
1108 if (simulate) {
1109 simulate_progress_bar();
1110 } else {
1111 if (arg == "data")
1112 ret_val = PartitionManager.Factory_Reset();
1113 else if (arg == "battery")
1114 ret_val = PartitionManager.Wipe_Battery_Stats();
1115 else if (arg == "rotate")
1116 ret_val = PartitionManager.Wipe_Rotate_Data();
1117 else if (arg == "dalvik")
1118 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1119 else if (arg == "DATAMEDIA") {
1120 ret_val = PartitionManager.Format_Data();
1121 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001122 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001123
1124 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1125 if (has_datamedia) {
1126 ret_val = PartitionManager.Wipe_Media_From_Data();
1127 } else {
1128 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1129 }
1130 } else if (arg == "EXTERNAL") {
1131 string External_Path;
1132
1133 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1134 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1135 } else if (arg == "ANDROIDSECURE") {
1136 ret_val = PartitionManager.Wipe_Android_Secure();
1137 } else if (arg == "LIST") {
1138 string Wipe_List, wipe_path;
1139 bool skip = false;
1140 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001141
1142 DataManager::GetValue("tw_wipe_list", Wipe_List);
1143 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1144 if (!Wipe_List.empty()) {
1145 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1146 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1147 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1148 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1149 if (wipe_path == "/and-sec") {
1150 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001151 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001152 ret_val = false;
1153 break;
1154 } else {
1155 skip = true;
1156 }
1157 } else if (wipe_path == "DALVIK") {
1158 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001159 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001160 ret_val = false;
1161 break;
1162 } else {
1163 skip = true;
1164 }
1165 } else if (wipe_path == "INTERNAL") {
1166 if (!PartitionManager.Wipe_Media_From_Data()) {
1167 ret_val = false;
1168 break;
1169 } else {
1170 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001171 }
1172 }
that3f7b1ac2014-12-30 11:30:13 +01001173 if (!skip) {
1174 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001175 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001176 ret_val = false;
1177 break;
1178 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1179 arg = wipe_path;
1180 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 } else {
that3f7b1ac2014-12-30 11:30:13 +01001182 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001183 }
that3f7b1ac2014-12-30 11:30:13 +01001184 start_pos = end_pos + 1;
1185 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001186 }
1187 }
that3f7b1ac2014-12-30 11:30:13 +01001188 } else
1189 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001190#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001191 if (arg == DataManager::GetSettingsStoragePath()) {
1192 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1193 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001194
that3f7b1ac2014-12-30 11:30:13 +01001195 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1196 LOGINFO("Making TWRP folder and saving settings.\n");
1197 Storage_Path += "/TWRP";
1198 mkdir(Storage_Path.c_str(), 0777);
1199 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001200 } else {
that3f7b1ac2014-12-30 11:30:13 +01001201 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1202 }
1203 }
1204#endif
1205 }
1206 PartitionManager.Update_System_Details();
1207 if (ret_val)
1208 ret_val = 0; // 0 is success
1209 else
1210 ret_val = 1; // 1 is failure
1211 operation_end(ret_val);
1212 return 0;
1213}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001214
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001215int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001216{
1217 operation_start("Refreshing Sizes");
1218 if (simulate) {
1219 simulate_progress_bar();
1220 } else
1221 PartitionManager.Update_System_Details();
1222 operation_end(0);
1223 return 0;
1224}
1225
1226int GUIAction::nandroid(std::string arg)
1227{
that3f7b1ac2014-12-30 11:30:13 +01001228 if (simulate) {
that73a52952015-01-28 23:07:34 +01001229 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001230 DataManager::SetValue("tw_partition", "Simulation");
1231 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001232 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001233 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001234 operation_start("Nandroid");
1235 int ret = 0;
1236
that3f7b1ac2014-12-30 11:30:13 +01001237 if (arg == "backup") {
1238 string Backup_Name;
1239 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001240 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001241 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(Backup_Name, true, true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001242 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001243 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1244 if (!PartitionManager.stop_backup.get_value()) {
1245 if (ret == false)
1246 ret = 1; // 1 for failure
1247 else
1248 ret = 0; // 0 for success
1249 DataManager::SetValue("tw_cancel_backup", 0);
1250 } else {
1251 DataManager::SetValue("tw_cancel_backup", 1);
1252 gui_msg("backup_cancel=Backup Cancelled");
1253 ret = 0;
1254 }
bigbiffce8f83c2015-12-12 18:30:21 -05001255 } else {
that3f7b1ac2014-12-30 11:30:13 +01001256 operation_end(1);
1257 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001258 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001259 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001260 } else if (arg == "restore") {
1261 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001262 int gui_adb_backup;
1263
that3f7b1ac2014-12-30 11:30:13 +01001264 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001265 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1266 if (gui_adb_backup) {
1267 DataManager::SetValue("tw_operation_state", 1);
1268 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1269 ret = 0; // success
1270 else
1271 ret = 1; // failure
1272 DataManager::SetValue("tw_enable_adb_backup", 0);
1273 ret = 0; // assume success???
1274 } else {
1275 if (PartitionManager.Run_Restore(Restore_Name))
1276 ret = 0; // success
1277 else
1278 ret = 1; // failure
1279 }
that3f7b1ac2014-12-30 11:30:13 +01001280 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001281 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001282 return -1;
1283 }
that73a52952015-01-28 23:07:34 +01001284 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001285 return ret;
1286 }
1287 return 0;
1288}
1289
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001290int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001291 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001292 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001293 }
1294 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001295 int op_status = PartitionManager.Cancel_Backup();
1296 if (op_status != 0)
1297 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001298 }
1299
1300 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001301}
Dees_Troy51a0e822012-09-05 15:24:24 -04001302
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001303int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001304{
that73a52952015-01-28 23:07:34 +01001305 int op_status = 0;
1306
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001307 operation_start("Fix Contexts");
1308 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001309 if (simulate) {
1310 simulate_progress_bar();
1311 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001312 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001313 if (op_status != 0)
1314 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001315 }
that73a52952015-01-28 23:07:34 +01001316 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001317 return 0;
1318}
1319
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001320int GUIAction::fixpermissions(std::string arg)
1321{
1322 return fixcontexts(arg);
1323}
1324
that3f7b1ac2014-12-30 11:30:13 +01001325int GUIAction::dd(std::string arg)
1326{
1327 operation_start("imaging");
1328
1329 if (simulate) {
1330 simulate_progress_bar();
1331 } else {
1332 string cmd = "dd " + arg;
1333 TWFunc::Exec_Cmd(cmd);
1334 }
1335 operation_end(0);
1336 return 0;
1337}
1338
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001339int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001340{
1341 operation_start("Partition SD Card");
1342 int ret_val = 0;
1343
1344 if (simulate) {
1345 simulate_progress_bar();
1346 } else {
1347 int allow_partition;
1348 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1349 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001350 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001351 } else {
1352 if (!PartitionManager.Partition_SDCard())
1353 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001354 }
that3f7b1ac2014-12-30 11:30:13 +01001355 }
1356 operation_end(ret_val);
1357 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001358
that3f7b1ac2014-12-30 11:30:13 +01001359}
Dees_Troy51a0e822012-09-05 15:24:24 -04001360
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001361int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001362{
1363 operation_start("Install HTC Dumlock");
1364 if (simulate) {
1365 simulate_progress_bar();
1366 } else
1367 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001368
that3f7b1ac2014-12-30 11:30:13 +01001369 operation_end(0);
1370 return 0;
1371}
Dees_Troy51a0e822012-09-05 15:24:24 -04001372
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001373int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001374{
1375 operation_start("HTC Dumlock Restore Boot");
1376 if (simulate) {
1377 simulate_progress_bar();
1378 } else
1379 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001380
that3f7b1ac2014-12-30 11:30:13 +01001381 operation_end(0);
1382 return 0;
1383}
Dees_Troy51a0e822012-09-05 15:24:24 -04001384
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001385int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001386{
1387 operation_start("HTC Dumlock Reflash Recovery");
1388 if (simulate) {
1389 simulate_progress_bar();
1390 } else
1391 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001392
that3f7b1ac2014-12-30 11:30:13 +01001393 operation_end(0);
1394 return 0;
1395}
Dees_Troy51a0e822012-09-05 15:24:24 -04001396
that3f7b1ac2014-12-30 11:30:13 +01001397int GUIAction::cmd(std::string arg)
1398{
1399 int op_status = 0;
1400
1401 operation_start("Command");
1402 LOGINFO("Running command: '%s'\n", arg.c_str());
1403 if (simulate) {
1404 simulate_progress_bar();
1405 } else {
1406 op_status = TWFunc::Exec_Cmd(arg);
1407 if (op_status != 0)
1408 op_status = 1;
1409 }
1410
1411 operation_end(op_status);
1412 return 0;
1413}
1414
1415int GUIAction::terminalcommand(std::string arg)
1416{
1417 int op_status = 0;
1418 string cmdpath, command;
1419
1420 DataManager::GetValue("tw_terminal_location", cmdpath);
1421 operation_start("CommandOutput");
1422 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1423 if (simulate) {
1424 simulate_progress_bar();
1425 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001426 } else if (arg == "exit") {
1427 LOGINFO("Exiting terminal\n");
1428 operation_end(op_status);
1429 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001430 } else {
1431 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1432 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001433 DataManager::SetValue("tw_terminal_state", 1);
1434 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001435 FILE* fp;
1436 char line[512];
1437
1438 fp = popen(command.c_str(), "r");
1439 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001440 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001441 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001442 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001443 struct timeval timeout;
1444 fd_set fdset;
1445
Matt Mowera8a89d12016-12-30 18:10:37 -06001446 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001447 {
1448 FD_ZERO(&fdset);
1449 FD_SET(fd, &fdset);
1450 timeout.tv_sec = 0;
1451 timeout.tv_usec = 400000;
1452 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1453 if (has_data == 0) {
1454 // Timeout reached
1455 DataManager::GetValue("tw_terminal_state", check);
1456 if (check == 0) {
1457 keep_going = 0;
1458 }
1459 } else if (has_data < 0) {
1460 // End of execution
1461 keep_going = 0;
1462 } else {
1463 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001464 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001465 gui_print("%s", line); // Display output
1466 else
1467 keep_going = 0; // Done executing
1468 }
1469 }
1470 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001471 }
thatc6085482015-01-09 22:12:43 +01001472 DataManager::SetValue("tw_operation_status", 0);
1473 DataManager::SetValue("tw_operation_state", 1);
1474 DataManager::SetValue("tw_terminal_state", 0);
1475 DataManager::SetValue("tw_background_thread_running", 0);
1476 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001477 }
1478 return 0;
1479}
1480
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001481int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001482{
that3f7b1ac2014-12-30 11:30:13 +01001483 LOGINFO("Sending kill command...\n");
1484 operation_start("KillCommand");
1485 DataManager::SetValue("tw_operation_status", 0);
1486 DataManager::SetValue("tw_operation_state", 1);
1487 DataManager::SetValue("tw_terminal_state", 0);
1488 DataManager::SetValue("tw_background_thread_running", 0);
1489 DataManager::SetValue(TW_ACTION_BUSY, 0);
1490 return 0;
1491}
1492
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001493int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001494{
1495 int op_status = 0;
1496 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001497 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001498 if (simulate) {
1499 simulate_progress_bar();
1500 } else {
1501 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001502 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001503 }
1504
1505 operation_end(op_status);
1506 return 0;
1507}
1508
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001509int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001510{
1511 int op_status = 0;
1512
1513 operation_start("CheckBackupName");
1514 if (simulate) {
1515 simulate_progress_bar();
1516 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001517 string Backup_Name;
1518 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1519 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001520 if (op_status != 0)
1521 op_status = 1;
1522 }
1523
1524 operation_end(op_status);
1525 return 0;
1526}
1527
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001528int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001529{
1530 int op_status = 0;
1531
1532 operation_start("Decrypt");
1533 if (simulate) {
1534 simulate_progress_bar();
1535 } else {
1536 string Password;
Noah Jacobson5a79f672019-04-28 00:10:07 -04001537 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001538 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson5a79f672019-04-28 00:10:07 -04001539
1540 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1541 DataManager::GetValue("tw_crypto_user_id", userID);
1542 if (userID != "") {
1543 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1544 if (userID != "0") {
1545 if (op_status != 0)
1546 op_status = 1;
1547 operation_end(op_status);
1548 return 0;
1549 }
1550 } else {
1551 LOGINFO("User ID not found\n");
1552 op_status = 1;
1553 }
1554 ::sleep(1);
1555 } else { // for FDE
1556 op_status = PartitionManager.Decrypt_Device(Password);
1557 }
1558
that3f7b1ac2014-12-30 11:30:13 +01001559 if (op_status != 0)
1560 op_status = 1;
1561 else {
that3f7b1ac2014-12-30 11:30:13 +01001562 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1563
Ethan Yonkercf50da52015-01-12 21:59:07 -06001564 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001565
Ethan Yonkercf50da52015-01-12 21:59:07 -06001566 // Check for a custom theme and load it if exists
1567 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1568 if (has_datamedia != 0) {
1569 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001570 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001571 } else {
1572 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001573 }
1574 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001575 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001576 }
1577 }
1578
1579 operation_end(op_status);
1580 return 0;
1581}
1582
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001583int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001584{
1585 operation_start("Sideload");
1586 if (simulate) {
1587 simulate_progress_bar();
1588 operation_end(0);
1589 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001590 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001591 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1592
1593 // wait for the adb connection
1594 int ret = apply_from_adb("/", &sideload_child_pid);
1595 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1596
1597 if (ret != 0) {
1598 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001599 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001600 ret = 1; // failure
1601 } else {
1602 int wipe_cache = 0;
1603 int wipe_dalvik = 0;
1604 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1605
1606 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1607 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1608 PartitionManager.Wipe_By_Path("/cache");
1609 if (wipe_dalvik)
1610 PartitionManager.Wipe_Dalvik_Cache();
1611 } else {
1612 ret = 1; // failure
1613 }
thatcc8ddca2015-01-03 01:59:36 +01001614 }
thatc6085482015-01-09 22:12:43 +01001615 if (sideload_child_pid) {
1616 LOGINFO("Signaling child sideload process to exit.\n");
1617 struct stat st;
1618 // Calling stat() on this magic filename signals the minadbd
1619 // subprocess to shut down.
1620 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1621 int status;
1622 LOGINFO("Waiting for child sideload process to exit.\n");
1623 waitpid(sideload_child_pid, &status, 0);
1624 }
Dees Troy28a85d22015-04-28 02:03:16 +00001625 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001626 TWFunc::Toggle_MTP(mtp_was_enabled);
1627 reinject_after_flash();
1628 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001629 }
that3f7b1ac2014-12-30 11:30:13 +01001630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
that3f7b1ac2014-12-30 11:30:13 +01001635 struct stat st;
1636 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001637 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001638 LOGINFO("Signaling child sideload process to exit.\n");
1639 // Calling stat() on this magic filename signals the minadbd
1640 // subprocess to shut down.
1641 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1642 if (!sideload_child_pid) {
1643 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001644 return 0;
1645 }
thatcc8ddca2015-01-03 01:59:36 +01001646 ::sleep(1);
1647 LOGINFO("Killing child sideload process.\n");
1648 kill(sideload_child_pid, SIGTERM);
1649 int status;
1650 LOGINFO("Waiting for child sideload process to exit.\n");
1651 waitpid(sideload_child_pid, &status, 0);
1652 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001653 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1654 return 0;
1655}
1656
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001657int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001658{
1659 operation_start("OpenRecoveryScript");
1660 if (simulate) {
1661 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001662 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001663 } else {
that10ae24f2015-12-26 20:53:51 +01001664 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001665 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001666 }
1667 return 0;
1668}
1669
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001670int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001671{
1672 int op_status = 0;
1673
1674 operation_start("Install SuperSU");
1675 if (simulate) {
1676 simulate_progress_bar();
1677 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001678 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001679 }
1680
1681 operation_end(op_status);
1682 return 0;
1683}
1684
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001685int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001686{
1687 int op_status = 0;
1688
1689 operation_start("Fixing Superuser Permissions");
1690 if (simulate) {
1691 simulate_progress_bar();
1692 } else {
1693 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1694 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1695 }
1696
1697 operation_end(op_status);
1698 return 0;
1699}
1700
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001701int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001702{
1703 int op_status = 0;
1704
1705 operation_start("Try Restore Decrypt");
1706 if (simulate) {
1707 simulate_progress_bar();
1708 } else {
1709 string Restore_Path, Filename, Password;
1710 DataManager::GetValue("tw_restore", Restore_Path);
1711 Restore_Path += "/";
1712 DataManager::GetValue("tw_restore_password", Password);
1713 TWFunc::SetPerformanceMode(true);
1714 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1715 op_status = 0; // success
1716 else
1717 op_status = 1; // fail
1718 TWFunc::SetPerformanceMode(false);
1719 }
1720
1721 operation_end(op_status);
1722 return 0;
1723}
1724
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001725int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001726{
1727 int op_status = 0;
1728
1729 operation_start("Repair Partition");
1730 if (simulate) {
1731 simulate_progress_bar();
1732 } else {
1733 string part_path;
1734 DataManager::GetValue("tw_partition_mount_point", part_path);
1735 if (PartitionManager.Repair_By_Path(part_path, true)) {
1736 op_status = 0; // success
1737 } else {
that3f7b1ac2014-12-30 11:30:13 +01001738 op_status = 1; // fail
1739 }
1740 }
1741
1742 operation_end(op_status);
1743 return 0;
1744}
1745
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001746int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001747{
1748 int op_status = 0;
1749
1750 operation_start("Resize Partition");
1751 if (simulate) {
1752 simulate_progress_bar();
1753 } else {
1754 string part_path;
1755 DataManager::GetValue("tw_partition_mount_point", part_path);
1756 if (PartitionManager.Resize_By_Path(part_path, true)) {
1757 op_status = 0; // success
1758 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001759 op_status = 1; // fail
1760 }
1761 }
1762
1763 operation_end(op_status);
1764 return 0;
1765}
1766
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001767int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001768{
1769 int op_status = 0;
1770
1771 operation_start("Change File System");
1772 if (simulate) {
1773 simulate_progress_bar();
1774 } else {
1775 string part_path, file_system;
1776 DataManager::GetValue("tw_partition_mount_point", part_path);
1777 DataManager::GetValue("tw_action_new_file_system", file_system);
1778 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1779 op_status = 0; // success
1780 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001781 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001782 op_status = 1; // fail
1783 }
1784 }
1785 PartitionManager.Update_System_Details();
1786 operation_end(op_status);
1787 return 0;
1788}
1789
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001790int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001791{
1792 int op_status = 0;
1793
1794 operation_start("Start MTP");
1795 if (PartitionManager.Enable_MTP())
1796 op_status = 0; // success
1797 else
1798 op_status = 1; // fail
1799
1800 operation_end(op_status);
1801 return 0;
1802}
1803
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001804int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001805{
1806 int op_status = 0;
1807
1808 operation_start("Stop MTP");
1809 if (PartitionManager.Disable_MTP())
1810 op_status = 0; // success
1811 else
1812 op_status = 1; // fail
1813
1814 operation_end(op_status);
1815 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001816}
1817
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001818int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001819{
1820 int op_status = 0;
epicXb7337372021-02-24 23:12:08 +05301821 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001822
1823 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001824 string path, filename;
1825 DataManager::GetValue("tw_zip_location", path);
1826 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001827
epicXb7337372021-02-24 23:12:08 +05301828#ifdef AB_OTA_UPDATER
1829 string target = DataManager::GetStrValue("tw_flash_partition");
1830 unsigned int pos = target.find_last_of(';');
1831 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1832 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1833 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1834
1835 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1836 {
1837 string current_slot = PartitionManager.Get_Active_Slot_Display();
1838 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1839
1840 PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A");
1841 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
1842 PartitionManager.Set_Active_Slot(current_slot);
1843
1844 DataManager::SetValue("tw_flash_both_slots", 0);
1845 flag = false;
1846 }
1847#endif
1848 if (flag)
1849 {
1850 if (PartitionManager.Flash_Image(path, filename))
1851 op_status = 0; // success
1852 else
1853 op_status = 1; // fail
1854 }
1855
Ethan Yonker96af84a2015-01-05 14:58:36 -06001856 operation_end(op_status);
1857 return 0;
1858}
1859
that10ae24f2015-12-26 20:53:51 +01001860int GUIAction::twcmd(std::string arg)
1861{
1862 operation_start("TWRP CLI Command");
1863 if (simulate)
1864 simulate_progress_bar();
1865 else
1866 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1867 operation_end(0);
1868 return 0;
1869}
1870
Dees_Troy51a0e822012-09-05 15:24:24 -04001871int GUIAction::getKeyByName(std::string key)
1872{
that8834a0f2016-01-05 23:29:30 +01001873 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001874 else if (key == "menu") return KEY_MENU;
1875 else if (key == "back") return KEY_BACK;
1876 else if (key == "search") return KEY_SEARCH;
1877 else if (key == "voldown") return KEY_VOLUMEDOWN;
1878 else if (key == "volup") return KEY_VOLUMEUP;
1879 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001880 int ret_val;
1881 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1882 if (!ret_val)
1883 return KEY_POWER;
1884 else
1885 return ret_val;
1886 }
1887
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001888 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001889}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001890
1891int GUIAction::checkpartitionlifetimewrites(std::string arg)
1892{
1893 int op_status = 0;
1894 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1895
1896 operation_start("Check Partition Lifetime Writes");
1897 if (sys) {
1898 if (sys->Check_Lifetime_Writes() != 0)
1899 DataManager::SetValue("tw_lifetime_writes", 1);
1900 else
1901 DataManager::SetValue("tw_lifetime_writes", 0);
1902 op_status = 0; // success
1903 } else {
1904 DataManager::SetValue("tw_lifetime_writes", 1);
1905 op_status = 1; // fail
1906 }
1907
1908 operation_end(op_status);
1909 return 0;
1910}
1911
1912int GUIAction::mountsystemtoggle(std::string arg)
1913{
1914 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001915 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001916 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001917
1918 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001919 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001920 op_status = 1; // fail
1921 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001922 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001923 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001924 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001925 DataManager::SetValue("tw_mount_system_ro", 0);
1926 Part->Change_Mount_Read_Only(false);
1927 } else {
1928 DataManager::SetValue("tw_mount_system_ro", 1);
1929 Part->Change_Mount_Read_Only(true);
1930 }
1931 if (remount_system) {
1932 Part->Mount(true);
1933 }
1934 op_status = 0; // success
1935 } else {
1936 op_status = 1; // fail
1937 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001938 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1939 if (Part) {
1940 if (arg == "0") {
1941 Part->Change_Mount_Read_Only(false);
1942 } else {
1943 Part->Change_Mount_Read_Only(true);
1944 }
1945 if (remount_vendor) {
1946 Part->Mount(true);
1947 }
1948 op_status = 0; // success
1949 } else {
1950 op_status = 1; // fail
1951 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001952 }
1953
1954 operation_end(op_status);
1955 return 0;
1956}
Ethan Yonker74db1572015-10-28 12:44:49 -05001957
1958int GUIAction::setlanguage(std::string arg __unused)
1959{
1960 int op_status = 0;
1961
1962 operation_start("Set Language");
1963 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1964 PageManager::RequestReload();
1965 op_status = 0; // success
1966
1967 operation_end(op_status);
1968 return 0;
1969}
Ethan Yonker1b190162016-12-05 15:25:19 -06001970
Matt Mower9472ba12016-01-20 18:12:47 -06001971int GUIAction::togglebacklight(std::string arg __unused)
1972{
1973 blankTimer.toggleBlank();
1974 return 0;
1975}
1976
Ethan Yonker1b190162016-12-05 15:25:19 -06001977int GUIAction::setbootslot(std::string arg)
1978{
1979 operation_start("Set Boot Slot");
1980 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001981 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001982 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001983 simulate_progress_bar();
1984 operation_end(0);
1985 return 0;
1986}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001987
1988int GUIAction::checkforapp(std::string arg __unused)
1989{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001990 operation_start("Check for TWRP App");
1991 if (!simulate)
1992 {
epicX9d930a22020-12-29 00:39:36 +05301993 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001994 } else
1995 simulate_progress_bar();
epicX9d930a22020-12-29 00:39:36 +05301996
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001997 operation_end(0);
1998 return 0;
1999}
2000
2001int GUIAction::installapp(std::string arg __unused)
2002{
2003 int op_status = 1;
2004 operation_start("Install TWRP App");
2005 if (!simulate)
2006 {
2007 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
2008 if (PartitionManager.Mount_By_Path("/data", true)) {
2009 string install_path = "/data/app";
2010 string context = "u:object_r:apk_data_file:s0";
2011 if (!TWFunc::Path_Exists(install_path)) {
2012 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2013 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2014 goto exit;
2015 }
2016 if (chown(install_path.c_str(), 1000, 1000)) {
2017 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2018 goto exit;
2019 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002020 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002021 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2022 goto exit;
2023 }
2024 }
2025 install_path += "/me.twrp.twrpapp-1";
2026 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2027 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2028 goto exit;
2029 }
2030 if (chown(install_path.c_str(), 1000, 1000)) {
2031 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2032 goto exit;
2033 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002034 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002035 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2036 goto exit;
2037 }
2038 install_path += "/base.apk";
2039 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2040 LOGERR("Error copying apk file\n");
2041 goto exit;
2042 }
2043 if (chown(install_path.c_str(), 1000, 1000)) {
2044 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2045 goto exit;
2046 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002047 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002048 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2049 goto exit;
2050 }
2051 sync();
2052 sync();
2053 }
2054 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002055 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2056 string base_path = PartitionManager.Get_Android_Root_Path();
2057 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002058 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2059 string install_path = base_path + "/priv-app";
2060 string context = "u:object_r:system_file:s0";
2061 if (!TWFunc::Path_Exists(install_path))
2062 install_path = base_path + "/app";
2063 if (TWFunc::Path_Exists(install_path)) {
2064 install_path += "/twrpapp";
2065 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2066 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002067 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002068 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2069 goto exit;
2070 }
2071 install_path += "/me.twrp.twrpapp.apk";
2072 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2073 LOGERR("Error copying apk file\n");
2074 goto exit;
2075 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002076 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002077 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2078 goto exit;
2079 }
Stefan Tauner86a43702020-01-11 22:28:53 +01002080
2081 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2082 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
2083 if (TWFunc::copy_file("/sbin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
2084 LOGERR("Error copying permission file\n");
2085 goto exit;
2086 }
2087 if (chown(permission_path.c_str(), 1000, 1000)) {
2088 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2089 goto exit;
2090 }
2091 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2092 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2093 goto exit;
2094 }
2095
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002096 sync();
2097 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002098 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002099 op_status = 0;
2100 } else {
2101 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2102 }
2103 }
2104 }
2105 }
2106 } else
2107 simulate_progress_bar();
2108exit:
epicX9d930a22020-12-29 00:39:36 +05302109 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002110 operation_end(0);
2111 return 0;
2112}
Ethan Yonker53796e72019-01-11 22:49:52 -06002113
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002114int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2115{
2116 int op_status = 1;
2117 operation_start("Uninstall TWRP System App");
2118 if (!simulate)
2119 {
2120 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2121 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2122 if (!Part) {
2123 LOGERR("Unabled to find system partition.\n");
2124 goto exit;
2125 }
2126 if (!Part->UnMount(true)) {
2127 goto exit;
2128 }
2129 if (Mount_System_RO > 0) {
2130 DataManager::SetValue("tw_mount_system_ro", 0);
2131 Part->Change_Mount_Read_Only(false);
2132 }
2133 if (Part->Mount(true)) {
2134 string base_path = PartitionManager.Get_Android_Root_Path();
2135 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2136 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2137 string uninstall_path = base_path + "/priv-app";
2138 if (!TWFunc::Path_Exists(uninstall_path))
2139 uninstall_path = base_path + "/app";
2140 uninstall_path += "/twrpapp";
2141 if (TWFunc::Path_Exists(uninstall_path)) {
2142 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2143 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2144 sync();
2145 op_status = 0;
2146 DataManager::SetValue("tw_app_installed_in_system", 0);
2147 DataManager::SetValue("tw_app_install_status", 0);
2148 } else {
2149 LOGERR("Unable to remove TWRP app from system.\n");
2150 }
2151 } else {
2152 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2153 }
2154 }
2155 Part->UnMount(true);
2156 if (Mount_System_RO > 0) {
2157 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2158 Part->Change_Mount_Read_Only(true);
2159 }
2160 } else
2161 simulate_progress_bar();
2162exit:
epicX9d930a22020-12-29 00:39:36 +05302163 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002164 operation_end(0);
2165 return 0;
2166}
2167
Ethan Yonker53796e72019-01-11 22:49:52 -06002168int GUIAction::repackimage(std::string arg __unused)
2169{
2170 int op_status = 1;
bigbiff56b02eb2020-07-06 20:24:34 -04002171 twrpRepacker repacker;
2172
Ethan Yonker53796e72019-01-11 22:49:52 -06002173 operation_start("Repack Image");
2174 if (!simulate)
2175 {
2176 std::string path = DataManager::GetStrValue("tw_filename");
2177 Repack_Options_struct Repack_Options;
2178 Repack_Options.Disable_Verity = false;
2179 Repack_Options.Disable_Force_Encrypt = false;
2180 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2181 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2182 Repack_Options.Type = REPLACE_KERNEL;
2183 else
2184 Repack_Options.Type = REPLACE_RAMDISK;
bigbiff56b02eb2020-07-06 20:24:34 -04002185 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002186 goto exit;
2187 } else
2188 simulate_progress_bar();
2189 op_status = 0;
2190exit:
2191 operation_end(op_status);
2192 return 0;
2193}
2194
2195int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2196{
2197 int op_status = 1;
bigbiff56b02eb2020-07-06 20:24:34 -04002198 twrpRepacker repacker;
2199
Ethan Yonker53796e72019-01-11 22:49:52 -06002200 operation_start("Repack Image");
2201 if (!simulate)
2202 {
2203 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2204 LOGERR("Image repacking tool not present in this TWRP build!");
2205 goto exit;
2206 }
2207 DataManager::SetProgress(0);
2208 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2209 if (part)
2210 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2211 else {
2212 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2213 goto exit;
2214 }
bigbiff56b02eb2020-07-06 20:24:34 -04002215 if (!repacker.Backup_Image_For_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
Ethan Yonker53796e72019-01-11 22:49:52 -06002216 goto exit;
2217 DataManager::SetProgress(.25);
2218 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
Mohd Faraz5738e762020-05-10 04:18:30 +05302219 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002220 if (TWFunc::Exec_Cmd(command) != 0) {
2221 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2222 goto exit;
2223 }
2224 std::string header_path = REPACK_ORIG_DIR;
2225 header_path += "header";
2226 if (TWFunc::Path_Exists(header_path)) {
2227 command = "cd " REPACK_ORIG_DIR " && sed -i \"s|$(grep '^cmdline=' header | cut -d= -f2-)|$(grep '^cmdline=' header | cut -d= -f2- | sed -e 's/skip_override//' -e 's/ */ /g' -e 's/[ \t]*$//')|\" header";
2228 if (TWFunc::Exec_Cmd(command) != 0) {
2229 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2230 goto exit;
2231 }
2232 }
2233 DataManager::SetProgress(.5);
2234 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
Mohd Faraz5738e762020-05-10 04:18:30 +05302235 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002236 if (TWFunc::Exec_Cmd(command) != 0) {
2237 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2238 goto exit;
2239 }
2240 DataManager::SetProgress(.75);
2241 std::string path = REPACK_ORIG_DIR;
2242 std::string file = "new-boot.img";
2243 DataManager::SetValue("tw_flash_partition", "/boot;");
2244 if (!PartitionManager.Flash_Image(path, file)) {
2245 LOGINFO("Error flashing new image\n");
2246 goto exit;
2247 }
2248 DataManager::SetProgress(1);
2249 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2250 } else
2251 simulate_progress_bar();
2252 op_status = 0;
2253exit:
2254 operation_end(op_status);
2255 return 0;
2256}
Mohd Faraz3c25ab32020-08-12 12:29:42 +00002257
2258int GUIAction::changeterminal(std::string arg) {
2259 bool res = true;
2260 std::string resp, cmd = "cd " + arg;
2261 DataManager::GetValue("tw_terminal_location", resp);
2262 if (arg.empty() && !resp.empty()) {
2263 cmd = "cd /";
2264 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2265 term->NotifyCharInput(cmd.at(iter));
2266 term->NotifyCharInput(13);
2267 DataManager::SetValue("tw_terminal_location", "");
2268 return 0;
2269 }
2270 if (term != NULL && !arg.empty()) {
2271 DataManager::SetValue("tw_terminal_location", arg);
2272 if (term->status()) {
2273 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2274 term->NotifyCharInput(cmd.at(iter));
2275 term->NotifyCharInput(13);
2276 }
2277 else if (chdir(arg.c_str()) != 0) {
2278 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2279 res = false;
2280 }
2281 }
2282 else {
2283 res = false;
2284 LOGINFO("Unable to switch to Terminal\n");
2285 }
2286 if (res)
2287 gui_changePage("terminalcommand");
2288 return 0;
2289}
Captain Throwback3b556102021-02-12 19:32:36 -05002290#ifndef TW_EXCLUDE_NANO
2291int GUIAction::editfile(std::string arg) {
2292 if (term != NULL) {
2293 for (uint8_t iter = 0; iter < arg.size(); iter++)
2294 term->NotifyCharInput(arg.at(iter));
2295 term->NotifyCharInput(13);
2296 }
2297 else
2298 LOGINFO("Unable to switch to Terminal\n");
2299 return 0;
2300}
2301#endif
epicX98053c32021-01-04 13:01:31 +05302302
2303int GUIAction::applycustomtwrpfolder(string arg __unused)
2304{
2305 operation_start("ChangingTWRPFolder");
2306 string storageFolder = DataManager::GetSettingsStoragePath();
2307 string newFolder = storageFolder + '/' + arg;
2308 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2309 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2310 bool ret = false;
2311
2312 if (TWFunc::Path_Exists(newFolder)) {
2313 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2314 } else {
2315 ret = true;
2316 }
2317
2318 if (newFolder != prevFolder && ret) {
2319 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2320 } else {
2321 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2322 }
2323
epicXfb610382021-03-01 00:02:57 +05302324 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicX98053c32021-01-04 13:01:31 +05302325
2326
2327 if (ret) {
2328 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2329 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2330 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2331 }
2332 operation_end((int)!ret);
2333 return 0;
2334}