blob: a623ccd5db32aa142797487708115473327f0a09 [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>
bigbiffdf8436b2020-08-30 16:22:34 -040036#include <android-base/properties.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040037
38#include <string>
39#include <sstream>
40#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041#include "../twrp-functions.hpp"
bigbiffad58e1b2020-07-06 20:24:34 -040042#include "../twrpRepacker.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040043#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
bigbiff1f9e4842020-10-31 11:33:15 -040045#include "twinstall/adb_install.h"
bigbiffd58ba182020-03-23 10:02:29 -040046
47#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050048#include "blanktimer.hpp"
bigbiff1f9e4842020-10-31 11:33:15 -040049#include "twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010050
Dees_Troy51a0e822012-09-05 15:24:24 -040051extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000054#include "cutils/properties.h"
bigbiff673c7ae2020-12-02 19:44:56 -050055#include "twinstall/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056};
bigbiffd58ba182020-03-23 10:02:29 -040057#include "set_metadata.h"
bigbiffd81833a2021-01-17 11:06:57 -050058#include "minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60#include "rapidxml.hpp"
61#include "objects.hpp"
bigbiffd58ba182020-03-23 10:02:29 -040062#include "tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040063
that3f7b1ac2014-12-30 11:30:13 +010064GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010065std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010066static string zip_queue[10];
67static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010068pid_t sideload_child_pid;
Noah Jacobson81d638d2019-04-28 00:10:07 -040069extern std::vector<users_struct> Users_List;
Mohd Faraz77bbeb02020-08-12 12:29:42 +000070extern GUITerminal* term;
that3f7b1ac2014-12-30 11:30:13 +010071
that73a52952015-01-28 23:07:34 +010072static void *ActionThread_work_wrapper(void *data);
73
74class ActionThread
75{
76public:
77 ActionThread();
78 ~ActionThread();
79
80 void threadActions(GUIAction *act);
81 void run(void *data);
82private:
83 friend void *ActionThread_work_wrapper(void*);
84 struct ThreadData
85 {
86 ActionThread *this_;
87 GUIAction *act;
88 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
89 };
90
91 pthread_t m_thread;
92 bool m_thread_running;
93 pthread_mutex_t m_act_lock;
94};
95
96static ActionThread action_thread; // for all kinds of longer running actions
97static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010098
99static void *ActionThread_work_wrapper(void *data)
100{
that73a52952015-01-28 23:07:34 +0100101 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +0100102 return NULL;
103}
104
105ActionThread::ActionThread()
106{
107 m_thread_running = false;
108 pthread_mutex_init(&m_act_lock, NULL);
109}
110
111ActionThread::~ActionThread()
112{
113 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600114 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100115 pthread_mutex_unlock(&m_act_lock);
116 pthread_join(m_thread, NULL);
117 } else {
118 pthread_mutex_unlock(&m_act_lock);
119 }
120 pthread_mutex_destroy(&m_act_lock);
121}
122
that7d3b54f2015-01-09 22:52:51 +0100123void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100124{
125 pthread_mutex_lock(&m_act_lock);
126 if (m_thread_running) {
127 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100128 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
129 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100130 } else {
131 m_thread_running = true;
132 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100133 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100134 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
135 }
136}
137
138void ActionThread::run(void *data)
139{
140 ThreadData *d = (ThreadData*)data;
141 GUIAction* act = d->act;
142
143 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100144 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100145 act->doAction(*it);
146
147 pthread_mutex_lock(&m_act_lock);
148 m_thread_running = false;
149 pthread_mutex_unlock(&m_act_lock);
150 delete d;
151}
152
Dees_Troy51a0e822012-09-05 15:24:24 -0400153GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100154 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400155{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 xml_node<>* child;
157 xml_node<>* actions;
158 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
that3f7b1ac2014-12-30 11:30:13 +0100162 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163#define ADD_ACTION(n) mf[#n] = &GUIAction::n
164#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100165 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100166 ADD_ACTION(reboot);
167 ADD_ACTION(home);
168 ADD_ACTION(key);
169 ADD_ACTION(page);
170 ADD_ACTION(reload);
171 ADD_ACTION(readBackup);
172 ADD_ACTION(set);
173 ADD_ACTION(clear);
174 ADD_ACTION(mount);
175 ADD_ACTION(unmount);
176 ADD_ACTION_EX("umount", unmount);
177 ADD_ACTION(restoredefaultsettings);
178 ADD_ACTION(copylog);
179 ADD_ACTION(compute);
180 ADD_ACTION_EX("addsubtract", compute);
181 ADD_ACTION(setguitimezone);
182 ADD_ACTION(overlay);
183 ADD_ACTION(queuezip);
184 ADD_ACTION(cancelzip);
185 ADD_ACTION(queueclear);
186 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500187 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100188 ADD_ACTION(appenddatetobackupname);
189 ADD_ACTION(generatebackupname);
190 ADD_ACTION(checkpartitionlist);
191 ADD_ACTION(getpartitiondetails);
192 ADD_ACTION(screenshot);
193 ADD_ACTION(setbrightness);
194 ADD_ACTION(fileexists);
195 ADD_ACTION(killterminal);
196 ADD_ACTION(checkbackupname);
197 ADD_ACTION(adbsideloadcancel);
198 ADD_ACTION(fixsu);
199 ADD_ACTION(startmtp);
200 ADD_ACTION(stopmtp);
201 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500202 ADD_ACTION(checkpartitionlifetimewrites);
203 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500204 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600205 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600206 ADD_ACTION(togglebacklight);
bigbiffdf8436b2020-08-30 16:22:34 -0400207 ADD_ACTION(enableadb);
208 ADD_ACTION(enablefastboot);
Mohd Faraz77bbeb02020-08-12 12:29:42 +0000209 ADD_ACTION(changeterminal);
bigbiffcfa875c2021-06-20 16:20:27 -0400210 ADD_ACTION(unmapsuperdevices);
thatc6085482015-01-09 22:12:43 +0100211
212 // remember actions that run in the caller thread
213 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
214 setActionsRunningInCallerThread.insert(it->first);
215
216 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100217 ADD_ACTION(flash);
218 ADD_ACTION(wipe);
219 ADD_ACTION(refreshsizes);
220 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600221 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100222 ADD_ACTION(fixpermissions);
223 ADD_ACTION(dd);
224 ADD_ACTION(partitionsd);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100225 ADD_ACTION(cmd);
226 ADD_ACTION(terminalcommand);
227 ADD_ACTION(reinjecttwrp);
228 ADD_ACTION(decrypt);
229 ADD_ACTION(adbsideload);
230 ADD_ACTION(openrecoveryscript);
231 ADD_ACTION(installsu);
232 ADD_ACTION(decrypt_backup);
233 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500234 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100235 ADD_ACTION(changefilesystem);
236 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100237 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600238 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600239 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500240 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600241 ADD_ACTION(repackimage);
242 ADD_ACTION(fixabrecoverybootloop);
epicXa721f952021-01-04 13:01:31 +0530243 ADD_ACTION(applycustomtwrpfolder);
Captain Throwback16dd81b2021-02-12 19:32:36 -0500244#ifndef TW_EXCLUDE_NANO
245 ADD_ACTION(editfile);
246#endif
that3f7b1ac2014-12-30 11:30:13 +0100247 }
248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600250 actions = FindNode(node, "actions");
251 if (actions) child = FindNode(actions, "action");
252 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 while (child)
257 {
258 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 attr = child->first_attribute("function");
261 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 action.mFunction = attr->value();
264 action.mArg = child->value();
265 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 child = child->next_sibling("action");
268 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600271 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 if (child)
273 {
274 attr = child->first_attribute("key");
275 if (attr)
276 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100277 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600278 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100279 {
280 const int key = getKeyByName(keys[i]);
281 mKeys[key] = false;
282 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200283 }
284 else
285 {
286 attr = child->first_attribute("x");
287 if (!attr) return;
288 mActionX = atol(attr->value());
289 attr = child->first_attribute("y");
290 if (!attr) return;
291 mActionY = atol(attr->value());
292 attr = child->first_attribute("w");
293 if (!attr) return;
294 mActionW = atol(attr->value());
295 attr = child->first_attribute("h");
296 if (!attr) return;
297 mActionH = atol(attr->value());
298 }
299 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400300}
301
Matt Mower25036b72016-06-24 12:30:18 -0500302int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400303{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200304 if (state == TOUCH_RELEASE)
305 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400306
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200307 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400308}
309
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400311{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600313 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100314 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100315
316 bool prevState = itr->second;
317 itr->second = down;
318
319 // If there is only one key for this action, wait for key up so it
320 // doesn't trigger with multi-key actions.
321 // Else, check if all buttons are pressed, then consume their release events
322 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600323 if (mKeys.size() == 1) {
324 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100325 doActions();
thatd4725ca2016-01-19 00:15:21 +0100326 return 0;
327 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600328 } else if (down) {
329 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
330 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100331 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100332 }
333
334 // Passed, all req buttons are pressed, reset them and consume release events
335 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600336 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100337 kb->ConsumeKeyRelease(itr->first);
338 itr->second = false;
339 }
340
341 doActions();
thatd4725ca2016-01-19 00:15:21 +0100342 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100343 }
344
thatd4725ca2016-01-19 00:15:21 +0100345 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400346}
347
Vojtech Bocek07220562014-02-08 02:05:33 +0100348int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400349{
Vojtech Bocek07220562014-02-08 02:05:33 +0100350 GUIObject::NotifyVarChange(varName, value);
351
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100352 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200353 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600354 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200355 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400356
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358}
359
360void GUIAction::simulate_progress_bar(void)
361{
Ethan Yonker74db1572015-10-28 12:44:49 -0500362 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 for (int i = 0; i < 5; i++)
364 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500365 if (PartitionManager.stop_backup.get_value()) {
366 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600367 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500368 DataManager::SetValue("ui_progress", 0);
369 PartitionManager.stop_backup.set_value(0);
370 return;
371 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400372 usleep(500000);
373 DataManager::SetValue("ui_progress", i * 20);
374 }
375}
376
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600377int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400378{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400380
381 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100382 DataManager::SetValue("ui_portion_size", 0);
383 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400384
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200385 if (filename.empty())
386 {
387 LOGERR("No file specified.\n");
388 return -1;
389 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400390
Ethan Yonker9598c072016-01-15 21:54:34 -0600391 if (!TWFunc::Path_Exists(filename)) {
392 if (!PartitionManager.Mount_By_Path(filename, true)) {
393 return -1;
394 }
395 if (!TWFunc::Path_Exists(filename)) {
396 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
397 return -1;
398 }
399 }
Dees_Troy657c3092012-09-10 20:32:10 -0400400
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 if (simulate) {
402 simulate_progress_bar();
403 } else {
bigbiffab036612021-06-24 21:31:40 -0400404 char apex_enabled[PROPERTY_VALUE_MAX];
405 property_get("twrp.apex.flattened", apex_enabled, "");
406 if (strcmp(apex_enabled, "true") == 0) {
407 umount("/apex");
408 }
epicX9597b842021-03-20 21:58:17 +0530409 ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR));
bigbiff1f9e4842020-10-31 11:33:15 -0400410 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 // Now, check if we need to ensure TWRP remains installed...
412 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400413 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 {
415 DataManager::SetValue("tw_operation", "Configuring TWRP");
416 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500417 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400418 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500420 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 }
422 }
423 }
424
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200425 // Done
426 DataManager::SetValue("ui_progress", 100);
427 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100428 DataManager::SetValue("ui_portion_size", 0);
429 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200430 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431}
432
that73a52952015-01-28 23:07:34 +0100433GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100434{
that73a52952015-01-28 23:07:34 +0100435 string func = gui_parse_text(action.mFunction);
436 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
437 if (needsThread) {
438 if (func == "cancelbackup")
439 return THREAD_CANCEL;
440 else
441 return THREAD_ACTION;
442 }
443 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100444}
445
Dees_Troy51a0e822012-09-05 15:24:24 -0400446int GUIAction::doActions()
447{
that3f7b1ac2014-12-30 11:30:13 +0100448 if (mActions.size() < 1)
449 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400450
that73a52952015-01-28 23:07:34 +0100451 // Determine in which thread to run the actions.
452 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
453 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100454 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100455 for (it = mActions.begin(); it != mActions.end(); ++it) {
456 ThreadType tt = getThreadType(*it);
457 if (tt == THREAD_NONE)
458 continue;
459 if (threadType == THREAD_NONE)
460 threadType = tt;
461 else if (threadType != tt) {
462 LOGERR("Can't mix normal and cancel actions in the same list.\n"
463 "Running the whole batch in the cancel thread.\n");
464 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100465 break;
466 }
that7d3b54f2015-01-09 22:52:51 +0100467 }
that73a52952015-01-28 23:07:34 +0100468
469 // Now run the actions in the desired thread.
470 switch (threadType) {
471 case THREAD_ACTION:
472 action_thread.threadActions(this);
473 break;
474
475 case THREAD_CANCEL:
476 cancel_thread.threadActions(this);
477 break;
478
479 default: {
480 // no iterators here because theme reloading might kill our object
481 const size_t cnt = mActions.size();
482 for (size_t i = 0; i < cnt; ++i)
483 doAction(mActions[i]);
484 }
thatc6085482015-01-09 22:12:43 +0100485 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400486
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488}
489
that3f7b1ac2014-12-30 11:30:13 +0100490int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400491{
that3f7b1ac2014-12-30 11:30:13 +0100492 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400493
that3f7b1ac2014-12-30 11:30:13 +0100494 std::string function = gui_parse_text(action.mFunction);
495 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400496
that3f7b1ac2014-12-30 11:30:13 +0100497 // find function and execute it
498 mapFunc::const_iterator funcitr = mf.find(function);
499 if (funcitr != mf.end())
500 return (this->*funcitr->second)(arg);
501
502 LOGERR("Unknown action '%s'\n", function.c_str());
503 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400504}
505
506void GUIAction::operation_start(const string operation_name)
507{
that3f7b1ac2014-12-30 11:30:13 +0100508 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000509 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 DataManager::SetValue(TW_ACTION_BUSY, 1);
511 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100512 DataManager::SetValue("ui_portion_size", 0);
513 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600516 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400517 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500518 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400519}
520
that3f7b1ac2014-12-30 11:30:13 +0100521void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400522{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000523 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 DataManager::SetValue("ui_progress", 100);
526 if (simulate) {
527 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
528 if (simulate_fail != 0)
529 DataManager::SetValue("tw_operation_status", 1);
530 else
531 DataManager::SetValue("tw_operation_status", 0);
532 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500533 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400534 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500535 }
536 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500538 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 }
540 DataManager::SetValue("tw_operation_state", 1);
541 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500542 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200543 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000544 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400545
546#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000547 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600548 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400549#endif
550
that3f7b1ac2014-12-30 11:30:13 +0100551 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400552}
553
that3f7b1ac2014-12-30 11:30:13 +0100554int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400555{
that3f7b1ac2014-12-30 11:30:13 +0100556 sync();
557 DataManager::SetValue("tw_gui_done", 1);
558 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400559
that3f7b1ac2014-12-30 11:30:13 +0100560 return 0;
561}
Dees_Troy51a0e822012-09-05 15:24:24 -0400562
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500563int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100564{
that3f7b1ac2014-12-30 11:30:13 +0100565 gui_changePage("main");
566 return 0;
567}
Dees_Troy51a0e822012-09-05 15:24:24 -0400568
that3f7b1ac2014-12-30 11:30:13 +0100569int GUIAction::key(std::string arg)
570{
571 const int key = getKeyByName(arg);
572 PageManager::NotifyKey(key, true);
573 PageManager::NotifyKey(key, false);
574 return 0;
575}
576
577int GUIAction::page(std::string arg)
578{
LuK133762326f42015-09-30 19:57:46 +0200579 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100580 std::string page_name = gui_parse_text(arg);
581 return gui_changePage(page_name);
582}
583
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500584int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100585{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500586 PageManager::RequestReload();
587 // The actual reload is handled in pages.cpp in PageManager::RunReload()
588 // The reload will occur on the next Update or Render call and will
589 // be performed in the rendoer thread instead of the action thread
590 // to prevent crashing which could occur when we start deleting
591 // GUI resources in the action thread while we attempt to render
592 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100593 return 0;
594}
Dees_Troy51a0e822012-09-05 15:24:24 -0400595
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500596int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100597{
598 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400599
that3f7b1ac2014-12-30 11:30:13 +0100600 DataManager::GetValue("tw_restore", Restore_Name);
601 PartitionManager.Set_Restore_Files(Restore_Name);
602 return 0;
603}
604
605int GUIAction::set(std::string arg)
606{
607 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200608 {
that3f7b1ac2014-12-30 11:30:13 +0100609 string varName = arg.substr(0, arg.find('='));
610 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400611
that3f7b1ac2014-12-30 11:30:13 +0100612 DataManager::GetValue(value, value);
613 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 }
that3f7b1ac2014-12-30 11:30:13 +0100615 else
616 DataManager::SetValue(arg, "1");
617 return 0;
618}
Dees_Troy51a0e822012-09-05 15:24:24 -0400619
that3f7b1ac2014-12-30 11:30:13 +0100620int GUIAction::clear(std::string arg)
621{
622 DataManager::SetValue(arg, "0");
623 return 0;
624}
Dees_Troy51a0e822012-09-05 15:24:24 -0400625
that3f7b1ac2014-12-30 11:30:13 +0100626int GUIAction::mount(std::string arg)
627{
628 if (arg == "usb") {
629 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100631 PartitionManager.usb_storage_enable();
632 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500633 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100634 } else if (!simulate) {
635 PartitionManager.Mount_By_Path(arg, true);
636 PartitionManager.Add_MTP_Storage(arg);
637 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500638 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100639 return 0;
640}
641
642int GUIAction::unmount(std::string arg)
643{
644 if (arg == "usb") {
645 if (!simulate)
646 PartitionManager.usb_storage_disable();
647 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500648 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100649 DataManager::SetValue(TW_ACTION_BUSY, 0);
650 } else if (!simulate) {
651 PartitionManager.UnMount_By_Path(arg, true);
652 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500653 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100654 return 0;
655}
656
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500657int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100658{
659 operation_start("Restore Defaults");
660 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500661 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100662 else {
663 DataManager::ResetDefaults();
664 PartitionManager.Update_System_Details();
665 PartitionManager.Mount_Current_Storage(true);
666 }
667 operation_end(0);
668 return 0;
669}
670
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500671int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100672{
673 operation_start("Copy Log");
674 if (!simulate)
675 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400676 string dst, curr_storage;
677 int copy_kernel_log = 0;
678
679 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100680 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400681 curr_storage = DataManager::GetCurrentStoragePath();
682 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100683 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
684 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400685 if (copy_kernel_log)
686 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100687 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400688 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100689 } else
690 simulate_progress_bar();
691 operation_end(0);
692 return 0;
693}
694
695
696int GUIAction::compute(std::string arg)
697{
698 if (arg.find("+") != string::npos)
699 {
700 string varName = arg.substr(0, arg.find('+'));
701 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
702 int amount_to_add = atoi(string_to_add.c_str());
703 int value;
704
705 DataManager::GetValue(varName, value);
706 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 return 0;
708 }
that3f7b1ac2014-12-30 11:30:13 +0100709 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 {
that3f7b1ac2014-12-30 11:30:13 +0100711 string varName = arg.substr(0, arg.find('-'));
712 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
713 int amount_to_subtract = atoi(string_to_subtract.c_str());
714 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400715
that3f7b1ac2014-12-30 11:30:13 +0100716 DataManager::GetValue(varName, value);
717 value -= amount_to_subtract;
718 if (value <= 0)
719 value = 0;
720 DataManager::SetValue(varName, value);
721 return 0;
722 }
723 if (arg.find("*") != string::npos)
724 {
725 string varName = arg.substr(0, arg.find('*'));
726 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
727 int multiply_by = atoi(multiply_by_str.c_str());
728 int value;
729
730 DataManager::GetValue(varName, value);
731 DataManager::SetValue(varName, value*multiply_by);
732 return 0;
733 }
734 if (arg.find("/") != string::npos)
735 {
736 string varName = arg.substr(0, arg.find('/'));
737 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
738 int divide_by = atoi(divide_by_str.c_str());
739 int value;
740
Matt Mowera8a89d12016-12-30 18:10:37 -0600741 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100742 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100744 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 }
746 return 0;
747 }
that3f7b1ac2014-12-30 11:30:13 +0100748 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
749 return -1;
750}
Dees_Troy51a0e822012-09-05 15:24:24 -0400751
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500752int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100753{
754 string SelectedZone;
755 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
756 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
757 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
758
759 int dst;
760 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
761
762 string offset;
763 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
764
765 string NewTimeZone = Zone;
766 if (offset != "0")
767 NewTimeZone += ":" + offset;
768
769 if (dst != 0)
770 NewTimeZone += DSTZone;
771
772 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
773 DataManager::update_tz_environment_variables();
774 return 0;
775}
776
777int GUIAction::overlay(std::string arg)
778{
779 return gui_changeOverlay(arg);
780}
781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500782int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100783{
784 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500785 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 return 0;
787 }
that3f7b1ac2014-12-30 11:30:13 +0100788 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
789 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
790 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100792 }
793 return 0;
794}
795
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500796int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100797{
798 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500799 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400800 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100801 } else {
802 zip_queue_index--;
803 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400804 }
that3f7b1ac2014-12-30 11:30:13 +0100805 return 0;
806}
Dees_Troy51a0e822012-09-05 15:24:24 -0400807
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500808int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100809{
810 zip_queue_index = 0;
811 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
812 return 0;
813}
Dees_Troy51a0e822012-09-05 15:24:24 -0400814
that3f7b1ac2014-12-30 11:30:13 +0100815int GUIAction::sleep(std::string arg)
816{
817 operation_start("Sleep");
818 usleep(atoi(arg.c_str()));
819 operation_end(0);
820 return 0;
821}
Dees Troyb21cc642013-09-10 17:36:41 +0000822
Matt Mower9a2a2052016-05-31 21:31:22 -0500823int GUIAction::sleepcounter(std::string arg)
824{
825 operation_start("SleepCounter");
826 // Ensure user notices countdown in case it needs to be cancelled
827 blankTimer.resetTimerAndUnblank();
828 int total = atoi(arg.c_str());
829 for (int t = total; t > 0; t--) {
830 int progress = (int)(((float)(total-t)/(float)total)*100.0);
831 DataManager::SetValue("ui_progress", progress);
832 ::sleep(1);
833 DataManager::SetValue("tw_sleep", t-1);
834 }
835 DataManager::SetValue("ui_progress", 100);
836 operation_end(0);
837 return 0;
838}
839
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500840int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100841{
842 operation_start("AppendDateToBackupName");
843 string Backup_Name;
844 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
845 Backup_Name += TWFunc::Get_Current_Date();
846 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
847 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
848 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500849 PageManager::NotifyKey(KEY_END, true);
850 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100851 operation_end(0);
852 return 0;
853}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500854
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500855int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100856{
857 operation_start("GenerateBackupName");
858 TWFunc::Auto_Generate_Backup_Name();
859 operation_end(0);
860 return 0;
861}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500862
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100864{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600865 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100866 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500867
Ethan Yonker483e9f42016-01-11 22:21:18 -0600868 if (arg.empty())
869 arg = "tw_wipe_list";
870 DataManager::GetValue(arg, List);
871 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
872 if (!List.empty()) {
873 size_t start_pos = 0, end_pos = List.find(";", start_pos);
874 while (end_pos != string::npos && start_pos < List.size()) {
875 part_path = List.substr(start_pos, end_pos - start_pos);
876 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
877 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100878 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400879 } else {
that3f7b1ac2014-12-30 11:30:13 +0100880 count++;
881 }
882 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100884 }
885 DataManager::SetValue("tw_check_partition_list", count);
886 } else {
887 DataManager::SetValue("tw_check_partition_list", 0);
888 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600889 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100890}
Dees_Troy51a0e822012-09-05 15:24:24 -0400891
Ethan Yonker483e9f42016-01-11 22:21:18 -0600892int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100893{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600894 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400895
Ethan Yonker483e9f42016-01-11 22:21:18 -0600896 if (arg.empty())
897 arg = "tw_wipe_list";
898 DataManager::GetValue(arg, List);
899 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
900 if (!List.empty()) {
901 size_t start_pos = 0, end_pos = List.find(";", start_pos);
902 part_path = List;
903 while (end_pos != string::npos && start_pos < List.size()) {
904 part_path = List.substr(start_pos, end_pos - start_pos);
905 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
906 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100907 // Do nothing
908 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600909 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100910 break;
911 }
912 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600913 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100914 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600915 if (!part_path.empty()) {
916 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100917 if (Part) {
918 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500919
that3f7b1ac2014-12-30 11:30:13 +0100920 DataManager::SetValue("tw_partition_name", Part->Display_Name);
921 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
922 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
923 DataManager::SetValue("tw_partition_size", Part->Size / mb);
924 DataManager::SetValue("tw_partition_used", Part->Used / mb);
925 DataManager::SetValue("tw_partition_free", Part->Free / mb);
926 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
927 DataManager::SetValue("tw_partition_removable", Part->Removable);
928 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
929
930 if (Part->Can_Repair())
931 DataManager::SetValue("tw_partition_can_repair", 1);
932 else
933 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500934 if (Part->Can_Resize())
935 DataManager::SetValue("tw_partition_can_resize", 1);
936 else
937 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400938 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100939 DataManager::SetValue("tw_partition_vfat", 1);
940 else
941 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400942 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100943 DataManager::SetValue("tw_partition_exfat", 1);
944 else
945 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100946 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100947 DataManager::SetValue("tw_partition_f2fs", 1);
948 else
949 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400950 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100951 DataManager::SetValue("tw_partition_ext", 1);
952 else
953 DataManager::SetValue("tw_partition_ext", 0);
954 return 0;
955 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600956 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100957 }
958 }
959 }
960 DataManager::SetValue("tw_partition_name", "");
961 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600962 // Set this to 0 to prevent trying to partition this device, just in case
963 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100964 return 0;
965}
966
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500967int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100968{
969 time_t tm;
970 char path[256];
971 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600972 uid_t uid = AID_MEDIA_RW;
973 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100974
975 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600976 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100977 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
978 } else {
979 strcpy(path, "/tmp/");
980 }
981
Matt Mowera8a89d12016-12-30 18:10:37 -0600982 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100983 return 0;
984
985 tm = time(NULL);
986 path_len = strlen(path);
987
988 // Screenshot_2014-01-01-18-21-38.png
989 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
990
991 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600992 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100993 chmod(path, 0666);
994 chown(path, uid, gid);
995
that677b13f2015-12-26 23:57:31 +0100996 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100997
VDavid0032034a412019-10-18 22:43:20 +0200998 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100999 gr_color(255, 255, 255, 255);
1000 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
1001 gr_flip();
1002 gui_forceRender();
1003 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001004 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +01001005 }
1006 return 0;
1007}
1008
1009int GUIAction::setbrightness(std::string arg)
1010{
1011 return TWFunc::Set_Brightness(arg);
1012}
1013
1014int GUIAction::fileexists(std::string arg)
1015{
1016 struct stat st;
1017 string newpath = arg + "/.";
1018
1019 operation_start("FileExists");
1020 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1021 operation_end(0);
1022 else
1023 operation_end(1);
1024 return 0;
1025}
1026
thatcc8ddca2015-01-03 01:59:36 +01001027void GUIAction::reinject_after_flash()
1028{
1029 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001030 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001031 if (simulate) {
1032 simulate_progress_bar();
1033 } else {
1034 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1035 if (Boot == NULL || Boot->Current_File_System != "emmc")
1036 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1037 else {
1038 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1039 TWFunc::Exec_Cmd(injectcmd);
1040 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001041 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001042 }
1043 }
1044}
1045
mauronofrio6d3bf892019-10-26 19:47:55 +02001046int GUIAction::ozip_decrypt(string zip_path)
1047{
bigbiffad58e1b2020-07-06 20:24:34 -04001048 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001049 return 1;
1050 }
1051 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1052 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1053 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1054 return 0;
1055}
1056
that3f7b1ac2014-12-30 11:30:13 +01001057int GUIAction::flash(std::string arg)
1058{
1059 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001060 // We're going to jump to this page first, like a loading page
1061 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001062 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001063 string zip_path = zip_queue[i];
1064 size_t slashpos = zip_path.find_last_of('/');
1065 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001066 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001067 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1068 {
1069 if((ozip_decrypt(zip_path)) != 0)
1070 {
1071 LOGERR("Unable to find ozip_decrypt!");
1072 break;
1073 }
1074 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1075 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1076 if (!TWFunc::Path_Exists(zip_path)) {
1077 LOGERR("Unable to find decrypted zip");
1078 break;
1079 }
1080 }
thatc7b631b2015-06-01 23:36:57 +02001081 DataManager::SetValue("tw_filename", zip_path);
1082 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001083 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1084
1085 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001086 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001087 TWFunc::SetPerformanceMode(false);
1088 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001089 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001090 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001091 break;
that3f7b1ac2014-12-30 11:30:13 +01001092 }
1093 }
1094 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001095
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001096 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001097 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001098 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001099 }
that3f7b1ac2014-12-30 11:30:13 +01001100
thatcc8ddca2015-01-03 01:59:36 +01001101 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001102 PartitionManager.Update_System_Details();
1103 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001104 // 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 -06001105 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001106 return 0;
1107}
1108
1109int GUIAction::wipe(std::string arg)
1110{
1111 operation_start("Format");
1112 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001113 int ret_val = false;
1114
1115 if (simulate) {
1116 simulate_progress_bar();
1117 } else {
1118 if (arg == "data")
1119 ret_val = PartitionManager.Factory_Reset();
1120 else if (arg == "battery")
1121 ret_val = PartitionManager.Wipe_Battery_Stats();
1122 else if (arg == "rotate")
1123 ret_val = PartitionManager.Wipe_Rotate_Data();
1124 else if (arg == "dalvik")
1125 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1126 else if (arg == "DATAMEDIA") {
1127 ret_val = PartitionManager.Format_Data();
1128 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001129 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001130
1131 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1132 if (has_datamedia) {
1133 ret_val = PartitionManager.Wipe_Media_From_Data();
1134 } else {
1135 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1136 }
1137 } else if (arg == "EXTERNAL") {
1138 string External_Path;
1139
1140 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1141 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1142 } else if (arg == "ANDROIDSECURE") {
1143 ret_val = PartitionManager.Wipe_Android_Secure();
1144 } else if (arg == "LIST") {
1145 string Wipe_List, wipe_path;
1146 bool skip = false;
1147 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001148
1149 DataManager::GetValue("tw_wipe_list", Wipe_List);
1150 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1151 if (!Wipe_List.empty()) {
1152 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1153 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1154 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1155 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1156 if (wipe_path == "/and-sec") {
1157 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001158 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001159 ret_val = false;
1160 break;
1161 } else {
1162 skip = true;
1163 }
1164 } else if (wipe_path == "DALVIK") {
1165 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001166 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001167 ret_val = false;
1168 break;
1169 } else {
1170 skip = true;
1171 }
1172 } else if (wipe_path == "INTERNAL") {
1173 if (!PartitionManager.Wipe_Media_From_Data()) {
1174 ret_val = false;
1175 break;
1176 } else {
1177 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001178 }
1179 }
that3f7b1ac2014-12-30 11:30:13 +01001180 if (!skip) {
1181 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001182 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001183 ret_val = false;
1184 break;
1185 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1186 arg = wipe_path;
1187 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 } else {
that3f7b1ac2014-12-30 11:30:13 +01001189 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001190 }
that3f7b1ac2014-12-30 11:30:13 +01001191 start_pos = end_pos + 1;
1192 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001193 }
1194 }
that3f7b1ac2014-12-30 11:30:13 +01001195 } else
1196 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001197#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001198 if (arg == DataManager::GetSettingsStoragePath()) {
1199 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1200 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001201
that3f7b1ac2014-12-30 11:30:13 +01001202 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1203 LOGINFO("Making TWRP folder and saving settings.\n");
1204 Storage_Path += "/TWRP";
1205 mkdir(Storage_Path.c_str(), 0777);
1206 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001207 } else {
that3f7b1ac2014-12-30 11:30:13 +01001208 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1209 }
1210 }
1211#endif
1212 }
1213 PartitionManager.Update_System_Details();
1214 if (ret_val)
1215 ret_val = 0; // 0 is success
1216 else
1217 ret_val = 1; // 1 is failure
1218 operation_end(ret_val);
1219 return 0;
1220}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001221
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001222int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001223{
1224 operation_start("Refreshing Sizes");
1225 if (simulate) {
1226 simulate_progress_bar();
1227 } else
1228 PartitionManager.Update_System_Details();
1229 operation_end(0);
1230 return 0;
1231}
1232
1233int GUIAction::nandroid(std::string arg)
1234{
that3f7b1ac2014-12-30 11:30:13 +01001235 if (simulate) {
that73a52952015-01-28 23:07:34 +01001236 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001237 DataManager::SetValue("tw_partition", "Simulation");
1238 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001239 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001240 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001241 operation_start("Nandroid");
1242 int ret = 0;
1243
that3f7b1ac2014-12-30 11:30:13 +01001244 if (arg == "backup") {
1245 string Backup_Name;
1246 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001247 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001248 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 -05001249 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001250 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1251 if (!PartitionManager.stop_backup.get_value()) {
1252 if (ret == false)
1253 ret = 1; // 1 for failure
1254 else
1255 ret = 0; // 0 for success
1256 DataManager::SetValue("tw_cancel_backup", 0);
1257 } else {
1258 DataManager::SetValue("tw_cancel_backup", 1);
1259 gui_msg("backup_cancel=Backup Cancelled");
1260 ret = 0;
1261 }
bigbiffce8f83c2015-12-12 18:30:21 -05001262 } else {
that3f7b1ac2014-12-30 11:30:13 +01001263 operation_end(1);
1264 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001265 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001266 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001267 } else if (arg == "restore") {
1268 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001269 int gui_adb_backup;
1270
that3f7b1ac2014-12-30 11:30:13 +01001271 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001272 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1273 if (gui_adb_backup) {
1274 DataManager::SetValue("tw_operation_state", 1);
1275 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1276 ret = 0; // success
1277 else
1278 ret = 1; // failure
1279 DataManager::SetValue("tw_enable_adb_backup", 0);
1280 ret = 0; // assume success???
1281 } else {
1282 if (PartitionManager.Run_Restore(Restore_Name))
1283 ret = 0; // success
1284 else
1285 ret = 1; // failure
1286 }
that3f7b1ac2014-12-30 11:30:13 +01001287 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001288 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001289 return -1;
1290 }
that73a52952015-01-28 23:07:34 +01001291 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001292 return ret;
1293 }
1294 return 0;
1295}
1296
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001297int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001298 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001299 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001300 }
1301 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001302 int op_status = PartitionManager.Cancel_Backup();
1303 if (op_status != 0)
1304 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001305 }
1306
1307 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001308}
Dees_Troy51a0e822012-09-05 15:24:24 -04001309
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001310int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001311{
that73a52952015-01-28 23:07:34 +01001312 int op_status = 0;
1313
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001314 operation_start("Fix Contexts");
1315 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001316 if (simulate) {
1317 simulate_progress_bar();
1318 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001319 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001320 if (op_status != 0)
1321 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001322 }
that73a52952015-01-28 23:07:34 +01001323 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001324 return 0;
1325}
1326
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001327int GUIAction::fixpermissions(std::string arg)
1328{
1329 return fixcontexts(arg);
1330}
1331
that3f7b1ac2014-12-30 11:30:13 +01001332int GUIAction::dd(std::string arg)
1333{
1334 operation_start("imaging");
1335
1336 if (simulate) {
1337 simulate_progress_bar();
1338 } else {
1339 string cmd = "dd " + arg;
1340 TWFunc::Exec_Cmd(cmd);
1341 }
1342 operation_end(0);
1343 return 0;
1344}
1345
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001346int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001347{
1348 operation_start("Partition SD Card");
1349 int ret_val = 0;
1350
1351 if (simulate) {
1352 simulate_progress_bar();
1353 } else {
1354 int allow_partition;
1355 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1356 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001357 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001358 } else {
1359 if (!PartitionManager.Partition_SDCard())
1360 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001361 }
that3f7b1ac2014-12-30 11:30:13 +01001362 }
1363 operation_end(ret_val);
1364 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001365
that3f7b1ac2014-12-30 11:30:13 +01001366}
Dees_Troy51a0e822012-09-05 15:24:24 -04001367
that3f7b1ac2014-12-30 11:30:13 +01001368int GUIAction::cmd(std::string arg)
1369{
1370 int op_status = 0;
1371
1372 operation_start("Command");
1373 LOGINFO("Running command: '%s'\n", arg.c_str());
1374 if (simulate) {
1375 simulate_progress_bar();
1376 } else {
1377 op_status = TWFunc::Exec_Cmd(arg);
1378 if (op_status != 0)
1379 op_status = 1;
1380 }
1381
1382 operation_end(op_status);
1383 return 0;
1384}
1385
1386int GUIAction::terminalcommand(std::string arg)
1387{
1388 int op_status = 0;
1389 string cmdpath, command;
1390
1391 DataManager::GetValue("tw_terminal_location", cmdpath);
1392 operation_start("CommandOutput");
1393 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1394 if (simulate) {
1395 simulate_progress_bar();
1396 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001397 } else if (arg == "exit") {
1398 LOGINFO("Exiting terminal\n");
1399 operation_end(op_status);
1400 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001401 } else {
1402 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1403 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001404 DataManager::SetValue("tw_terminal_state", 1);
1405 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001406 FILE* fp;
1407 char line[512];
1408
1409 fp = popen(command.c_str(), "r");
1410 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001411 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001412 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001413 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001414 struct timeval timeout;
1415 fd_set fdset;
1416
Matt Mowera8a89d12016-12-30 18:10:37 -06001417 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001418 {
1419 FD_ZERO(&fdset);
1420 FD_SET(fd, &fdset);
1421 timeout.tv_sec = 0;
1422 timeout.tv_usec = 400000;
1423 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1424 if (has_data == 0) {
1425 // Timeout reached
1426 DataManager::GetValue("tw_terminal_state", check);
1427 if (check == 0) {
1428 keep_going = 0;
1429 }
1430 } else if (has_data < 0) {
1431 // End of execution
1432 keep_going = 0;
1433 } else {
1434 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001435 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001436 gui_print("%s", line); // Display output
1437 else
1438 keep_going = 0; // Done executing
1439 }
1440 }
1441 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001442 }
thatc6085482015-01-09 22:12:43 +01001443 DataManager::SetValue("tw_operation_status", 0);
1444 DataManager::SetValue("tw_operation_state", 1);
1445 DataManager::SetValue("tw_terminal_state", 0);
1446 DataManager::SetValue("tw_background_thread_running", 0);
1447 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001448 }
1449 return 0;
1450}
1451
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001452int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001453{
that3f7b1ac2014-12-30 11:30:13 +01001454 LOGINFO("Sending kill command...\n");
1455 operation_start("KillCommand");
1456 DataManager::SetValue("tw_operation_status", 0);
1457 DataManager::SetValue("tw_operation_state", 1);
1458 DataManager::SetValue("tw_terminal_state", 0);
1459 DataManager::SetValue("tw_background_thread_running", 0);
1460 DataManager::SetValue(TW_ACTION_BUSY, 0);
1461 return 0;
1462}
1463
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001464int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001465{
1466 int op_status = 0;
1467 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001468 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001469 if (simulate) {
1470 simulate_progress_bar();
1471 } else {
1472 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001474 }
1475
1476 operation_end(op_status);
1477 return 0;
1478}
1479
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001480int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001481{
1482 int op_status = 0;
1483
1484 operation_start("CheckBackupName");
1485 if (simulate) {
1486 simulate_progress_bar();
1487 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001488 string Backup_Name;
1489 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1490 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001491 if (op_status != 0)
1492 op_status = 1;
1493 }
1494
1495 operation_end(op_status);
1496 return 0;
1497}
1498
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001499int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001500{
1501 int op_status = 0;
1502
1503 operation_start("Decrypt");
1504 if (simulate) {
1505 simulate_progress_bar();
1506 } else {
1507 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001508 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001509 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001510
1511 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1512 DataManager::GetValue("tw_crypto_user_id", userID);
1513 if (userID != "") {
1514 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1515 if (userID != "0") {
1516 if (op_status != 0)
1517 op_status = 1;
1518 operation_end(op_status);
1519 return 0;
1520 }
1521 } else {
1522 LOGINFO("User ID not found\n");
1523 op_status = 1;
1524 }
1525 ::sleep(1);
1526 } else { // for FDE
1527 op_status = PartitionManager.Decrypt_Device(Password);
1528 }
1529
that3f7b1ac2014-12-30 11:30:13 +01001530 if (op_status != 0)
1531 op_status = 1;
1532 else {
that3f7b1ac2014-12-30 11:30:13 +01001533 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1534
Ethan Yonkercf50da52015-01-12 21:59:07 -06001535 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001536
Ethan Yonkercf50da52015-01-12 21:59:07 -06001537 // Check for a custom theme and load it if exists
1538 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1539 if (has_datamedia != 0) {
1540 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001541 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001542 } else {
1543 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001544 }
1545 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001546 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001547 }
1548 }
1549
1550 operation_end(op_status);
1551 return 0;
1552}
1553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001554int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001555{
1556 operation_start("Sideload");
1557 if (simulate) {
1558 simulate_progress_bar();
1559 operation_end(0);
1560 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001562 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1563
1564 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001565 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001566 int ret = twrp_sideload("/", &reboot_action);
1567 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001568 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1569
1570 if (ret != 0) {
1571 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001572 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001573 ret = 1; // failure
1574 } else {
1575 int wipe_cache = 0;
1576 int wipe_dalvik = 0;
1577 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001578 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1579 PartitionManager.Wipe_By_Path("/cache");
1580 if (wipe_dalvik)
1581 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001582 }
thatc6085482015-01-09 22:12:43 +01001583 TWFunc::Toggle_MTP(mtp_was_enabled);
1584 reinject_after_flash();
1585 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001586 }
that3f7b1ac2014-12-30 11:30:13 +01001587 return 0;
1588}
1589
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001590int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001591{
that3f7b1ac2014-12-30 11:30:13 +01001592 struct stat st;
1593 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001595 LOGINFO("Signaling child sideload process to exit.\n");
1596 // Calling stat() on this magic filename signals the minadbd
1597 // subprocess to shut down.
1598 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001599 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001600 if (!sideload_child_pid) {
1601 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001602 return 0;
1603 }
thatcc8ddca2015-01-03 01:59:36 +01001604 ::sleep(1);
1605 LOGINFO("Killing child sideload process.\n");
1606 kill(sideload_child_pid, SIGTERM);
1607 int status;
1608 LOGINFO("Waiting for child sideload process to exit.\n");
1609 waitpid(sideload_child_pid, &status, 0);
1610 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001611 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1612 return 0;
1613}
1614
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001615int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001616{
1617 operation_start("OpenRecoveryScript");
1618 if (simulate) {
1619 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001620 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001621 } else {
that10ae24f2015-12-26 20:53:51 +01001622 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001623 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001624 }
1625 return 0;
1626}
1627
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001628int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001629{
1630 int op_status = 0;
1631
1632 operation_start("Install SuperSU");
1633 if (simulate) {
1634 simulate_progress_bar();
1635 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001636 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001637 }
1638
1639 operation_end(op_status);
1640 return 0;
1641}
1642
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001643int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001644{
1645 int op_status = 0;
1646
1647 operation_start("Fixing Superuser Permissions");
1648 if (simulate) {
1649 simulate_progress_bar();
1650 } else {
1651 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1652 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1653 }
1654
1655 operation_end(op_status);
1656 return 0;
1657}
1658
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001659int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001660{
1661 int op_status = 0;
1662
1663 operation_start("Try Restore Decrypt");
1664 if (simulate) {
1665 simulate_progress_bar();
1666 } else {
1667 string Restore_Path, Filename, Password;
1668 DataManager::GetValue("tw_restore", Restore_Path);
1669 Restore_Path += "/";
1670 DataManager::GetValue("tw_restore_password", Password);
1671 TWFunc::SetPerformanceMode(true);
1672 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1673 op_status = 0; // success
1674 else
1675 op_status = 1; // fail
1676 TWFunc::SetPerformanceMode(false);
1677 }
1678
1679 operation_end(op_status);
1680 return 0;
1681}
1682
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001683int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001684{
1685 int op_status = 0;
1686
1687 operation_start("Repair Partition");
1688 if (simulate) {
1689 simulate_progress_bar();
1690 } else {
1691 string part_path;
1692 DataManager::GetValue("tw_partition_mount_point", part_path);
1693 if (PartitionManager.Repair_By_Path(part_path, true)) {
1694 op_status = 0; // success
1695 } else {
that3f7b1ac2014-12-30 11:30:13 +01001696 op_status = 1; // fail
1697 }
1698 }
1699
1700 operation_end(op_status);
1701 return 0;
1702}
1703
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001704int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001705{
1706 int op_status = 0;
1707
1708 operation_start("Resize Partition");
1709 if (simulate) {
1710 simulate_progress_bar();
1711 } else {
1712 string part_path;
1713 DataManager::GetValue("tw_partition_mount_point", part_path);
1714 if (PartitionManager.Resize_By_Path(part_path, true)) {
1715 op_status = 0; // success
1716 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001717 op_status = 1; // fail
1718 }
1719 }
1720
1721 operation_end(op_status);
1722 return 0;
1723}
1724
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001725int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001726{
1727 int op_status = 0;
1728
1729 operation_start("Change File System");
1730 if (simulate) {
1731 simulate_progress_bar();
1732 } else {
1733 string part_path, file_system;
1734 DataManager::GetValue("tw_partition_mount_point", part_path);
1735 DataManager::GetValue("tw_action_new_file_system", file_system);
1736 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1737 op_status = 0; // success
1738 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001739 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001740 op_status = 1; // fail
1741 }
1742 }
1743 PartitionManager.Update_System_Details();
1744 operation_end(op_status);
1745 return 0;
1746}
1747
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001748int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001749{
1750 int op_status = 0;
1751
1752 operation_start("Start MTP");
1753 if (PartitionManager.Enable_MTP())
1754 op_status = 0; // success
1755 else
1756 op_status = 1; // fail
1757
1758 operation_end(op_status);
1759 return 0;
1760}
1761
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001762int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001763{
1764 int op_status = 0;
1765
1766 operation_start("Stop MTP");
1767 if (PartitionManager.Disable_MTP())
1768 op_status = 0; // success
1769 else
1770 op_status = 1; // fail
1771
1772 operation_end(op_status);
1773 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001774}
1775
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001776int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001777{
1778 int op_status = 0;
epicX8f52c0a2021-02-24 23:12:08 +05301779 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001780
1781 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001782 string path, filename;
1783 DataManager::GetValue("tw_zip_location", path);
1784 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001785
epicX8f52c0a2021-02-24 23:12:08 +05301786#ifdef AB_OTA_UPDATER
1787 string target = DataManager::GetStrValue("tw_flash_partition");
1788 unsigned int pos = target.find_last_of(';');
1789 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1790 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1791 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1792
1793 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1794 {
1795 string current_slot = PartitionManager.Get_Active_Slot_Display();
1796 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1797
1798 PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A");
1799 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
1800 PartitionManager.Set_Active_Slot(current_slot);
1801
1802 DataManager::SetValue("tw_flash_both_slots", 0);
1803 flag = false;
1804 }
1805#endif
1806 if (flag)
1807 {
1808 if (PartitionManager.Flash_Image(path, filename))
1809 op_status = 0; // success
1810 else
1811 op_status = 1; // fail
1812 }
1813
Ethan Yonker96af84a2015-01-05 14:58:36 -06001814 operation_end(op_status);
1815 return 0;
1816}
1817
that10ae24f2015-12-26 20:53:51 +01001818int GUIAction::twcmd(std::string arg)
1819{
1820 operation_start("TWRP CLI Command");
1821 if (simulate)
1822 simulate_progress_bar();
1823 else
1824 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1825 operation_end(0);
1826 return 0;
1827}
1828
Dees_Troy51a0e822012-09-05 15:24:24 -04001829int GUIAction::getKeyByName(std::string key)
1830{
that8834a0f2016-01-05 23:29:30 +01001831 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001832 else if (key == "menu") return KEY_MENU;
1833 else if (key == "back") return KEY_BACK;
1834 else if (key == "search") return KEY_SEARCH;
1835 else if (key == "voldown") return KEY_VOLUMEDOWN;
1836 else if (key == "volup") return KEY_VOLUMEUP;
1837 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001838 int ret_val;
1839 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1840 if (!ret_val)
1841 return KEY_POWER;
1842 else
1843 return ret_val;
1844 }
1845
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001846 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001847}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001848
1849int GUIAction::checkpartitionlifetimewrites(std::string arg)
1850{
1851 int op_status = 0;
1852 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1853
1854 operation_start("Check Partition Lifetime Writes");
1855 if (sys) {
1856 if (sys->Check_Lifetime_Writes() != 0)
1857 DataManager::SetValue("tw_lifetime_writes", 1);
1858 else
1859 DataManager::SetValue("tw_lifetime_writes", 0);
1860 op_status = 0; // success
1861 } else {
1862 DataManager::SetValue("tw_lifetime_writes", 1);
1863 op_status = 1; // fail
1864 }
1865
1866 operation_end(op_status);
1867 return 0;
1868}
1869
1870int GUIAction::mountsystemtoggle(std::string arg)
1871{
1872 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001873 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001874 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001875
1876 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001877 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001878 op_status = 1; // fail
1879 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001880 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001881 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001882 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001883 DataManager::SetValue("tw_mount_system_ro", 0);
1884 Part->Change_Mount_Read_Only(false);
1885 } else {
1886 DataManager::SetValue("tw_mount_system_ro", 1);
1887 Part->Change_Mount_Read_Only(true);
1888 }
1889 if (remount_system) {
1890 Part->Mount(true);
1891 }
1892 op_status = 0; // success
1893 } else {
1894 op_status = 1; // fail
1895 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001896 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1897 if (Part) {
1898 if (arg == "0") {
1899 Part->Change_Mount_Read_Only(false);
1900 } else {
1901 Part->Change_Mount_Read_Only(true);
1902 }
1903 if (remount_vendor) {
1904 Part->Mount(true);
1905 }
1906 op_status = 0; // success
1907 } else {
1908 op_status = 1; // fail
1909 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001910 }
1911
1912 operation_end(op_status);
1913 return 0;
1914}
Ethan Yonker74db1572015-10-28 12:44:49 -05001915
1916int GUIAction::setlanguage(std::string arg __unused)
1917{
1918 int op_status = 0;
1919
1920 operation_start("Set Language");
1921 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1922 PageManager::RequestReload();
1923 op_status = 0; // success
1924
1925 operation_end(op_status);
1926 return 0;
1927}
Ethan Yonker1b190162016-12-05 15:25:19 -06001928
Matt Mower9472ba12016-01-20 18:12:47 -06001929int GUIAction::togglebacklight(std::string arg __unused)
1930{
1931 blankTimer.toggleBlank();
1932 return 0;
1933}
1934
Ethan Yonker1b190162016-12-05 15:25:19 -06001935int GUIAction::setbootslot(std::string arg)
1936{
1937 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001938 if (!simulate) {
1939 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1940 // PartitionManager failed to unmount /vendor, this should not happen,
1941 // but in case it does, do a lazy unmount
1942 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1943 umount2("/vendor", MNT_DETACH);
1944 PartitionManager.Set_Active_Slot(arg);
1945 } else {
1946 PartitionManager.Set_Active_Slot(arg);
1947 }
1948 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001949 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001950 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001951 operation_end(0);
1952 return 0;
1953}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001954
1955int GUIAction::checkforapp(std::string arg __unused)
1956{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001957 operation_start("Check for TWRP App");
1958 if (!simulate)
1959 {
epicX271bb3a2020-12-30 01:03:18 +05301960 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001961 } else
1962 simulate_progress_bar();
epicX271bb3a2020-12-30 01:03:18 +05301963
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001964 operation_end(0);
1965 return 0;
1966}
1967
1968int GUIAction::installapp(std::string arg __unused)
1969{
1970 int op_status = 1;
1971 operation_start("Install TWRP App");
1972 if (!simulate)
1973 {
1974 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1975 if (PartitionManager.Mount_By_Path("/data", true)) {
1976 string install_path = "/data/app";
1977 string context = "u:object_r:apk_data_file:s0";
1978 if (!TWFunc::Path_Exists(install_path)) {
1979 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1980 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1981 goto exit;
1982 }
1983 if (chown(install_path.c_str(), 1000, 1000)) {
1984 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1985 goto exit;
1986 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001987 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001988 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1989 goto exit;
1990 }
1991 }
1992 install_path += "/me.twrp.twrpapp-1";
1993 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1994 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1995 goto exit;
1996 }
1997 if (chown(install_path.c_str(), 1000, 1000)) {
1998 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1999 goto exit;
2000 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002001 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002002 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2003 goto exit;
2004 }
2005 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002006 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002007 LOGERR("Error copying apk file\n");
2008 goto exit;
2009 }
2010 if (chown(install_path.c_str(), 1000, 1000)) {
2011 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2012 goto exit;
2013 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002014 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002015 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2016 goto exit;
2017 }
2018 sync();
2019 sync();
2020 }
2021 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002022 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2023 string base_path = PartitionManager.Get_Android_Root_Path();
2024 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002025 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2026 string install_path = base_path + "/priv-app";
2027 string context = "u:object_r:system_file:s0";
2028 if (!TWFunc::Path_Exists(install_path))
2029 install_path = base_path + "/app";
2030 if (TWFunc::Path_Exists(install_path)) {
2031 install_path += "/twrpapp";
2032 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2033 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
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 += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002039 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002040 LOGERR("Error copying apk file\n");
2041 goto exit;
2042 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002043 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002044 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2045 goto exit;
2046 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002047
2048 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2049 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002050 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002051 LOGERR("Error copying permission file\n");
2052 goto exit;
2053 }
2054 if (chown(permission_path.c_str(), 1000, 1000)) {
2055 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2056 goto exit;
2057 }
2058 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2059 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2060 goto exit;
2061 }
2062
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002063 sync();
2064 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002065 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002066 op_status = 0;
2067 } else {
2068 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2069 }
2070 }
2071 }
2072 }
2073 } else
2074 simulate_progress_bar();
2075exit:
epicX271bb3a2020-12-30 01:03:18 +05302076 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002077 operation_end(0);
2078 return 0;
2079}
Ethan Yonker53796e72019-01-11 22:49:52 -06002080
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002081int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2082{
2083 int op_status = 1;
2084 operation_start("Uninstall TWRP System App");
2085 if (!simulate)
2086 {
2087 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2088 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2089 if (!Part) {
2090 LOGERR("Unabled to find system partition.\n");
2091 goto exit;
2092 }
2093 if (!Part->UnMount(true)) {
2094 goto exit;
2095 }
2096 if (Mount_System_RO > 0) {
2097 DataManager::SetValue("tw_mount_system_ro", 0);
2098 Part->Change_Mount_Read_Only(false);
2099 }
2100 if (Part->Mount(true)) {
2101 string base_path = PartitionManager.Get_Android_Root_Path();
2102 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2103 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2104 string uninstall_path = base_path + "/priv-app";
2105 if (!TWFunc::Path_Exists(uninstall_path))
2106 uninstall_path = base_path + "/app";
2107 uninstall_path += "/twrpapp";
2108 if (TWFunc::Path_Exists(uninstall_path)) {
2109 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2110 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2111 sync();
2112 op_status = 0;
2113 DataManager::SetValue("tw_app_installed_in_system", 0);
2114 DataManager::SetValue("tw_app_install_status", 0);
2115 } else {
2116 LOGERR("Unable to remove TWRP app from system.\n");
2117 }
2118 } else {
2119 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2120 }
2121 }
2122 Part->UnMount(true);
2123 if (Mount_System_RO > 0) {
2124 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2125 Part->Change_Mount_Read_Only(true);
2126 }
2127 } else
2128 simulate_progress_bar();
2129exit:
epicX271bb3a2020-12-30 01:03:18 +05302130 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002131 operation_end(0);
2132 return 0;
2133}
2134
Ethan Yonker53796e72019-01-11 22:49:52 -06002135int GUIAction::repackimage(std::string arg __unused)
2136{
2137 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002138 twrpRepacker repacker;
2139
Ethan Yonker53796e72019-01-11 22:49:52 -06002140 operation_start("Repack Image");
2141 if (!simulate)
2142 {
2143 std::string path = DataManager::GetStrValue("tw_filename");
2144 Repack_Options_struct Repack_Options;
2145 Repack_Options.Disable_Verity = false;
2146 Repack_Options.Disable_Force_Encrypt = false;
2147 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2148 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2149 Repack_Options.Type = REPLACE_KERNEL;
2150 else
2151 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002152 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002153 goto exit;
2154 } else
2155 simulate_progress_bar();
2156 op_status = 0;
2157exit:
2158 operation_end(op_status);
2159 return 0;
2160}
2161
2162int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2163{
2164 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002165 twrpRepacker repacker;
2166
Ethan Yonker53796e72019-01-11 22:49:52 -06002167 operation_start("Repack Image");
2168 if (!simulate)
2169 {
bigbiffad58e1b2020-07-06 20:24:34 -04002170 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002171 LOGERR("Image repacking tool not present in this TWRP build!");
2172 goto exit;
2173 }
2174 DataManager::SetProgress(0);
2175 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2176 if (part)
2177 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2178 else {
2179 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2180 goto exit;
2181 }
bigbiffad58e1b2020-07-06 20:24:34 -04002182 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 -06002183 goto exit;
2184 DataManager::SetProgress(.25);
2185 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002186 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002187 if (TWFunc::Exec_Cmd(command) != 0) {
2188 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2189 goto exit;
2190 }
2191 std::string header_path = REPACK_ORIG_DIR;
2192 header_path += "header";
2193 if (TWFunc::Path_Exists(header_path)) {
2194 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";
2195 if (TWFunc::Exec_Cmd(command) != 0) {
2196 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2197 goto exit;
2198 }
2199 }
2200 DataManager::SetProgress(.5);
2201 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002202 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002203 if (TWFunc::Exec_Cmd(command) != 0) {
2204 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2205 goto exit;
2206 }
2207 DataManager::SetProgress(.75);
2208 std::string path = REPACK_ORIG_DIR;
2209 std::string file = "new-boot.img";
2210 DataManager::SetValue("tw_flash_partition", "/boot;");
2211 if (!PartitionManager.Flash_Image(path, file)) {
2212 LOGINFO("Error flashing new image\n");
2213 goto exit;
2214 }
2215 DataManager::SetProgress(1);
2216 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2217 } else
2218 simulate_progress_bar();
2219 op_status = 0;
2220exit:
2221 operation_end(op_status);
2222 return 0;
2223}
bigbiffdf8436b2020-08-30 16:22:34 -04002224
2225
2226int GUIAction::enableadb(std::string arg __unused) {
2227 android::base::SetProperty("sys.usb.config", "none");
2228 android::base::SetProperty("sys.usb.config", "adb");
2229 return 0;
2230}
2231
2232int GUIAction::enablefastboot(std::string arg __unused) {
2233 android::base::SetProperty("sys.usb.config", "none");
2234 android::base::SetProperty("sys.usb.config", "fastboot");
2235 return 0;
2236}
Mohd Faraz77bbeb02020-08-12 12:29:42 +00002237
2238int GUIAction::changeterminal(std::string arg) {
2239 bool res = true;
2240 std::string resp, cmd = "cd " + arg;
2241 DataManager::GetValue("tw_terminal_location", resp);
2242 if (arg.empty() && !resp.empty()) {
2243 cmd = "cd /";
2244 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2245 term->NotifyCharInput(cmd.at(iter));
2246 term->NotifyCharInput(13);
2247 DataManager::SetValue("tw_terminal_location", "");
2248 return 0;
2249 }
2250 if (term != NULL && !arg.empty()) {
2251 DataManager::SetValue("tw_terminal_location", arg);
2252 if (term->status()) {
2253 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2254 term->NotifyCharInput(cmd.at(iter));
2255 term->NotifyCharInput(13);
2256 }
2257 else if (chdir(arg.c_str()) != 0) {
2258 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2259 res = false;
2260 }
2261 }
2262 else {
2263 res = false;
2264 LOGINFO("Unable to switch to Terminal\n");
2265 }
2266 if (res)
2267 gui_changePage("terminalcommand");
2268 return 0;
2269}
bigbiffcfa875c2021-06-20 16:20:27 -04002270
2271int GUIAction::unmapsuperdevices(std::string arg __unused) {
2272 int op_status = 1;
2273
2274 operation_start("Remove Super Devices");
2275 if (simulate) {
2276 simulate_progress_bar();
2277 } else {
2278 if (PartitionManager.Unmap_Super_Devices()) {
2279 op_status = 0;
2280 }
2281 }
2282
2283 operation_end(op_status);
2284 return 0;
2285}
2286
Captain Throwback16dd81b2021-02-12 19:32:36 -05002287#ifndef TW_EXCLUDE_NANO
2288int GUIAction::editfile(std::string arg) {
2289 if (term != NULL) {
2290 for (uint8_t iter = 0; iter < arg.size(); iter++)
2291 term->NotifyCharInput(arg.at(iter));
2292 term->NotifyCharInput(13);
2293 }
2294 else
2295 LOGINFO("Unable to switch to Terminal\n");
2296 return 0;
2297}
2298#endif
epicXa721f952021-01-04 13:01:31 +05302299
2300int GUIAction::applycustomtwrpfolder(string arg __unused)
2301{
2302 operation_start("ChangingTWRPFolder");
2303 string storageFolder = DataManager::GetSettingsStoragePath();
2304 string newFolder = storageFolder + '/' + arg;
2305 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2306 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2307 bool ret = false;
2308
2309 if (TWFunc::Path_Exists(newFolder)) {
2310 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2311 } else {
2312 ret = true;
2313 }
2314
2315 if (newFolder != prevFolder && ret) {
2316 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2317 } else {
2318 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2319 }
2320
epicX11e90832021-03-01 00:02:57 +05302321 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicXa721f952021-01-04 13:01:31 +05302322
2323
2324 if (ret) {
2325 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2326 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2327 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2328 }
2329 operation_end((int)!ret);
2330 return 0;
2331}