blob: 4783937f92a159210c852227ee91801049d36134 [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"
bigbiffd58ba182020-03-23 10:02:29 -040055#include "install/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056};
bigbiffd58ba182020-03-23 10:02:29 -040057#include "set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010058#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 Farazd59a6042020-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 Farazd59a6042020-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);
224 ADD_ACTION(installhtcdumlock);
225 ADD_ACTION(htcdumlockrestoreboot);
226 ADD_ACTION(htcdumlockreflashrecovery);
227 ADD_ACTION(cmd);
228 ADD_ACTION(terminalcommand);
229 ADD_ACTION(reinjecttwrp);
230 ADD_ACTION(decrypt);
231 ADD_ACTION(adbsideload);
232 ADD_ACTION(openrecoveryscript);
233 ADD_ACTION(installsu);
234 ADD_ACTION(decrypt_backup);
235 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500236 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100237 ADD_ACTION(changefilesystem);
238 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100239 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600240 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600241 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500242 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600243 ADD_ACTION(repackimage);
244 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100245 }
246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600248 actions = FindNode(node, "actions");
249 if (actions) child = FindNode(actions, "action");
250 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 while (child)
255 {
256 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 attr = child->first_attribute("function");
259 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 action.mFunction = attr->value();
262 action.mArg = child->value();
263 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 child = child->next_sibling("action");
266 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600269 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 if (child)
271 {
272 attr = child->first_attribute("key");
273 if (attr)
274 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100275 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600276 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100277 {
278 const int key = getKeyByName(keys[i]);
279 mKeys[key] = false;
280 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 }
282 else
283 {
284 attr = child->first_attribute("x");
285 if (!attr) return;
286 mActionX = atol(attr->value());
287 attr = child->first_attribute("y");
288 if (!attr) return;
289 mActionY = atol(attr->value());
290 attr = child->first_attribute("w");
291 if (!attr) return;
292 mActionW = atol(attr->value());
293 attr = child->first_attribute("h");
294 if (!attr) return;
295 mActionH = atol(attr->value());
296 }
297 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
Matt Mower25036b72016-06-24 12:30:18 -0500300int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400301{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (state == TOUCH_RELEASE)
303 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400304
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400306}
307
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100308int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400309{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600311 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100312 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313
314 bool prevState = itr->second;
315 itr->second = down;
316
317 // If there is only one key for this action, wait for key up so it
318 // doesn't trigger with multi-key actions.
319 // Else, check if all buttons are pressed, then consume their release events
320 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600321 if (mKeys.size() == 1) {
322 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100323 doActions();
thatd4725ca2016-01-19 00:15:21 +0100324 return 0;
325 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600326 } else if (down) {
327 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
328 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100329 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100330 }
331
332 // Passed, all req buttons are pressed, reset them and consume release events
333 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600334 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 kb->ConsumeKeyRelease(itr->first);
336 itr->second = false;
337 }
338
339 doActions();
thatd4725ca2016-01-19 00:15:21 +0100340 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100341 }
342
thatd4725ca2016-01-19 00:15:21 +0100343 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
Vojtech Bocek07220562014-02-08 02:05:33 +0100346int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400347{
Vojtech Bocek07220562014-02-08 02:05:33 +0100348 GUIObject::NotifyVarChange(varName, value);
349
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100350 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200351 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600352 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200353 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400354
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200355 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356}
357
358void GUIAction::simulate_progress_bar(void)
359{
Ethan Yonker74db1572015-10-28 12:44:49 -0500360 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400361 for (int i = 0; i < 5; i++)
362 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500363 if (PartitionManager.stop_backup.get_value()) {
364 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600365 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500366 DataManager::SetValue("ui_progress", 0);
367 PartitionManager.stop_backup.set_value(0);
368 return;
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 usleep(500000);
371 DataManager::SetValue("ui_progress", i * 20);
372 }
373}
374
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600375int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400376{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200377 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
379 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100380 DataManager::SetValue("ui_portion_size", 0);
381 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 if (filename.empty())
384 {
385 LOGERR("No file specified.\n");
386 return -1;
387 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400388
Ethan Yonker9598c072016-01-15 21:54:34 -0600389 if (!TWFunc::Path_Exists(filename)) {
390 if (!PartitionManager.Mount_By_Path(filename, true)) {
391 return -1;
392 }
393 if (!TWFunc::Path_Exists(filename)) {
394 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
395 return -1;
396 }
397 }
Dees_Troy657c3092012-09-10 20:32:10 -0400398
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 if (simulate) {
400 simulate_progress_bar();
401 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400402 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
bigbiff1f9e4842020-10-31 11:33:15 -0400403 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400404
405 // Now, check if we need to ensure TWRP remains installed...
406 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400407 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 {
409 DataManager::SetValue("tw_operation", "Configuring TWRP");
410 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500411 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400412 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500414 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 }
416 }
417 }
418
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 // Done
420 DataManager::SetValue("ui_progress", 100);
421 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100422 DataManager::SetValue("ui_portion_size", 0);
423 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425}
426
that73a52952015-01-28 23:07:34 +0100427GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100428{
that73a52952015-01-28 23:07:34 +0100429 string func = gui_parse_text(action.mFunction);
430 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
431 if (needsThread) {
432 if (func == "cancelbackup")
433 return THREAD_CANCEL;
434 else
435 return THREAD_ACTION;
436 }
437 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100438}
439
Dees_Troy51a0e822012-09-05 15:24:24 -0400440int GUIAction::doActions()
441{
that3f7b1ac2014-12-30 11:30:13 +0100442 if (mActions.size() < 1)
443 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400444
that73a52952015-01-28 23:07:34 +0100445 // Determine in which thread to run the actions.
446 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
447 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100448 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100449 for (it = mActions.begin(); it != mActions.end(); ++it) {
450 ThreadType tt = getThreadType(*it);
451 if (tt == THREAD_NONE)
452 continue;
453 if (threadType == THREAD_NONE)
454 threadType = tt;
455 else if (threadType != tt) {
456 LOGERR("Can't mix normal and cancel actions in the same list.\n"
457 "Running the whole batch in the cancel thread.\n");
458 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100459 break;
460 }
that7d3b54f2015-01-09 22:52:51 +0100461 }
that73a52952015-01-28 23:07:34 +0100462
463 // Now run the actions in the desired thread.
464 switch (threadType) {
465 case THREAD_ACTION:
466 action_thread.threadActions(this);
467 break;
468
469 case THREAD_CANCEL:
470 cancel_thread.threadActions(this);
471 break;
472
473 default: {
474 // no iterators here because theme reloading might kill our object
475 const size_t cnt = mActions.size();
476 for (size_t i = 0; i < cnt; ++i)
477 doAction(mActions[i]);
478 }
thatc6085482015-01-09 22:12:43 +0100479 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400480
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200481 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482}
483
that3f7b1ac2014-12-30 11:30:13 +0100484int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400485{
that3f7b1ac2014-12-30 11:30:13 +0100486 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400487
that3f7b1ac2014-12-30 11:30:13 +0100488 std::string function = gui_parse_text(action.mFunction);
489 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400490
that3f7b1ac2014-12-30 11:30:13 +0100491 // find function and execute it
492 mapFunc::const_iterator funcitr = mf.find(function);
493 if (funcitr != mf.end())
494 return (this->*funcitr->second)(arg);
495
496 LOGERR("Unknown action '%s'\n", function.c_str());
497 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498}
499
500void GUIAction::operation_start(const string operation_name)
501{
that3f7b1ac2014-12-30 11:30:13 +0100502 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000503 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 DataManager::SetValue(TW_ACTION_BUSY, 1);
505 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100506 DataManager::SetValue("ui_portion_size", 0);
507 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600510 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400511 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500512 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400513}
514
that3f7b1ac2014-12-30 11:30:13 +0100515void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400516{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000517 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 DataManager::SetValue("ui_progress", 100);
520 if (simulate) {
521 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
522 if (simulate_fail != 0)
523 DataManager::SetValue("tw_operation_status", 1);
524 else
525 DataManager::SetValue("tw_operation_status", 0);
526 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500527 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400528 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500529 }
530 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500532 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400533 }
534 DataManager::SetValue("tw_operation_state", 1);
535 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500536 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200537 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000538 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400539
540#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000541 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600542 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400543#endif
544
that3f7b1ac2014-12-30 11:30:13 +0100545 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400546}
547
that3f7b1ac2014-12-30 11:30:13 +0100548int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400549{
that3f7b1ac2014-12-30 11:30:13 +0100550 sync();
551 DataManager::SetValue("tw_gui_done", 1);
552 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
that3f7b1ac2014-12-30 11:30:13 +0100554 return 0;
555}
Dees_Troy51a0e822012-09-05 15:24:24 -0400556
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500557int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100558{
that3f7b1ac2014-12-30 11:30:13 +0100559 gui_changePage("main");
560 return 0;
561}
Dees_Troy51a0e822012-09-05 15:24:24 -0400562
that3f7b1ac2014-12-30 11:30:13 +0100563int GUIAction::key(std::string arg)
564{
565 const int key = getKeyByName(arg);
566 PageManager::NotifyKey(key, true);
567 PageManager::NotifyKey(key, false);
568 return 0;
569}
570
571int GUIAction::page(std::string arg)
572{
LuK133762326f42015-09-30 19:57:46 +0200573 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100574 std::string page_name = gui_parse_text(arg);
575 return gui_changePage(page_name);
576}
577
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500578int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100579{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500580 PageManager::RequestReload();
581 // The actual reload is handled in pages.cpp in PageManager::RunReload()
582 // The reload will occur on the next Update or Render call and will
583 // be performed in the rendoer thread instead of the action thread
584 // to prevent crashing which could occur when we start deleting
585 // GUI resources in the action thread while we attempt to render
586 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100587 return 0;
588}
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500590int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100591{
592 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400593
that3f7b1ac2014-12-30 11:30:13 +0100594 DataManager::GetValue("tw_restore", Restore_Name);
595 PartitionManager.Set_Restore_Files(Restore_Name);
596 return 0;
597}
598
599int GUIAction::set(std::string arg)
600{
601 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200602 {
that3f7b1ac2014-12-30 11:30:13 +0100603 string varName = arg.substr(0, arg.find('='));
604 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400605
that3f7b1ac2014-12-30 11:30:13 +0100606 DataManager::GetValue(value, value);
607 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200608 }
that3f7b1ac2014-12-30 11:30:13 +0100609 else
610 DataManager::SetValue(arg, "1");
611 return 0;
612}
Dees_Troy51a0e822012-09-05 15:24:24 -0400613
that3f7b1ac2014-12-30 11:30:13 +0100614int GUIAction::clear(std::string arg)
615{
616 DataManager::SetValue(arg, "0");
617 return 0;
618}
Dees_Troy51a0e822012-09-05 15:24:24 -0400619
that3f7b1ac2014-12-30 11:30:13 +0100620int GUIAction::mount(std::string arg)
621{
622 if (arg == "usb") {
623 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100625 PartitionManager.usb_storage_enable();
626 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500627 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100628 } else if (!simulate) {
629 PartitionManager.Mount_By_Path(arg, true);
630 PartitionManager.Add_MTP_Storage(arg);
631 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500632 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100633 return 0;
634}
635
636int GUIAction::unmount(std::string arg)
637{
638 if (arg == "usb") {
639 if (!simulate)
640 PartitionManager.usb_storage_disable();
641 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500642 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100643 DataManager::SetValue(TW_ACTION_BUSY, 0);
644 } else if (!simulate) {
645 PartitionManager.UnMount_By_Path(arg, true);
646 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500647 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100648 return 0;
649}
650
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500651int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100652{
653 operation_start("Restore Defaults");
654 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500655 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100656 else {
657 DataManager::ResetDefaults();
658 PartitionManager.Update_System_Details();
659 PartitionManager.Mount_Current_Storage(true);
660 }
661 operation_end(0);
662 return 0;
663}
664
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500665int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100666{
667 operation_start("Copy Log");
668 if (!simulate)
669 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400670 string dst, curr_storage;
671 int copy_kernel_log = 0;
672
673 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100674 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400675 curr_storage = DataManager::GetCurrentStoragePath();
676 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100677 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
678 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400679 if (copy_kernel_log)
680 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100681 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400682 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100683 } else
684 simulate_progress_bar();
685 operation_end(0);
686 return 0;
687}
688
689
690int GUIAction::compute(std::string arg)
691{
692 if (arg.find("+") != string::npos)
693 {
694 string varName = arg.substr(0, arg.find('+'));
695 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
696 int amount_to_add = atoi(string_to_add.c_str());
697 int value;
698
699 DataManager::GetValue(varName, value);
700 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 return 0;
702 }
that3f7b1ac2014-12-30 11:30:13 +0100703 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 {
that3f7b1ac2014-12-30 11:30:13 +0100705 string varName = arg.substr(0, arg.find('-'));
706 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
707 int amount_to_subtract = atoi(string_to_subtract.c_str());
708 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400709
that3f7b1ac2014-12-30 11:30:13 +0100710 DataManager::GetValue(varName, value);
711 value -= amount_to_subtract;
712 if (value <= 0)
713 value = 0;
714 DataManager::SetValue(varName, value);
715 return 0;
716 }
717 if (arg.find("*") != string::npos)
718 {
719 string varName = arg.substr(0, arg.find('*'));
720 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
721 int multiply_by = atoi(multiply_by_str.c_str());
722 int value;
723
724 DataManager::GetValue(varName, value);
725 DataManager::SetValue(varName, value*multiply_by);
726 return 0;
727 }
728 if (arg.find("/") != string::npos)
729 {
730 string varName = arg.substr(0, arg.find('/'));
731 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
732 int divide_by = atoi(divide_by_str.c_str());
733 int value;
734
Matt Mowera8a89d12016-12-30 18:10:37 -0600735 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100736 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100738 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 }
740 return 0;
741 }
that3f7b1ac2014-12-30 11:30:13 +0100742 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
743 return -1;
744}
Dees_Troy51a0e822012-09-05 15:24:24 -0400745
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500746int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100747{
748 string SelectedZone;
749 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
750 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
751 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
752
753 int dst;
754 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
755
756 string offset;
757 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
758
759 string NewTimeZone = Zone;
760 if (offset != "0")
761 NewTimeZone += ":" + offset;
762
763 if (dst != 0)
764 NewTimeZone += DSTZone;
765
766 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
767 DataManager::update_tz_environment_variables();
768 return 0;
769}
770
771int GUIAction::overlay(std::string arg)
772{
773 return gui_changeOverlay(arg);
774}
775
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500776int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100777{
778 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500779 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 return 0;
781 }
that3f7b1ac2014-12-30 11:30:13 +0100782 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
783 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
784 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100786 }
787 return 0;
788}
789
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500790int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100791{
792 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500793 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400794 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100795 } else {
796 zip_queue_index--;
797 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 }
that3f7b1ac2014-12-30 11:30:13 +0100799 return 0;
800}
Dees_Troy51a0e822012-09-05 15:24:24 -0400801
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500802int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100803{
804 zip_queue_index = 0;
805 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
806 return 0;
807}
Dees_Troy51a0e822012-09-05 15:24:24 -0400808
that3f7b1ac2014-12-30 11:30:13 +0100809int GUIAction::sleep(std::string arg)
810{
811 operation_start("Sleep");
812 usleep(atoi(arg.c_str()));
813 operation_end(0);
814 return 0;
815}
Dees Troyb21cc642013-09-10 17:36:41 +0000816
Matt Mower9a2a2052016-05-31 21:31:22 -0500817int GUIAction::sleepcounter(std::string arg)
818{
819 operation_start("SleepCounter");
820 // Ensure user notices countdown in case it needs to be cancelled
821 blankTimer.resetTimerAndUnblank();
822 int total = atoi(arg.c_str());
823 for (int t = total; t > 0; t--) {
824 int progress = (int)(((float)(total-t)/(float)total)*100.0);
825 DataManager::SetValue("ui_progress", progress);
826 ::sleep(1);
827 DataManager::SetValue("tw_sleep", t-1);
828 }
829 DataManager::SetValue("ui_progress", 100);
830 operation_end(0);
831 return 0;
832}
833
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500834int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100835{
836 operation_start("AppendDateToBackupName");
837 string Backup_Name;
838 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
839 Backup_Name += TWFunc::Get_Current_Date();
840 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
841 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
842 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500843 PageManager::NotifyKey(KEY_END, true);
844 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100845 operation_end(0);
846 return 0;
847}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500848
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500849int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100850{
851 operation_start("GenerateBackupName");
852 TWFunc::Auto_Generate_Backup_Name();
853 operation_end(0);
854 return 0;
855}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500856
Ethan Yonker483e9f42016-01-11 22:21:18 -0600857int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100858{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600859 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100860 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500861
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862 if (arg.empty())
863 arg = "tw_wipe_list";
864 DataManager::GetValue(arg, List);
865 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
866 if (!List.empty()) {
867 size_t start_pos = 0, end_pos = List.find(";", start_pos);
868 while (end_pos != string::npos && start_pos < List.size()) {
869 part_path = List.substr(start_pos, end_pos - start_pos);
870 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
871 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100872 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400873 } else {
that3f7b1ac2014-12-30 11:30:13 +0100874 count++;
875 }
876 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600877 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100878 }
879 DataManager::SetValue("tw_check_partition_list", count);
880 } else {
881 DataManager::SetValue("tw_check_partition_list", 0);
882 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600883 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100884}
Dees_Troy51a0e822012-09-05 15:24:24 -0400885
Ethan Yonker483e9f42016-01-11 22:21:18 -0600886int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100887{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600888 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400889
Ethan Yonker483e9f42016-01-11 22:21:18 -0600890 if (arg.empty())
891 arg = "tw_wipe_list";
892 DataManager::GetValue(arg, List);
893 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
894 if (!List.empty()) {
895 size_t start_pos = 0, end_pos = List.find(";", start_pos);
896 part_path = List;
897 while (end_pos != string::npos && start_pos < List.size()) {
898 part_path = List.substr(start_pos, end_pos - start_pos);
899 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
900 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100901 // Do nothing
902 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600903 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100904 break;
905 }
906 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600907 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100908 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600909 if (!part_path.empty()) {
910 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100911 if (Part) {
912 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500913
that3f7b1ac2014-12-30 11:30:13 +0100914 DataManager::SetValue("tw_partition_name", Part->Display_Name);
915 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
916 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
917 DataManager::SetValue("tw_partition_size", Part->Size / mb);
918 DataManager::SetValue("tw_partition_used", Part->Used / mb);
919 DataManager::SetValue("tw_partition_free", Part->Free / mb);
920 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
921 DataManager::SetValue("tw_partition_removable", Part->Removable);
922 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
923
924 if (Part->Can_Repair())
925 DataManager::SetValue("tw_partition_can_repair", 1);
926 else
927 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500928 if (Part->Can_Resize())
929 DataManager::SetValue("tw_partition_can_resize", 1);
930 else
931 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400932 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100933 DataManager::SetValue("tw_partition_vfat", 1);
934 else
935 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400936 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100937 DataManager::SetValue("tw_partition_exfat", 1);
938 else
939 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100940 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100941 DataManager::SetValue("tw_partition_f2fs", 1);
942 else
943 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400944 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100945 DataManager::SetValue("tw_partition_ext", 1);
946 else
947 DataManager::SetValue("tw_partition_ext", 0);
948 return 0;
949 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600950 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100951 }
952 }
953 }
954 DataManager::SetValue("tw_partition_name", "");
955 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600956 // Set this to 0 to prevent trying to partition this device, just in case
957 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100958 return 0;
959}
960
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500961int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100962{
963 time_t tm;
964 char path[256];
965 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600966 uid_t uid = AID_MEDIA_RW;
967 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100968
969 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600970 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100971 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
972 } else {
973 strcpy(path, "/tmp/");
974 }
975
Matt Mowera8a89d12016-12-30 18:10:37 -0600976 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100977 return 0;
978
979 tm = time(NULL);
980 path_len = strlen(path);
981
982 // Screenshot_2014-01-01-18-21-38.png
983 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
984
985 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600986 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100987 chmod(path, 0666);
988 chown(path, uid, gid);
989
that677b13f2015-12-26 23:57:31 +0100990 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100991
VDavid0032034a412019-10-18 22:43:20 +0200992 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100993 gr_color(255, 255, 255, 255);
994 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
995 gr_flip();
996 gui_forceRender();
997 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500998 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100999 }
1000 return 0;
1001}
1002
1003int GUIAction::setbrightness(std::string arg)
1004{
1005 return TWFunc::Set_Brightness(arg);
1006}
1007
1008int GUIAction::fileexists(std::string arg)
1009{
1010 struct stat st;
1011 string newpath = arg + "/.";
1012
1013 operation_start("FileExists");
1014 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1015 operation_end(0);
1016 else
1017 operation_end(1);
1018 return 0;
1019}
1020
thatcc8ddca2015-01-03 01:59:36 +01001021void GUIAction::reinject_after_flash()
1022{
1023 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001024 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001025 if (simulate) {
1026 simulate_progress_bar();
1027 } else {
1028 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1029 if (Boot == NULL || Boot->Current_File_System != "emmc")
1030 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1031 else {
1032 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1033 TWFunc::Exec_Cmd(injectcmd);
1034 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001035 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001036 }
1037 }
1038}
1039
mauronofrio6d3bf892019-10-26 19:47:55 +02001040int GUIAction::ozip_decrypt(string zip_path)
1041{
bigbiffad58e1b2020-07-06 20:24:34 -04001042 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001043 return 1;
1044 }
1045 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1046 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1047 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1048 return 0;
1049}
1050
that3f7b1ac2014-12-30 11:30:13 +01001051int GUIAction::flash(std::string arg)
1052{
1053 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001054 // We're going to jump to this page first, like a loading page
1055 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001056 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001057 string zip_path = zip_queue[i];
1058 size_t slashpos = zip_path.find_last_of('/');
1059 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001060 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001061 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1062 {
1063 if((ozip_decrypt(zip_path)) != 0)
1064 {
1065 LOGERR("Unable to find ozip_decrypt!");
1066 break;
1067 }
1068 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1069 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1070 if (!TWFunc::Path_Exists(zip_path)) {
1071 LOGERR("Unable to find decrypted zip");
1072 break;
1073 }
1074 }
thatc7b631b2015-06-01 23:36:57 +02001075 DataManager::SetValue("tw_filename", zip_path);
1076 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001077 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1078
1079 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001080 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001081 TWFunc::SetPerformanceMode(false);
1082 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001083 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001084 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001085 break;
that3f7b1ac2014-12-30 11:30:13 +01001086 }
1087 }
1088 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001089
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001090 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001091 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001092 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001093 }
that3f7b1ac2014-12-30 11:30:13 +01001094
thatcc8ddca2015-01-03 01:59:36 +01001095 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001096 PartitionManager.Update_System_Details();
1097 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001098 // 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 -06001099 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001100 return 0;
1101}
1102
1103int GUIAction::wipe(std::string arg)
1104{
1105 operation_start("Format");
1106 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001107 int ret_val = false;
1108
1109 if (simulate) {
1110 simulate_progress_bar();
1111 } else {
1112 if (arg == "data")
1113 ret_val = PartitionManager.Factory_Reset();
1114 else if (arg == "battery")
1115 ret_val = PartitionManager.Wipe_Battery_Stats();
1116 else if (arg == "rotate")
1117 ret_val = PartitionManager.Wipe_Rotate_Data();
1118 else if (arg == "dalvik")
1119 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1120 else if (arg == "DATAMEDIA") {
1121 ret_val = PartitionManager.Format_Data();
1122 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001123 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001124
1125 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1126 if (has_datamedia) {
1127 ret_val = PartitionManager.Wipe_Media_From_Data();
1128 } else {
1129 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1130 }
1131 } else if (arg == "EXTERNAL") {
1132 string External_Path;
1133
1134 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1135 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1136 } else if (arg == "ANDROIDSECURE") {
1137 ret_val = PartitionManager.Wipe_Android_Secure();
1138 } else if (arg == "LIST") {
1139 string Wipe_List, wipe_path;
1140 bool skip = false;
1141 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001142
1143 DataManager::GetValue("tw_wipe_list", Wipe_List);
1144 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1145 if (!Wipe_List.empty()) {
1146 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1147 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1148 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1149 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1150 if (wipe_path == "/and-sec") {
1151 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001152 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001153 ret_val = false;
1154 break;
1155 } else {
1156 skip = true;
1157 }
1158 } else if (wipe_path == "DALVIK") {
1159 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001160 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001161 ret_val = false;
1162 break;
1163 } else {
1164 skip = true;
1165 }
1166 } else if (wipe_path == "INTERNAL") {
1167 if (!PartitionManager.Wipe_Media_From_Data()) {
1168 ret_val = false;
1169 break;
1170 } else {
1171 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001172 }
1173 }
that3f7b1ac2014-12-30 11:30:13 +01001174 if (!skip) {
1175 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001176 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001177 ret_val = false;
1178 break;
1179 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1180 arg = wipe_path;
1181 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001182 } else {
that3f7b1ac2014-12-30 11:30:13 +01001183 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001184 }
that3f7b1ac2014-12-30 11:30:13 +01001185 start_pos = end_pos + 1;
1186 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001187 }
1188 }
that3f7b1ac2014-12-30 11:30:13 +01001189 } else
1190 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001191#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001192 if (arg == DataManager::GetSettingsStoragePath()) {
1193 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1194 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001195
that3f7b1ac2014-12-30 11:30:13 +01001196 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1197 LOGINFO("Making TWRP folder and saving settings.\n");
1198 Storage_Path += "/TWRP";
1199 mkdir(Storage_Path.c_str(), 0777);
1200 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001201 } else {
that3f7b1ac2014-12-30 11:30:13 +01001202 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1203 }
1204 }
1205#endif
1206 }
1207 PartitionManager.Update_System_Details();
1208 if (ret_val)
1209 ret_val = 0; // 0 is success
1210 else
1211 ret_val = 1; // 1 is failure
1212 operation_end(ret_val);
1213 return 0;
1214}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001215
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001216int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001217{
1218 operation_start("Refreshing Sizes");
1219 if (simulate) {
1220 simulate_progress_bar();
1221 } else
1222 PartitionManager.Update_System_Details();
1223 operation_end(0);
1224 return 0;
1225}
1226
1227int GUIAction::nandroid(std::string arg)
1228{
that3f7b1ac2014-12-30 11:30:13 +01001229 if (simulate) {
that73a52952015-01-28 23:07:34 +01001230 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001231 DataManager::SetValue("tw_partition", "Simulation");
1232 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001233 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001234 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001235 operation_start("Nandroid");
1236 int ret = 0;
1237
that3f7b1ac2014-12-30 11:30:13 +01001238 if (arg == "backup") {
1239 string Backup_Name;
1240 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001241 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001242 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 -05001243 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001244 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1245 if (!PartitionManager.stop_backup.get_value()) {
1246 if (ret == false)
1247 ret = 1; // 1 for failure
1248 else
1249 ret = 0; // 0 for success
1250 DataManager::SetValue("tw_cancel_backup", 0);
1251 } else {
1252 DataManager::SetValue("tw_cancel_backup", 1);
1253 gui_msg("backup_cancel=Backup Cancelled");
1254 ret = 0;
1255 }
bigbiffce8f83c2015-12-12 18:30:21 -05001256 } else {
that3f7b1ac2014-12-30 11:30:13 +01001257 operation_end(1);
1258 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001259 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001260 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001261 } else if (arg == "restore") {
1262 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001263 int gui_adb_backup;
1264
that3f7b1ac2014-12-30 11:30:13 +01001265 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001266 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1267 if (gui_adb_backup) {
1268 DataManager::SetValue("tw_operation_state", 1);
1269 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1270 ret = 0; // success
1271 else
1272 ret = 1; // failure
1273 DataManager::SetValue("tw_enable_adb_backup", 0);
1274 ret = 0; // assume success???
1275 } else {
1276 if (PartitionManager.Run_Restore(Restore_Name))
1277 ret = 0; // success
1278 else
1279 ret = 1; // failure
1280 }
that3f7b1ac2014-12-30 11:30:13 +01001281 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001282 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001283 return -1;
1284 }
that73a52952015-01-28 23:07:34 +01001285 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001286 return ret;
1287 }
1288 return 0;
1289}
1290
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001291int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001292 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001293 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001294 }
1295 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001296 int op_status = PartitionManager.Cancel_Backup();
1297 if (op_status != 0)
1298 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001299 }
1300
1301 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001302}
Dees_Troy51a0e822012-09-05 15:24:24 -04001303
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001304int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001305{
that73a52952015-01-28 23:07:34 +01001306 int op_status = 0;
1307
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001308 operation_start("Fix Contexts");
1309 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001310 if (simulate) {
1311 simulate_progress_bar();
1312 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001313 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001314 if (op_status != 0)
1315 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001316 }
that73a52952015-01-28 23:07:34 +01001317 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001318 return 0;
1319}
1320
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001321int GUIAction::fixpermissions(std::string arg)
1322{
1323 return fixcontexts(arg);
1324}
1325
that3f7b1ac2014-12-30 11:30:13 +01001326int GUIAction::dd(std::string arg)
1327{
1328 operation_start("imaging");
1329
1330 if (simulate) {
1331 simulate_progress_bar();
1332 } else {
1333 string cmd = "dd " + arg;
1334 TWFunc::Exec_Cmd(cmd);
1335 }
1336 operation_end(0);
1337 return 0;
1338}
1339
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001340int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001341{
1342 operation_start("Partition SD Card");
1343 int ret_val = 0;
1344
1345 if (simulate) {
1346 simulate_progress_bar();
1347 } else {
1348 int allow_partition;
1349 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1350 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001351 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001352 } else {
1353 if (!PartitionManager.Partition_SDCard())
1354 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001355 }
that3f7b1ac2014-12-30 11:30:13 +01001356 }
1357 operation_end(ret_val);
1358 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001359
that3f7b1ac2014-12-30 11:30:13 +01001360}
Dees_Troy51a0e822012-09-05 15:24:24 -04001361
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001362int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001363{
1364 operation_start("Install HTC Dumlock");
1365 if (simulate) {
1366 simulate_progress_bar();
1367 } else
1368 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001369
that3f7b1ac2014-12-30 11:30:13 +01001370 operation_end(0);
1371 return 0;
1372}
Dees_Troy51a0e822012-09-05 15:24:24 -04001373
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001374int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001375{
1376 operation_start("HTC Dumlock Restore Boot");
1377 if (simulate) {
1378 simulate_progress_bar();
1379 } else
1380 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001381
that3f7b1ac2014-12-30 11:30:13 +01001382 operation_end(0);
1383 return 0;
1384}
Dees_Troy51a0e822012-09-05 15:24:24 -04001385
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001386int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001387{
1388 operation_start("HTC Dumlock Reflash Recovery");
1389 if (simulate) {
1390 simulate_progress_bar();
1391 } else
1392 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001393
that3f7b1ac2014-12-30 11:30:13 +01001394 operation_end(0);
1395 return 0;
1396}
Dees_Troy51a0e822012-09-05 15:24:24 -04001397
that3f7b1ac2014-12-30 11:30:13 +01001398int GUIAction::cmd(std::string arg)
1399{
1400 int op_status = 0;
1401
1402 operation_start("Command");
1403 LOGINFO("Running command: '%s'\n", arg.c_str());
1404 if (simulate) {
1405 simulate_progress_bar();
1406 } else {
1407 op_status = TWFunc::Exec_Cmd(arg);
1408 if (op_status != 0)
1409 op_status = 1;
1410 }
1411
1412 operation_end(op_status);
1413 return 0;
1414}
1415
1416int GUIAction::terminalcommand(std::string arg)
1417{
1418 int op_status = 0;
1419 string cmdpath, command;
1420
1421 DataManager::GetValue("tw_terminal_location", cmdpath);
1422 operation_start("CommandOutput");
1423 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1424 if (simulate) {
1425 simulate_progress_bar();
1426 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001427 } else if (arg == "exit") {
1428 LOGINFO("Exiting terminal\n");
1429 operation_end(op_status);
1430 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001431 } else {
1432 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1433 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001434 DataManager::SetValue("tw_terminal_state", 1);
1435 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001436 FILE* fp;
1437 char line[512];
1438
1439 fp = popen(command.c_str(), "r");
1440 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001442 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001443 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001444 struct timeval timeout;
1445 fd_set fdset;
1446
Matt Mowera8a89d12016-12-30 18:10:37 -06001447 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001448 {
1449 FD_ZERO(&fdset);
1450 FD_SET(fd, &fdset);
1451 timeout.tv_sec = 0;
1452 timeout.tv_usec = 400000;
1453 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1454 if (has_data == 0) {
1455 // Timeout reached
1456 DataManager::GetValue("tw_terminal_state", check);
1457 if (check == 0) {
1458 keep_going = 0;
1459 }
1460 } else if (has_data < 0) {
1461 // End of execution
1462 keep_going = 0;
1463 } else {
1464 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001465 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001466 gui_print("%s", line); // Display output
1467 else
1468 keep_going = 0; // Done executing
1469 }
1470 }
1471 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001472 }
thatc6085482015-01-09 22:12:43 +01001473 DataManager::SetValue("tw_operation_status", 0);
1474 DataManager::SetValue("tw_operation_state", 1);
1475 DataManager::SetValue("tw_terminal_state", 0);
1476 DataManager::SetValue("tw_background_thread_running", 0);
1477 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001478 }
1479 return 0;
1480}
1481
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001482int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001483{
that3f7b1ac2014-12-30 11:30:13 +01001484 LOGINFO("Sending kill command...\n");
1485 operation_start("KillCommand");
1486 DataManager::SetValue("tw_operation_status", 0);
1487 DataManager::SetValue("tw_operation_state", 1);
1488 DataManager::SetValue("tw_terminal_state", 0);
1489 DataManager::SetValue("tw_background_thread_running", 0);
1490 DataManager::SetValue(TW_ACTION_BUSY, 0);
1491 return 0;
1492}
1493
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001494int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001495{
1496 int op_status = 0;
1497 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001498 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001499 if (simulate) {
1500 simulate_progress_bar();
1501 } else {
1502 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001504 }
1505
1506 operation_end(op_status);
1507 return 0;
1508}
1509
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001510int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001511{
1512 int op_status = 0;
1513
1514 operation_start("CheckBackupName");
1515 if (simulate) {
1516 simulate_progress_bar();
1517 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001518 string Backup_Name;
1519 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1520 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001521 if (op_status != 0)
1522 op_status = 1;
1523 }
1524
1525 operation_end(op_status);
1526 return 0;
1527}
1528
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001529int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001530{
1531 int op_status = 0;
1532
1533 operation_start("Decrypt");
1534 if (simulate) {
1535 simulate_progress_bar();
1536 } else {
1537 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001538 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001539 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001540
1541 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1542 DataManager::GetValue("tw_crypto_user_id", userID);
1543 if (userID != "") {
1544 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1545 if (userID != "0") {
1546 if (op_status != 0)
1547 op_status = 1;
1548 operation_end(op_status);
1549 return 0;
1550 }
1551 } else {
1552 LOGINFO("User ID not found\n");
1553 op_status = 1;
1554 }
1555 ::sleep(1);
1556 } else { // for FDE
1557 op_status = PartitionManager.Decrypt_Device(Password);
1558 }
1559
that3f7b1ac2014-12-30 11:30:13 +01001560 if (op_status != 0)
1561 op_status = 1;
1562 else {
that3f7b1ac2014-12-30 11:30:13 +01001563 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1564
Ethan Yonkercf50da52015-01-12 21:59:07 -06001565 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001566
Ethan Yonkercf50da52015-01-12 21:59:07 -06001567 // Check for a custom theme and load it if exists
1568 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1569 if (has_datamedia != 0) {
1570 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001571 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001572 } else {
1573 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001574 }
1575 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001576 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001577 }
1578 }
1579
1580 operation_end(op_status);
1581 return 0;
1582}
1583
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001584int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001585{
1586 operation_start("Sideload");
1587 if (simulate) {
1588 simulate_progress_bar();
1589 operation_end(0);
1590 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001591 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001592 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1593
1594 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001595 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001596 int ret = twrp_sideload("/", &reboot_action);
1597 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001598 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1599
1600 if (ret != 0) {
1601 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001602 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001603 ret = 1; // failure
1604 } else {
1605 int wipe_cache = 0;
1606 int wipe_dalvik = 0;
1607 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001608 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1609 PartitionManager.Wipe_By_Path("/cache");
1610 if (wipe_dalvik)
1611 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001612 }
thatc6085482015-01-09 22:12:43 +01001613 TWFunc::Toggle_MTP(mtp_was_enabled);
1614 reinject_after_flash();
1615 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001616 }
that3f7b1ac2014-12-30 11:30:13 +01001617 return 0;
1618}
1619
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001620int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001621{
that3f7b1ac2014-12-30 11:30:13 +01001622 struct stat st;
1623 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001625 LOGINFO("Signaling child sideload process to exit.\n");
1626 // Calling stat() on this magic filename signals the minadbd
1627 // subprocess to shut down.
1628 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001629 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001630 if (!sideload_child_pid) {
1631 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001632 return 0;
1633 }
thatcc8ddca2015-01-03 01:59:36 +01001634 ::sleep(1);
1635 LOGINFO("Killing child sideload process.\n");
1636 kill(sideload_child_pid, SIGTERM);
1637 int status;
1638 LOGINFO("Waiting for child sideload process to exit.\n");
1639 waitpid(sideload_child_pid, &status, 0);
1640 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001641 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1642 return 0;
1643}
1644
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001645int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001646{
1647 operation_start("OpenRecoveryScript");
1648 if (simulate) {
1649 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001650 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001651 } else {
that10ae24f2015-12-26 20:53:51 +01001652 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001653 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001654 }
1655 return 0;
1656}
1657
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001658int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001659{
1660 int op_status = 0;
1661
1662 operation_start("Install SuperSU");
1663 if (simulate) {
1664 simulate_progress_bar();
1665 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001666 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001667 }
1668
1669 operation_end(op_status);
1670 return 0;
1671}
1672
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001673int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001674{
1675 int op_status = 0;
1676
1677 operation_start("Fixing Superuser Permissions");
1678 if (simulate) {
1679 simulate_progress_bar();
1680 } else {
1681 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1682 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1683 }
1684
1685 operation_end(op_status);
1686 return 0;
1687}
1688
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001689int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001690{
1691 int op_status = 0;
1692
1693 operation_start("Try Restore Decrypt");
1694 if (simulate) {
1695 simulate_progress_bar();
1696 } else {
1697 string Restore_Path, Filename, Password;
1698 DataManager::GetValue("tw_restore", Restore_Path);
1699 Restore_Path += "/";
1700 DataManager::GetValue("tw_restore_password", Password);
1701 TWFunc::SetPerformanceMode(true);
1702 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1703 op_status = 0; // success
1704 else
1705 op_status = 1; // fail
1706 TWFunc::SetPerformanceMode(false);
1707 }
1708
1709 operation_end(op_status);
1710 return 0;
1711}
1712
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001713int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001714{
1715 int op_status = 0;
1716
1717 operation_start("Repair Partition");
1718 if (simulate) {
1719 simulate_progress_bar();
1720 } else {
1721 string part_path;
1722 DataManager::GetValue("tw_partition_mount_point", part_path);
1723 if (PartitionManager.Repair_By_Path(part_path, true)) {
1724 op_status = 0; // success
1725 } else {
that3f7b1ac2014-12-30 11:30:13 +01001726 op_status = 1; // fail
1727 }
1728 }
1729
1730 operation_end(op_status);
1731 return 0;
1732}
1733
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001734int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001735{
1736 int op_status = 0;
1737
1738 operation_start("Resize Partition");
1739 if (simulate) {
1740 simulate_progress_bar();
1741 } else {
1742 string part_path;
1743 DataManager::GetValue("tw_partition_mount_point", part_path);
1744 if (PartitionManager.Resize_By_Path(part_path, true)) {
1745 op_status = 0; // success
1746 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001747 op_status = 1; // fail
1748 }
1749 }
1750
1751 operation_end(op_status);
1752 return 0;
1753}
1754
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001755int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001756{
1757 int op_status = 0;
1758
1759 operation_start("Change File System");
1760 if (simulate) {
1761 simulate_progress_bar();
1762 } else {
1763 string part_path, file_system;
1764 DataManager::GetValue("tw_partition_mount_point", part_path);
1765 DataManager::GetValue("tw_action_new_file_system", file_system);
1766 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1767 op_status = 0; // success
1768 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001769 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001770 op_status = 1; // fail
1771 }
1772 }
1773 PartitionManager.Update_System_Details();
1774 operation_end(op_status);
1775 return 0;
1776}
1777
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001778int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001779{
1780 int op_status = 0;
1781
1782 operation_start("Start MTP");
1783 if (PartitionManager.Enable_MTP())
1784 op_status = 0; // success
1785 else
1786 op_status = 1; // fail
1787
1788 operation_end(op_status);
1789 return 0;
1790}
1791
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001792int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001793{
1794 int op_status = 0;
1795
1796 operation_start("Stop MTP");
1797 if (PartitionManager.Disable_MTP())
1798 op_status = 0; // success
1799 else
1800 op_status = 1; // fail
1801
1802 operation_end(op_status);
1803 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001804}
1805
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001806int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001807{
1808 int op_status = 0;
1809
1810 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001811 string path, filename;
1812 DataManager::GetValue("tw_zip_location", path);
1813 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001814 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001815 op_status = 0; // success
1816 else
1817 op_status = 1; // fail
1818
1819 operation_end(op_status);
1820 return 0;
1821}
1822
that10ae24f2015-12-26 20:53:51 +01001823int GUIAction::twcmd(std::string arg)
1824{
1825 operation_start("TWRP CLI Command");
1826 if (simulate)
1827 simulate_progress_bar();
1828 else
1829 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1830 operation_end(0);
1831 return 0;
1832}
1833
Dees_Troy51a0e822012-09-05 15:24:24 -04001834int GUIAction::getKeyByName(std::string key)
1835{
that8834a0f2016-01-05 23:29:30 +01001836 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001837 else if (key == "menu") return KEY_MENU;
1838 else if (key == "back") return KEY_BACK;
1839 else if (key == "search") return KEY_SEARCH;
1840 else if (key == "voldown") return KEY_VOLUMEDOWN;
1841 else if (key == "volup") return KEY_VOLUMEUP;
1842 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001843 int ret_val;
1844 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1845 if (!ret_val)
1846 return KEY_POWER;
1847 else
1848 return ret_val;
1849 }
1850
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001851 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001852}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001853
1854int GUIAction::checkpartitionlifetimewrites(std::string arg)
1855{
1856 int op_status = 0;
1857 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1858
1859 operation_start("Check Partition Lifetime Writes");
1860 if (sys) {
1861 if (sys->Check_Lifetime_Writes() != 0)
1862 DataManager::SetValue("tw_lifetime_writes", 1);
1863 else
1864 DataManager::SetValue("tw_lifetime_writes", 0);
1865 op_status = 0; // success
1866 } else {
1867 DataManager::SetValue("tw_lifetime_writes", 1);
1868 op_status = 1; // fail
1869 }
1870
1871 operation_end(op_status);
1872 return 0;
1873}
1874
1875int GUIAction::mountsystemtoggle(std::string arg)
1876{
1877 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001878 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001879 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001880
1881 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001882 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001883 op_status = 1; // fail
1884 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001885 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001886 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001887 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001888 DataManager::SetValue("tw_mount_system_ro", 0);
1889 Part->Change_Mount_Read_Only(false);
1890 } else {
1891 DataManager::SetValue("tw_mount_system_ro", 1);
1892 Part->Change_Mount_Read_Only(true);
1893 }
1894 if (remount_system) {
1895 Part->Mount(true);
1896 }
1897 op_status = 0; // success
1898 } else {
1899 op_status = 1; // fail
1900 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001901 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1902 if (Part) {
1903 if (arg == "0") {
1904 Part->Change_Mount_Read_Only(false);
1905 } else {
1906 Part->Change_Mount_Read_Only(true);
1907 }
1908 if (remount_vendor) {
1909 Part->Mount(true);
1910 }
1911 op_status = 0; // success
1912 } else {
1913 op_status = 1; // fail
1914 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001915 }
1916
1917 operation_end(op_status);
1918 return 0;
1919}
Ethan Yonker74db1572015-10-28 12:44:49 -05001920
1921int GUIAction::setlanguage(std::string arg __unused)
1922{
1923 int op_status = 0;
1924
1925 operation_start("Set Language");
1926 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1927 PageManager::RequestReload();
1928 op_status = 0; // success
1929
1930 operation_end(op_status);
1931 return 0;
1932}
Ethan Yonker1b190162016-12-05 15:25:19 -06001933
Matt Mower9472ba12016-01-20 18:12:47 -06001934int GUIAction::togglebacklight(std::string arg __unused)
1935{
1936 blankTimer.toggleBlank();
1937 return 0;
1938}
1939
Ethan Yonker1b190162016-12-05 15:25:19 -06001940int GUIAction::setbootslot(std::string arg)
1941{
1942 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001943 if (!simulate) {
1944 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1945 // PartitionManager failed to unmount /vendor, this should not happen,
1946 // but in case it does, do a lazy unmount
1947 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1948 umount2("/vendor", MNT_DETACH);
1949 PartitionManager.Set_Active_Slot(arg);
1950 } else {
1951 PartitionManager.Set_Active_Slot(arg);
1952 }
1953 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001954 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001955 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001956 operation_end(0);
1957 return 0;
1958}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001959
1960int GUIAction::checkforapp(std::string arg __unused)
1961{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001962 operation_start("Check for TWRP App");
1963 if (!simulate)
1964 {
epicXa4a7c522020-12-30 01:03:18 +05301965 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001966 } else
1967 simulate_progress_bar();
epicXa4a7c522020-12-30 01:03:18 +05301968
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001969 operation_end(0);
1970 return 0;
1971}
1972
1973int GUIAction::installapp(std::string arg __unused)
1974{
1975 int op_status = 1;
1976 operation_start("Install TWRP App");
1977 if (!simulate)
1978 {
1979 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1980 if (PartitionManager.Mount_By_Path("/data", true)) {
1981 string install_path = "/data/app";
1982 string context = "u:object_r:apk_data_file:s0";
1983 if (!TWFunc::Path_Exists(install_path)) {
1984 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1985 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1986 goto exit;
1987 }
1988 if (chown(install_path.c_str(), 1000, 1000)) {
1989 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1990 goto exit;
1991 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001992 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001993 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1994 goto exit;
1995 }
1996 }
1997 install_path += "/me.twrp.twrpapp-1";
1998 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1999 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2000 goto exit;
2001 }
2002 if (chown(install_path.c_str(), 1000, 1000)) {
2003 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002006 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002007 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2008 goto exit;
2009 }
2010 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002011 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002012 LOGERR("Error copying apk file\n");
2013 goto exit;
2014 }
2015 if (chown(install_path.c_str(), 1000, 1000)) {
2016 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2017 goto exit;
2018 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002019 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002020 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2021 goto exit;
2022 }
2023 sync();
2024 sync();
2025 }
2026 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002027 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2028 string base_path = PartitionManager.Get_Android_Root_Path();
2029 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002030 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2031 string install_path = base_path + "/priv-app";
2032 string context = "u:object_r:system_file:s0";
2033 if (!TWFunc::Path_Exists(install_path))
2034 install_path = base_path + "/app";
2035 if (TWFunc::Path_Exists(install_path)) {
2036 install_path += "/twrpapp";
2037 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2038 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002039 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002040 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2041 goto exit;
2042 }
2043 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002044 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002045 LOGERR("Error copying apk file\n");
2046 goto exit;
2047 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002048 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002049 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2050 goto exit;
2051 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002052
2053 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2054 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002055 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002056 LOGERR("Error copying permission file\n");
2057 goto exit;
2058 }
2059 if (chown(permission_path.c_str(), 1000, 1000)) {
2060 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2061 goto exit;
2062 }
2063 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2064 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2065 goto exit;
2066 }
2067
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002068 sync();
2069 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002070 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002071 op_status = 0;
2072 } else {
2073 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2074 }
2075 }
2076 }
2077 }
2078 } else
2079 simulate_progress_bar();
2080exit:
epicXa4a7c522020-12-30 01:03:18 +05302081 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002082 operation_end(0);
2083 return 0;
2084}
Ethan Yonker53796e72019-01-11 22:49:52 -06002085
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002086int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2087{
2088 int op_status = 1;
2089 operation_start("Uninstall TWRP System App");
2090 if (!simulate)
2091 {
2092 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2093 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2094 if (!Part) {
2095 LOGERR("Unabled to find system partition.\n");
2096 goto exit;
2097 }
2098 if (!Part->UnMount(true)) {
2099 goto exit;
2100 }
2101 if (Mount_System_RO > 0) {
2102 DataManager::SetValue("tw_mount_system_ro", 0);
2103 Part->Change_Mount_Read_Only(false);
2104 }
2105 if (Part->Mount(true)) {
2106 string base_path = PartitionManager.Get_Android_Root_Path();
2107 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2108 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2109 string uninstall_path = base_path + "/priv-app";
2110 if (!TWFunc::Path_Exists(uninstall_path))
2111 uninstall_path = base_path + "/app";
2112 uninstall_path += "/twrpapp";
2113 if (TWFunc::Path_Exists(uninstall_path)) {
2114 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2115 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2116 sync();
2117 op_status = 0;
2118 DataManager::SetValue("tw_app_installed_in_system", 0);
2119 DataManager::SetValue("tw_app_install_status", 0);
2120 } else {
2121 LOGERR("Unable to remove TWRP app from system.\n");
2122 }
2123 } else {
2124 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2125 }
2126 }
2127 Part->UnMount(true);
2128 if (Mount_System_RO > 0) {
2129 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2130 Part->Change_Mount_Read_Only(true);
2131 }
2132 } else
2133 simulate_progress_bar();
2134exit:
epicXa4a7c522020-12-30 01:03:18 +05302135 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002136 operation_end(0);
2137 return 0;
2138}
2139
Ethan Yonker53796e72019-01-11 22:49:52 -06002140int GUIAction::repackimage(std::string arg __unused)
2141{
2142 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002143 twrpRepacker repacker;
2144
Ethan Yonker53796e72019-01-11 22:49:52 -06002145 operation_start("Repack Image");
2146 if (!simulate)
2147 {
2148 std::string path = DataManager::GetStrValue("tw_filename");
2149 Repack_Options_struct Repack_Options;
2150 Repack_Options.Disable_Verity = false;
2151 Repack_Options.Disable_Force_Encrypt = false;
2152 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2153 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2154 Repack_Options.Type = REPLACE_KERNEL;
2155 else
2156 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002157 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002158 goto exit;
2159 } else
2160 simulate_progress_bar();
2161 op_status = 0;
2162exit:
2163 operation_end(op_status);
2164 return 0;
2165}
2166
2167int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2168{
2169 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002170 twrpRepacker repacker;
2171
Ethan Yonker53796e72019-01-11 22:49:52 -06002172 operation_start("Repack Image");
2173 if (!simulate)
2174 {
bigbiffad58e1b2020-07-06 20:24:34 -04002175 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002176 LOGERR("Image repacking tool not present in this TWRP build!");
2177 goto exit;
2178 }
2179 DataManager::SetProgress(0);
2180 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2181 if (part)
2182 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2183 else {
2184 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2185 goto exit;
2186 }
bigbiffad58e1b2020-07-06 20:24:34 -04002187 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 -06002188 goto exit;
2189 DataManager::SetProgress(.25);
2190 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002191 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002192 if (TWFunc::Exec_Cmd(command) != 0) {
2193 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2194 goto exit;
2195 }
2196 std::string header_path = REPACK_ORIG_DIR;
2197 header_path += "header";
2198 if (TWFunc::Path_Exists(header_path)) {
2199 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";
2200 if (TWFunc::Exec_Cmd(command) != 0) {
2201 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2202 goto exit;
2203 }
2204 }
2205 DataManager::SetProgress(.5);
2206 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002207 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002208 if (TWFunc::Exec_Cmd(command) != 0) {
2209 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2210 goto exit;
2211 }
2212 DataManager::SetProgress(.75);
2213 std::string path = REPACK_ORIG_DIR;
2214 std::string file = "new-boot.img";
2215 DataManager::SetValue("tw_flash_partition", "/boot;");
2216 if (!PartitionManager.Flash_Image(path, file)) {
2217 LOGINFO("Error flashing new image\n");
2218 goto exit;
2219 }
2220 DataManager::SetProgress(1);
2221 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2222 } else
2223 simulate_progress_bar();
2224 op_status = 0;
2225exit:
2226 operation_end(op_status);
2227 return 0;
2228}
bigbiffdf8436b2020-08-30 16:22:34 -04002229
2230
2231int GUIAction::enableadb(std::string arg __unused) {
2232 android::base::SetProperty("sys.usb.config", "none");
2233 android::base::SetProperty("sys.usb.config", "adb");
2234 return 0;
2235}
2236
2237int GUIAction::enablefastboot(std::string arg __unused) {
2238 android::base::SetProperty("sys.usb.config", "none");
2239 android::base::SetProperty("sys.usb.config", "fastboot");
2240 return 0;
2241}
Mohd Farazd59a6042020-08-12 12:29:42 +00002242
2243int GUIAction::changeterminal(std::string arg) {
2244 bool res = true;
2245 std::string resp, cmd = "cd " + arg;
2246 DataManager::GetValue("tw_terminal_location", resp);
2247 if (arg.empty() && !resp.empty()) {
2248 cmd = "cd /";
2249 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2250 term->NotifyCharInput(cmd.at(iter));
2251 term->NotifyCharInput(13);
2252 DataManager::SetValue("tw_terminal_location", "");
2253 return 0;
2254 }
2255 if (term != NULL && !arg.empty()) {
2256 DataManager::SetValue("tw_terminal_location", arg);
2257 if (term->status()) {
2258 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2259 term->NotifyCharInput(cmd.at(iter));
2260 term->NotifyCharInput(13);
2261 }
2262 else if (chdir(arg.c_str()) != 0) {
2263 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2264 res = false;
2265 }
2266 }
2267 else {
2268 res = false;
2269 LOGINFO("Unable to switch to Terminal\n");
2270 }
2271 if (res)
2272 gui_changePage("terminalcommand");
2273 return 0;
2274}