blob: ccb7e344edcfdfcbd580961f045e88e810b39ef6 [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
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050046#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010047
Dees_Troy51a0e822012-09-05 15:24:24 -040048extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000049#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053};
Ethan Yonkerf1179622016-08-25 15:32:21 -050054#include "../set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
that3f7b1ac2014-12-30 11:30:13 +010061GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010062std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010063static string zip_queue[10];
64static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010065pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010066
that73a52952015-01-28 23:07:34 +010067static void *ActionThread_work_wrapper(void *data);
68
69class ActionThread
70{
71public:
72 ActionThread();
73 ~ActionThread();
74
75 void threadActions(GUIAction *act);
76 void run(void *data);
77private:
78 friend void *ActionThread_work_wrapper(void*);
79 struct ThreadData
80 {
81 ActionThread *this_;
82 GUIAction *act;
83 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
84 };
85
86 pthread_t m_thread;
87 bool m_thread_running;
88 pthread_mutex_t m_act_lock;
89};
90
91static ActionThread action_thread; // for all kinds of longer running actions
92static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010093
94static void *ActionThread_work_wrapper(void *data)
95{
that73a52952015-01-28 23:07:34 +010096 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010097 return NULL;
98}
99
100ActionThread::ActionThread()
101{
102 m_thread_running = false;
103 pthread_mutex_init(&m_act_lock, NULL);
104}
105
106ActionThread::~ActionThread()
107{
108 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600109 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100110 pthread_mutex_unlock(&m_act_lock);
111 pthread_join(m_thread, NULL);
112 } else {
113 pthread_mutex_unlock(&m_act_lock);
114 }
115 pthread_mutex_destroy(&m_act_lock);
116}
117
that7d3b54f2015-01-09 22:52:51 +0100118void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100119{
120 pthread_mutex_lock(&m_act_lock);
121 if (m_thread_running) {
122 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100123 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
124 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100125 } else {
126 m_thread_running = true;
127 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100128 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100129 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
130 }
131}
132
133void ActionThread::run(void *data)
134{
135 ThreadData *d = (ThreadData*)data;
136 GUIAction* act = d->act;
137
138 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100139 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100140 act->doAction(*it);
141
142 pthread_mutex_lock(&m_act_lock);
143 m_thread_running = false;
144 pthread_mutex_unlock(&m_act_lock);
145 delete d;
146}
147
Dees_Troy51a0e822012-09-05 15:24:24 -0400148GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100149 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400150{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 xml_node<>* child;
152 xml_node<>* actions;
153 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
that3f7b1ac2014-12-30 11:30:13 +0100157 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100158#define ADD_ACTION(n) mf[#n] = &GUIAction::n
159#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100160 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100161 ADD_ACTION(reboot);
162 ADD_ACTION(home);
163 ADD_ACTION(key);
164 ADD_ACTION(page);
165 ADD_ACTION(reload);
166 ADD_ACTION(readBackup);
167 ADD_ACTION(set);
168 ADD_ACTION(clear);
169 ADD_ACTION(mount);
170 ADD_ACTION(unmount);
171 ADD_ACTION_EX("umount", unmount);
172 ADD_ACTION(restoredefaultsettings);
173 ADD_ACTION(copylog);
174 ADD_ACTION(compute);
175 ADD_ACTION_EX("addsubtract", compute);
176 ADD_ACTION(setguitimezone);
177 ADD_ACTION(overlay);
178 ADD_ACTION(queuezip);
179 ADD_ACTION(cancelzip);
180 ADD_ACTION(queueclear);
181 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500182 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100183 ADD_ACTION(appenddatetobackupname);
184 ADD_ACTION(generatebackupname);
185 ADD_ACTION(checkpartitionlist);
186 ADD_ACTION(getpartitiondetails);
187 ADD_ACTION(screenshot);
188 ADD_ACTION(setbrightness);
189 ADD_ACTION(fileexists);
190 ADD_ACTION(killterminal);
191 ADD_ACTION(checkbackupname);
192 ADD_ACTION(adbsideloadcancel);
193 ADD_ACTION(fixsu);
194 ADD_ACTION(startmtp);
195 ADD_ACTION(stopmtp);
196 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500197 ADD_ACTION(checkpartitionlifetimewrites);
198 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500199 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600200 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600201 ADD_ACTION(togglebacklight);
thatc6085482015-01-09 22:12:43 +0100202
203 // remember actions that run in the caller thread
204 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
205 setActionsRunningInCallerThread.insert(it->first);
206
207 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100208 ADD_ACTION(flash);
209 ADD_ACTION(wipe);
210 ADD_ACTION(refreshsizes);
211 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600212 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100213 ADD_ACTION(fixpermissions);
214 ADD_ACTION(dd);
215 ADD_ACTION(partitionsd);
216 ADD_ACTION(installhtcdumlock);
217 ADD_ACTION(htcdumlockrestoreboot);
218 ADD_ACTION(htcdumlockreflashrecovery);
219 ADD_ACTION(cmd);
220 ADD_ACTION(terminalcommand);
221 ADD_ACTION(reinjecttwrp);
222 ADD_ACTION(decrypt);
223 ADD_ACTION(adbsideload);
224 ADD_ACTION(openrecoveryscript);
225 ADD_ACTION(installsu);
226 ADD_ACTION(decrypt_backup);
227 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500228 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100229 ADD_ACTION(changefilesystem);
230 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100231 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600232 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600233 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500234 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600235 ADD_ACTION(repackimage);
236 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100237 }
238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600240 actions = FindNode(node, "actions");
241 if (actions) child = FindNode(actions, "action");
242 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 while (child)
247 {
248 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 attr = child->first_attribute("function");
251 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 action.mFunction = attr->value();
254 action.mArg = child->value();
255 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 child = child->next_sibling("action");
258 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600261 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 if (child)
263 {
264 attr = child->first_attribute("key");
265 if (attr)
266 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100267 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600268 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100269 {
270 const int key = getKeyByName(keys[i]);
271 mKeys[key] = false;
272 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 }
274 else
275 {
276 attr = child->first_attribute("x");
277 if (!attr) return;
278 mActionX = atol(attr->value());
279 attr = child->first_attribute("y");
280 if (!attr) return;
281 mActionY = atol(attr->value());
282 attr = child->first_attribute("w");
283 if (!attr) return;
284 mActionW = atol(attr->value());
285 attr = child->first_attribute("h");
286 if (!attr) return;
287 mActionH = atol(attr->value());
288 }
289 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}
291
Matt Mower25036b72016-06-24 12:30:18 -0500292int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400293{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 if (state == TOUCH_RELEASE)
295 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100300int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400301{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100302 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600303 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100304 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100305
306 bool prevState = itr->second;
307 itr->second = down;
308
309 // If there is only one key for this action, wait for key up so it
310 // doesn't trigger with multi-key actions.
311 // Else, check if all buttons are pressed, then consume their release events
312 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600313 if (mKeys.size() == 1) {
314 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100315 doActions();
thatd4725ca2016-01-19 00:15:21 +0100316 return 0;
317 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600318 } else if (down) {
319 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
320 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100321 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100322 }
323
324 // Passed, all req buttons are pressed, reset them and consume release events
325 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600326 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100327 kb->ConsumeKeyRelease(itr->first);
328 itr->second = false;
329 }
330
331 doActions();
thatd4725ca2016-01-19 00:15:21 +0100332 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100333 }
334
thatd4725ca2016-01-19 00:15:21 +0100335 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336}
337
Vojtech Bocek07220562014-02-08 02:05:33 +0100338int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400339{
Vojtech Bocek07220562014-02-08 02:05:33 +0100340 GUIObject::NotifyVarChange(varName, value);
341
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100342 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200343 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600344 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400346
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400348}
349
350void GUIAction::simulate_progress_bar(void)
351{
Ethan Yonker74db1572015-10-28 12:44:49 -0500352 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400353 for (int i = 0; i < 5; i++)
354 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500355 if (PartitionManager.stop_backup.get_value()) {
356 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600357 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500358 DataManager::SetValue("ui_progress", 0);
359 PartitionManager.stop_backup.set_value(0);
360 return;
361 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400362 usleep(500000);
363 DataManager::SetValue("ui_progress", i * 20);
364 }
365}
366
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600367int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400368{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
371 DataManager::SetValue("ui_progress", 0);
372
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 if (filename.empty())
374 {
375 LOGERR("No file specified.\n");
376 return -1;
377 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
Ethan Yonker9598c072016-01-15 21:54:34 -0600379 if (!TWFunc::Path_Exists(filename)) {
380 if (!PartitionManager.Mount_By_Path(filename, true)) {
381 return -1;
382 }
383 if (!TWFunc::Path_Exists(filename)) {
384 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
385 return -1;
386 }
387 }
Dees_Troy657c3092012-09-10 20:32:10 -0400388
Dees_Troy51a0e822012-09-05 15:24:24 -0400389 if (simulate) {
390 simulate_progress_bar();
391 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400392 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400393
394 // Now, check if we need to ensure TWRP remains installed...
395 struct stat st;
396 if (stat("/sbin/installTwrp", &st) == 0)
397 {
398 DataManager::SetValue("tw_operation", "Configuring TWRP");
399 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500400 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200401 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500403 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 }
405 }
406 }
407
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200408 // Done
409 DataManager::SetValue("ui_progress", 100);
410 DataManager::SetValue("ui_progress", 0);
411 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400412}
413
that73a52952015-01-28 23:07:34 +0100414GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100415{
that73a52952015-01-28 23:07:34 +0100416 string func = gui_parse_text(action.mFunction);
417 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
418 if (needsThread) {
419 if (func == "cancelbackup")
420 return THREAD_CANCEL;
421 else
422 return THREAD_ACTION;
423 }
424 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100425}
426
Dees_Troy51a0e822012-09-05 15:24:24 -0400427int GUIAction::doActions()
428{
that3f7b1ac2014-12-30 11:30:13 +0100429 if (mActions.size() < 1)
430 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431
that73a52952015-01-28 23:07:34 +0100432 // Determine in which thread to run the actions.
433 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
434 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100435 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100436 for (it = mActions.begin(); it != mActions.end(); ++it) {
437 ThreadType tt = getThreadType(*it);
438 if (tt == THREAD_NONE)
439 continue;
440 if (threadType == THREAD_NONE)
441 threadType = tt;
442 else if (threadType != tt) {
443 LOGERR("Can't mix normal and cancel actions in the same list.\n"
444 "Running the whole batch in the cancel thread.\n");
445 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100446 break;
447 }
that7d3b54f2015-01-09 22:52:51 +0100448 }
that73a52952015-01-28 23:07:34 +0100449
450 // Now run the actions in the desired thread.
451 switch (threadType) {
452 case THREAD_ACTION:
453 action_thread.threadActions(this);
454 break;
455
456 case THREAD_CANCEL:
457 cancel_thread.threadActions(this);
458 break;
459
460 default: {
461 // no iterators here because theme reloading might kill our object
462 const size_t cnt = mActions.size();
463 for (size_t i = 0; i < cnt; ++i)
464 doAction(mActions[i]);
465 }
thatc6085482015-01-09 22:12:43 +0100466 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200468 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400469}
470
that3f7b1ac2014-12-30 11:30:13 +0100471int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400472{
that3f7b1ac2014-12-30 11:30:13 +0100473 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474
that3f7b1ac2014-12-30 11:30:13 +0100475 std::string function = gui_parse_text(action.mFunction);
476 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477
that3f7b1ac2014-12-30 11:30:13 +0100478 // find function and execute it
479 mapFunc::const_iterator funcitr = mf.find(function);
480 if (funcitr != mf.end())
481 return (this->*funcitr->second)(arg);
482
483 LOGERR("Unknown action '%s'\n", function.c_str());
484 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485}
486
487void GUIAction::operation_start(const string operation_name)
488{
that3f7b1ac2014-12-30 11:30:13 +0100489 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000490 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 DataManager::SetValue(TW_ACTION_BUSY, 1);
492 DataManager::SetValue("ui_progress", 0);
493 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600495 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500496 bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR;
497 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400498}
499
that3f7b1ac2014-12-30 11:30:13 +0100500void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400501{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000502 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 DataManager::SetValue("ui_progress", 100);
505 if (simulate) {
506 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
507 if (simulate_fail != 0)
508 DataManager::SetValue("tw_operation_status", 1);
509 else
510 DataManager::SetValue("tw_operation_status", 0);
511 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500512 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500514 }
515 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500517 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 }
519 DataManager::SetValue("tw_operation_state", 1);
520 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500521 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200522 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000523 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400524
525#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000526 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600527 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400528#endif
529
that3f7b1ac2014-12-30 11:30:13 +0100530 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400531}
532
that3f7b1ac2014-12-30 11:30:13 +0100533int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400534{
that3f7b1ac2014-12-30 11:30:13 +0100535 sync();
536 DataManager::SetValue("tw_gui_done", 1);
537 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
that3f7b1ac2014-12-30 11:30:13 +0100539 return 0;
540}
Dees_Troy51a0e822012-09-05 15:24:24 -0400541
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500542int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100543{
that3f7b1ac2014-12-30 11:30:13 +0100544 gui_changePage("main");
545 return 0;
546}
Dees_Troy51a0e822012-09-05 15:24:24 -0400547
that3f7b1ac2014-12-30 11:30:13 +0100548int GUIAction::key(std::string arg)
549{
550 const int key = getKeyByName(arg);
551 PageManager::NotifyKey(key, true);
552 PageManager::NotifyKey(key, false);
553 return 0;
554}
555
556int GUIAction::page(std::string arg)
557{
LuK133762326f42015-09-30 19:57:46 +0200558 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100559 std::string page_name = gui_parse_text(arg);
560 return gui_changePage(page_name);
561}
562
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500563int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100564{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500565 PageManager::RequestReload();
566 // The actual reload is handled in pages.cpp in PageManager::RunReload()
567 // The reload will occur on the next Update or Render call and will
568 // be performed in the rendoer thread instead of the action thread
569 // to prevent crashing which could occur when we start deleting
570 // GUI resources in the action thread while we attempt to render
571 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100572 return 0;
573}
Dees_Troy51a0e822012-09-05 15:24:24 -0400574
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500575int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100576{
577 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400578
that3f7b1ac2014-12-30 11:30:13 +0100579 DataManager::GetValue("tw_restore", Restore_Name);
580 PartitionManager.Set_Restore_Files(Restore_Name);
581 return 0;
582}
583
584int GUIAction::set(std::string arg)
585{
586 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200587 {
that3f7b1ac2014-12-30 11:30:13 +0100588 string varName = arg.substr(0, arg.find('='));
589 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400590
that3f7b1ac2014-12-30 11:30:13 +0100591 DataManager::GetValue(value, value);
592 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 }
that3f7b1ac2014-12-30 11:30:13 +0100594 else
595 DataManager::SetValue(arg, "1");
596 return 0;
597}
Dees_Troy51a0e822012-09-05 15:24:24 -0400598
that3f7b1ac2014-12-30 11:30:13 +0100599int GUIAction::clear(std::string arg)
600{
601 DataManager::SetValue(arg, "0");
602 return 0;
603}
Dees_Troy51a0e822012-09-05 15:24:24 -0400604
that3f7b1ac2014-12-30 11:30:13 +0100605int GUIAction::mount(std::string arg)
606{
607 if (arg == "usb") {
608 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100610 PartitionManager.usb_storage_enable();
611 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500612 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100613 } else if (!simulate) {
614 PartitionManager.Mount_By_Path(arg, true);
615 PartitionManager.Add_MTP_Storage(arg);
616 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100618 return 0;
619}
620
621int GUIAction::unmount(std::string arg)
622{
623 if (arg == "usb") {
624 if (!simulate)
625 PartitionManager.usb_storage_disable();
626 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500627 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100628 DataManager::SetValue(TW_ACTION_BUSY, 0);
629 } else if (!simulate) {
630 PartitionManager.UnMount_By_Path(arg, true);
631 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500632 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100633 return 0;
634}
635
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500636int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100637{
638 operation_start("Restore Defaults");
639 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100641 else {
642 DataManager::ResetDefaults();
643 PartitionManager.Update_System_Details();
644 PartitionManager.Mount_Current_Storage(true);
645 }
646 operation_end(0);
647 return 0;
648}
649
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500650int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100651{
652 operation_start("Copy Log");
653 if (!simulate)
654 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400655 string dst, curr_storage;
656 int copy_kernel_log = 0;
657
658 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100659 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400660 curr_storage = DataManager::GetCurrentStoragePath();
661 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100662 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
663 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400664 if (copy_kernel_log)
665 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100666 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400667 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100668 } else
669 simulate_progress_bar();
670 operation_end(0);
671 return 0;
672}
673
674
675int GUIAction::compute(std::string arg)
676{
677 if (arg.find("+") != string::npos)
678 {
679 string varName = arg.substr(0, arg.find('+'));
680 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
681 int amount_to_add = atoi(string_to_add.c_str());
682 int value;
683
684 DataManager::GetValue(varName, value);
685 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400686 return 0;
687 }
that3f7b1ac2014-12-30 11:30:13 +0100688 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400689 {
that3f7b1ac2014-12-30 11:30:13 +0100690 string varName = arg.substr(0, arg.find('-'));
691 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
692 int amount_to_subtract = atoi(string_to_subtract.c_str());
693 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400694
that3f7b1ac2014-12-30 11:30:13 +0100695 DataManager::GetValue(varName, value);
696 value -= amount_to_subtract;
697 if (value <= 0)
698 value = 0;
699 DataManager::SetValue(varName, value);
700 return 0;
701 }
702 if (arg.find("*") != string::npos)
703 {
704 string varName = arg.substr(0, arg.find('*'));
705 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
706 int multiply_by = atoi(multiply_by_str.c_str());
707 int value;
708
709 DataManager::GetValue(varName, value);
710 DataManager::SetValue(varName, value*multiply_by);
711 return 0;
712 }
713 if (arg.find("/") != string::npos)
714 {
715 string varName = arg.substr(0, arg.find('/'));
716 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
717 int divide_by = atoi(divide_by_str.c_str());
718 int value;
719
Matt Mowera8a89d12016-12-30 18:10:37 -0600720 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100721 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100723 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 }
725 return 0;
726 }
that3f7b1ac2014-12-30 11:30:13 +0100727 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
728 return -1;
729}
Dees_Troy51a0e822012-09-05 15:24:24 -0400730
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500731int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100732{
733 string SelectedZone;
734 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
735 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
736 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
737
738 int dst;
739 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
740
741 string offset;
742 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
743
744 string NewTimeZone = Zone;
745 if (offset != "0")
746 NewTimeZone += ":" + offset;
747
748 if (dst != 0)
749 NewTimeZone += DSTZone;
750
751 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
752 DataManager::update_tz_environment_variables();
753 return 0;
754}
755
756int GUIAction::overlay(std::string arg)
757{
758 return gui_changeOverlay(arg);
759}
760
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500761int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100762{
763 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500764 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 return 0;
766 }
that3f7b1ac2014-12-30 11:30:13 +0100767 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
768 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
769 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100771 }
772 return 0;
773}
774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500775int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100776{
777 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500778 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400779 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100780 } else {
781 zip_queue_index--;
782 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400783 }
that3f7b1ac2014-12-30 11:30:13 +0100784 return 0;
785}
Dees_Troy51a0e822012-09-05 15:24:24 -0400786
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500787int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100788{
789 zip_queue_index = 0;
790 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
791 return 0;
792}
Dees_Troy51a0e822012-09-05 15:24:24 -0400793
that3f7b1ac2014-12-30 11:30:13 +0100794int GUIAction::sleep(std::string arg)
795{
796 operation_start("Sleep");
797 usleep(atoi(arg.c_str()));
798 operation_end(0);
799 return 0;
800}
Dees Troyb21cc642013-09-10 17:36:41 +0000801
Matt Mower9a2a2052016-05-31 21:31:22 -0500802int GUIAction::sleepcounter(std::string arg)
803{
804 operation_start("SleepCounter");
805 // Ensure user notices countdown in case it needs to be cancelled
806 blankTimer.resetTimerAndUnblank();
807 int total = atoi(arg.c_str());
808 for (int t = total; t > 0; t--) {
809 int progress = (int)(((float)(total-t)/(float)total)*100.0);
810 DataManager::SetValue("ui_progress", progress);
811 ::sleep(1);
812 DataManager::SetValue("tw_sleep", t-1);
813 }
814 DataManager::SetValue("ui_progress", 100);
815 operation_end(0);
816 return 0;
817}
818
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500819int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100820{
821 operation_start("AppendDateToBackupName");
822 string Backup_Name;
823 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
824 Backup_Name += TWFunc::Get_Current_Date();
825 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
826 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
827 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500828 PageManager::NotifyKey(KEY_END, true);
829 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100830 operation_end(0);
831 return 0;
832}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500833
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500834int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100835{
836 operation_start("GenerateBackupName");
837 TWFunc::Auto_Generate_Backup_Name();
838 operation_end(0);
839 return 0;
840}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500841
Ethan Yonker483e9f42016-01-11 22:21:18 -0600842int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100843{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600844 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100845 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500846
Ethan Yonker483e9f42016-01-11 22:21:18 -0600847 if (arg.empty())
848 arg = "tw_wipe_list";
849 DataManager::GetValue(arg, List);
850 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
851 if (!List.empty()) {
852 size_t start_pos = 0, end_pos = List.find(";", start_pos);
853 while (end_pos != string::npos && start_pos < List.size()) {
854 part_path = List.substr(start_pos, end_pos - start_pos);
855 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
856 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100857 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 } else {
that3f7b1ac2014-12-30 11:30:13 +0100859 count++;
860 }
861 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100863 }
864 DataManager::SetValue("tw_check_partition_list", count);
865 } else {
866 DataManager::SetValue("tw_check_partition_list", 0);
867 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600868 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100869}
Dees_Troy51a0e822012-09-05 15:24:24 -0400870
Ethan Yonker483e9f42016-01-11 22:21:18 -0600871int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100872{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600873 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400874
Ethan Yonker483e9f42016-01-11 22:21:18 -0600875 if (arg.empty())
876 arg = "tw_wipe_list";
877 DataManager::GetValue(arg, List);
878 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
879 if (!List.empty()) {
880 size_t start_pos = 0, end_pos = List.find(";", start_pos);
881 part_path = List;
882 while (end_pos != string::npos && start_pos < List.size()) {
883 part_path = List.substr(start_pos, end_pos - start_pos);
884 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
885 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100886 // Do nothing
887 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600888 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100889 break;
890 }
891 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600892 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100893 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600894 if (!part_path.empty()) {
895 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100896 if (Part) {
897 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500898
that3f7b1ac2014-12-30 11:30:13 +0100899 DataManager::SetValue("tw_partition_name", Part->Display_Name);
900 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
901 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
902 DataManager::SetValue("tw_partition_size", Part->Size / mb);
903 DataManager::SetValue("tw_partition_used", Part->Used / mb);
904 DataManager::SetValue("tw_partition_free", Part->Free / mb);
905 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
906 DataManager::SetValue("tw_partition_removable", Part->Removable);
907 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
908
909 if (Part->Can_Repair())
910 DataManager::SetValue("tw_partition_can_repair", 1);
911 else
912 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500913 if (Part->Can_Resize())
914 DataManager::SetValue("tw_partition_can_resize", 1);
915 else
916 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600917 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100918 DataManager::SetValue("tw_partition_vfat", 1);
919 else
920 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600921 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100922 DataManager::SetValue("tw_partition_exfat", 1);
923 else
924 DataManager::SetValue("tw_partition_exfat", 0);
925 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
926 DataManager::SetValue("tw_partition_f2fs", 1);
927 else
928 DataManager::SetValue("tw_partition_f2fs", 0);
929 if (TWFunc::Path_Exists("/sbin/mke2fs"))
930 DataManager::SetValue("tw_partition_ext", 1);
931 else
932 DataManager::SetValue("tw_partition_ext", 0);
933 return 0;
934 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600935 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100936 }
937 }
938 }
939 DataManager::SetValue("tw_partition_name", "");
940 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600941 // Set this to 0 to prevent trying to partition this device, just in case
942 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100943 return 0;
944}
945
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500946int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100947{
948 time_t tm;
949 char path[256];
950 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600951 uid_t uid = AID_MEDIA_RW;
952 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100953
954 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600955 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100956 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
957 } else {
958 strcpy(path, "/tmp/");
959 }
960
Matt Mowera8a89d12016-12-30 18:10:37 -0600961 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100962 return 0;
963
964 tm = time(NULL);
965 path_len = strlen(path);
966
967 // Screenshot_2014-01-01-18-21-38.png
968 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
969
970 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600971 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100972 chmod(path, 0666);
973 chown(path, uid, gid);
974
that677b13f2015-12-26 23:57:31 +0100975 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100976
977 // blink to notify that the screenshow was taken
978 gr_color(255, 255, 255, 255);
979 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
980 gr_flip();
981 gui_forceRender();
982 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500983 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100984 }
985 return 0;
986}
987
988int GUIAction::setbrightness(std::string arg)
989{
990 return TWFunc::Set_Brightness(arg);
991}
992
993int GUIAction::fileexists(std::string arg)
994{
995 struct stat st;
996 string newpath = arg + "/.";
997
998 operation_start("FileExists");
999 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1000 operation_end(0);
1001 else
1002 operation_end(1);
1003 return 0;
1004}
1005
thatcc8ddca2015-01-03 01:59:36 +01001006void GUIAction::reinject_after_flash()
1007{
1008 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001009 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001010 if (simulate) {
1011 simulate_progress_bar();
1012 } else {
1013 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1014 if (Boot == NULL || Boot->Current_File_System != "emmc")
1015 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1016 else {
1017 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1018 TWFunc::Exec_Cmd(injectcmd);
1019 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001020 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001021 }
1022 }
1023}
1024
that3f7b1ac2014-12-30 11:30:13 +01001025int GUIAction::flash(std::string arg)
1026{
1027 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001028 // We're going to jump to this page first, like a loading page
1029 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001030 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001031 string zip_path = zip_queue[i];
1032 size_t slashpos = zip_path.find_last_of('/');
1033 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001034 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001035 DataManager::SetValue("tw_filename", zip_path);
1036 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001037 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1038
1039 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001040 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001041 TWFunc::SetPerformanceMode(false);
1042 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001043 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001044 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001045 break;
that3f7b1ac2014-12-30 11:30:13 +01001046 }
1047 }
1048 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001049
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001050 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001051 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001052 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001053 }
that3f7b1ac2014-12-30 11:30:13 +01001054
thatcc8ddca2015-01-03 01:59:36 +01001055 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001056 PartitionManager.Update_System_Details();
1057 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001058 // 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 -06001059 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001060 return 0;
1061}
1062
1063int GUIAction::wipe(std::string arg)
1064{
1065 operation_start("Format");
1066 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001067 int ret_val = false;
1068
1069 if (simulate) {
1070 simulate_progress_bar();
1071 } else {
1072 if (arg == "data")
1073 ret_val = PartitionManager.Factory_Reset();
1074 else if (arg == "battery")
1075 ret_val = PartitionManager.Wipe_Battery_Stats();
1076 else if (arg == "rotate")
1077 ret_val = PartitionManager.Wipe_Rotate_Data();
1078 else if (arg == "dalvik")
1079 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1080 else if (arg == "DATAMEDIA") {
1081 ret_val = PartitionManager.Format_Data();
1082 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001083 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001084
1085 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1086 if (has_datamedia) {
1087 ret_val = PartitionManager.Wipe_Media_From_Data();
1088 } else {
1089 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1090 }
1091 } else if (arg == "EXTERNAL") {
1092 string External_Path;
1093
1094 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1095 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1096 } else if (arg == "ANDROIDSECURE") {
1097 ret_val = PartitionManager.Wipe_Android_Secure();
1098 } else if (arg == "LIST") {
1099 string Wipe_List, wipe_path;
1100 bool skip = false;
1101 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001102
1103 DataManager::GetValue("tw_wipe_list", Wipe_List);
1104 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1105 if (!Wipe_List.empty()) {
1106 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1107 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1108 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1109 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1110 if (wipe_path == "/and-sec") {
1111 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001112 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001113 ret_val = false;
1114 break;
1115 } else {
1116 skip = true;
1117 }
1118 } else if (wipe_path == "DALVIK") {
1119 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001120 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001121 ret_val = false;
1122 break;
1123 } else {
1124 skip = true;
1125 }
1126 } else if (wipe_path == "INTERNAL") {
1127 if (!PartitionManager.Wipe_Media_From_Data()) {
1128 ret_val = false;
1129 break;
1130 } else {
1131 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001132 }
1133 }
that3f7b1ac2014-12-30 11:30:13 +01001134 if (!skip) {
1135 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001136 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001137 ret_val = false;
1138 break;
1139 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1140 arg = wipe_path;
1141 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 } else {
that3f7b1ac2014-12-30 11:30:13 +01001143 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001144 }
that3f7b1ac2014-12-30 11:30:13 +01001145 start_pos = end_pos + 1;
1146 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001147 }
1148 }
that3f7b1ac2014-12-30 11:30:13 +01001149 } else
1150 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001151#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001152 if (arg == DataManager::GetSettingsStoragePath()) {
1153 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1154 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001155
that3f7b1ac2014-12-30 11:30:13 +01001156 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1157 LOGINFO("Making TWRP folder and saving settings.\n");
1158 Storage_Path += "/TWRP";
1159 mkdir(Storage_Path.c_str(), 0777);
1160 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001161 } else {
that3f7b1ac2014-12-30 11:30:13 +01001162 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1163 }
1164 }
1165#endif
1166 }
1167 PartitionManager.Update_System_Details();
1168 if (ret_val)
1169 ret_val = 0; // 0 is success
1170 else
1171 ret_val = 1; // 1 is failure
1172 operation_end(ret_val);
1173 return 0;
1174}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001175
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001176int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001177{
1178 operation_start("Refreshing Sizes");
1179 if (simulate) {
1180 simulate_progress_bar();
1181 } else
1182 PartitionManager.Update_System_Details();
1183 operation_end(0);
1184 return 0;
1185}
1186
1187int GUIAction::nandroid(std::string arg)
1188{
that3f7b1ac2014-12-30 11:30:13 +01001189 if (simulate) {
that73a52952015-01-28 23:07:34 +01001190 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001191 DataManager::SetValue("tw_partition", "Simulation");
1192 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001193 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001194 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 operation_start("Nandroid");
1196 int ret = 0;
1197
that3f7b1ac2014-12-30 11:30:13 +01001198 if (arg == "backup") {
1199 string Backup_Name;
1200 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001201 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001202 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 -05001203 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001204 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1205 if (!PartitionManager.stop_backup.get_value()) {
1206 if (ret == false)
1207 ret = 1; // 1 for failure
1208 else
1209 ret = 0; // 0 for success
1210 DataManager::SetValue("tw_cancel_backup", 0);
1211 } else {
1212 DataManager::SetValue("tw_cancel_backup", 1);
1213 gui_msg("backup_cancel=Backup Cancelled");
1214 ret = 0;
1215 }
bigbiffce8f83c2015-12-12 18:30:21 -05001216 } else {
that3f7b1ac2014-12-30 11:30:13 +01001217 operation_end(1);
1218 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001219 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001220 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001221 } else if (arg == "restore") {
1222 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001223 int gui_adb_backup;
1224
that3f7b1ac2014-12-30 11:30:13 +01001225 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001226 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1227 if (gui_adb_backup) {
1228 DataManager::SetValue("tw_operation_state", 1);
1229 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1230 ret = 0; // success
1231 else
1232 ret = 1; // failure
1233 DataManager::SetValue("tw_enable_adb_backup", 0);
1234 ret = 0; // assume success???
1235 } else {
1236 if (PartitionManager.Run_Restore(Restore_Name))
1237 ret = 0; // success
1238 else
1239 ret = 1; // failure
1240 }
that3f7b1ac2014-12-30 11:30:13 +01001241 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001242 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001243 return -1;
1244 }
that73a52952015-01-28 23:07:34 +01001245 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001246 return ret;
1247 }
1248 return 0;
1249}
1250
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001251int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001252 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001253 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001254 }
1255 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001256 int op_status = PartitionManager.Cancel_Backup();
1257 if (op_status != 0)
1258 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001259 }
1260
1261 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001262}
Dees_Troy51a0e822012-09-05 15:24:24 -04001263
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001264int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001265{
that73a52952015-01-28 23:07:34 +01001266 int op_status = 0;
1267
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001268 operation_start("Fix Contexts");
1269 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001270 if (simulate) {
1271 simulate_progress_bar();
1272 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001273 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001274 if (op_status != 0)
1275 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001276 }
that73a52952015-01-28 23:07:34 +01001277 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001278 return 0;
1279}
1280
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001281int GUIAction::fixpermissions(std::string arg)
1282{
1283 return fixcontexts(arg);
1284}
1285
that3f7b1ac2014-12-30 11:30:13 +01001286int GUIAction::dd(std::string arg)
1287{
1288 operation_start("imaging");
1289
1290 if (simulate) {
1291 simulate_progress_bar();
1292 } else {
1293 string cmd = "dd " + arg;
1294 TWFunc::Exec_Cmd(cmd);
1295 }
1296 operation_end(0);
1297 return 0;
1298}
1299
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001300int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001301{
1302 operation_start("Partition SD Card");
1303 int ret_val = 0;
1304
1305 if (simulate) {
1306 simulate_progress_bar();
1307 } else {
1308 int allow_partition;
1309 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1310 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001311 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001312 } else {
1313 if (!PartitionManager.Partition_SDCard())
1314 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001315 }
that3f7b1ac2014-12-30 11:30:13 +01001316 }
1317 operation_end(ret_val);
1318 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001319
that3f7b1ac2014-12-30 11:30:13 +01001320}
Dees_Troy51a0e822012-09-05 15:24:24 -04001321
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001322int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001323{
1324 operation_start("Install HTC Dumlock");
1325 if (simulate) {
1326 simulate_progress_bar();
1327 } else
1328 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001329
that3f7b1ac2014-12-30 11:30:13 +01001330 operation_end(0);
1331 return 0;
1332}
Dees_Troy51a0e822012-09-05 15:24:24 -04001333
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001334int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001335{
1336 operation_start("HTC Dumlock Restore Boot");
1337 if (simulate) {
1338 simulate_progress_bar();
1339 } else
1340 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001341
that3f7b1ac2014-12-30 11:30:13 +01001342 operation_end(0);
1343 return 0;
1344}
Dees_Troy51a0e822012-09-05 15:24:24 -04001345
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001346int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001347{
1348 operation_start("HTC Dumlock Reflash Recovery");
1349 if (simulate) {
1350 simulate_progress_bar();
1351 } else
1352 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001353
that3f7b1ac2014-12-30 11:30:13 +01001354 operation_end(0);
1355 return 0;
1356}
Dees_Troy51a0e822012-09-05 15:24:24 -04001357
that3f7b1ac2014-12-30 11:30:13 +01001358int GUIAction::cmd(std::string arg)
1359{
1360 int op_status = 0;
1361
1362 operation_start("Command");
1363 LOGINFO("Running command: '%s'\n", arg.c_str());
1364 if (simulate) {
1365 simulate_progress_bar();
1366 } else {
1367 op_status = TWFunc::Exec_Cmd(arg);
1368 if (op_status != 0)
1369 op_status = 1;
1370 }
1371
1372 operation_end(op_status);
1373 return 0;
1374}
1375
1376int GUIAction::terminalcommand(std::string arg)
1377{
1378 int op_status = 0;
1379 string cmdpath, command;
1380
1381 DataManager::GetValue("tw_terminal_location", cmdpath);
1382 operation_start("CommandOutput");
1383 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1384 if (simulate) {
1385 simulate_progress_bar();
1386 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001387 } else if (arg == "exit") {
1388 LOGINFO("Exiting terminal\n");
1389 operation_end(op_status);
1390 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001391 } else {
1392 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1393 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001394 DataManager::SetValue("tw_terminal_state", 1);
1395 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001396 FILE* fp;
1397 char line[512];
1398
1399 fp = popen(command.c_str(), "r");
1400 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001401 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001402 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001403 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001404 struct timeval timeout;
1405 fd_set fdset;
1406
Matt Mowera8a89d12016-12-30 18:10:37 -06001407 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001408 {
1409 FD_ZERO(&fdset);
1410 FD_SET(fd, &fdset);
1411 timeout.tv_sec = 0;
1412 timeout.tv_usec = 400000;
1413 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1414 if (has_data == 0) {
1415 // Timeout reached
1416 DataManager::GetValue("tw_terminal_state", check);
1417 if (check == 0) {
1418 keep_going = 0;
1419 }
1420 } else if (has_data < 0) {
1421 // End of execution
1422 keep_going = 0;
1423 } else {
1424 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001425 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001426 gui_print("%s", line); // Display output
1427 else
1428 keep_going = 0; // Done executing
1429 }
1430 }
1431 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001432 }
thatc6085482015-01-09 22:12:43 +01001433 DataManager::SetValue("tw_operation_status", 0);
1434 DataManager::SetValue("tw_operation_state", 1);
1435 DataManager::SetValue("tw_terminal_state", 0);
1436 DataManager::SetValue("tw_background_thread_running", 0);
1437 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001438 }
1439 return 0;
1440}
1441
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001442int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001443{
that3f7b1ac2014-12-30 11:30:13 +01001444 LOGINFO("Sending kill command...\n");
1445 operation_start("KillCommand");
1446 DataManager::SetValue("tw_operation_status", 0);
1447 DataManager::SetValue("tw_operation_state", 1);
1448 DataManager::SetValue("tw_terminal_state", 0);
1449 DataManager::SetValue("tw_background_thread_running", 0);
1450 DataManager::SetValue(TW_ACTION_BUSY, 0);
1451 return 0;
1452}
1453
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001454int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001455{
1456 int op_status = 0;
1457 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001458 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001459 if (simulate) {
1460 simulate_progress_bar();
1461 } else {
1462 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001463 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001464 }
1465
1466 operation_end(op_status);
1467 return 0;
1468}
1469
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001470int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001471{
1472 int op_status = 0;
1473
1474 operation_start("CheckBackupName");
1475 if (simulate) {
1476 simulate_progress_bar();
1477 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001478 string Backup_Name;
1479 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1480 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001481 if (op_status != 0)
1482 op_status = 1;
1483 }
1484
1485 operation_end(op_status);
1486 return 0;
1487}
1488
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001489int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001490{
1491 int op_status = 0;
1492
1493 operation_start("Decrypt");
1494 if (simulate) {
1495 simulate_progress_bar();
1496 } else {
1497 string Password;
1498 DataManager::GetValue("tw_crypto_password", Password);
1499 op_status = PartitionManager.Decrypt_Device(Password);
1500 if (op_status != 0)
1501 op_status = 1;
1502 else {
that3f7b1ac2014-12-30 11:30:13 +01001503
1504 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1505
Ethan Yonkercf50da52015-01-12 21:59:07 -06001506 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001507
Ethan Yonkercf50da52015-01-12 21:59:07 -06001508 // Check for a custom theme and load it if exists
1509 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1510 if (has_datamedia != 0) {
1511 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001512 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001513 } else {
1514 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001515 }
1516 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001517 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001518 }
1519 }
1520
1521 operation_end(op_status);
1522 return 0;
1523}
1524
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001525int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001526{
1527 operation_start("Sideload");
1528 if (simulate) {
1529 simulate_progress_bar();
1530 operation_end(0);
1531 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001532 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001533 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1534
1535 // wait for the adb connection
1536 int ret = apply_from_adb("/", &sideload_child_pid);
1537 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1538
1539 if (ret != 0) {
1540 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001541 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001542 ret = 1; // failure
1543 } else {
1544 int wipe_cache = 0;
1545 int wipe_dalvik = 0;
1546 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1547
1548 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1549 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1550 PartitionManager.Wipe_By_Path("/cache");
1551 if (wipe_dalvik)
1552 PartitionManager.Wipe_Dalvik_Cache();
1553 } else {
1554 ret = 1; // failure
1555 }
thatcc8ddca2015-01-03 01:59:36 +01001556 }
thatc6085482015-01-09 22:12:43 +01001557 if (sideload_child_pid) {
1558 LOGINFO("Signaling child sideload process to exit.\n");
1559 struct stat st;
1560 // Calling stat() on this magic filename signals the minadbd
1561 // subprocess to shut down.
1562 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1563 int status;
1564 LOGINFO("Waiting for child sideload process to exit.\n");
1565 waitpid(sideload_child_pid, &status, 0);
1566 }
Dees Troy28a85d22015-04-28 02:03:16 +00001567 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001568 TWFunc::Toggle_MTP(mtp_was_enabled);
1569 reinject_after_flash();
1570 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001571 }
that3f7b1ac2014-12-30 11:30:13 +01001572 return 0;
1573}
1574
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001575int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001576{
that3f7b1ac2014-12-30 11:30:13 +01001577 struct stat st;
1578 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001579 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001580 LOGINFO("Signaling child sideload process to exit.\n");
1581 // Calling stat() on this magic filename signals the minadbd
1582 // subprocess to shut down.
1583 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1584 if (!sideload_child_pid) {
1585 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001586 return 0;
1587 }
thatcc8ddca2015-01-03 01:59:36 +01001588 ::sleep(1);
1589 LOGINFO("Killing child sideload process.\n");
1590 kill(sideload_child_pid, SIGTERM);
1591 int status;
1592 LOGINFO("Waiting for child sideload process to exit.\n");
1593 waitpid(sideload_child_pid, &status, 0);
1594 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001595 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1596 return 0;
1597}
1598
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001599int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001600{
1601 operation_start("OpenRecoveryScript");
1602 if (simulate) {
1603 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001604 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001605 } else {
that10ae24f2015-12-26 20:53:51 +01001606 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001607 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001608 }
1609 return 0;
1610}
1611
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001612int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001613{
1614 int op_status = 0;
1615
1616 operation_start("Install SuperSU");
1617 if (simulate) {
1618 simulate_progress_bar();
1619 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001620 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001621 }
1622
1623 operation_end(op_status);
1624 return 0;
1625}
1626
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001627int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001628{
1629 int op_status = 0;
1630
1631 operation_start("Fixing Superuser Permissions");
1632 if (simulate) {
1633 simulate_progress_bar();
1634 } else {
1635 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1636 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1637 }
1638
1639 operation_end(op_status);
1640 return 0;
1641}
1642
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001643int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001644{
1645 int op_status = 0;
1646
1647 operation_start("Try Restore Decrypt");
1648 if (simulate) {
1649 simulate_progress_bar();
1650 } else {
1651 string Restore_Path, Filename, Password;
1652 DataManager::GetValue("tw_restore", Restore_Path);
1653 Restore_Path += "/";
1654 DataManager::GetValue("tw_restore_password", Password);
1655 TWFunc::SetPerformanceMode(true);
1656 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1657 op_status = 0; // success
1658 else
1659 op_status = 1; // fail
1660 TWFunc::SetPerformanceMode(false);
1661 }
1662
1663 operation_end(op_status);
1664 return 0;
1665}
1666
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001667int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001668{
1669 int op_status = 0;
1670
1671 operation_start("Repair Partition");
1672 if (simulate) {
1673 simulate_progress_bar();
1674 } else {
1675 string part_path;
1676 DataManager::GetValue("tw_partition_mount_point", part_path);
1677 if (PartitionManager.Repair_By_Path(part_path, true)) {
1678 op_status = 0; // success
1679 } else {
that3f7b1ac2014-12-30 11:30:13 +01001680 op_status = 1; // fail
1681 }
1682 }
1683
1684 operation_end(op_status);
1685 return 0;
1686}
1687
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001688int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001689{
1690 int op_status = 0;
1691
1692 operation_start("Resize Partition");
1693 if (simulate) {
1694 simulate_progress_bar();
1695 } else {
1696 string part_path;
1697 DataManager::GetValue("tw_partition_mount_point", part_path);
1698 if (PartitionManager.Resize_By_Path(part_path, true)) {
1699 op_status = 0; // success
1700 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001701 op_status = 1; // fail
1702 }
1703 }
1704
1705 operation_end(op_status);
1706 return 0;
1707}
1708
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001709int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001710{
1711 int op_status = 0;
1712
1713 operation_start("Change File System");
1714 if (simulate) {
1715 simulate_progress_bar();
1716 } else {
1717 string part_path, file_system;
1718 DataManager::GetValue("tw_partition_mount_point", part_path);
1719 DataManager::GetValue("tw_action_new_file_system", file_system);
1720 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1721 op_status = 0; // success
1722 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001723 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001724 op_status = 1; // fail
1725 }
1726 }
1727 PartitionManager.Update_System_Details();
1728 operation_end(op_status);
1729 return 0;
1730}
1731
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001732int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001733{
1734 int op_status = 0;
1735
1736 operation_start("Start MTP");
1737 if (PartitionManager.Enable_MTP())
1738 op_status = 0; // success
1739 else
1740 op_status = 1; // fail
1741
1742 operation_end(op_status);
1743 return 0;
1744}
1745
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001746int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001747{
1748 int op_status = 0;
1749
1750 operation_start("Stop MTP");
1751 if (PartitionManager.Disable_MTP())
1752 op_status = 0; // success
1753 else
1754 op_status = 1; // fail
1755
1756 operation_end(op_status);
1757 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001758}
1759
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001760int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001761{
1762 int op_status = 0;
1763
1764 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001765 string path, filename;
1766 DataManager::GetValue("tw_zip_location", path);
1767 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001768 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001769 op_status = 0; // success
1770 else
1771 op_status = 1; // fail
1772
1773 operation_end(op_status);
1774 return 0;
1775}
1776
that10ae24f2015-12-26 20:53:51 +01001777int GUIAction::twcmd(std::string arg)
1778{
1779 operation_start("TWRP CLI Command");
1780 if (simulate)
1781 simulate_progress_bar();
1782 else
1783 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1784 operation_end(0);
1785 return 0;
1786}
1787
Dees_Troy51a0e822012-09-05 15:24:24 -04001788int GUIAction::getKeyByName(std::string key)
1789{
that8834a0f2016-01-05 23:29:30 +01001790 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001791 else if (key == "menu") return KEY_MENU;
1792 else if (key == "back") return KEY_BACK;
1793 else if (key == "search") return KEY_SEARCH;
1794 else if (key == "voldown") return KEY_VOLUMEDOWN;
1795 else if (key == "volup") return KEY_VOLUMEUP;
1796 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001797 int ret_val;
1798 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1799 if (!ret_val)
1800 return KEY_POWER;
1801 else
1802 return ret_val;
1803 }
1804
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001805 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001806}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001807
1808int GUIAction::checkpartitionlifetimewrites(std::string arg)
1809{
1810 int op_status = 0;
1811 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1812
1813 operation_start("Check Partition Lifetime Writes");
1814 if (sys) {
1815 if (sys->Check_Lifetime_Writes() != 0)
1816 DataManager::SetValue("tw_lifetime_writes", 1);
1817 else
1818 DataManager::SetValue("tw_lifetime_writes", 0);
1819 op_status = 0; // success
1820 } else {
1821 DataManager::SetValue("tw_lifetime_writes", 1);
1822 op_status = 1; // fail
1823 }
1824
1825 operation_end(op_status);
1826 return 0;
1827}
1828
1829int GUIAction::mountsystemtoggle(std::string arg)
1830{
1831 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001832 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001833 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001834
1835 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001836 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001837 op_status = 1; // fail
1838 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001839 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001840 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001841 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001842 DataManager::SetValue("tw_mount_system_ro", 0);
1843 Part->Change_Mount_Read_Only(false);
1844 } else {
1845 DataManager::SetValue("tw_mount_system_ro", 1);
1846 Part->Change_Mount_Read_Only(true);
1847 }
1848 if (remount_system) {
1849 Part->Mount(true);
1850 }
1851 op_status = 0; // success
1852 } else {
1853 op_status = 1; // fail
1854 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001855 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1856 if (Part) {
1857 if (arg == "0") {
1858 Part->Change_Mount_Read_Only(false);
1859 } else {
1860 Part->Change_Mount_Read_Only(true);
1861 }
1862 if (remount_vendor) {
1863 Part->Mount(true);
1864 }
1865 op_status = 0; // success
1866 } else {
1867 op_status = 1; // fail
1868 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001869 }
1870
1871 operation_end(op_status);
1872 return 0;
1873}
Ethan Yonker74db1572015-10-28 12:44:49 -05001874
1875int GUIAction::setlanguage(std::string arg __unused)
1876{
1877 int op_status = 0;
1878
1879 operation_start("Set Language");
1880 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1881 PageManager::RequestReload();
1882 op_status = 0; // success
1883
1884 operation_end(op_status);
1885 return 0;
1886}
Ethan Yonker1b190162016-12-05 15:25:19 -06001887
Matt Mower9472ba12016-01-20 18:12:47 -06001888int GUIAction::togglebacklight(std::string arg __unused)
1889{
1890 blankTimer.toggleBlank();
1891 return 0;
1892}
1893
Ethan Yonker1b190162016-12-05 15:25:19 -06001894int GUIAction::setbootslot(std::string arg)
1895{
1896 operation_start("Set Boot Slot");
1897 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001898 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001899 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001900 simulate_progress_bar();
1901 operation_end(0);
1902 return 0;
1903}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001904
1905int GUIAction::checkforapp(std::string arg __unused)
1906{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001907 operation_start("Check for TWRP App");
1908 if (!simulate)
1909 {
1910 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1911 int sdkver = 0;
1912 if (!sdkverstr.empty()) {
1913 sdkver = atoi(sdkverstr.c_str());
1914 }
1915 if (sdkver <= 13) {
1916 if (sdkver == 0)
1917 LOGINFO("Unable to read sdk version from build prop\n");
1918 else
1919 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001920 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 -06001921 goto exit;
1922 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001923 if (TWFunc::Is_TWRP_App_In_System()) {
1924 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1925 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001926 }
1927 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001928 const char parent_path[] = "/data/app";
1929 const char app_prefix[] = "me.twrp.twrpapp-";
1930 DIR *d = opendir(parent_path);
1931 if (d) {
1932 struct dirent *p;
1933 while ((p = readdir(d))) {
1934 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1935 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001936 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001937 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001938 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 -06001939 goto exit;
1940 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001941 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001943 } else {
1944 LOGINFO("Data partition cannot be mounted during app check\n");
1945 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 -06001946 }
1947 } else
1948 simulate_progress_bar();
1949 LOGINFO("App not installed\n");
1950 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1951exit:
1952 operation_end(0);
1953 return 0;
1954}
1955
1956int GUIAction::installapp(std::string arg __unused)
1957{
1958 int op_status = 1;
1959 operation_start("Install TWRP App");
1960 if (!simulate)
1961 {
1962 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1963 if (PartitionManager.Mount_By_Path("/data", true)) {
1964 string install_path = "/data/app";
1965 string context = "u:object_r:apk_data_file:s0";
1966 if (!TWFunc::Path_Exists(install_path)) {
1967 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1968 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1969 goto exit;
1970 }
1971 if (chown(install_path.c_str(), 1000, 1000)) {
1972 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1973 goto exit;
1974 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001975 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001976 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1977 goto exit;
1978 }
1979 }
1980 install_path += "/me.twrp.twrpapp-1";
1981 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1982 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1983 goto exit;
1984 }
1985 if (chown(install_path.c_str(), 1000, 1000)) {
1986 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001989 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001990 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
1993 install_path += "/base.apk";
1994 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1995 LOGERR("Error copying apk file\n");
1996 goto exit;
1997 }
1998 if (chown(install_path.c_str(), 1000, 1000)) {
1999 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2000 goto exit;
2001 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002002 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002003 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
2006 sync();
2007 sync();
2008 }
2009 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002010 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2011 string base_path = PartitionManager.Get_Android_Root_Path();
2012 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002013 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2014 string install_path = base_path + "/priv-app";
2015 string context = "u:object_r:system_file:s0";
2016 if (!TWFunc::Path_Exists(install_path))
2017 install_path = base_path + "/app";
2018 if (TWFunc::Path_Exists(install_path)) {
2019 install_path += "/twrpapp";
2020 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2021 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002022 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002023 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2024 goto exit;
2025 }
2026 install_path += "/me.twrp.twrpapp.apk";
2027 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2028 LOGERR("Error copying apk file\n");
2029 goto exit;
2030 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002031 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002032 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2033 goto exit;
2034 }
2035 sync();
2036 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002037 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002038 op_status = 0;
2039 } else {
2040 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2041 }
2042 }
2043 }
2044 }
2045 } else
2046 simulate_progress_bar();
2047exit:
2048 operation_end(0);
2049 return 0;
2050}
Ethan Yonker53796e72019-01-11 22:49:52 -06002051
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002052int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2053{
2054 int op_status = 1;
2055 operation_start("Uninstall TWRP System App");
2056 if (!simulate)
2057 {
2058 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2059 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2060 if (!Part) {
2061 LOGERR("Unabled to find system partition.\n");
2062 goto exit;
2063 }
2064 if (!Part->UnMount(true)) {
2065 goto exit;
2066 }
2067 if (Mount_System_RO > 0) {
2068 DataManager::SetValue("tw_mount_system_ro", 0);
2069 Part->Change_Mount_Read_Only(false);
2070 }
2071 if (Part->Mount(true)) {
2072 string base_path = PartitionManager.Get_Android_Root_Path();
2073 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2074 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2075 string uninstall_path = base_path + "/priv-app";
2076 if (!TWFunc::Path_Exists(uninstall_path))
2077 uninstall_path = base_path + "/app";
2078 uninstall_path += "/twrpapp";
2079 if (TWFunc::Path_Exists(uninstall_path)) {
2080 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2081 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2082 sync();
2083 op_status = 0;
2084 DataManager::SetValue("tw_app_installed_in_system", 0);
2085 DataManager::SetValue("tw_app_install_status", 0);
2086 } else {
2087 LOGERR("Unable to remove TWRP app from system.\n");
2088 }
2089 } else {
2090 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2091 }
2092 }
2093 Part->UnMount(true);
2094 if (Mount_System_RO > 0) {
2095 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2096 Part->Change_Mount_Read_Only(true);
2097 }
2098 } else
2099 simulate_progress_bar();
2100exit:
2101 operation_end(0);
2102 return 0;
2103}
2104
Ethan Yonker53796e72019-01-11 22:49:52 -06002105int GUIAction::repackimage(std::string arg __unused)
2106{
2107 int op_status = 1;
2108 operation_start("Repack Image");
2109 if (!simulate)
2110 {
2111 std::string path = DataManager::GetStrValue("tw_filename");
2112 Repack_Options_struct Repack_Options;
2113 Repack_Options.Disable_Verity = false;
2114 Repack_Options.Disable_Force_Encrypt = false;
2115 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2116 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2117 Repack_Options.Type = REPLACE_KERNEL;
2118 else
2119 Repack_Options.Type = REPLACE_RAMDISK;
2120 if (!PartitionManager.Repack_Images(path, Repack_Options))
2121 goto exit;
2122 } else
2123 simulate_progress_bar();
2124 op_status = 0;
2125exit:
2126 operation_end(op_status);
2127 return 0;
2128}
2129
2130int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2131{
2132 int op_status = 1;
2133 operation_start("Repack Image");
2134 if (!simulate)
2135 {
2136 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2137 LOGERR("Image repacking tool not present in this TWRP build!");
2138 goto exit;
2139 }
2140 DataManager::SetProgress(0);
2141 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2142 if (part)
2143 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2144 else {
2145 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2146 goto exit;
2147 }
2148 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2149 goto exit;
2150 DataManager::SetProgress(.25);
2151 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
2152 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
2153 if (TWFunc::Exec_Cmd(command) != 0) {
2154 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2155 goto exit;
2156 }
2157 std::string header_path = REPACK_ORIG_DIR;
2158 header_path += "header";
2159 if (TWFunc::Path_Exists(header_path)) {
2160 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";
2161 if (TWFunc::Exec_Cmd(command) != 0) {
2162 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2163 goto exit;
2164 }
2165 }
2166 DataManager::SetProgress(.5);
2167 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
2168 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --repack " REPACK_ORIG_DIR "boot.img";
2169 if (TWFunc::Exec_Cmd(command) != 0) {
2170 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2171 goto exit;
2172 }
2173 DataManager::SetProgress(.75);
2174 std::string path = REPACK_ORIG_DIR;
2175 std::string file = "new-boot.img";
2176 DataManager::SetValue("tw_flash_partition", "/boot;");
2177 if (!PartitionManager.Flash_Image(path, file)) {
2178 LOGINFO("Error flashing new image\n");
2179 goto exit;
2180 }
2181 DataManager::SetProgress(1);
2182 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2183 } else
2184 simulate_progress_bar();
2185 op_status = 0;
2186exit:
2187 operation_end(op_status);
2188 return 0;
2189}