blob: 894323407b200fd44ef6d5529a136258f67cef3b [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);
thatc6085482015-01-09 22:12:43 +0100210
211 // remember actions that run in the caller thread
212 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
213 setActionsRunningInCallerThread.insert(it->first);
214
215 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100216 ADD_ACTION(flash);
217 ADD_ACTION(wipe);
218 ADD_ACTION(refreshsizes);
219 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600220 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100221 ADD_ACTION(fixpermissions);
222 ADD_ACTION(dd);
223 ADD_ACTION(partitionsd);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100224 ADD_ACTION(cmd);
225 ADD_ACTION(terminalcommand);
226 ADD_ACTION(reinjecttwrp);
227 ADD_ACTION(decrypt);
228 ADD_ACTION(adbsideload);
229 ADD_ACTION(openrecoveryscript);
230 ADD_ACTION(installsu);
231 ADD_ACTION(decrypt_backup);
232 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500233 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100234 ADD_ACTION(changefilesystem);
235 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100236 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600237 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600238 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500239 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600240 ADD_ACTION(repackimage);
241 ADD_ACTION(fixabrecoverybootloop);
epicXa721f952021-01-04 13:01:31 +0530242 ADD_ACTION(applycustomtwrpfolder);
Captain Throwback16dd81b2021-02-12 19:32:36 -0500243#ifndef TW_EXCLUDE_NANO
244 ADD_ACTION(editfile);
245#endif
that3f7b1ac2014-12-30 11:30:13 +0100246 }
247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600249 actions = FindNode(node, "actions");
250 if (actions) child = FindNode(actions, "action");
251 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 while (child)
256 {
257 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 attr = child->first_attribute("function");
260 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 action.mFunction = attr->value();
263 action.mArg = child->value();
264 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400265
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 child = child->next_sibling("action");
267 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600270 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200271 if (child)
272 {
273 attr = child->first_attribute("key");
274 if (attr)
275 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100276 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600277 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100278 {
279 const int key = getKeyByName(keys[i]);
280 mKeys[key] = false;
281 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 }
283 else
284 {
285 attr = child->first_attribute("x");
286 if (!attr) return;
287 mActionX = atol(attr->value());
288 attr = child->first_attribute("y");
289 if (!attr) return;
290 mActionY = atol(attr->value());
291 attr = child->first_attribute("w");
292 if (!attr) return;
293 mActionW = atol(attr->value());
294 attr = child->first_attribute("h");
295 if (!attr) return;
296 mActionH = atol(attr->value());
297 }
298 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400299}
300
Matt Mower25036b72016-06-24 12:30:18 -0500301int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400302{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 if (state == TOUCH_RELEASE)
304 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400305
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200306 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400307}
308
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100309int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400310{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100311 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600312 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100313 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100314
315 bool prevState = itr->second;
316 itr->second = down;
317
318 // If there is only one key for this action, wait for key up so it
319 // doesn't trigger with multi-key actions.
320 // Else, check if all buttons are pressed, then consume their release events
321 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600322 if (mKeys.size() == 1) {
323 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100324 doActions();
thatd4725ca2016-01-19 00:15:21 +0100325 return 0;
326 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600327 } else if (down) {
328 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
329 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100330 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100331 }
332
333 // Passed, all req buttons are pressed, reset them and consume release events
334 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600335 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100336 kb->ConsumeKeyRelease(itr->first);
337 itr->second = false;
338 }
339
340 doActions();
thatd4725ca2016-01-19 00:15:21 +0100341 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100342 }
343
thatd4725ca2016-01-19 00:15:21 +0100344 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
Vojtech Bocek07220562014-02-08 02:05:33 +0100347int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400348{
Vojtech Bocek07220562014-02-08 02:05:33 +0100349 GUIObject::NotifyVarChange(varName, value);
350
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100351 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200352 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600353 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200354 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400357}
358
359void GUIAction::simulate_progress_bar(void)
360{
Ethan Yonker74db1572015-10-28 12:44:49 -0500361 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400362 for (int i = 0; i < 5; i++)
363 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500364 if (PartitionManager.stop_backup.get_value()) {
365 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600366 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500367 DataManager::SetValue("ui_progress", 0);
368 PartitionManager.stop_backup.set_value(0);
369 return;
370 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400371 usleep(500000);
372 DataManager::SetValue("ui_progress", i * 20);
373 }
374}
375
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600376int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400377{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200378 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
380 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100381 DataManager::SetValue("ui_portion_size", 0);
382 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400383
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200384 if (filename.empty())
385 {
386 LOGERR("No file specified.\n");
387 return -1;
388 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400389
Ethan Yonker9598c072016-01-15 21:54:34 -0600390 if (!TWFunc::Path_Exists(filename)) {
391 if (!PartitionManager.Mount_By_Path(filename, true)) {
392 return -1;
393 }
394 if (!TWFunc::Path_Exists(filename)) {
395 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
396 return -1;
397 }
398 }
Dees_Troy657c3092012-09-10 20:32:10 -0400399
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 if (simulate) {
401 simulate_progress_bar();
402 } else {
bigbiffab036612021-06-24 21:31:40 -0400403 char apex_enabled[PROPERTY_VALUE_MAX];
404 property_get("twrp.apex.flattened", apex_enabled, "");
405 if (strcmp(apex_enabled, "true") == 0) {
406 umount("/apex");
407 }
epicX9597b842021-03-20 21:58:17 +0530408 ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR));
bigbiff1f9e4842020-10-31 11:33:15 -0400409 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 // Now, check if we need to ensure TWRP remains installed...
411 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400412 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 {
414 DataManager::SetValue("tw_operation", "Configuring TWRP");
415 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500416 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400417 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500419 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 }
421 }
422 }
423
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 // Done
425 DataManager::SetValue("ui_progress", 100);
426 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100427 DataManager::SetValue("ui_portion_size", 0);
428 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200429 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430}
431
that73a52952015-01-28 23:07:34 +0100432GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100433{
that73a52952015-01-28 23:07:34 +0100434 string func = gui_parse_text(action.mFunction);
435 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
436 if (needsThread) {
437 if (func == "cancelbackup")
438 return THREAD_CANCEL;
439 else
440 return THREAD_ACTION;
441 }
442 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100443}
444
Dees_Troy51a0e822012-09-05 15:24:24 -0400445int GUIAction::doActions()
446{
that3f7b1ac2014-12-30 11:30:13 +0100447 if (mActions.size() < 1)
448 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
that73a52952015-01-28 23:07:34 +0100450 // Determine in which thread to run the actions.
451 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
452 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100453 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100454 for (it = mActions.begin(); it != mActions.end(); ++it) {
455 ThreadType tt = getThreadType(*it);
456 if (tt == THREAD_NONE)
457 continue;
458 if (threadType == THREAD_NONE)
459 threadType = tt;
460 else if (threadType != tt) {
461 LOGERR("Can't mix normal and cancel actions in the same list.\n"
462 "Running the whole batch in the cancel thread.\n");
463 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100464 break;
465 }
that7d3b54f2015-01-09 22:52:51 +0100466 }
that73a52952015-01-28 23:07:34 +0100467
468 // Now run the actions in the desired thread.
469 switch (threadType) {
470 case THREAD_ACTION:
471 action_thread.threadActions(this);
472 break;
473
474 case THREAD_CANCEL:
475 cancel_thread.threadActions(this);
476 break;
477
478 default: {
479 // no iterators here because theme reloading might kill our object
480 const size_t cnt = mActions.size();
481 for (size_t i = 0; i < cnt; ++i)
482 doAction(mActions[i]);
483 }
thatc6085482015-01-09 22:12:43 +0100484 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200486 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487}
488
that3f7b1ac2014-12-30 11:30:13 +0100489int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400490{
that3f7b1ac2014-12-30 11:30:13 +0100491 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492
that3f7b1ac2014-12-30 11:30:13 +0100493 std::string function = gui_parse_text(action.mFunction);
494 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495
that3f7b1ac2014-12-30 11:30:13 +0100496 // find function and execute it
497 mapFunc::const_iterator funcitr = mf.find(function);
498 if (funcitr != mf.end())
499 return (this->*funcitr->second)(arg);
500
501 LOGERR("Unknown action '%s'\n", function.c_str());
502 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400503}
504
505void GUIAction::operation_start(const string operation_name)
506{
that3f7b1ac2014-12-30 11:30:13 +0100507 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000508 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 DataManager::SetValue(TW_ACTION_BUSY, 1);
510 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100511 DataManager::SetValue("ui_portion_size", 0);
512 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600515 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400516 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500517 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400518}
519
that3f7b1ac2014-12-30 11:30:13 +0100520void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400521{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000522 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 DataManager::SetValue("ui_progress", 100);
525 if (simulate) {
526 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
527 if (simulate_fail != 0)
528 DataManager::SetValue("tw_operation_status", 1);
529 else
530 DataManager::SetValue("tw_operation_status", 0);
531 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500532 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400533 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500534 }
535 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400536 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500537 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400538 }
539 DataManager::SetValue("tw_operation_state", 1);
540 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500541 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200542 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000543 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400544
545#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000546 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600547 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400548#endif
549
that3f7b1ac2014-12-30 11:30:13 +0100550 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400551}
552
that3f7b1ac2014-12-30 11:30:13 +0100553int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400554{
that3f7b1ac2014-12-30 11:30:13 +0100555 sync();
556 DataManager::SetValue("tw_gui_done", 1);
557 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400558
that3f7b1ac2014-12-30 11:30:13 +0100559 return 0;
560}
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500562int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100563{
that3f7b1ac2014-12-30 11:30:13 +0100564 gui_changePage("main");
565 return 0;
566}
Dees_Troy51a0e822012-09-05 15:24:24 -0400567
that3f7b1ac2014-12-30 11:30:13 +0100568int GUIAction::key(std::string arg)
569{
570 const int key = getKeyByName(arg);
571 PageManager::NotifyKey(key, true);
572 PageManager::NotifyKey(key, false);
573 return 0;
574}
575
576int GUIAction::page(std::string arg)
577{
LuK133762326f42015-09-30 19:57:46 +0200578 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100579 std::string page_name = gui_parse_text(arg);
580 return gui_changePage(page_name);
581}
582
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500583int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100584{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500585 PageManager::RequestReload();
586 // The actual reload is handled in pages.cpp in PageManager::RunReload()
587 // The reload will occur on the next Update or Render call and will
588 // be performed in the rendoer thread instead of the action thread
589 // to prevent crashing which could occur when we start deleting
590 // GUI resources in the action thread while we attempt to render
591 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100592 return 0;
593}
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500595int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100596{
597 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400598
that3f7b1ac2014-12-30 11:30:13 +0100599 DataManager::GetValue("tw_restore", Restore_Name);
600 PartitionManager.Set_Restore_Files(Restore_Name);
601 return 0;
602}
603
604int GUIAction::set(std::string arg)
605{
606 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607 {
that3f7b1ac2014-12-30 11:30:13 +0100608 string varName = arg.substr(0, arg.find('='));
609 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400610
that3f7b1ac2014-12-30 11:30:13 +0100611 DataManager::GetValue(value, value);
612 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200613 }
that3f7b1ac2014-12-30 11:30:13 +0100614 else
615 DataManager::SetValue(arg, "1");
616 return 0;
617}
Dees_Troy51a0e822012-09-05 15:24:24 -0400618
that3f7b1ac2014-12-30 11:30:13 +0100619int GUIAction::clear(std::string arg)
620{
621 DataManager::SetValue(arg, "0");
622 return 0;
623}
Dees_Troy51a0e822012-09-05 15:24:24 -0400624
that3f7b1ac2014-12-30 11:30:13 +0100625int GUIAction::mount(std::string arg)
626{
627 if (arg == "usb") {
628 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400629 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100630 PartitionManager.usb_storage_enable();
631 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500632 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100633 } else if (!simulate) {
634 PartitionManager.Mount_By_Path(arg, true);
635 PartitionManager.Add_MTP_Storage(arg);
636 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500637 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100638 return 0;
639}
640
641int GUIAction::unmount(std::string arg)
642{
643 if (arg == "usb") {
644 if (!simulate)
645 PartitionManager.usb_storage_disable();
646 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500647 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100648 DataManager::SetValue(TW_ACTION_BUSY, 0);
649 } else if (!simulate) {
650 PartitionManager.UnMount_By_Path(arg, true);
651 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500652 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100653 return 0;
654}
655
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500656int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100657{
658 operation_start("Restore Defaults");
659 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500660 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100661 else {
662 DataManager::ResetDefaults();
663 PartitionManager.Update_System_Details();
664 PartitionManager.Mount_Current_Storage(true);
665 }
666 operation_end(0);
667 return 0;
668}
669
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500670int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100671{
672 operation_start("Copy Log");
673 if (!simulate)
674 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400675 string dst, curr_storage;
676 int copy_kernel_log = 0;
677
678 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100679 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400680 curr_storage = DataManager::GetCurrentStoragePath();
681 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100682 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
683 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400684 if (copy_kernel_log)
685 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100686 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400687 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100688 } else
689 simulate_progress_bar();
690 operation_end(0);
691 return 0;
692}
693
694
695int GUIAction::compute(std::string arg)
696{
697 if (arg.find("+") != string::npos)
698 {
699 string varName = arg.substr(0, arg.find('+'));
700 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
701 int amount_to_add = atoi(string_to_add.c_str());
702 int value;
703
704 DataManager::GetValue(varName, value);
705 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400706 return 0;
707 }
that3f7b1ac2014-12-30 11:30:13 +0100708 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 {
that3f7b1ac2014-12-30 11:30:13 +0100710 string varName = arg.substr(0, arg.find('-'));
711 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
712 int amount_to_subtract = atoi(string_to_subtract.c_str());
713 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400714
that3f7b1ac2014-12-30 11:30:13 +0100715 DataManager::GetValue(varName, value);
716 value -= amount_to_subtract;
717 if (value <= 0)
718 value = 0;
719 DataManager::SetValue(varName, value);
720 return 0;
721 }
722 if (arg.find("*") != string::npos)
723 {
724 string varName = arg.substr(0, arg.find('*'));
725 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
726 int multiply_by = atoi(multiply_by_str.c_str());
727 int value;
728
729 DataManager::GetValue(varName, value);
730 DataManager::SetValue(varName, value*multiply_by);
731 return 0;
732 }
733 if (arg.find("/") != string::npos)
734 {
735 string varName = arg.substr(0, arg.find('/'));
736 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
737 int divide_by = atoi(divide_by_str.c_str());
738 int value;
739
Matt Mowera8a89d12016-12-30 18:10:37 -0600740 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100741 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100743 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 }
745 return 0;
746 }
that3f7b1ac2014-12-30 11:30:13 +0100747 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
748 return -1;
749}
Dees_Troy51a0e822012-09-05 15:24:24 -0400750
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500751int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100752{
753 string SelectedZone;
754 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
755 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
756 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
757
758 int dst;
759 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
760
761 string offset;
762 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
763
764 string NewTimeZone = Zone;
765 if (offset != "0")
766 NewTimeZone += ":" + offset;
767
768 if (dst != 0)
769 NewTimeZone += DSTZone;
770
771 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
772 DataManager::update_tz_environment_variables();
773 return 0;
774}
775
776int GUIAction::overlay(std::string arg)
777{
778 return gui_changeOverlay(arg);
779}
780
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500781int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100782{
783 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500784 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 return 0;
786 }
that3f7b1ac2014-12-30 11:30:13 +0100787 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
788 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
789 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100791 }
792 return 0;
793}
794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500795int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100796{
797 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500798 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400799 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100800 } else {
801 zip_queue_index--;
802 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400803 }
that3f7b1ac2014-12-30 11:30:13 +0100804 return 0;
805}
Dees_Troy51a0e822012-09-05 15:24:24 -0400806
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500807int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100808{
809 zip_queue_index = 0;
810 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
811 return 0;
812}
Dees_Troy51a0e822012-09-05 15:24:24 -0400813
that3f7b1ac2014-12-30 11:30:13 +0100814int GUIAction::sleep(std::string arg)
815{
816 operation_start("Sleep");
817 usleep(atoi(arg.c_str()));
818 operation_end(0);
819 return 0;
820}
Dees Troyb21cc642013-09-10 17:36:41 +0000821
Matt Mower9a2a2052016-05-31 21:31:22 -0500822int GUIAction::sleepcounter(std::string arg)
823{
824 operation_start("SleepCounter");
825 // Ensure user notices countdown in case it needs to be cancelled
826 blankTimer.resetTimerAndUnblank();
827 int total = atoi(arg.c_str());
828 for (int t = total; t > 0; t--) {
829 int progress = (int)(((float)(total-t)/(float)total)*100.0);
830 DataManager::SetValue("ui_progress", progress);
831 ::sleep(1);
832 DataManager::SetValue("tw_sleep", t-1);
833 }
834 DataManager::SetValue("ui_progress", 100);
835 operation_end(0);
836 return 0;
837}
838
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500839int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100840{
841 operation_start("AppendDateToBackupName");
842 string Backup_Name;
843 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
844 Backup_Name += TWFunc::Get_Current_Date();
845 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
846 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
847 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500848 PageManager::NotifyKey(KEY_END, true);
849 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100850 operation_end(0);
851 return 0;
852}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500853
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500854int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100855{
856 operation_start("GenerateBackupName");
857 TWFunc::Auto_Generate_Backup_Name();
858 operation_end(0);
859 return 0;
860}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500861
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100863{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600864 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100865 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500866
Ethan Yonker483e9f42016-01-11 22:21:18 -0600867 if (arg.empty())
868 arg = "tw_wipe_list";
869 DataManager::GetValue(arg, List);
870 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
871 if (!List.empty()) {
872 size_t start_pos = 0, end_pos = List.find(";", start_pos);
873 while (end_pos != string::npos && start_pos < List.size()) {
874 part_path = List.substr(start_pos, end_pos - start_pos);
875 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
876 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100877 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400878 } else {
that3f7b1ac2014-12-30 11:30:13 +0100879 count++;
880 }
881 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600882 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100883 }
884 DataManager::SetValue("tw_check_partition_list", count);
885 } else {
886 DataManager::SetValue("tw_check_partition_list", 0);
887 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600888 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100889}
Dees_Troy51a0e822012-09-05 15:24:24 -0400890
Ethan Yonker483e9f42016-01-11 22:21:18 -0600891int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100892{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600893 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400894
Ethan Yonker483e9f42016-01-11 22:21:18 -0600895 if (arg.empty())
896 arg = "tw_wipe_list";
897 DataManager::GetValue(arg, List);
898 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
899 if (!List.empty()) {
900 size_t start_pos = 0, end_pos = List.find(";", start_pos);
901 part_path = List;
902 while (end_pos != string::npos && start_pos < List.size()) {
903 part_path = List.substr(start_pos, end_pos - start_pos);
904 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
905 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100906 // Do nothing
907 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600908 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100909 break;
910 }
911 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600912 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100913 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600914 if (!part_path.empty()) {
915 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100916 if (Part) {
917 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500918
that3f7b1ac2014-12-30 11:30:13 +0100919 DataManager::SetValue("tw_partition_name", Part->Display_Name);
920 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
921 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
922 DataManager::SetValue("tw_partition_size", Part->Size / mb);
923 DataManager::SetValue("tw_partition_used", Part->Used / mb);
924 DataManager::SetValue("tw_partition_free", Part->Free / mb);
925 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
926 DataManager::SetValue("tw_partition_removable", Part->Removable);
927 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
928
929 if (Part->Can_Repair())
930 DataManager::SetValue("tw_partition_can_repair", 1);
931 else
932 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500933 if (Part->Can_Resize())
934 DataManager::SetValue("tw_partition_can_resize", 1);
935 else
936 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400937 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100938 DataManager::SetValue("tw_partition_vfat", 1);
939 else
940 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400941 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100942 DataManager::SetValue("tw_partition_exfat", 1);
943 else
944 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100945 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100946 DataManager::SetValue("tw_partition_f2fs", 1);
947 else
948 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400949 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100950 DataManager::SetValue("tw_partition_ext", 1);
951 else
952 DataManager::SetValue("tw_partition_ext", 0);
953 return 0;
954 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600955 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100956 }
957 }
958 }
959 DataManager::SetValue("tw_partition_name", "");
960 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600961 // Set this to 0 to prevent trying to partition this device, just in case
962 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100963 return 0;
964}
965
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500966int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100967{
968 time_t tm;
969 char path[256];
970 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600971 uid_t uid = AID_MEDIA_RW;
972 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100973
974 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600975 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100976 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
977 } else {
978 strcpy(path, "/tmp/");
979 }
980
Matt Mowera8a89d12016-12-30 18:10:37 -0600981 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100982 return 0;
983
984 tm = time(NULL);
985 path_len = strlen(path);
986
987 // Screenshot_2014-01-01-18-21-38.png
988 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
989
990 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600991 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100992 chmod(path, 0666);
993 chown(path, uid, gid);
994
that677b13f2015-12-26 23:57:31 +0100995 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100996
VDavid0032034a412019-10-18 22:43:20 +0200997 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100998 gr_color(255, 255, 255, 255);
999 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
1000 gr_flip();
1001 gui_forceRender();
1002 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001003 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +01001004 }
1005 return 0;
1006}
1007
1008int GUIAction::setbrightness(std::string arg)
1009{
1010 return TWFunc::Set_Brightness(arg);
1011}
1012
1013int GUIAction::fileexists(std::string arg)
1014{
1015 struct stat st;
1016 string newpath = arg + "/.";
1017
1018 operation_start("FileExists");
1019 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1020 operation_end(0);
1021 else
1022 operation_end(1);
1023 return 0;
1024}
1025
thatcc8ddca2015-01-03 01:59:36 +01001026void GUIAction::reinject_after_flash()
1027{
1028 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001029 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001030 if (simulate) {
1031 simulate_progress_bar();
1032 } else {
1033 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1034 if (Boot == NULL || Boot->Current_File_System != "emmc")
1035 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1036 else {
1037 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1038 TWFunc::Exec_Cmd(injectcmd);
1039 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001040 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001041 }
1042 }
1043}
1044
mauronofrio6d3bf892019-10-26 19:47:55 +02001045int GUIAction::ozip_decrypt(string zip_path)
1046{
bigbiffad58e1b2020-07-06 20:24:34 -04001047 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001048 return 1;
1049 }
1050 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1051 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1052 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1053 return 0;
1054}
1055
that3f7b1ac2014-12-30 11:30:13 +01001056int GUIAction::flash(std::string arg)
1057{
1058 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001059 // We're going to jump to this page first, like a loading page
1060 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001061 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001062 string zip_path = zip_queue[i];
1063 size_t slashpos = zip_path.find_last_of('/');
1064 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001065 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001066 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1067 {
1068 if((ozip_decrypt(zip_path)) != 0)
1069 {
1070 LOGERR("Unable to find ozip_decrypt!");
1071 break;
1072 }
1073 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1074 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1075 if (!TWFunc::Path_Exists(zip_path)) {
1076 LOGERR("Unable to find decrypted zip");
1077 break;
1078 }
1079 }
thatc7b631b2015-06-01 23:36:57 +02001080 DataManager::SetValue("tw_filename", zip_path);
1081 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001082 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1083
1084 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001085 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001086 TWFunc::SetPerformanceMode(false);
1087 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001088 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001089 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001090 break;
that3f7b1ac2014-12-30 11:30:13 +01001091 }
1092 }
1093 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001094
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001095 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001096 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001097 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001098 }
that3f7b1ac2014-12-30 11:30:13 +01001099
thatcc8ddca2015-01-03 01:59:36 +01001100 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001101 PartitionManager.Update_System_Details();
1102 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001103 // 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 -06001104 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001105 return 0;
1106}
1107
1108int GUIAction::wipe(std::string arg)
1109{
1110 operation_start("Format");
1111 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001112 int ret_val = false;
1113
1114 if (simulate) {
1115 simulate_progress_bar();
1116 } else {
1117 if (arg == "data")
1118 ret_val = PartitionManager.Factory_Reset();
1119 else if (arg == "battery")
1120 ret_val = PartitionManager.Wipe_Battery_Stats();
1121 else if (arg == "rotate")
1122 ret_val = PartitionManager.Wipe_Rotate_Data();
1123 else if (arg == "dalvik")
1124 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1125 else if (arg == "DATAMEDIA") {
1126 ret_val = PartitionManager.Format_Data();
1127 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001128 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001129
1130 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1131 if (has_datamedia) {
1132 ret_val = PartitionManager.Wipe_Media_From_Data();
1133 } else {
1134 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1135 }
1136 } else if (arg == "EXTERNAL") {
1137 string External_Path;
1138
1139 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1140 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1141 } else if (arg == "ANDROIDSECURE") {
1142 ret_val = PartitionManager.Wipe_Android_Secure();
1143 } else if (arg == "LIST") {
1144 string Wipe_List, wipe_path;
1145 bool skip = false;
1146 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001147
1148 DataManager::GetValue("tw_wipe_list", Wipe_List);
1149 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1150 if (!Wipe_List.empty()) {
1151 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1152 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1153 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1154 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1155 if (wipe_path == "/and-sec") {
1156 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001157 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001158 ret_val = false;
1159 break;
1160 } else {
1161 skip = true;
1162 }
1163 } else if (wipe_path == "DALVIK") {
1164 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001165 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001166 ret_val = false;
1167 break;
1168 } else {
1169 skip = true;
1170 }
1171 } else if (wipe_path == "INTERNAL") {
1172 if (!PartitionManager.Wipe_Media_From_Data()) {
1173 ret_val = false;
1174 break;
1175 } else {
1176 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001177 }
1178 }
that3f7b1ac2014-12-30 11:30:13 +01001179 if (!skip) {
1180 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001181 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001182 ret_val = false;
1183 break;
1184 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1185 arg = wipe_path;
1186 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 } else {
that3f7b1ac2014-12-30 11:30:13 +01001188 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001189 }
that3f7b1ac2014-12-30 11:30:13 +01001190 start_pos = end_pos + 1;
1191 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001192 }
1193 }
that3f7b1ac2014-12-30 11:30:13 +01001194 } else
1195 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001196#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001197 if (arg == DataManager::GetSettingsStoragePath()) {
1198 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1199 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001200
that3f7b1ac2014-12-30 11:30:13 +01001201 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1202 LOGINFO("Making TWRP folder and saving settings.\n");
1203 Storage_Path += "/TWRP";
1204 mkdir(Storage_Path.c_str(), 0777);
1205 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001206 } else {
that3f7b1ac2014-12-30 11:30:13 +01001207 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1208 }
1209 }
1210#endif
1211 }
1212 PartitionManager.Update_System_Details();
1213 if (ret_val)
1214 ret_val = 0; // 0 is success
1215 else
1216 ret_val = 1; // 1 is failure
1217 operation_end(ret_val);
1218 return 0;
1219}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001220
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001221int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001222{
1223 operation_start("Refreshing Sizes");
1224 if (simulate) {
1225 simulate_progress_bar();
1226 } else
1227 PartitionManager.Update_System_Details();
1228 operation_end(0);
1229 return 0;
1230}
1231
1232int GUIAction::nandroid(std::string arg)
1233{
that3f7b1ac2014-12-30 11:30:13 +01001234 if (simulate) {
that73a52952015-01-28 23:07:34 +01001235 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001236 DataManager::SetValue("tw_partition", "Simulation");
1237 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001238 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001239 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001240 operation_start("Nandroid");
1241 int ret = 0;
1242
that3f7b1ac2014-12-30 11:30:13 +01001243 if (arg == "backup") {
1244 string Backup_Name;
1245 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001246 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001247 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 -05001248 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001249 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1250 if (!PartitionManager.stop_backup.get_value()) {
1251 if (ret == false)
1252 ret = 1; // 1 for failure
1253 else
1254 ret = 0; // 0 for success
1255 DataManager::SetValue("tw_cancel_backup", 0);
1256 } else {
1257 DataManager::SetValue("tw_cancel_backup", 1);
1258 gui_msg("backup_cancel=Backup Cancelled");
1259 ret = 0;
1260 }
bigbiffce8f83c2015-12-12 18:30:21 -05001261 } else {
that3f7b1ac2014-12-30 11:30:13 +01001262 operation_end(1);
1263 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001264 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001265 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001266 } else if (arg == "restore") {
1267 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001268 int gui_adb_backup;
1269
that3f7b1ac2014-12-30 11:30:13 +01001270 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001271 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1272 if (gui_adb_backup) {
1273 DataManager::SetValue("tw_operation_state", 1);
1274 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1275 ret = 0; // success
1276 else
1277 ret = 1; // failure
1278 DataManager::SetValue("tw_enable_adb_backup", 0);
1279 ret = 0; // assume success???
1280 } else {
1281 if (PartitionManager.Run_Restore(Restore_Name))
1282 ret = 0; // success
1283 else
1284 ret = 1; // failure
1285 }
that3f7b1ac2014-12-30 11:30:13 +01001286 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001287 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001288 return -1;
1289 }
that73a52952015-01-28 23:07:34 +01001290 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001291 return ret;
1292 }
1293 return 0;
1294}
1295
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001296int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001297 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001298 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001299 }
1300 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001301 int op_status = PartitionManager.Cancel_Backup();
1302 if (op_status != 0)
1303 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001304 }
1305
1306 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001307}
Dees_Troy51a0e822012-09-05 15:24:24 -04001308
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001309int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001310{
that73a52952015-01-28 23:07:34 +01001311 int op_status = 0;
1312
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001313 operation_start("Fix Contexts");
1314 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001315 if (simulate) {
1316 simulate_progress_bar();
1317 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001318 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001319 if (op_status != 0)
1320 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001321 }
that73a52952015-01-28 23:07:34 +01001322 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001323 return 0;
1324}
1325
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001326int GUIAction::fixpermissions(std::string arg)
1327{
1328 return fixcontexts(arg);
1329}
1330
that3f7b1ac2014-12-30 11:30:13 +01001331int GUIAction::dd(std::string arg)
1332{
1333 operation_start("imaging");
1334
1335 if (simulate) {
1336 simulate_progress_bar();
1337 } else {
1338 string cmd = "dd " + arg;
1339 TWFunc::Exec_Cmd(cmd);
1340 }
1341 operation_end(0);
1342 return 0;
1343}
1344
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001345int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001346{
1347 operation_start("Partition SD Card");
1348 int ret_val = 0;
1349
1350 if (simulate) {
1351 simulate_progress_bar();
1352 } else {
1353 int allow_partition;
1354 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1355 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001356 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001357 } else {
1358 if (!PartitionManager.Partition_SDCard())
1359 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001360 }
that3f7b1ac2014-12-30 11:30:13 +01001361 }
1362 operation_end(ret_val);
1363 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001364
that3f7b1ac2014-12-30 11:30:13 +01001365}
Dees_Troy51a0e822012-09-05 15:24:24 -04001366
that3f7b1ac2014-12-30 11:30:13 +01001367int GUIAction::cmd(std::string arg)
1368{
1369 int op_status = 0;
1370
1371 operation_start("Command");
1372 LOGINFO("Running command: '%s'\n", arg.c_str());
1373 if (simulate) {
1374 simulate_progress_bar();
1375 } else {
1376 op_status = TWFunc::Exec_Cmd(arg);
1377 if (op_status != 0)
1378 op_status = 1;
1379 }
1380
1381 operation_end(op_status);
1382 return 0;
1383}
1384
1385int GUIAction::terminalcommand(std::string arg)
1386{
1387 int op_status = 0;
1388 string cmdpath, command;
1389
1390 DataManager::GetValue("tw_terminal_location", cmdpath);
1391 operation_start("CommandOutput");
1392 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1393 if (simulate) {
1394 simulate_progress_bar();
1395 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001396 } else if (arg == "exit") {
1397 LOGINFO("Exiting terminal\n");
1398 operation_end(op_status);
1399 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001400 } else {
1401 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1402 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001403 DataManager::SetValue("tw_terminal_state", 1);
1404 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001405 FILE* fp;
1406 char line[512];
1407
1408 fp = popen(command.c_str(), "r");
1409 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001411 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001412 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001413 struct timeval timeout;
1414 fd_set fdset;
1415
Matt Mowera8a89d12016-12-30 18:10:37 -06001416 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001417 {
1418 FD_ZERO(&fdset);
1419 FD_SET(fd, &fdset);
1420 timeout.tv_sec = 0;
1421 timeout.tv_usec = 400000;
1422 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1423 if (has_data == 0) {
1424 // Timeout reached
1425 DataManager::GetValue("tw_terminal_state", check);
1426 if (check == 0) {
1427 keep_going = 0;
1428 }
1429 } else if (has_data < 0) {
1430 // End of execution
1431 keep_going = 0;
1432 } else {
1433 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001434 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001435 gui_print("%s", line); // Display output
1436 else
1437 keep_going = 0; // Done executing
1438 }
1439 }
1440 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001441 }
thatc6085482015-01-09 22:12:43 +01001442 DataManager::SetValue("tw_operation_status", 0);
1443 DataManager::SetValue("tw_operation_state", 1);
1444 DataManager::SetValue("tw_terminal_state", 0);
1445 DataManager::SetValue("tw_background_thread_running", 0);
1446 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001447 }
1448 return 0;
1449}
1450
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001451int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001452{
that3f7b1ac2014-12-30 11:30:13 +01001453 LOGINFO("Sending kill command...\n");
1454 operation_start("KillCommand");
1455 DataManager::SetValue("tw_operation_status", 0);
1456 DataManager::SetValue("tw_operation_state", 1);
1457 DataManager::SetValue("tw_terminal_state", 0);
1458 DataManager::SetValue("tw_background_thread_running", 0);
1459 DataManager::SetValue(TW_ACTION_BUSY, 0);
1460 return 0;
1461}
1462
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001463int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001464{
1465 int op_status = 0;
1466 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001467 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001468 if (simulate) {
1469 simulate_progress_bar();
1470 } else {
1471 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001472 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001473 }
1474
1475 operation_end(op_status);
1476 return 0;
1477}
1478
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001479int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001480{
1481 int op_status = 0;
1482
1483 operation_start("CheckBackupName");
1484 if (simulate) {
1485 simulate_progress_bar();
1486 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001487 string Backup_Name;
1488 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1489 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001490 if (op_status != 0)
1491 op_status = 1;
1492 }
1493
1494 operation_end(op_status);
1495 return 0;
1496}
1497
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001498int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001499{
1500 int op_status = 0;
1501
1502 operation_start("Decrypt");
1503 if (simulate) {
1504 simulate_progress_bar();
1505 } else {
1506 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001507 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001508 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001509
1510 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1511 DataManager::GetValue("tw_crypto_user_id", userID);
1512 if (userID != "") {
1513 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1514 if (userID != "0") {
1515 if (op_status != 0)
1516 op_status = 1;
1517 operation_end(op_status);
1518 return 0;
1519 }
1520 } else {
1521 LOGINFO("User ID not found\n");
1522 op_status = 1;
1523 }
1524 ::sleep(1);
1525 } else { // for FDE
1526 op_status = PartitionManager.Decrypt_Device(Password);
1527 }
1528
that3f7b1ac2014-12-30 11:30:13 +01001529 if (op_status != 0)
1530 op_status = 1;
1531 else {
that3f7b1ac2014-12-30 11:30:13 +01001532 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1533
Ethan Yonkercf50da52015-01-12 21:59:07 -06001534 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001535
Ethan Yonkercf50da52015-01-12 21:59:07 -06001536 // Check for a custom theme and load it if exists
1537 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1538 if (has_datamedia != 0) {
1539 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001540 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001541 } else {
1542 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001543 }
1544 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001545 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001546 }
1547 }
1548
1549 operation_end(op_status);
1550 return 0;
1551}
1552
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001553int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001554{
1555 operation_start("Sideload");
1556 if (simulate) {
1557 simulate_progress_bar();
1558 operation_end(0);
1559 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001560 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001561 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1562
1563 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001564 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001565 int ret = twrp_sideload("/", &reboot_action);
1566 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001567 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1568
1569 if (ret != 0) {
1570 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001571 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001572 ret = 1; // failure
1573 } else {
1574 int wipe_cache = 0;
1575 int wipe_dalvik = 0;
1576 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001577 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1578 PartitionManager.Wipe_By_Path("/cache");
1579 if (wipe_dalvik)
1580 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001581 }
thatc6085482015-01-09 22:12:43 +01001582 TWFunc::Toggle_MTP(mtp_was_enabled);
1583 reinject_after_flash();
1584 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001585 }
that3f7b1ac2014-12-30 11:30:13 +01001586 return 0;
1587}
1588
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001589int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001590{
that3f7b1ac2014-12-30 11:30:13 +01001591 struct stat st;
1592 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001593 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001594 LOGINFO("Signaling child sideload process to exit.\n");
1595 // Calling stat() on this magic filename signals the minadbd
1596 // subprocess to shut down.
1597 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001598 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001599 if (!sideload_child_pid) {
1600 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001601 return 0;
1602 }
thatcc8ddca2015-01-03 01:59:36 +01001603 ::sleep(1);
1604 LOGINFO("Killing child sideload process.\n");
1605 kill(sideload_child_pid, SIGTERM);
1606 int status;
1607 LOGINFO("Waiting for child sideload process to exit.\n");
1608 waitpid(sideload_child_pid, &status, 0);
1609 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001610 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1611 return 0;
1612}
1613
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001614int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001615{
1616 operation_start("OpenRecoveryScript");
1617 if (simulate) {
1618 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001619 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001620 } else {
that10ae24f2015-12-26 20:53:51 +01001621 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001622 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001623 }
1624 return 0;
1625}
1626
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001627int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001628{
1629 int op_status = 0;
1630
1631 operation_start("Install SuperSU");
1632 if (simulate) {
1633 simulate_progress_bar();
1634 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001635 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001636 }
1637
1638 operation_end(op_status);
1639 return 0;
1640}
1641
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001642int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001643{
1644 int op_status = 0;
1645
1646 operation_start("Fixing Superuser Permissions");
1647 if (simulate) {
1648 simulate_progress_bar();
1649 } else {
1650 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1651 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1652 }
1653
1654 operation_end(op_status);
1655 return 0;
1656}
1657
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001658int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001659{
1660 int op_status = 0;
1661
1662 operation_start("Try Restore Decrypt");
1663 if (simulate) {
1664 simulate_progress_bar();
1665 } else {
1666 string Restore_Path, Filename, Password;
1667 DataManager::GetValue("tw_restore", Restore_Path);
1668 Restore_Path += "/";
1669 DataManager::GetValue("tw_restore_password", Password);
1670 TWFunc::SetPerformanceMode(true);
1671 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1672 op_status = 0; // success
1673 else
1674 op_status = 1; // fail
1675 TWFunc::SetPerformanceMode(false);
1676 }
1677
1678 operation_end(op_status);
1679 return 0;
1680}
1681
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001682int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001683{
1684 int op_status = 0;
1685
1686 operation_start("Repair Partition");
1687 if (simulate) {
1688 simulate_progress_bar();
1689 } else {
1690 string part_path;
1691 DataManager::GetValue("tw_partition_mount_point", part_path);
1692 if (PartitionManager.Repair_By_Path(part_path, true)) {
1693 op_status = 0; // success
1694 } else {
that3f7b1ac2014-12-30 11:30:13 +01001695 op_status = 1; // fail
1696 }
1697 }
1698
1699 operation_end(op_status);
1700 return 0;
1701}
1702
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001703int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001704{
1705 int op_status = 0;
1706
1707 operation_start("Resize Partition");
1708 if (simulate) {
1709 simulate_progress_bar();
1710 } else {
1711 string part_path;
1712 DataManager::GetValue("tw_partition_mount_point", part_path);
1713 if (PartitionManager.Resize_By_Path(part_path, true)) {
1714 op_status = 0; // success
1715 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001716 op_status = 1; // fail
1717 }
1718 }
1719
1720 operation_end(op_status);
1721 return 0;
1722}
1723
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001724int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001725{
1726 int op_status = 0;
1727
1728 operation_start("Change File System");
1729 if (simulate) {
1730 simulate_progress_bar();
1731 } else {
1732 string part_path, file_system;
1733 DataManager::GetValue("tw_partition_mount_point", part_path);
1734 DataManager::GetValue("tw_action_new_file_system", file_system);
1735 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1736 op_status = 0; // success
1737 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001738 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001739 op_status = 1; // fail
1740 }
1741 }
1742 PartitionManager.Update_System_Details();
1743 operation_end(op_status);
1744 return 0;
1745}
1746
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001747int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001748{
1749 int op_status = 0;
1750
1751 operation_start("Start MTP");
1752 if (PartitionManager.Enable_MTP())
1753 op_status = 0; // success
1754 else
1755 op_status = 1; // fail
1756
1757 operation_end(op_status);
1758 return 0;
1759}
1760
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001761int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001762{
1763 int op_status = 0;
1764
1765 operation_start("Stop MTP");
1766 if (PartitionManager.Disable_MTP())
1767 op_status = 0; // success
1768 else
1769 op_status = 1; // fail
1770
1771 operation_end(op_status);
1772 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001773}
1774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001775int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001776{
1777 int op_status = 0;
epicX8f52c0a2021-02-24 23:12:08 +05301778 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001779
1780 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001781 string path, filename;
1782 DataManager::GetValue("tw_zip_location", path);
1783 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001784
epicX8f52c0a2021-02-24 23:12:08 +05301785#ifdef AB_OTA_UPDATER
1786 string target = DataManager::GetStrValue("tw_flash_partition");
1787 unsigned int pos = target.find_last_of(';');
1788 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1789 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1790 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1791
1792 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1793 {
1794 string current_slot = PartitionManager.Get_Active_Slot_Display();
1795 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1796
1797 PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A");
1798 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
1799 PartitionManager.Set_Active_Slot(current_slot);
1800
1801 DataManager::SetValue("tw_flash_both_slots", 0);
1802 flag = false;
1803 }
1804#endif
1805 if (flag)
1806 {
1807 if (PartitionManager.Flash_Image(path, filename))
1808 op_status = 0; // success
1809 else
1810 op_status = 1; // fail
1811 }
1812
Ethan Yonker96af84a2015-01-05 14:58:36 -06001813 operation_end(op_status);
1814 return 0;
1815}
1816
that10ae24f2015-12-26 20:53:51 +01001817int GUIAction::twcmd(std::string arg)
1818{
1819 operation_start("TWRP CLI Command");
1820 if (simulate)
1821 simulate_progress_bar();
1822 else
1823 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1824 operation_end(0);
1825 return 0;
1826}
1827
Dees_Troy51a0e822012-09-05 15:24:24 -04001828int GUIAction::getKeyByName(std::string key)
1829{
that8834a0f2016-01-05 23:29:30 +01001830 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001831 else if (key == "menu") return KEY_MENU;
1832 else if (key == "back") return KEY_BACK;
1833 else if (key == "search") return KEY_SEARCH;
1834 else if (key == "voldown") return KEY_VOLUMEDOWN;
1835 else if (key == "volup") return KEY_VOLUMEUP;
1836 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001837 int ret_val;
1838 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1839 if (!ret_val)
1840 return KEY_POWER;
1841 else
1842 return ret_val;
1843 }
1844
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001845 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001846}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001847
1848int GUIAction::checkpartitionlifetimewrites(std::string arg)
1849{
1850 int op_status = 0;
1851 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1852
1853 operation_start("Check Partition Lifetime Writes");
1854 if (sys) {
1855 if (sys->Check_Lifetime_Writes() != 0)
1856 DataManager::SetValue("tw_lifetime_writes", 1);
1857 else
1858 DataManager::SetValue("tw_lifetime_writes", 0);
1859 op_status = 0; // success
1860 } else {
1861 DataManager::SetValue("tw_lifetime_writes", 1);
1862 op_status = 1; // fail
1863 }
1864
1865 operation_end(op_status);
1866 return 0;
1867}
1868
1869int GUIAction::mountsystemtoggle(std::string arg)
1870{
1871 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001872 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001873 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001874
1875 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001876 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001877 op_status = 1; // fail
1878 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001879 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001880 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001881 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001882 DataManager::SetValue("tw_mount_system_ro", 0);
1883 Part->Change_Mount_Read_Only(false);
1884 } else {
1885 DataManager::SetValue("tw_mount_system_ro", 1);
1886 Part->Change_Mount_Read_Only(true);
1887 }
1888 if (remount_system) {
1889 Part->Mount(true);
1890 }
1891 op_status = 0; // success
1892 } else {
1893 op_status = 1; // fail
1894 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001895 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1896 if (Part) {
1897 if (arg == "0") {
1898 Part->Change_Mount_Read_Only(false);
1899 } else {
1900 Part->Change_Mount_Read_Only(true);
1901 }
1902 if (remount_vendor) {
1903 Part->Mount(true);
1904 }
1905 op_status = 0; // success
1906 } else {
1907 op_status = 1; // fail
1908 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001909 }
1910
1911 operation_end(op_status);
1912 return 0;
1913}
Ethan Yonker74db1572015-10-28 12:44:49 -05001914
1915int GUIAction::setlanguage(std::string arg __unused)
1916{
1917 int op_status = 0;
1918
1919 operation_start("Set Language");
1920 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1921 PageManager::RequestReload();
1922 op_status = 0; // success
1923
1924 operation_end(op_status);
1925 return 0;
1926}
Ethan Yonker1b190162016-12-05 15:25:19 -06001927
Matt Mower9472ba12016-01-20 18:12:47 -06001928int GUIAction::togglebacklight(std::string arg __unused)
1929{
1930 blankTimer.toggleBlank();
1931 return 0;
1932}
1933
Ethan Yonker1b190162016-12-05 15:25:19 -06001934int GUIAction::setbootslot(std::string arg)
1935{
1936 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001937 if (!simulate) {
1938 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1939 // PartitionManager failed to unmount /vendor, this should not happen,
1940 // but in case it does, do a lazy unmount
1941 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1942 umount2("/vendor", MNT_DETACH);
1943 PartitionManager.Set_Active_Slot(arg);
1944 } else {
1945 PartitionManager.Set_Active_Slot(arg);
1946 }
1947 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001948 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001949 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001950 operation_end(0);
1951 return 0;
1952}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001953
1954int GUIAction::checkforapp(std::string arg __unused)
1955{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001956 operation_start("Check for TWRP App");
1957 if (!simulate)
1958 {
epicX271bb3a2020-12-30 01:03:18 +05301959 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001960 } else
1961 simulate_progress_bar();
epicX271bb3a2020-12-30 01:03:18 +05301962
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001963 operation_end(0);
1964 return 0;
1965}
1966
1967int GUIAction::installapp(std::string arg __unused)
1968{
1969 int op_status = 1;
1970 operation_start("Install TWRP App");
1971 if (!simulate)
1972 {
1973 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1974 if (PartitionManager.Mount_By_Path("/data", true)) {
1975 string install_path = "/data/app";
1976 string context = "u:object_r:apk_data_file:s0";
1977 if (!TWFunc::Path_Exists(install_path)) {
1978 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1979 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1980 goto exit;
1981 }
1982 if (chown(install_path.c_str(), 1000, 1000)) {
1983 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1984 goto exit;
1985 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001986 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001987 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1988 goto exit;
1989 }
1990 }
1991 install_path += "/me.twrp.twrpapp-1";
1992 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1993 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1994 goto exit;
1995 }
1996 if (chown(install_path.c_str(), 1000, 1000)) {
1997 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1998 goto exit;
1999 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002000 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002001 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2002 goto exit;
2003 }
2004 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002005 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002006 LOGERR("Error copying apk file\n");
2007 goto exit;
2008 }
2009 if (chown(install_path.c_str(), 1000, 1000)) {
2010 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2011 goto exit;
2012 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002013 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002014 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2015 goto exit;
2016 }
2017 sync();
2018 sync();
2019 }
2020 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002021 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2022 string base_path = PartitionManager.Get_Android_Root_Path();
2023 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002024 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2025 string install_path = base_path + "/priv-app";
2026 string context = "u:object_r:system_file:s0";
2027 if (!TWFunc::Path_Exists(install_path))
2028 install_path = base_path + "/app";
2029 if (TWFunc::Path_Exists(install_path)) {
2030 install_path += "/twrpapp";
2031 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2032 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002033 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002034 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2035 goto exit;
2036 }
2037 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002038 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002039 LOGERR("Error copying apk file\n");
2040 goto exit;
2041 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002042 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002043 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2044 goto exit;
2045 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002046
2047 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2048 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002049 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002050 LOGERR("Error copying permission file\n");
2051 goto exit;
2052 }
2053 if (chown(permission_path.c_str(), 1000, 1000)) {
2054 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2055 goto exit;
2056 }
2057 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2058 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2059 goto exit;
2060 }
2061
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002062 sync();
2063 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002064 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002065 op_status = 0;
2066 } else {
2067 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2068 }
2069 }
2070 }
2071 }
2072 } else
2073 simulate_progress_bar();
2074exit:
epicX271bb3a2020-12-30 01:03:18 +05302075 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002076 operation_end(0);
2077 return 0;
2078}
Ethan Yonker53796e72019-01-11 22:49:52 -06002079
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002080int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2081{
2082 int op_status = 1;
2083 operation_start("Uninstall TWRP System App");
2084 if (!simulate)
2085 {
2086 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2087 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2088 if (!Part) {
2089 LOGERR("Unabled to find system partition.\n");
2090 goto exit;
2091 }
2092 if (!Part->UnMount(true)) {
2093 goto exit;
2094 }
2095 if (Mount_System_RO > 0) {
2096 DataManager::SetValue("tw_mount_system_ro", 0);
2097 Part->Change_Mount_Read_Only(false);
2098 }
2099 if (Part->Mount(true)) {
2100 string base_path = PartitionManager.Get_Android_Root_Path();
2101 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2102 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2103 string uninstall_path = base_path + "/priv-app";
2104 if (!TWFunc::Path_Exists(uninstall_path))
2105 uninstall_path = base_path + "/app";
2106 uninstall_path += "/twrpapp";
2107 if (TWFunc::Path_Exists(uninstall_path)) {
2108 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2109 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2110 sync();
2111 op_status = 0;
2112 DataManager::SetValue("tw_app_installed_in_system", 0);
2113 DataManager::SetValue("tw_app_install_status", 0);
2114 } else {
2115 LOGERR("Unable to remove TWRP app from system.\n");
2116 }
2117 } else {
2118 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2119 }
2120 }
2121 Part->UnMount(true);
2122 if (Mount_System_RO > 0) {
2123 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2124 Part->Change_Mount_Read_Only(true);
2125 }
2126 } else
2127 simulate_progress_bar();
2128exit:
epicX271bb3a2020-12-30 01:03:18 +05302129 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002130 operation_end(0);
2131 return 0;
2132}
2133
Ethan Yonker53796e72019-01-11 22:49:52 -06002134int GUIAction::repackimage(std::string arg __unused)
2135{
2136 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002137 twrpRepacker repacker;
2138
Ethan Yonker53796e72019-01-11 22:49:52 -06002139 operation_start("Repack Image");
2140 if (!simulate)
2141 {
2142 std::string path = DataManager::GetStrValue("tw_filename");
2143 Repack_Options_struct Repack_Options;
2144 Repack_Options.Disable_Verity = false;
2145 Repack_Options.Disable_Force_Encrypt = false;
2146 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2147 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2148 Repack_Options.Type = REPLACE_KERNEL;
2149 else
2150 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002151 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002152 goto exit;
2153 } else
2154 simulate_progress_bar();
2155 op_status = 0;
2156exit:
2157 operation_end(op_status);
2158 return 0;
2159}
2160
2161int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2162{
2163 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002164 twrpRepacker repacker;
2165
Ethan Yonker53796e72019-01-11 22:49:52 -06002166 operation_start("Repack Image");
2167 if (!simulate)
2168 {
bigbiffad58e1b2020-07-06 20:24:34 -04002169 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002170 LOGERR("Image repacking tool not present in this TWRP build!");
2171 goto exit;
2172 }
2173 DataManager::SetProgress(0);
2174 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2175 if (part)
2176 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2177 else {
2178 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2179 goto exit;
2180 }
bigbiffad58e1b2020-07-06 20:24:34 -04002181 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 -06002182 goto exit;
2183 DataManager::SetProgress(.25);
2184 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002185 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002186 if (TWFunc::Exec_Cmd(command) != 0) {
2187 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2188 goto exit;
2189 }
2190 std::string header_path = REPACK_ORIG_DIR;
2191 header_path += "header";
2192 if (TWFunc::Path_Exists(header_path)) {
2193 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";
2194 if (TWFunc::Exec_Cmd(command) != 0) {
2195 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2196 goto exit;
2197 }
2198 }
2199 DataManager::SetProgress(.5);
2200 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002201 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002202 if (TWFunc::Exec_Cmd(command) != 0) {
2203 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2204 goto exit;
2205 }
2206 DataManager::SetProgress(.75);
2207 std::string path = REPACK_ORIG_DIR;
2208 std::string file = "new-boot.img";
2209 DataManager::SetValue("tw_flash_partition", "/boot;");
2210 if (!PartitionManager.Flash_Image(path, file)) {
2211 LOGINFO("Error flashing new image\n");
2212 goto exit;
2213 }
2214 DataManager::SetProgress(1);
2215 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2216 } else
2217 simulate_progress_bar();
2218 op_status = 0;
2219exit:
2220 operation_end(op_status);
2221 return 0;
2222}
bigbiffdf8436b2020-08-30 16:22:34 -04002223
2224
2225int GUIAction::enableadb(std::string arg __unused) {
2226 android::base::SetProperty("sys.usb.config", "none");
2227 android::base::SetProperty("sys.usb.config", "adb");
2228 return 0;
2229}
2230
2231int GUIAction::enablefastboot(std::string arg __unused) {
2232 android::base::SetProperty("sys.usb.config", "none");
2233 android::base::SetProperty("sys.usb.config", "fastboot");
2234 return 0;
2235}
Mohd Faraz77bbeb02020-08-12 12:29:42 +00002236
2237int GUIAction::changeterminal(std::string arg) {
2238 bool res = true;
2239 std::string resp, cmd = "cd " + arg;
2240 DataManager::GetValue("tw_terminal_location", resp);
2241 if (arg.empty() && !resp.empty()) {
2242 cmd = "cd /";
2243 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2244 term->NotifyCharInput(cmd.at(iter));
2245 term->NotifyCharInput(13);
2246 DataManager::SetValue("tw_terminal_location", "");
2247 return 0;
2248 }
2249 if (term != NULL && !arg.empty()) {
2250 DataManager::SetValue("tw_terminal_location", arg);
2251 if (term->status()) {
2252 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2253 term->NotifyCharInput(cmd.at(iter));
2254 term->NotifyCharInput(13);
2255 }
2256 else if (chdir(arg.c_str()) != 0) {
2257 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2258 res = false;
2259 }
2260 }
2261 else {
2262 res = false;
2263 LOGINFO("Unable to switch to Terminal\n");
2264 }
2265 if (res)
2266 gui_changePage("terminalcommand");
2267 return 0;
2268}
Captain Throwback16dd81b2021-02-12 19:32:36 -05002269#ifndef TW_EXCLUDE_NANO
2270int GUIAction::editfile(std::string arg) {
2271 if (term != NULL) {
2272 for (uint8_t iter = 0; iter < arg.size(); iter++)
2273 term->NotifyCharInput(arg.at(iter));
2274 term->NotifyCharInput(13);
2275 }
2276 else
2277 LOGINFO("Unable to switch to Terminal\n");
2278 return 0;
2279}
2280#endif
epicXa721f952021-01-04 13:01:31 +05302281
2282int GUIAction::applycustomtwrpfolder(string arg __unused)
2283{
2284 operation_start("ChangingTWRPFolder");
2285 string storageFolder = DataManager::GetSettingsStoragePath();
2286 string newFolder = storageFolder + '/' + arg;
2287 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2288 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2289 bool ret = false;
2290
2291 if (TWFunc::Path_Exists(newFolder)) {
2292 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2293 } else {
2294 ret = true;
2295 }
2296
2297 if (newFolder != prevFolder && ret) {
2298 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2299 } else {
2300 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2301 }
2302
epicX11e90832021-03-01 00:02:57 +05302303 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicXa721f952021-01-04 13:01:31 +05302304
2305
2306 if (ret) {
2307 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2308 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2309 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2310 }
2311 operation_end((int)!ret);
2312 return 0;
2313}