blob: 52d924b5795012119d9ef83b90c76cfd187cc30b [file] [log] [blame]
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001/*
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Matt Mower0c88b842017-01-15 16:00:49 -060035#include <private/android_filesystem_config.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
bigbiffd58ba182020-03-23 10:02:29 -040043#include "install/adb_install.h"
44
45#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050046#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050047#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010048
Dees_Troy51a0e822012-09-05 15:24:24 -040049extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000050#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
bigbiffd58ba182020-03-23 10:02:29 -040053#include "install/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
bigbiffd58ba182020-03-23 10:02:29 -040055#include "set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010056#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040057
58#include "rapidxml.hpp"
59#include "objects.hpp"
bigbiffd58ba182020-03-23 10:02:29 -040060#include "tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040061
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010067
that73a52952015-01-28 23:07:34 +010068static void *ActionThread_work_wrapper(void *data);
69
70class ActionThread
71{
72public:
73 ActionThread();
74 ~ActionThread();
75
76 void threadActions(GUIAction *act);
77 void run(void *data);
78private:
79 friend void *ActionThread_work_wrapper(void*);
80 struct ThreadData
81 {
82 ActionThread *this_;
83 GUIAction *act;
84 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
85 };
86
87 pthread_t m_thread;
88 bool m_thread_running;
89 pthread_mutex_t m_act_lock;
90};
91
92static ActionThread action_thread; // for all kinds of longer running actions
93static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010094
95static void *ActionThread_work_wrapper(void *data)
96{
that73a52952015-01-28 23:07:34 +010097 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010098 return NULL;
99}
100
101ActionThread::ActionThread()
102{
103 m_thread_running = false;
104 pthread_mutex_init(&m_act_lock, NULL);
105}
106
107ActionThread::~ActionThread()
108{
109 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600110 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100111 pthread_mutex_unlock(&m_act_lock);
112 pthread_join(m_thread, NULL);
113 } else {
114 pthread_mutex_unlock(&m_act_lock);
115 }
116 pthread_mutex_destroy(&m_act_lock);
117}
118
that7d3b54f2015-01-09 22:52:51 +0100119void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100120{
121 pthread_mutex_lock(&m_act_lock);
122 if (m_thread_running) {
123 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100124 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
125 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100126 } else {
127 m_thread_running = true;
128 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100129 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100130 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
131 }
132}
133
134void ActionThread::run(void *data)
135{
136 ThreadData *d = (ThreadData*)data;
137 GUIAction* act = d->act;
138
139 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100140 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100141 act->doAction(*it);
142
143 pthread_mutex_lock(&m_act_lock);
144 m_thread_running = false;
145 pthread_mutex_unlock(&m_act_lock);
146 delete d;
147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 xml_node<>* child;
153 xml_node<>* actions;
154 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
that3f7b1ac2014-12-30 11:30:13 +0100158 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100159#define ADD_ACTION(n) mf[#n] = &GUIAction::n
160#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100161 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100162 ADD_ACTION(reboot);
163 ADD_ACTION(home);
164 ADD_ACTION(key);
165 ADD_ACTION(page);
166 ADD_ACTION(reload);
167 ADD_ACTION(readBackup);
168 ADD_ACTION(set);
169 ADD_ACTION(clear);
170 ADD_ACTION(mount);
171 ADD_ACTION(unmount);
172 ADD_ACTION_EX("umount", unmount);
173 ADD_ACTION(restoredefaultsettings);
174 ADD_ACTION(copylog);
175 ADD_ACTION(compute);
176 ADD_ACTION_EX("addsubtract", compute);
177 ADD_ACTION(setguitimezone);
178 ADD_ACTION(overlay);
179 ADD_ACTION(queuezip);
180 ADD_ACTION(cancelzip);
181 ADD_ACTION(queueclear);
182 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500183 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600201 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600202 ADD_ACTION(togglebacklight);
thatc6085482015-01-09 22:12:43 +0100203
204 // remember actions that run in the caller thread
205 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
206 setActionsRunningInCallerThread.insert(it->first);
207
208 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100209 ADD_ACTION(flash);
210 ADD_ACTION(wipe);
211 ADD_ACTION(refreshsizes);
212 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600213 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100214 ADD_ACTION(fixpermissions);
215 ADD_ACTION(dd);
216 ADD_ACTION(partitionsd);
217 ADD_ACTION(installhtcdumlock);
218 ADD_ACTION(htcdumlockrestoreboot);
219 ADD_ACTION(htcdumlockreflashrecovery);
220 ADD_ACTION(cmd);
221 ADD_ACTION(terminalcommand);
222 ADD_ACTION(reinjecttwrp);
223 ADD_ACTION(decrypt);
224 ADD_ACTION(adbsideload);
225 ADD_ACTION(openrecoveryscript);
226 ADD_ACTION(installsu);
227 ADD_ACTION(decrypt_backup);
228 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500229 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100230 ADD_ACTION(changefilesystem);
231 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100232 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600233 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600234 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500235 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600236 ADD_ACTION(repackimage);
237 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100238 }
239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600241 actions = FindNode(node, "actions");
242 if (actions) child = FindNode(actions, "action");
243 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 while (child)
248 {
249 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 attr = child->first_attribute("function");
252 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 action.mFunction = attr->value();
255 action.mArg = child->value();
256 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 child = child->next_sibling("action");
259 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600262 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 if (child)
264 {
265 attr = child->first_attribute("key");
266 if (attr)
267 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100268 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600269 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100270 {
271 const int key = getKeyByName(keys[i]);
272 mKeys[key] = false;
273 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 }
275 else
276 {
277 attr = child->first_attribute("x");
278 if (!attr) return;
279 mActionX = atol(attr->value());
280 attr = child->first_attribute("y");
281 if (!attr) return;
282 mActionY = atol(attr->value());
283 attr = child->first_attribute("w");
284 if (!attr) return;
285 mActionW = atol(attr->value());
286 attr = child->first_attribute("h");
287 if (!attr) return;
288 mActionH = atol(attr->value());
289 }
290 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
Matt Mower25036b72016-06-24 12:30:18 -0500293int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400294{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (state == TOUCH_RELEASE)
296 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299}
300
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100301int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400302{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100303 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600304 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100305 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100306
307 bool prevState = itr->second;
308 itr->second = down;
309
310 // If there is only one key for this action, wait for key up so it
311 // doesn't trigger with multi-key actions.
312 // Else, check if all buttons are pressed, then consume their release events
313 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600314 if (mKeys.size() == 1) {
315 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100316 doActions();
thatd4725ca2016-01-19 00:15:21 +0100317 return 0;
318 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600319 } else if (down) {
320 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
321 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100322 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100323 }
324
325 // Passed, all req buttons are pressed, reset them and consume release events
326 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600327 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100328 kb->ConsumeKeyRelease(itr->first);
329 itr->second = false;
330 }
331
332 doActions();
thatd4725ca2016-01-19 00:15:21 +0100333 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100334 }
335
thatd4725ca2016-01-19 00:15:21 +0100336 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337}
338
Vojtech Bocek07220562014-02-08 02:05:33 +0100339int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400340{
Vojtech Bocek07220562014-02-08 02:05:33 +0100341 GUIObject::NotifyVarChange(varName, value);
342
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100343 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600345 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200346 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200348 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
351void GUIAction::simulate_progress_bar(void)
352{
Ethan Yonker74db1572015-10-28 12:44:49 -0500353 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 for (int i = 0; i < 5; i++)
355 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500356 if (PartitionManager.stop_backup.get_value()) {
357 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600358 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500359 DataManager::SetValue("ui_progress", 0);
360 PartitionManager.stop_backup.set_value(0);
361 return;
362 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 usleep(500000);
364 DataManager::SetValue("ui_progress", i * 20);
365 }
366}
367
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600368int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400369{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
372 DataManager::SetValue("ui_progress", 0);
373
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 if (filename.empty())
375 {
376 LOGERR("No file specified.\n");
377 return -1;
378 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Ethan Yonker9598c072016-01-15 21:54:34 -0600380 if (!TWFunc::Path_Exists(filename)) {
381 if (!PartitionManager.Mount_By_Path(filename, true)) {
382 return -1;
383 }
384 if (!TWFunc::Path_Exists(filename)) {
385 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
386 return -1;
387 }
388 }
Dees_Troy657c3092012-09-10 20:32:10 -0400389
Dees_Troy51a0e822012-09-05 15:24:24 -0400390 if (simulate) {
391 simulate_progress_bar();
392 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400393 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400394
395 // Now, check if we need to ensure TWRP remains installed...
396 struct stat st;
397 if (stat("/sbin/installTwrp", &st) == 0)
398 {
399 DataManager::SetValue("tw_operation", "Configuring TWRP");
400 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500401 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200402 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500404 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 }
406 }
407 }
408
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200409 // Done
410 DataManager::SetValue("ui_progress", 100);
411 DataManager::SetValue("ui_progress", 0);
412 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413}
414
that73a52952015-01-28 23:07:34 +0100415GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100416{
that73a52952015-01-28 23:07:34 +0100417 string func = gui_parse_text(action.mFunction);
418 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
419 if (needsThread) {
420 if (func == "cancelbackup")
421 return THREAD_CANCEL;
422 else
423 return THREAD_ACTION;
424 }
425 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100426}
427
Dees_Troy51a0e822012-09-05 15:24:24 -0400428int GUIAction::doActions()
429{
that3f7b1ac2014-12-30 11:30:13 +0100430 if (mActions.size() < 1)
431 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432
that73a52952015-01-28 23:07:34 +0100433 // Determine in which thread to run the actions.
434 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
435 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100436 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100437 for (it = mActions.begin(); it != mActions.end(); ++it) {
438 ThreadType tt = getThreadType(*it);
439 if (tt == THREAD_NONE)
440 continue;
441 if (threadType == THREAD_NONE)
442 threadType = tt;
443 else if (threadType != tt) {
444 LOGERR("Can't mix normal and cancel actions in the same list.\n"
445 "Running the whole batch in the cancel thread.\n");
446 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100447 break;
448 }
that7d3b54f2015-01-09 22:52:51 +0100449 }
that73a52952015-01-28 23:07:34 +0100450
451 // Now run the actions in the desired thread.
452 switch (threadType) {
453 case THREAD_ACTION:
454 action_thread.threadActions(this);
455 break;
456
457 case THREAD_CANCEL:
458 cancel_thread.threadActions(this);
459 break;
460
461 default: {
462 // no iterators here because theme reloading might kill our object
463 const size_t cnt = mActions.size();
464 for (size_t i = 0; i < cnt; ++i)
465 doAction(mActions[i]);
466 }
thatc6085482015-01-09 22:12:43 +0100467 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200469 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400470}
471
that3f7b1ac2014-12-30 11:30:13 +0100472int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400473{
that3f7b1ac2014-12-30 11:30:13 +0100474 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475
that3f7b1ac2014-12-30 11:30:13 +0100476 std::string function = gui_parse_text(action.mFunction);
477 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478
that3f7b1ac2014-12-30 11:30:13 +0100479 // find function and execute it
480 mapFunc::const_iterator funcitr = mf.find(function);
481 if (funcitr != mf.end())
482 return (this->*funcitr->second)(arg);
483
484 LOGERR("Unknown action '%s'\n", function.c_str());
485 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486}
487
488void GUIAction::operation_start(const string operation_name)
489{
that3f7b1ac2014-12-30 11:30:13 +0100490 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000491 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 DataManager::SetValue(TW_ACTION_BUSY, 1);
493 DataManager::SetValue("ui_progress", 0);
494 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600496 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500497 bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR;
498 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400499}
500
that3f7b1ac2014-12-30 11:30:13 +0100501void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400502{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000503 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 DataManager::SetValue("ui_progress", 100);
506 if (simulate) {
507 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
508 if (simulate_fail != 0)
509 DataManager::SetValue("tw_operation_status", 1);
510 else
511 DataManager::SetValue("tw_operation_status", 0);
512 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500513 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500515 }
516 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500518 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 }
520 DataManager::SetValue("tw_operation_state", 1);
521 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500522 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200523 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000524 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400525
526#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000527 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600528 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400529#endif
530
that3f7b1ac2014-12-30 11:30:13 +0100531 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400532}
533
that3f7b1ac2014-12-30 11:30:13 +0100534int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400535{
that3f7b1ac2014-12-30 11:30:13 +0100536 sync();
537 DataManager::SetValue("tw_gui_done", 1);
538 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400539
that3f7b1ac2014-12-30 11:30:13 +0100540 return 0;
541}
Dees_Troy51a0e822012-09-05 15:24:24 -0400542
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500543int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100544{
that3f7b1ac2014-12-30 11:30:13 +0100545 gui_changePage("main");
546 return 0;
547}
Dees_Troy51a0e822012-09-05 15:24:24 -0400548
that3f7b1ac2014-12-30 11:30:13 +0100549int GUIAction::key(std::string arg)
550{
551 const int key = getKeyByName(arg);
552 PageManager::NotifyKey(key, true);
553 PageManager::NotifyKey(key, false);
554 return 0;
555}
556
557int GUIAction::page(std::string arg)
558{
LuK133762326f42015-09-30 19:57:46 +0200559 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100560 std::string page_name = gui_parse_text(arg);
561 return gui_changePage(page_name);
562}
563
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500564int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100565{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500566 PageManager::RequestReload();
567 // The actual reload is handled in pages.cpp in PageManager::RunReload()
568 // The reload will occur on the next Update or Render call and will
569 // be performed in the rendoer thread instead of the action thread
570 // to prevent crashing which could occur when we start deleting
571 // GUI resources in the action thread while we attempt to render
572 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100573 return 0;
574}
Dees_Troy51a0e822012-09-05 15:24:24 -0400575
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500576int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100577{
578 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400579
that3f7b1ac2014-12-30 11:30:13 +0100580 DataManager::GetValue("tw_restore", Restore_Name);
581 PartitionManager.Set_Restore_Files(Restore_Name);
582 return 0;
583}
584
585int GUIAction::set(std::string arg)
586{
587 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 {
that3f7b1ac2014-12-30 11:30:13 +0100589 string varName = arg.substr(0, arg.find('='));
590 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400591
that3f7b1ac2014-12-30 11:30:13 +0100592 DataManager::GetValue(value, value);
593 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 }
that3f7b1ac2014-12-30 11:30:13 +0100595 else
596 DataManager::SetValue(arg, "1");
597 return 0;
598}
Dees_Troy51a0e822012-09-05 15:24:24 -0400599
that3f7b1ac2014-12-30 11:30:13 +0100600int GUIAction::clear(std::string arg)
601{
602 DataManager::SetValue(arg, "0");
603 return 0;
604}
Dees_Troy51a0e822012-09-05 15:24:24 -0400605
that3f7b1ac2014-12-30 11:30:13 +0100606int GUIAction::mount(std::string arg)
607{
608 if (arg == "usb") {
609 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400610 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100611 PartitionManager.usb_storage_enable();
612 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500613 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100614 } else if (!simulate) {
615 PartitionManager.Mount_By_Path(arg, true);
616 PartitionManager.Add_MTP_Storage(arg);
617 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100619 return 0;
620}
621
622int GUIAction::unmount(std::string arg)
623{
624 if (arg == "usb") {
625 if (!simulate)
626 PartitionManager.usb_storage_disable();
627 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500628 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100629 DataManager::SetValue(TW_ACTION_BUSY, 0);
630 } else if (!simulate) {
631 PartitionManager.UnMount_By_Path(arg, true);
632 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500633 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100634 return 0;
635}
636
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500637int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100638{
639 operation_start("Restore Defaults");
640 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500641 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100642 else {
643 DataManager::ResetDefaults();
644 PartitionManager.Update_System_Details();
645 PartitionManager.Mount_Current_Storage(true);
646 }
647 operation_end(0);
648 return 0;
649}
650
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500651int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100652{
653 operation_start("Copy Log");
654 if (!simulate)
655 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400656 string dst, curr_storage;
657 int copy_kernel_log = 0;
658
659 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100660 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400661 curr_storage = DataManager::GetCurrentStoragePath();
662 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100663 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
664 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400665 if (copy_kernel_log)
666 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100667 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400668 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100669 } else
670 simulate_progress_bar();
671 operation_end(0);
672 return 0;
673}
674
675
676int GUIAction::compute(std::string arg)
677{
678 if (arg.find("+") != string::npos)
679 {
680 string varName = arg.substr(0, arg.find('+'));
681 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
682 int amount_to_add = atoi(string_to_add.c_str());
683 int value;
684
685 DataManager::GetValue(varName, value);
686 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 return 0;
688 }
that3f7b1ac2014-12-30 11:30:13 +0100689 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 {
that3f7b1ac2014-12-30 11:30:13 +0100691 string varName = arg.substr(0, arg.find('-'));
692 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
693 int amount_to_subtract = atoi(string_to_subtract.c_str());
694 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400695
that3f7b1ac2014-12-30 11:30:13 +0100696 DataManager::GetValue(varName, value);
697 value -= amount_to_subtract;
698 if (value <= 0)
699 value = 0;
700 DataManager::SetValue(varName, value);
701 return 0;
702 }
703 if (arg.find("*") != string::npos)
704 {
705 string varName = arg.substr(0, arg.find('*'));
706 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
707 int multiply_by = atoi(multiply_by_str.c_str());
708 int value;
709
710 DataManager::GetValue(varName, value);
711 DataManager::SetValue(varName, value*multiply_by);
712 return 0;
713 }
714 if (arg.find("/") != string::npos)
715 {
716 string varName = arg.substr(0, arg.find('/'));
717 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
718 int divide_by = atoi(divide_by_str.c_str());
719 int value;
720
Matt Mowera8a89d12016-12-30 18:10:37 -0600721 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100722 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400723 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100724 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 }
726 return 0;
727 }
that3f7b1ac2014-12-30 11:30:13 +0100728 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
729 return -1;
730}
Dees_Troy51a0e822012-09-05 15:24:24 -0400731
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500732int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100733{
734 string SelectedZone;
735 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
736 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
737 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
738
739 int dst;
740 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
741
742 string offset;
743 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
744
745 string NewTimeZone = Zone;
746 if (offset != "0")
747 NewTimeZone += ":" + offset;
748
749 if (dst != 0)
750 NewTimeZone += DSTZone;
751
752 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
753 DataManager::update_tz_environment_variables();
754 return 0;
755}
756
757int GUIAction::overlay(std::string arg)
758{
759 return gui_changeOverlay(arg);
760}
761
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500762int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100763{
764 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500765 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 return 0;
767 }
that3f7b1ac2014-12-30 11:30:13 +0100768 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
769 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
770 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100772 }
773 return 0;
774}
775
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500776int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100777{
778 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500779 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100781 } else {
782 zip_queue_index--;
783 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 }
that3f7b1ac2014-12-30 11:30:13 +0100785 return 0;
786}
Dees_Troy51a0e822012-09-05 15:24:24 -0400787
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500788int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100789{
790 zip_queue_index = 0;
791 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
792 return 0;
793}
Dees_Troy51a0e822012-09-05 15:24:24 -0400794
that3f7b1ac2014-12-30 11:30:13 +0100795int GUIAction::sleep(std::string arg)
796{
797 operation_start("Sleep");
798 usleep(atoi(arg.c_str()));
799 operation_end(0);
800 return 0;
801}
Dees Troyb21cc642013-09-10 17:36:41 +0000802
Matt Mower9a2a2052016-05-31 21:31:22 -0500803int GUIAction::sleepcounter(std::string arg)
804{
805 operation_start("SleepCounter");
806 // Ensure user notices countdown in case it needs to be cancelled
807 blankTimer.resetTimerAndUnblank();
808 int total = atoi(arg.c_str());
809 for (int t = total; t > 0; t--) {
810 int progress = (int)(((float)(total-t)/(float)total)*100.0);
811 DataManager::SetValue("ui_progress", progress);
812 ::sleep(1);
813 DataManager::SetValue("tw_sleep", t-1);
814 }
815 DataManager::SetValue("ui_progress", 100);
816 operation_end(0);
817 return 0;
818}
819
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500820int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100821{
822 operation_start("AppendDateToBackupName");
823 string Backup_Name;
824 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
825 Backup_Name += TWFunc::Get_Current_Date();
826 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
827 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
828 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500829 PageManager::NotifyKey(KEY_END, true);
830 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100831 operation_end(0);
832 return 0;
833}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500834
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500835int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100836{
837 operation_start("GenerateBackupName");
838 TWFunc::Auto_Generate_Backup_Name();
839 operation_end(0);
840 return 0;
841}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500842
Ethan Yonker483e9f42016-01-11 22:21:18 -0600843int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100844{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600845 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100846 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500847
Ethan Yonker483e9f42016-01-11 22:21:18 -0600848 if (arg.empty())
849 arg = "tw_wipe_list";
850 DataManager::GetValue(arg, List);
851 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
852 if (!List.empty()) {
853 size_t start_pos = 0, end_pos = List.find(";", start_pos);
854 while (end_pos != string::npos && start_pos < List.size()) {
855 part_path = List.substr(start_pos, end_pos - start_pos);
856 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
857 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100858 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 } else {
that3f7b1ac2014-12-30 11:30:13 +0100860 count++;
861 }
862 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100864 }
865 DataManager::SetValue("tw_check_partition_list", count);
866 } else {
867 DataManager::SetValue("tw_check_partition_list", 0);
868 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600869 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100870}
Dees_Troy51a0e822012-09-05 15:24:24 -0400871
Ethan Yonker483e9f42016-01-11 22:21:18 -0600872int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100873{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600874 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400875
Ethan Yonker483e9f42016-01-11 22:21:18 -0600876 if (arg.empty())
877 arg = "tw_wipe_list";
878 DataManager::GetValue(arg, List);
879 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
880 if (!List.empty()) {
881 size_t start_pos = 0, end_pos = List.find(";", start_pos);
882 part_path = List;
883 while (end_pos != string::npos && start_pos < List.size()) {
884 part_path = List.substr(start_pos, end_pos - start_pos);
885 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
886 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100887 // Do nothing
888 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100890 break;
891 }
892 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600893 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100894 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600895 if (!part_path.empty()) {
896 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100897 if (Part) {
898 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500899
that3f7b1ac2014-12-30 11:30:13 +0100900 DataManager::SetValue("tw_partition_name", Part->Display_Name);
901 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
902 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
903 DataManager::SetValue("tw_partition_size", Part->Size / mb);
904 DataManager::SetValue("tw_partition_used", Part->Used / mb);
905 DataManager::SetValue("tw_partition_free", Part->Free / mb);
906 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
907 DataManager::SetValue("tw_partition_removable", Part->Removable);
908 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
909
910 if (Part->Can_Repair())
911 DataManager::SetValue("tw_partition_can_repair", 1);
912 else
913 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500914 if (Part->Can_Resize())
915 DataManager::SetValue("tw_partition_can_resize", 1);
916 else
917 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600918 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100919 DataManager::SetValue("tw_partition_vfat", 1);
920 else
921 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600922 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100923 DataManager::SetValue("tw_partition_exfat", 1);
924 else
925 DataManager::SetValue("tw_partition_exfat", 0);
926 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
927 DataManager::SetValue("tw_partition_f2fs", 1);
928 else
929 DataManager::SetValue("tw_partition_f2fs", 0);
930 if (TWFunc::Path_Exists("/sbin/mke2fs"))
931 DataManager::SetValue("tw_partition_ext", 1);
932 else
933 DataManager::SetValue("tw_partition_ext", 0);
934 return 0;
935 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600936 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100937 }
938 }
939 }
940 DataManager::SetValue("tw_partition_name", "");
941 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600942 // Set this to 0 to prevent trying to partition this device, just in case
943 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100944 return 0;
945}
946
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500947int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100948{
949 time_t tm;
950 char path[256];
951 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600952 uid_t uid = AID_MEDIA_RW;
953 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100954
955 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600956 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100957 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
958 } else {
959 strcpy(path, "/tmp/");
960 }
961
Matt Mowera8a89d12016-12-30 18:10:37 -0600962 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100963 return 0;
964
965 tm = time(NULL);
966 path_len = strlen(path);
967
968 // Screenshot_2014-01-01-18-21-38.png
969 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
970
971 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600972 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100973 chmod(path, 0666);
974 chown(path, uid, gid);
975
that677b13f2015-12-26 23:57:31 +0100976 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100977
VDavid0032034a412019-10-18 22:43:20 +0200978 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100979 gr_color(255, 255, 255, 255);
980 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
981 gr_flip();
982 gui_forceRender();
983 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500984 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100985 }
986 return 0;
987}
988
989int GUIAction::setbrightness(std::string arg)
990{
991 return TWFunc::Set_Brightness(arg);
992}
993
994int GUIAction::fileexists(std::string arg)
995{
996 struct stat st;
997 string newpath = arg + "/.";
998
999 operation_start("FileExists");
1000 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1001 operation_end(0);
1002 else
1003 operation_end(1);
1004 return 0;
1005}
1006
thatcc8ddca2015-01-03 01:59:36 +01001007void GUIAction::reinject_after_flash()
1008{
1009 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001010 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001011 if (simulate) {
1012 simulate_progress_bar();
1013 } else {
1014 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1015 if (Boot == NULL || Boot->Current_File_System != "emmc")
1016 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1017 else {
1018 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1019 TWFunc::Exec_Cmd(injectcmd);
1020 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001021 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001022 }
1023 }
1024}
1025
that3f7b1ac2014-12-30 11:30:13 +01001026int GUIAction::flash(std::string arg)
1027{
1028 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001029 // We're going to jump to this page first, like a loading page
1030 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001031 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001032 string zip_path = zip_queue[i];
1033 size_t slashpos = zip_path.find_last_of('/');
1034 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001035 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001036 DataManager::SetValue("tw_filename", zip_path);
1037 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001038 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1039
1040 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001041 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001042 TWFunc::SetPerformanceMode(false);
1043 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001044 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001045 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001046 break;
that3f7b1ac2014-12-30 11:30:13 +01001047 }
1048 }
1049 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001050
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001051 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001052 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001053 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001054 }
that3f7b1ac2014-12-30 11:30:13 +01001055
thatcc8ddca2015-01-03 01:59:36 +01001056 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001057 PartitionManager.Update_System_Details();
1058 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001059 // 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 -06001060 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001061 return 0;
1062}
1063
1064int GUIAction::wipe(std::string arg)
1065{
1066 operation_start("Format");
1067 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001068 int ret_val = false;
1069
1070 if (simulate) {
1071 simulate_progress_bar();
1072 } else {
1073 if (arg == "data")
1074 ret_val = PartitionManager.Factory_Reset();
1075 else if (arg == "battery")
1076 ret_val = PartitionManager.Wipe_Battery_Stats();
1077 else if (arg == "rotate")
1078 ret_val = PartitionManager.Wipe_Rotate_Data();
1079 else if (arg == "dalvik")
1080 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1081 else if (arg == "DATAMEDIA") {
1082 ret_val = PartitionManager.Format_Data();
1083 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001084 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001085
1086 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1087 if (has_datamedia) {
1088 ret_val = PartitionManager.Wipe_Media_From_Data();
1089 } else {
1090 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1091 }
1092 } else if (arg == "EXTERNAL") {
1093 string External_Path;
1094
1095 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1096 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1097 } else if (arg == "ANDROIDSECURE") {
1098 ret_val = PartitionManager.Wipe_Android_Secure();
1099 } else if (arg == "LIST") {
1100 string Wipe_List, wipe_path;
1101 bool skip = false;
1102 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001103
1104 DataManager::GetValue("tw_wipe_list", Wipe_List);
1105 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1106 if (!Wipe_List.empty()) {
1107 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1108 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1109 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1110 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1111 if (wipe_path == "/and-sec") {
1112 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001113 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001114 ret_val = false;
1115 break;
1116 } else {
1117 skip = true;
1118 }
1119 } else if (wipe_path == "DALVIK") {
1120 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001121 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001122 ret_val = false;
1123 break;
1124 } else {
1125 skip = true;
1126 }
1127 } else if (wipe_path == "INTERNAL") {
1128 if (!PartitionManager.Wipe_Media_From_Data()) {
1129 ret_val = false;
1130 break;
1131 } else {
1132 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001133 }
1134 }
that3f7b1ac2014-12-30 11:30:13 +01001135 if (!skip) {
1136 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001137 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001138 ret_val = false;
1139 break;
1140 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1141 arg = wipe_path;
1142 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 } else {
that3f7b1ac2014-12-30 11:30:13 +01001144 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 }
that3f7b1ac2014-12-30 11:30:13 +01001146 start_pos = end_pos + 1;
1147 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001148 }
1149 }
that3f7b1ac2014-12-30 11:30:13 +01001150 } else
1151 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001152#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001153 if (arg == DataManager::GetSettingsStoragePath()) {
1154 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1155 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001156
that3f7b1ac2014-12-30 11:30:13 +01001157 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1158 LOGINFO("Making TWRP folder and saving settings.\n");
1159 Storage_Path += "/TWRP";
1160 mkdir(Storage_Path.c_str(), 0777);
1161 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001162 } else {
that3f7b1ac2014-12-30 11:30:13 +01001163 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1164 }
1165 }
1166#endif
1167 }
1168 PartitionManager.Update_System_Details();
1169 if (ret_val)
1170 ret_val = 0; // 0 is success
1171 else
1172 ret_val = 1; // 1 is failure
1173 operation_end(ret_val);
1174 return 0;
1175}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001176
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001177int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001178{
1179 operation_start("Refreshing Sizes");
1180 if (simulate) {
1181 simulate_progress_bar();
1182 } else
1183 PartitionManager.Update_System_Details();
1184 operation_end(0);
1185 return 0;
1186}
1187
1188int GUIAction::nandroid(std::string arg)
1189{
that3f7b1ac2014-12-30 11:30:13 +01001190 if (simulate) {
that73a52952015-01-28 23:07:34 +01001191 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001192 DataManager::SetValue("tw_partition", "Simulation");
1193 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001194 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001195 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001196 operation_start("Nandroid");
1197 int ret = 0;
1198
that3f7b1ac2014-12-30 11:30:13 +01001199 if (arg == "backup") {
1200 string Backup_Name;
1201 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001202 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001203 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 -05001204 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001205 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1206 if (!PartitionManager.stop_backup.get_value()) {
1207 if (ret == false)
1208 ret = 1; // 1 for failure
1209 else
1210 ret = 0; // 0 for success
1211 DataManager::SetValue("tw_cancel_backup", 0);
1212 } else {
1213 DataManager::SetValue("tw_cancel_backup", 1);
1214 gui_msg("backup_cancel=Backup Cancelled");
1215 ret = 0;
1216 }
bigbiffce8f83c2015-12-12 18:30:21 -05001217 } else {
that3f7b1ac2014-12-30 11:30:13 +01001218 operation_end(1);
1219 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001220 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001221 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001222 } else if (arg == "restore") {
1223 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001224 int gui_adb_backup;
1225
that3f7b1ac2014-12-30 11:30:13 +01001226 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001227 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1228 if (gui_adb_backup) {
1229 DataManager::SetValue("tw_operation_state", 1);
1230 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1231 ret = 0; // success
1232 else
1233 ret = 1; // failure
1234 DataManager::SetValue("tw_enable_adb_backup", 0);
1235 ret = 0; // assume success???
1236 } else {
1237 if (PartitionManager.Run_Restore(Restore_Name))
1238 ret = 0; // success
1239 else
1240 ret = 1; // failure
1241 }
that3f7b1ac2014-12-30 11:30:13 +01001242 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001243 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001244 return -1;
1245 }
that73a52952015-01-28 23:07:34 +01001246 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001247 return ret;
1248 }
1249 return 0;
1250}
1251
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001252int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001253 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001254 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001255 }
1256 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001257 int op_status = PartitionManager.Cancel_Backup();
1258 if (op_status != 0)
1259 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001260 }
1261
1262 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001263}
Dees_Troy51a0e822012-09-05 15:24:24 -04001264
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001265int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001266{
that73a52952015-01-28 23:07:34 +01001267 int op_status = 0;
1268
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001269 operation_start("Fix Contexts");
1270 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001271 if (simulate) {
1272 simulate_progress_bar();
1273 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001274 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001275 if (op_status != 0)
1276 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001277 }
that73a52952015-01-28 23:07:34 +01001278 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001279 return 0;
1280}
1281
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001282int GUIAction::fixpermissions(std::string arg)
1283{
1284 return fixcontexts(arg);
1285}
1286
that3f7b1ac2014-12-30 11:30:13 +01001287int GUIAction::dd(std::string arg)
1288{
1289 operation_start("imaging");
1290
1291 if (simulate) {
1292 simulate_progress_bar();
1293 } else {
1294 string cmd = "dd " + arg;
1295 TWFunc::Exec_Cmd(cmd);
1296 }
1297 operation_end(0);
1298 return 0;
1299}
1300
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001301int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001302{
1303 operation_start("Partition SD Card");
1304 int ret_val = 0;
1305
1306 if (simulate) {
1307 simulate_progress_bar();
1308 } else {
1309 int allow_partition;
1310 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1311 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001312 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001313 } else {
1314 if (!PartitionManager.Partition_SDCard())
1315 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001316 }
that3f7b1ac2014-12-30 11:30:13 +01001317 }
1318 operation_end(ret_val);
1319 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001320
that3f7b1ac2014-12-30 11:30:13 +01001321}
Dees_Troy51a0e822012-09-05 15:24:24 -04001322
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001323int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001324{
1325 operation_start("Install HTC Dumlock");
1326 if (simulate) {
1327 simulate_progress_bar();
1328 } else
1329 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001330
that3f7b1ac2014-12-30 11:30:13 +01001331 operation_end(0);
1332 return 0;
1333}
Dees_Troy51a0e822012-09-05 15:24:24 -04001334
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001335int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001336{
1337 operation_start("HTC Dumlock Restore Boot");
1338 if (simulate) {
1339 simulate_progress_bar();
1340 } else
1341 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001342
that3f7b1ac2014-12-30 11:30:13 +01001343 operation_end(0);
1344 return 0;
1345}
Dees_Troy51a0e822012-09-05 15:24:24 -04001346
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001347int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001348{
1349 operation_start("HTC Dumlock Reflash Recovery");
1350 if (simulate) {
1351 simulate_progress_bar();
1352 } else
1353 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001354
that3f7b1ac2014-12-30 11:30:13 +01001355 operation_end(0);
1356 return 0;
1357}
Dees_Troy51a0e822012-09-05 15:24:24 -04001358
that3f7b1ac2014-12-30 11:30:13 +01001359int GUIAction::cmd(std::string arg)
1360{
1361 int op_status = 0;
1362
1363 operation_start("Command");
1364 LOGINFO("Running command: '%s'\n", arg.c_str());
1365 if (simulate) {
1366 simulate_progress_bar();
1367 } else {
1368 op_status = TWFunc::Exec_Cmd(arg);
1369 if (op_status != 0)
1370 op_status = 1;
1371 }
1372
1373 operation_end(op_status);
1374 return 0;
1375}
1376
1377int GUIAction::terminalcommand(std::string arg)
1378{
1379 int op_status = 0;
1380 string cmdpath, command;
1381
1382 DataManager::GetValue("tw_terminal_location", cmdpath);
1383 operation_start("CommandOutput");
1384 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1385 if (simulate) {
1386 simulate_progress_bar();
1387 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001388 } else if (arg == "exit") {
1389 LOGINFO("Exiting terminal\n");
1390 operation_end(op_status);
1391 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001392 } else {
1393 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1394 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001395 DataManager::SetValue("tw_terminal_state", 1);
1396 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001397 FILE* fp;
1398 char line[512];
1399
1400 fp = popen(command.c_str(), "r");
1401 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001402 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001403 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001404 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001405 struct timeval timeout;
1406 fd_set fdset;
1407
Matt Mowera8a89d12016-12-30 18:10:37 -06001408 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001409 {
1410 FD_ZERO(&fdset);
1411 FD_SET(fd, &fdset);
1412 timeout.tv_sec = 0;
1413 timeout.tv_usec = 400000;
1414 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1415 if (has_data == 0) {
1416 // Timeout reached
1417 DataManager::GetValue("tw_terminal_state", check);
1418 if (check == 0) {
1419 keep_going = 0;
1420 }
1421 } else if (has_data < 0) {
1422 // End of execution
1423 keep_going = 0;
1424 } else {
1425 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001426 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001427 gui_print("%s", line); // Display output
1428 else
1429 keep_going = 0; // Done executing
1430 }
1431 }
1432 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001433 }
thatc6085482015-01-09 22:12:43 +01001434 DataManager::SetValue("tw_operation_status", 0);
1435 DataManager::SetValue("tw_operation_state", 1);
1436 DataManager::SetValue("tw_terminal_state", 0);
1437 DataManager::SetValue("tw_background_thread_running", 0);
1438 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001439 }
1440 return 0;
1441}
1442
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001443int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001444{
that3f7b1ac2014-12-30 11:30:13 +01001445 LOGINFO("Sending kill command...\n");
1446 operation_start("KillCommand");
1447 DataManager::SetValue("tw_operation_status", 0);
1448 DataManager::SetValue("tw_operation_state", 1);
1449 DataManager::SetValue("tw_terminal_state", 0);
1450 DataManager::SetValue("tw_background_thread_running", 0);
1451 DataManager::SetValue(TW_ACTION_BUSY, 0);
1452 return 0;
1453}
1454
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001455int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001456{
1457 int op_status = 0;
1458 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001459 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001460 if (simulate) {
1461 simulate_progress_bar();
1462 } else {
1463 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001465 }
1466
1467 operation_end(op_status);
1468 return 0;
1469}
1470
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001471int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001472{
1473 int op_status = 0;
1474
1475 operation_start("CheckBackupName");
1476 if (simulate) {
1477 simulate_progress_bar();
1478 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001479 string Backup_Name;
1480 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1481 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001482 if (op_status != 0)
1483 op_status = 1;
1484 }
1485
1486 operation_end(op_status);
1487 return 0;
1488}
1489
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001490int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001491{
1492 int op_status = 0;
1493
1494 operation_start("Decrypt");
1495 if (simulate) {
1496 simulate_progress_bar();
1497 } else {
1498 string Password;
1499 DataManager::GetValue("tw_crypto_password", Password);
1500 op_status = PartitionManager.Decrypt_Device(Password);
1501 if (op_status != 0)
1502 op_status = 1;
1503 else {
that3f7b1ac2014-12-30 11:30:13 +01001504
1505 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1506
Ethan Yonkercf50da52015-01-12 21:59:07 -06001507 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001508
Ethan Yonkercf50da52015-01-12 21:59:07 -06001509 // Check for a custom theme and load it if exists
1510 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1511 if (has_datamedia != 0) {
1512 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001513 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001514 } else {
1515 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001516 }
1517 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001518 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001519 }
1520 }
1521
1522 operation_end(op_status);
1523 return 0;
1524}
1525
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001526int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001527{
1528 operation_start("Sideload");
1529 if (simulate) {
1530 simulate_progress_bar();
1531 operation_end(0);
1532 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001533 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001534 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1535
1536 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001537 // int ret = apply_from_adb("/", &sideload_child_pid);
1538 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
1539 int ret = ApplyFromAdb("/", &reboot_action);
thatc6085482015-01-09 22:12:43 +01001540 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1541
1542 if (ret != 0) {
1543 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001544 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001545 ret = 1; // failure
1546 } else {
1547 int wipe_cache = 0;
1548 int wipe_dalvik = 0;
1549 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1550
1551 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1552 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1553 PartitionManager.Wipe_By_Path("/cache");
1554 if (wipe_dalvik)
1555 PartitionManager.Wipe_Dalvik_Cache();
1556 } else {
1557 ret = 1; // failure
1558 }
thatcc8ddca2015-01-03 01:59:36 +01001559 }
thatc6085482015-01-09 22:12:43 +01001560 if (sideload_child_pid) {
1561 LOGINFO("Signaling child sideload process to exit.\n");
1562 struct stat st;
1563 // Calling stat() on this magic filename signals the minadbd
1564 // subprocess to shut down.
1565 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1566 int status;
1567 LOGINFO("Waiting for child sideload process to exit.\n");
1568 waitpid(sideload_child_pid, &status, 0);
1569 }
Dees Troy28a85d22015-04-28 02:03:16 +00001570 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001571 TWFunc::Toggle_MTP(mtp_was_enabled);
1572 reinject_after_flash();
1573 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001574 }
that3f7b1ac2014-12-30 11:30:13 +01001575 return 0;
1576}
1577
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001578int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001579{
that3f7b1ac2014-12-30 11:30:13 +01001580 struct stat st;
1581 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001582 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001583 LOGINFO("Signaling child sideload process to exit.\n");
1584 // Calling stat() on this magic filename signals the minadbd
1585 // subprocess to shut down.
1586 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1587 if (!sideload_child_pid) {
1588 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001589 return 0;
1590 }
thatcc8ddca2015-01-03 01:59:36 +01001591 ::sleep(1);
1592 LOGINFO("Killing child sideload process.\n");
1593 kill(sideload_child_pid, SIGTERM);
1594 int status;
1595 LOGINFO("Waiting for child sideload process to exit.\n");
1596 waitpid(sideload_child_pid, &status, 0);
1597 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001598 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1599 return 0;
1600}
1601
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001602int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001603{
1604 operation_start("OpenRecoveryScript");
1605 if (simulate) {
1606 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001607 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001608 } else {
that10ae24f2015-12-26 20:53:51 +01001609 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001610 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001611 }
1612 return 0;
1613}
1614
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001615int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001616{
1617 int op_status = 0;
1618
1619 operation_start("Install SuperSU");
1620 if (simulate) {
1621 simulate_progress_bar();
1622 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001623 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001624 }
1625
1626 operation_end(op_status);
1627 return 0;
1628}
1629
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001630int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001631{
1632 int op_status = 0;
1633
1634 operation_start("Fixing Superuser Permissions");
1635 if (simulate) {
1636 simulate_progress_bar();
1637 } else {
1638 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1639 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1640 }
1641
1642 operation_end(op_status);
1643 return 0;
1644}
1645
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001646int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001647{
1648 int op_status = 0;
1649
1650 operation_start("Try Restore Decrypt");
1651 if (simulate) {
1652 simulate_progress_bar();
1653 } else {
1654 string Restore_Path, Filename, Password;
1655 DataManager::GetValue("tw_restore", Restore_Path);
1656 Restore_Path += "/";
1657 DataManager::GetValue("tw_restore_password", Password);
1658 TWFunc::SetPerformanceMode(true);
1659 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1660 op_status = 0; // success
1661 else
1662 op_status = 1; // fail
1663 TWFunc::SetPerformanceMode(false);
1664 }
1665
1666 operation_end(op_status);
1667 return 0;
1668}
1669
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001670int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001671{
1672 int op_status = 0;
1673
1674 operation_start("Repair Partition");
1675 if (simulate) {
1676 simulate_progress_bar();
1677 } else {
1678 string part_path;
1679 DataManager::GetValue("tw_partition_mount_point", part_path);
1680 if (PartitionManager.Repair_By_Path(part_path, true)) {
1681 op_status = 0; // success
1682 } else {
that3f7b1ac2014-12-30 11:30:13 +01001683 op_status = 1; // fail
1684 }
1685 }
1686
1687 operation_end(op_status);
1688 return 0;
1689}
1690
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001691int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001692{
1693 int op_status = 0;
1694
1695 operation_start("Resize Partition");
1696 if (simulate) {
1697 simulate_progress_bar();
1698 } else {
1699 string part_path;
1700 DataManager::GetValue("tw_partition_mount_point", part_path);
1701 if (PartitionManager.Resize_By_Path(part_path, true)) {
1702 op_status = 0; // success
1703 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001704 op_status = 1; // fail
1705 }
1706 }
1707
1708 operation_end(op_status);
1709 return 0;
1710}
1711
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001712int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001713{
1714 int op_status = 0;
1715
1716 operation_start("Change File System");
1717 if (simulate) {
1718 simulate_progress_bar();
1719 } else {
1720 string part_path, file_system;
1721 DataManager::GetValue("tw_partition_mount_point", part_path);
1722 DataManager::GetValue("tw_action_new_file_system", file_system);
1723 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1724 op_status = 0; // success
1725 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001727 op_status = 1; // fail
1728 }
1729 }
1730 PartitionManager.Update_System_Details();
1731 operation_end(op_status);
1732 return 0;
1733}
1734
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001735int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001736{
1737 int op_status = 0;
1738
1739 operation_start("Start MTP");
1740 if (PartitionManager.Enable_MTP())
1741 op_status = 0; // success
1742 else
1743 op_status = 1; // fail
1744
1745 operation_end(op_status);
1746 return 0;
1747}
1748
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001749int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001750{
1751 int op_status = 0;
1752
1753 operation_start("Stop MTP");
1754 if (PartitionManager.Disable_MTP())
1755 op_status = 0; // success
1756 else
1757 op_status = 1; // fail
1758
1759 operation_end(op_status);
1760 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001761}
1762
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001763int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001764{
1765 int op_status = 0;
1766
1767 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001768 string path, filename;
1769 DataManager::GetValue("tw_zip_location", path);
1770 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001771 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001772 op_status = 0; // success
1773 else
1774 op_status = 1; // fail
1775
1776 operation_end(op_status);
1777 return 0;
1778}
1779
that10ae24f2015-12-26 20:53:51 +01001780int GUIAction::twcmd(std::string arg)
1781{
1782 operation_start("TWRP CLI Command");
1783 if (simulate)
1784 simulate_progress_bar();
1785 else
1786 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1787 operation_end(0);
1788 return 0;
1789}
1790
Dees_Troy51a0e822012-09-05 15:24:24 -04001791int GUIAction::getKeyByName(std::string key)
1792{
that8834a0f2016-01-05 23:29:30 +01001793 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001794 else if (key == "menu") return KEY_MENU;
1795 else if (key == "back") return KEY_BACK;
1796 else if (key == "search") return KEY_SEARCH;
1797 else if (key == "voldown") return KEY_VOLUMEDOWN;
1798 else if (key == "volup") return KEY_VOLUMEUP;
1799 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001800 int ret_val;
1801 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1802 if (!ret_val)
1803 return KEY_POWER;
1804 else
1805 return ret_val;
1806 }
1807
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001808 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001809}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001810
1811int GUIAction::checkpartitionlifetimewrites(std::string arg)
1812{
1813 int op_status = 0;
1814 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1815
1816 operation_start("Check Partition Lifetime Writes");
1817 if (sys) {
1818 if (sys->Check_Lifetime_Writes() != 0)
1819 DataManager::SetValue("tw_lifetime_writes", 1);
1820 else
1821 DataManager::SetValue("tw_lifetime_writes", 0);
1822 op_status = 0; // success
1823 } else {
1824 DataManager::SetValue("tw_lifetime_writes", 1);
1825 op_status = 1; // fail
1826 }
1827
1828 operation_end(op_status);
1829 return 0;
1830}
1831
1832int GUIAction::mountsystemtoggle(std::string arg)
1833{
1834 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001835 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001836 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001837
1838 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001839 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001840 op_status = 1; // fail
1841 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001842 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001843 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001844 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001845 DataManager::SetValue("tw_mount_system_ro", 0);
1846 Part->Change_Mount_Read_Only(false);
1847 } else {
1848 DataManager::SetValue("tw_mount_system_ro", 1);
1849 Part->Change_Mount_Read_Only(true);
1850 }
1851 if (remount_system) {
1852 Part->Mount(true);
1853 }
1854 op_status = 0; // success
1855 } else {
1856 op_status = 1; // fail
1857 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001858 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1859 if (Part) {
1860 if (arg == "0") {
1861 Part->Change_Mount_Read_Only(false);
1862 } else {
1863 Part->Change_Mount_Read_Only(true);
1864 }
1865 if (remount_vendor) {
1866 Part->Mount(true);
1867 }
1868 op_status = 0; // success
1869 } else {
1870 op_status = 1; // fail
1871 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001872 }
1873
1874 operation_end(op_status);
1875 return 0;
1876}
Ethan Yonker74db1572015-10-28 12:44:49 -05001877
1878int GUIAction::setlanguage(std::string arg __unused)
1879{
1880 int op_status = 0;
1881
1882 operation_start("Set Language");
1883 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1884 PageManager::RequestReload();
1885 op_status = 0; // success
1886
1887 operation_end(op_status);
1888 return 0;
1889}
Ethan Yonker1b190162016-12-05 15:25:19 -06001890
Matt Mower9472ba12016-01-20 18:12:47 -06001891int GUIAction::togglebacklight(std::string arg __unused)
1892{
1893 blankTimer.toggleBlank();
1894 return 0;
1895}
1896
Ethan Yonker1b190162016-12-05 15:25:19 -06001897int GUIAction::setbootslot(std::string arg)
1898{
1899 operation_start("Set Boot Slot");
1900 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001901 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001902 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001903 simulate_progress_bar();
1904 operation_end(0);
1905 return 0;
1906}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001907
1908int GUIAction::checkforapp(std::string arg __unused)
1909{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001910 operation_start("Check for TWRP App");
1911 if (!simulate)
1912 {
1913 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1914 int sdkver = 0;
1915 if (!sdkverstr.empty()) {
1916 sdkver = atoi(sdkverstr.c_str());
1917 }
1918 if (sdkver <= 13) {
1919 if (sdkver == 0)
1920 LOGINFO("Unable to read sdk version from build prop\n");
1921 else
1922 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001923 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001924 goto exit;
1925 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001926 if (TWFunc::Is_TWRP_App_In_System()) {
1927 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1928 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001929 }
1930 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001931 const char parent_path[] = "/data/app";
1932 const char app_prefix[] = "me.twrp.twrpapp-";
1933 DIR *d = opendir(parent_path);
1934 if (d) {
1935 struct dirent *p;
1936 while ((p = readdir(d))) {
1937 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1938 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001939 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001940 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001941 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942 goto exit;
1943 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001944 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001945 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001946 } else {
1947 LOGINFO("Data partition cannot be mounted during app check\n");
1948 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001949 }
1950 } else
1951 simulate_progress_bar();
1952 LOGINFO("App not installed\n");
1953 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1954exit:
1955 operation_end(0);
1956 return 0;
1957}
1958
1959int GUIAction::installapp(std::string arg __unused)
1960{
1961 int op_status = 1;
1962 operation_start("Install TWRP App");
1963 if (!simulate)
1964 {
1965 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1966 if (PartitionManager.Mount_By_Path("/data", true)) {
1967 string install_path = "/data/app";
1968 string context = "u:object_r:apk_data_file:s0";
1969 if (!TWFunc::Path_Exists(install_path)) {
1970 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1971 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1972 goto exit;
1973 }
1974 if (chown(install_path.c_str(), 1000, 1000)) {
1975 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1976 goto exit;
1977 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001978 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001979 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1980 goto exit;
1981 }
1982 }
1983 install_path += "/me.twrp.twrpapp-1";
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 install_path += "/base.apk";
1997 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1998 LOGERR("Error copying apk file\n");
1999 goto exit;
2000 }
2001 if (chown(install_path.c_str(), 1000, 1000)) {
2002 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2003 goto exit;
2004 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002005 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002006 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2007 goto exit;
2008 }
2009 sync();
2010 sync();
2011 }
2012 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002013 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2014 string base_path = PartitionManager.Get_Android_Root_Path();
2015 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002016 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2017 string install_path = base_path + "/priv-app";
2018 string context = "u:object_r:system_file:s0";
2019 if (!TWFunc::Path_Exists(install_path))
2020 install_path = base_path + "/app";
2021 if (TWFunc::Path_Exists(install_path)) {
2022 install_path += "/twrpapp";
2023 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2024 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002025 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002026 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2027 goto exit;
2028 }
2029 install_path += "/me.twrp.twrpapp.apk";
2030 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2031 LOGERR("Error copying apk file\n");
2032 goto exit;
2033 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002034 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002035 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2036 goto exit;
2037 }
2038 sync();
2039 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002040 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002041 op_status = 0;
2042 } else {
2043 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2044 }
2045 }
2046 }
2047 }
2048 } else
2049 simulate_progress_bar();
2050exit:
2051 operation_end(0);
2052 return 0;
2053}
Ethan Yonker53796e72019-01-11 22:49:52 -06002054
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002055int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2056{
2057 int op_status = 1;
2058 operation_start("Uninstall TWRP System App");
2059 if (!simulate)
2060 {
2061 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2062 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2063 if (!Part) {
2064 LOGERR("Unabled to find system partition.\n");
2065 goto exit;
2066 }
2067 if (!Part->UnMount(true)) {
2068 goto exit;
2069 }
2070 if (Mount_System_RO > 0) {
2071 DataManager::SetValue("tw_mount_system_ro", 0);
2072 Part->Change_Mount_Read_Only(false);
2073 }
2074 if (Part->Mount(true)) {
2075 string base_path = PartitionManager.Get_Android_Root_Path();
2076 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2077 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2078 string uninstall_path = base_path + "/priv-app";
2079 if (!TWFunc::Path_Exists(uninstall_path))
2080 uninstall_path = base_path + "/app";
2081 uninstall_path += "/twrpapp";
2082 if (TWFunc::Path_Exists(uninstall_path)) {
2083 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2084 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2085 sync();
2086 op_status = 0;
2087 DataManager::SetValue("tw_app_installed_in_system", 0);
2088 DataManager::SetValue("tw_app_install_status", 0);
2089 } else {
2090 LOGERR("Unable to remove TWRP app from system.\n");
2091 }
2092 } else {
2093 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2094 }
2095 }
2096 Part->UnMount(true);
2097 if (Mount_System_RO > 0) {
2098 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2099 Part->Change_Mount_Read_Only(true);
2100 }
2101 } else
2102 simulate_progress_bar();
2103exit:
2104 operation_end(0);
2105 return 0;
2106}
2107
Ethan Yonker53796e72019-01-11 22:49:52 -06002108int GUIAction::repackimage(std::string arg __unused)
2109{
2110 int op_status = 1;
2111 operation_start("Repack Image");
2112 if (!simulate)
2113 {
2114 std::string path = DataManager::GetStrValue("tw_filename");
2115 Repack_Options_struct Repack_Options;
2116 Repack_Options.Disable_Verity = false;
2117 Repack_Options.Disable_Force_Encrypt = false;
2118 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2119 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2120 Repack_Options.Type = REPLACE_KERNEL;
2121 else
2122 Repack_Options.Type = REPLACE_RAMDISK;
2123 if (!PartitionManager.Repack_Images(path, Repack_Options))
2124 goto exit;
2125 } else
2126 simulate_progress_bar();
2127 op_status = 0;
2128exit:
2129 operation_end(op_status);
2130 return 0;
2131}
2132
2133int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2134{
2135 int op_status = 1;
2136 operation_start("Repack Image");
2137 if (!simulate)
2138 {
2139 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2140 LOGERR("Image repacking tool not present in this TWRP build!");
2141 goto exit;
2142 }
2143 DataManager::SetProgress(0);
2144 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2145 if (part)
2146 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2147 else {
2148 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2149 goto exit;
2150 }
2151 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2152 goto exit;
2153 DataManager::SetProgress(.25);
2154 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
2155 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
2156 if (TWFunc::Exec_Cmd(command) != 0) {
2157 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2158 goto exit;
2159 }
2160 std::string header_path = REPACK_ORIG_DIR;
2161 header_path += "header";
2162 if (TWFunc::Path_Exists(header_path)) {
2163 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";
2164 if (TWFunc::Exec_Cmd(command) != 0) {
2165 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2166 goto exit;
2167 }
2168 }
2169 DataManager::SetProgress(.5);
2170 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
2171 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --repack " REPACK_ORIG_DIR "boot.img";
2172 if (TWFunc::Exec_Cmd(command) != 0) {
2173 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2174 goto exit;
2175 }
2176 DataManager::SetProgress(.75);
2177 std::string path = REPACK_ORIG_DIR;
2178 std::string file = "new-boot.img";
2179 DataManager::SetValue("tw_flash_partition", "/boot;");
2180 if (!PartitionManager.Flash_Image(path, file)) {
2181 LOGINFO("Error flashing new image\n");
2182 goto exit;
2183 }
2184 DataManager::SetProgress(1);
2185 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2186 } else
2187 simulate_progress_bar();
2188 op_status = 0;
2189exit:
2190 operation_end(op_status);
2191 return 0;
2192}