blob: 1143ba3195a1bdc328ce44fcf2540d533dd5a67d [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);
Chaosmasterab164372020-02-03 15:38:02 +0100372 DataManager::SetValue("ui_portion_size", 0);
373 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400374
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200375 if (filename.empty())
376 {
377 LOGERR("No file specified.\n");
378 return -1;
379 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400380
Ethan Yonker9598c072016-01-15 21:54:34 -0600381 if (!TWFunc::Path_Exists(filename)) {
382 if (!PartitionManager.Mount_By_Path(filename, true)) {
383 return -1;
384 }
385 if (!TWFunc::Path_Exists(filename)) {
386 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
387 return -1;
388 }
389 }
Dees_Troy657c3092012-09-10 20:32:10 -0400390
Dees_Troy51a0e822012-09-05 15:24:24 -0400391 if (simulate) {
392 simulate_progress_bar();
393 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400394 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400395
396 // Now, check if we need to ensure TWRP remains installed...
397 struct stat st;
398 if (stat("/sbin/installTwrp", &st) == 0)
399 {
400 DataManager::SetValue("tw_operation", "Configuring TWRP");
401 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500402 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200403 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500405 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 }
407 }
408 }
409
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 // Done
411 DataManager::SetValue("ui_progress", 100);
412 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100413 DataManager::SetValue("ui_portion_size", 0);
414 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200415 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416}
417
that73a52952015-01-28 23:07:34 +0100418GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100419{
that73a52952015-01-28 23:07:34 +0100420 string func = gui_parse_text(action.mFunction);
421 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
422 if (needsThread) {
423 if (func == "cancelbackup")
424 return THREAD_CANCEL;
425 else
426 return THREAD_ACTION;
427 }
428 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100429}
430
Dees_Troy51a0e822012-09-05 15:24:24 -0400431int GUIAction::doActions()
432{
that3f7b1ac2014-12-30 11:30:13 +0100433 if (mActions.size() < 1)
434 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400435
that73a52952015-01-28 23:07:34 +0100436 // Determine in which thread to run the actions.
437 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
438 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100439 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100440 for (it = mActions.begin(); it != mActions.end(); ++it) {
441 ThreadType tt = getThreadType(*it);
442 if (tt == THREAD_NONE)
443 continue;
444 if (threadType == THREAD_NONE)
445 threadType = tt;
446 else if (threadType != tt) {
447 LOGERR("Can't mix normal and cancel actions in the same list.\n"
448 "Running the whole batch in the cancel thread.\n");
449 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100450 break;
451 }
that7d3b54f2015-01-09 22:52:51 +0100452 }
that73a52952015-01-28 23:07:34 +0100453
454 // Now run the actions in the desired thread.
455 switch (threadType) {
456 case THREAD_ACTION:
457 action_thread.threadActions(this);
458 break;
459
460 case THREAD_CANCEL:
461 cancel_thread.threadActions(this);
462 break;
463
464 default: {
465 // no iterators here because theme reloading might kill our object
466 const size_t cnt = mActions.size();
467 for (size_t i = 0; i < cnt; ++i)
468 doAction(mActions[i]);
469 }
thatc6085482015-01-09 22:12:43 +0100470 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200472 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400473}
474
that3f7b1ac2014-12-30 11:30:13 +0100475int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400476{
that3f7b1ac2014-12-30 11:30:13 +0100477 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478
that3f7b1ac2014-12-30 11:30:13 +0100479 std::string function = gui_parse_text(action.mFunction);
480 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481
that3f7b1ac2014-12-30 11:30:13 +0100482 // find function and execute it
483 mapFunc::const_iterator funcitr = mf.find(function);
484 if (funcitr != mf.end())
485 return (this->*funcitr->second)(arg);
486
487 LOGERR("Unknown action '%s'\n", function.c_str());
488 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400489}
490
491void GUIAction::operation_start(const string operation_name)
492{
that3f7b1ac2014-12-30 11:30:13 +0100493 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000494 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 DataManager::SetValue(TW_ACTION_BUSY, 1);
496 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100497 DataManager::SetValue("ui_portion_size", 0);
498 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600501 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500502 bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR;
503 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400504}
505
that3f7b1ac2014-12-30 11:30:13 +0100506void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400507{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000508 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 DataManager::SetValue("ui_progress", 100);
511 if (simulate) {
512 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
513 if (simulate_fail != 0)
514 DataManager::SetValue("tw_operation_status", 1);
515 else
516 DataManager::SetValue("tw_operation_status", 0);
517 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500518 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500520 }
521 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500523 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 }
525 DataManager::SetValue("tw_operation_state", 1);
526 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500527 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200528 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000529 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400530
531#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000532 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600533 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400534#endif
535
that3f7b1ac2014-12-30 11:30:13 +0100536 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400537}
538
that3f7b1ac2014-12-30 11:30:13 +0100539int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400540{
that3f7b1ac2014-12-30 11:30:13 +0100541 sync();
542 DataManager::SetValue("tw_gui_done", 1);
543 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400544
that3f7b1ac2014-12-30 11:30:13 +0100545 return 0;
546}
Dees_Troy51a0e822012-09-05 15:24:24 -0400547
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500548int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100549{
that3f7b1ac2014-12-30 11:30:13 +0100550 gui_changePage("main");
551 return 0;
552}
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
that3f7b1ac2014-12-30 11:30:13 +0100554int GUIAction::key(std::string arg)
555{
556 const int key = getKeyByName(arg);
557 PageManager::NotifyKey(key, true);
558 PageManager::NotifyKey(key, false);
559 return 0;
560}
561
562int GUIAction::page(std::string arg)
563{
LuK133762326f42015-09-30 19:57:46 +0200564 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100565 std::string page_name = gui_parse_text(arg);
566 return gui_changePage(page_name);
567}
568
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500569int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100570{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500571 PageManager::RequestReload();
572 // The actual reload is handled in pages.cpp in PageManager::RunReload()
573 // The reload will occur on the next Update or Render call and will
574 // be performed in the rendoer thread instead of the action thread
575 // to prevent crashing which could occur when we start deleting
576 // GUI resources in the action thread while we attempt to render
577 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100578 return 0;
579}
Dees_Troy51a0e822012-09-05 15:24:24 -0400580
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500581int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100582{
583 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400584
that3f7b1ac2014-12-30 11:30:13 +0100585 DataManager::GetValue("tw_restore", Restore_Name);
586 PartitionManager.Set_Restore_Files(Restore_Name);
587 return 0;
588}
589
590int GUIAction::set(std::string arg)
591{
592 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 {
that3f7b1ac2014-12-30 11:30:13 +0100594 string varName = arg.substr(0, arg.find('='));
595 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400596
that3f7b1ac2014-12-30 11:30:13 +0100597 DataManager::GetValue(value, value);
598 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 }
that3f7b1ac2014-12-30 11:30:13 +0100600 else
601 DataManager::SetValue(arg, "1");
602 return 0;
603}
Dees_Troy51a0e822012-09-05 15:24:24 -0400604
that3f7b1ac2014-12-30 11:30:13 +0100605int GUIAction::clear(std::string arg)
606{
607 DataManager::SetValue(arg, "0");
608 return 0;
609}
Dees_Troy51a0e822012-09-05 15:24:24 -0400610
that3f7b1ac2014-12-30 11:30:13 +0100611int GUIAction::mount(std::string arg)
612{
613 if (arg == "usb") {
614 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400615 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100616 PartitionManager.usb_storage_enable();
617 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100619 } else if (!simulate) {
620 PartitionManager.Mount_By_Path(arg, true);
621 PartitionManager.Add_MTP_Storage(arg);
622 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500623 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100624 return 0;
625}
626
627int GUIAction::unmount(std::string arg)
628{
629 if (arg == "usb") {
630 if (!simulate)
631 PartitionManager.usb_storage_disable();
632 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500633 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100634 DataManager::SetValue(TW_ACTION_BUSY, 0);
635 } else if (!simulate) {
636 PartitionManager.UnMount_By_Path(arg, true);
637 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500638 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100639 return 0;
640}
641
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500642int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100643{
644 operation_start("Restore Defaults");
645 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100647 else {
648 DataManager::ResetDefaults();
649 PartitionManager.Update_System_Details();
650 PartitionManager.Mount_Current_Storage(true);
651 }
652 operation_end(0);
653 return 0;
654}
655
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500656int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100657{
658 operation_start("Copy Log");
659 if (!simulate)
660 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400661 string dst, curr_storage;
662 int copy_kernel_log = 0;
663
664 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100665 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400666 curr_storage = DataManager::GetCurrentStoragePath();
667 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100668 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
669 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400670 if (copy_kernel_log)
671 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100672 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400673 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100674 } else
675 simulate_progress_bar();
676 operation_end(0);
677 return 0;
678}
679
680
681int GUIAction::compute(std::string arg)
682{
683 if (arg.find("+") != string::npos)
684 {
685 string varName = arg.substr(0, arg.find('+'));
686 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
687 int amount_to_add = atoi(string_to_add.c_str());
688 int value;
689
690 DataManager::GetValue(varName, value);
691 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 return 0;
693 }
that3f7b1ac2014-12-30 11:30:13 +0100694 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400695 {
that3f7b1ac2014-12-30 11:30:13 +0100696 string varName = arg.substr(0, arg.find('-'));
697 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
698 int amount_to_subtract = atoi(string_to_subtract.c_str());
699 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400700
that3f7b1ac2014-12-30 11:30:13 +0100701 DataManager::GetValue(varName, value);
702 value -= amount_to_subtract;
703 if (value <= 0)
704 value = 0;
705 DataManager::SetValue(varName, value);
706 return 0;
707 }
708 if (arg.find("*") != string::npos)
709 {
710 string varName = arg.substr(0, arg.find('*'));
711 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
712 int multiply_by = atoi(multiply_by_str.c_str());
713 int value;
714
715 DataManager::GetValue(varName, value);
716 DataManager::SetValue(varName, value*multiply_by);
717 return 0;
718 }
719 if (arg.find("/") != string::npos)
720 {
721 string varName = arg.substr(0, arg.find('/'));
722 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
723 int divide_by = atoi(divide_by_str.c_str());
724 int value;
725
Matt Mowera8a89d12016-12-30 18:10:37 -0600726 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100727 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100729 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 }
731 return 0;
732 }
that3f7b1ac2014-12-30 11:30:13 +0100733 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
734 return -1;
735}
Dees_Troy51a0e822012-09-05 15:24:24 -0400736
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500737int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100738{
739 string SelectedZone;
740 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
741 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
742 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
743
744 int dst;
745 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
746
747 string offset;
748 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
749
750 string NewTimeZone = Zone;
751 if (offset != "0")
752 NewTimeZone += ":" + offset;
753
754 if (dst != 0)
755 NewTimeZone += DSTZone;
756
757 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
758 DataManager::update_tz_environment_variables();
759 return 0;
760}
761
762int GUIAction::overlay(std::string arg)
763{
764 return gui_changeOverlay(arg);
765}
766
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500767int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100768{
769 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500770 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 return 0;
772 }
that3f7b1ac2014-12-30 11:30:13 +0100773 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
774 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
775 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100777 }
778 return 0;
779}
780
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500781int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100782{
783 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500784 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100786 } else {
787 zip_queue_index--;
788 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400789 }
that3f7b1ac2014-12-30 11:30:13 +0100790 return 0;
791}
Dees_Troy51a0e822012-09-05 15:24:24 -0400792
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500793int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100794{
795 zip_queue_index = 0;
796 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
797 return 0;
798}
Dees_Troy51a0e822012-09-05 15:24:24 -0400799
that3f7b1ac2014-12-30 11:30:13 +0100800int GUIAction::sleep(std::string arg)
801{
802 operation_start("Sleep");
803 usleep(atoi(arg.c_str()));
804 operation_end(0);
805 return 0;
806}
Dees Troyb21cc642013-09-10 17:36:41 +0000807
Matt Mower9a2a2052016-05-31 21:31:22 -0500808int GUIAction::sleepcounter(std::string arg)
809{
810 operation_start("SleepCounter");
811 // Ensure user notices countdown in case it needs to be cancelled
812 blankTimer.resetTimerAndUnblank();
813 int total = atoi(arg.c_str());
814 for (int t = total; t > 0; t--) {
815 int progress = (int)(((float)(total-t)/(float)total)*100.0);
816 DataManager::SetValue("ui_progress", progress);
817 ::sleep(1);
818 DataManager::SetValue("tw_sleep", t-1);
819 }
820 DataManager::SetValue("ui_progress", 100);
821 operation_end(0);
822 return 0;
823}
824
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500825int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100826{
827 operation_start("AppendDateToBackupName");
828 string Backup_Name;
829 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
830 Backup_Name += TWFunc::Get_Current_Date();
831 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
832 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
833 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500834 PageManager::NotifyKey(KEY_END, true);
835 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100836 operation_end(0);
837 return 0;
838}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500839
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500840int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100841{
842 operation_start("GenerateBackupName");
843 TWFunc::Auto_Generate_Backup_Name();
844 operation_end(0);
845 return 0;
846}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500847
Ethan Yonker483e9f42016-01-11 22:21:18 -0600848int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100849{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600850 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100851 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500852
Ethan Yonker483e9f42016-01-11 22:21:18 -0600853 if (arg.empty())
854 arg = "tw_wipe_list";
855 DataManager::GetValue(arg, List);
856 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
857 if (!List.empty()) {
858 size_t start_pos = 0, end_pos = List.find(";", start_pos);
859 while (end_pos != string::npos && start_pos < List.size()) {
860 part_path = List.substr(start_pos, end_pos - start_pos);
861 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
862 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100863 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400864 } else {
that3f7b1ac2014-12-30 11:30:13 +0100865 count++;
866 }
867 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600868 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100869 }
870 DataManager::SetValue("tw_check_partition_list", count);
871 } else {
872 DataManager::SetValue("tw_check_partition_list", 0);
873 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600874 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100875}
Dees_Troy51a0e822012-09-05 15:24:24 -0400876
Ethan Yonker483e9f42016-01-11 22:21:18 -0600877int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100878{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600879 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400880
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 if (arg.empty())
882 arg = "tw_wipe_list";
883 DataManager::GetValue(arg, List);
884 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
885 if (!List.empty()) {
886 size_t start_pos = 0, end_pos = List.find(";", start_pos);
887 part_path = List;
888 while (end_pos != string::npos && start_pos < List.size()) {
889 part_path = List.substr(start_pos, end_pos - start_pos);
890 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
891 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100892 // Do nothing
893 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600894 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100895 break;
896 }
897 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600898 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100899 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 if (!part_path.empty()) {
901 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100902 if (Part) {
903 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500904
that3f7b1ac2014-12-30 11:30:13 +0100905 DataManager::SetValue("tw_partition_name", Part->Display_Name);
906 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
907 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
908 DataManager::SetValue("tw_partition_size", Part->Size / mb);
909 DataManager::SetValue("tw_partition_used", Part->Used / mb);
910 DataManager::SetValue("tw_partition_free", Part->Free / mb);
911 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
912 DataManager::SetValue("tw_partition_removable", Part->Removable);
913 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
914
915 if (Part->Can_Repair())
916 DataManager::SetValue("tw_partition_can_repair", 1);
917 else
918 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500919 if (Part->Can_Resize())
920 DataManager::SetValue("tw_partition_can_resize", 1);
921 else
922 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600923 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100924 DataManager::SetValue("tw_partition_vfat", 1);
925 else
926 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600927 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100928 DataManager::SetValue("tw_partition_exfat", 1);
929 else
930 DataManager::SetValue("tw_partition_exfat", 0);
931 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
932 DataManager::SetValue("tw_partition_f2fs", 1);
933 else
934 DataManager::SetValue("tw_partition_f2fs", 0);
935 if (TWFunc::Path_Exists("/sbin/mke2fs"))
936 DataManager::SetValue("tw_partition_ext", 1);
937 else
938 DataManager::SetValue("tw_partition_ext", 0);
939 return 0;
940 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600941 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100942 }
943 }
944 }
945 DataManager::SetValue("tw_partition_name", "");
946 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600947 // Set this to 0 to prevent trying to partition this device, just in case
948 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100949 return 0;
950}
951
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500952int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100953{
954 time_t tm;
955 char path[256];
956 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600957 uid_t uid = AID_MEDIA_RW;
958 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100959
960 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600961 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100962 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
963 } else {
964 strcpy(path, "/tmp/");
965 }
966
Matt Mowera8a89d12016-12-30 18:10:37 -0600967 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100968 return 0;
969
970 tm = time(NULL);
971 path_len = strlen(path);
972
973 // Screenshot_2014-01-01-18-21-38.png
974 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
975
976 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600977 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100978 chmod(path, 0666);
979 chown(path, uid, gid);
980
that677b13f2015-12-26 23:57:31 +0100981 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100982
VDavid0032034a412019-10-18 22:43:20 +0200983 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100984 gr_color(255, 255, 255, 255);
985 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
986 gr_flip();
987 gui_forceRender();
988 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500989 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100990 }
991 return 0;
992}
993
994int GUIAction::setbrightness(std::string arg)
995{
996 return TWFunc::Set_Brightness(arg);
997}
998
999int GUIAction::fileexists(std::string arg)
1000{
1001 struct stat st;
1002 string newpath = arg + "/.";
1003
1004 operation_start("FileExists");
1005 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1006 operation_end(0);
1007 else
1008 operation_end(1);
1009 return 0;
1010}
1011
thatcc8ddca2015-01-03 01:59:36 +01001012void GUIAction::reinject_after_flash()
1013{
1014 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001015 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001016 if (simulate) {
1017 simulate_progress_bar();
1018 } else {
1019 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1020 if (Boot == NULL || Boot->Current_File_System != "emmc")
1021 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1022 else {
1023 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1024 TWFunc::Exec_Cmd(injectcmd);
1025 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001026 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001027 }
1028 }
1029}
1030
that3f7b1ac2014-12-30 11:30:13 +01001031int GUIAction::flash(std::string arg)
1032{
1033 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001034 // We're going to jump to this page first, like a loading page
1035 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001036 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001037 string zip_path = zip_queue[i];
1038 size_t slashpos = zip_path.find_last_of('/');
1039 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001040 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001041 DataManager::SetValue("tw_filename", zip_path);
1042 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001043 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1044
1045 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001046 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001047 TWFunc::SetPerformanceMode(false);
1048 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001049 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001050 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001051 break;
that3f7b1ac2014-12-30 11:30:13 +01001052 }
1053 }
1054 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001055
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001056 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001057 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001058 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001059 }
that3f7b1ac2014-12-30 11:30:13 +01001060
thatcc8ddca2015-01-03 01:59:36 +01001061 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001062 PartitionManager.Update_System_Details();
1063 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001064 // 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 -06001065 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001066 return 0;
1067}
1068
1069int GUIAction::wipe(std::string arg)
1070{
1071 operation_start("Format");
1072 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001073 int ret_val = false;
1074
1075 if (simulate) {
1076 simulate_progress_bar();
1077 } else {
1078 if (arg == "data")
1079 ret_val = PartitionManager.Factory_Reset();
1080 else if (arg == "battery")
1081 ret_val = PartitionManager.Wipe_Battery_Stats();
1082 else if (arg == "rotate")
1083 ret_val = PartitionManager.Wipe_Rotate_Data();
1084 else if (arg == "dalvik")
1085 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1086 else if (arg == "DATAMEDIA") {
1087 ret_val = PartitionManager.Format_Data();
1088 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001089 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001090
1091 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1092 if (has_datamedia) {
1093 ret_val = PartitionManager.Wipe_Media_From_Data();
1094 } else {
1095 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1096 }
1097 } else if (arg == "EXTERNAL") {
1098 string External_Path;
1099
1100 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1101 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1102 } else if (arg == "ANDROIDSECURE") {
1103 ret_val = PartitionManager.Wipe_Android_Secure();
1104 } else if (arg == "LIST") {
1105 string Wipe_List, wipe_path;
1106 bool skip = false;
1107 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001108
1109 DataManager::GetValue("tw_wipe_list", Wipe_List);
1110 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1111 if (!Wipe_List.empty()) {
1112 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1113 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1114 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1115 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1116 if (wipe_path == "/and-sec") {
1117 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001118 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001119 ret_val = false;
1120 break;
1121 } else {
1122 skip = true;
1123 }
1124 } else if (wipe_path == "DALVIK") {
1125 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001126 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001127 ret_val = false;
1128 break;
1129 } else {
1130 skip = true;
1131 }
1132 } else if (wipe_path == "INTERNAL") {
1133 if (!PartitionManager.Wipe_Media_From_Data()) {
1134 ret_val = false;
1135 break;
1136 } else {
1137 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001138 }
1139 }
that3f7b1ac2014-12-30 11:30:13 +01001140 if (!skip) {
1141 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001142 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001143 ret_val = false;
1144 break;
1145 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1146 arg = wipe_path;
1147 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001148 } else {
that3f7b1ac2014-12-30 11:30:13 +01001149 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 }
that3f7b1ac2014-12-30 11:30:13 +01001151 start_pos = end_pos + 1;
1152 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001153 }
1154 }
that3f7b1ac2014-12-30 11:30:13 +01001155 } else
1156 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001157#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001158 if (arg == DataManager::GetSettingsStoragePath()) {
1159 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1160 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001161
that3f7b1ac2014-12-30 11:30:13 +01001162 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1163 LOGINFO("Making TWRP folder and saving settings.\n");
1164 Storage_Path += "/TWRP";
1165 mkdir(Storage_Path.c_str(), 0777);
1166 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001167 } else {
that3f7b1ac2014-12-30 11:30:13 +01001168 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1169 }
1170 }
1171#endif
1172 }
1173 PartitionManager.Update_System_Details();
1174 if (ret_val)
1175 ret_val = 0; // 0 is success
1176 else
1177 ret_val = 1; // 1 is failure
1178 operation_end(ret_val);
1179 return 0;
1180}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001181
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001182int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001183{
1184 operation_start("Refreshing Sizes");
1185 if (simulate) {
1186 simulate_progress_bar();
1187 } else
1188 PartitionManager.Update_System_Details();
1189 operation_end(0);
1190 return 0;
1191}
1192
1193int GUIAction::nandroid(std::string arg)
1194{
that3f7b1ac2014-12-30 11:30:13 +01001195 if (simulate) {
that73a52952015-01-28 23:07:34 +01001196 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001197 DataManager::SetValue("tw_partition", "Simulation");
1198 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001199 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001200 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001201 operation_start("Nandroid");
1202 int ret = 0;
1203
that3f7b1ac2014-12-30 11:30:13 +01001204 if (arg == "backup") {
1205 string Backup_Name;
1206 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001207 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001208 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 -05001209 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001210 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1211 if (!PartitionManager.stop_backup.get_value()) {
1212 if (ret == false)
1213 ret = 1; // 1 for failure
1214 else
1215 ret = 0; // 0 for success
1216 DataManager::SetValue("tw_cancel_backup", 0);
1217 } else {
1218 DataManager::SetValue("tw_cancel_backup", 1);
1219 gui_msg("backup_cancel=Backup Cancelled");
1220 ret = 0;
1221 }
bigbiffce8f83c2015-12-12 18:30:21 -05001222 } else {
that3f7b1ac2014-12-30 11:30:13 +01001223 operation_end(1);
1224 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001225 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001226 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001227 } else if (arg == "restore") {
1228 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001229 int gui_adb_backup;
1230
that3f7b1ac2014-12-30 11:30:13 +01001231 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001232 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1233 if (gui_adb_backup) {
1234 DataManager::SetValue("tw_operation_state", 1);
1235 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1236 ret = 0; // success
1237 else
1238 ret = 1; // failure
1239 DataManager::SetValue("tw_enable_adb_backup", 0);
1240 ret = 0; // assume success???
1241 } else {
1242 if (PartitionManager.Run_Restore(Restore_Name))
1243 ret = 0; // success
1244 else
1245 ret = 1; // failure
1246 }
that3f7b1ac2014-12-30 11:30:13 +01001247 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001248 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001249 return -1;
1250 }
that73a52952015-01-28 23:07:34 +01001251 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001252 return ret;
1253 }
1254 return 0;
1255}
1256
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001257int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001258 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001259 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001260 }
1261 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001262 int op_status = PartitionManager.Cancel_Backup();
1263 if (op_status != 0)
1264 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001265 }
1266
1267 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001268}
Dees_Troy51a0e822012-09-05 15:24:24 -04001269
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001270int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001271{
that73a52952015-01-28 23:07:34 +01001272 int op_status = 0;
1273
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001274 operation_start("Fix Contexts");
1275 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001276 if (simulate) {
1277 simulate_progress_bar();
1278 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001279 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001280 if (op_status != 0)
1281 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001282 }
that73a52952015-01-28 23:07:34 +01001283 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001284 return 0;
1285}
1286
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001287int GUIAction::fixpermissions(std::string arg)
1288{
1289 return fixcontexts(arg);
1290}
1291
that3f7b1ac2014-12-30 11:30:13 +01001292int GUIAction::dd(std::string arg)
1293{
1294 operation_start("imaging");
1295
1296 if (simulate) {
1297 simulate_progress_bar();
1298 } else {
1299 string cmd = "dd " + arg;
1300 TWFunc::Exec_Cmd(cmd);
1301 }
1302 operation_end(0);
1303 return 0;
1304}
1305
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001306int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001307{
1308 operation_start("Partition SD Card");
1309 int ret_val = 0;
1310
1311 if (simulate) {
1312 simulate_progress_bar();
1313 } else {
1314 int allow_partition;
1315 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1316 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001317 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001318 } else {
1319 if (!PartitionManager.Partition_SDCard())
1320 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001321 }
that3f7b1ac2014-12-30 11:30:13 +01001322 }
1323 operation_end(ret_val);
1324 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001325
that3f7b1ac2014-12-30 11:30:13 +01001326}
Dees_Troy51a0e822012-09-05 15:24:24 -04001327
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001328int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001329{
1330 operation_start("Install HTC Dumlock");
1331 if (simulate) {
1332 simulate_progress_bar();
1333 } else
1334 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001335
that3f7b1ac2014-12-30 11:30:13 +01001336 operation_end(0);
1337 return 0;
1338}
Dees_Troy51a0e822012-09-05 15:24:24 -04001339
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001340int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001341{
1342 operation_start("HTC Dumlock Restore Boot");
1343 if (simulate) {
1344 simulate_progress_bar();
1345 } else
1346 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001347
that3f7b1ac2014-12-30 11:30:13 +01001348 operation_end(0);
1349 return 0;
1350}
Dees_Troy51a0e822012-09-05 15:24:24 -04001351
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001352int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001353{
1354 operation_start("HTC Dumlock Reflash Recovery");
1355 if (simulate) {
1356 simulate_progress_bar();
1357 } else
1358 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001359
that3f7b1ac2014-12-30 11:30:13 +01001360 operation_end(0);
1361 return 0;
1362}
Dees_Troy51a0e822012-09-05 15:24:24 -04001363
that3f7b1ac2014-12-30 11:30:13 +01001364int GUIAction::cmd(std::string arg)
1365{
1366 int op_status = 0;
1367
1368 operation_start("Command");
1369 LOGINFO("Running command: '%s'\n", arg.c_str());
1370 if (simulate) {
1371 simulate_progress_bar();
1372 } else {
1373 op_status = TWFunc::Exec_Cmd(arg);
1374 if (op_status != 0)
1375 op_status = 1;
1376 }
1377
1378 operation_end(op_status);
1379 return 0;
1380}
1381
1382int GUIAction::terminalcommand(std::string arg)
1383{
1384 int op_status = 0;
1385 string cmdpath, command;
1386
1387 DataManager::GetValue("tw_terminal_location", cmdpath);
1388 operation_start("CommandOutput");
1389 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1390 if (simulate) {
1391 simulate_progress_bar();
1392 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001393 } else if (arg == "exit") {
1394 LOGINFO("Exiting terminal\n");
1395 operation_end(op_status);
1396 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001397 } else {
1398 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1399 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001400 DataManager::SetValue("tw_terminal_state", 1);
1401 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001402 FILE* fp;
1403 char line[512];
1404
1405 fp = popen(command.c_str(), "r");
1406 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001407 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001408 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001409 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001410 struct timeval timeout;
1411 fd_set fdset;
1412
Matt Mowera8a89d12016-12-30 18:10:37 -06001413 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001414 {
1415 FD_ZERO(&fdset);
1416 FD_SET(fd, &fdset);
1417 timeout.tv_sec = 0;
1418 timeout.tv_usec = 400000;
1419 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1420 if (has_data == 0) {
1421 // Timeout reached
1422 DataManager::GetValue("tw_terminal_state", check);
1423 if (check == 0) {
1424 keep_going = 0;
1425 }
1426 } else if (has_data < 0) {
1427 // End of execution
1428 keep_going = 0;
1429 } else {
1430 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001431 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001432 gui_print("%s", line); // Display output
1433 else
1434 keep_going = 0; // Done executing
1435 }
1436 }
1437 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001438 }
thatc6085482015-01-09 22:12:43 +01001439 DataManager::SetValue("tw_operation_status", 0);
1440 DataManager::SetValue("tw_operation_state", 1);
1441 DataManager::SetValue("tw_terminal_state", 0);
1442 DataManager::SetValue("tw_background_thread_running", 0);
1443 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001444 }
1445 return 0;
1446}
1447
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001448int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001449{
that3f7b1ac2014-12-30 11:30:13 +01001450 LOGINFO("Sending kill command...\n");
1451 operation_start("KillCommand");
1452 DataManager::SetValue("tw_operation_status", 0);
1453 DataManager::SetValue("tw_operation_state", 1);
1454 DataManager::SetValue("tw_terminal_state", 0);
1455 DataManager::SetValue("tw_background_thread_running", 0);
1456 DataManager::SetValue(TW_ACTION_BUSY, 0);
1457 return 0;
1458}
1459
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001460int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001461{
1462 int op_status = 0;
1463 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001465 if (simulate) {
1466 simulate_progress_bar();
1467 } else {
1468 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001469 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001470 }
1471
1472 operation_end(op_status);
1473 return 0;
1474}
1475
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001476int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001477{
1478 int op_status = 0;
1479
1480 operation_start("CheckBackupName");
1481 if (simulate) {
1482 simulate_progress_bar();
1483 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001484 string Backup_Name;
1485 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1486 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001487 if (op_status != 0)
1488 op_status = 1;
1489 }
1490
1491 operation_end(op_status);
1492 return 0;
1493}
1494
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001495int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001496{
1497 int op_status = 0;
1498
1499 operation_start("Decrypt");
1500 if (simulate) {
1501 simulate_progress_bar();
1502 } else {
1503 string Password;
1504 DataManager::GetValue("tw_crypto_password", Password);
1505 op_status = PartitionManager.Decrypt_Device(Password);
1506 if (op_status != 0)
1507 op_status = 1;
1508 else {
that3f7b1ac2014-12-30 11:30:13 +01001509
1510 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1511
Ethan Yonkercf50da52015-01-12 21:59:07 -06001512 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001513
Ethan Yonkercf50da52015-01-12 21:59:07 -06001514 // Check for a custom theme and load it if exists
1515 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1516 if (has_datamedia != 0) {
1517 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001518 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001519 } else {
1520 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001521 }
1522 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001523 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001524 }
1525 }
1526
1527 operation_end(op_status);
1528 return 0;
1529}
1530
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001531int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001532{
1533 operation_start("Sideload");
1534 if (simulate) {
1535 simulate_progress_bar();
1536 operation_end(0);
1537 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001538 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001539 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1540
1541 // wait for the adb connection
1542 int ret = apply_from_adb("/", &sideload_child_pid);
1543 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1544
1545 if (ret != 0) {
1546 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001547 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001548 ret = 1; // failure
1549 } else {
1550 int wipe_cache = 0;
1551 int wipe_dalvik = 0;
1552 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1553
1554 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1555 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1556 PartitionManager.Wipe_By_Path("/cache");
1557 if (wipe_dalvik)
1558 PartitionManager.Wipe_Dalvik_Cache();
1559 } else {
1560 ret = 1; // failure
1561 }
thatcc8ddca2015-01-03 01:59:36 +01001562 }
thatc6085482015-01-09 22:12:43 +01001563 if (sideload_child_pid) {
1564 LOGINFO("Signaling child sideload process to exit.\n");
1565 struct stat st;
1566 // Calling stat() on this magic filename signals the minadbd
1567 // subprocess to shut down.
1568 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1569 int status;
1570 LOGINFO("Waiting for child sideload process to exit.\n");
1571 waitpid(sideload_child_pid, &status, 0);
1572 }
Dees Troy28a85d22015-04-28 02:03:16 +00001573 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001574 TWFunc::Toggle_MTP(mtp_was_enabled);
1575 reinject_after_flash();
1576 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001577 }
that3f7b1ac2014-12-30 11:30:13 +01001578 return 0;
1579}
1580
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001581int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001582{
that3f7b1ac2014-12-30 11:30:13 +01001583 struct stat st;
1584 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001585 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001586 LOGINFO("Signaling child sideload process to exit.\n");
1587 // Calling stat() on this magic filename signals the minadbd
1588 // subprocess to shut down.
1589 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1590 if (!sideload_child_pid) {
1591 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001592 return 0;
1593 }
thatcc8ddca2015-01-03 01:59:36 +01001594 ::sleep(1);
1595 LOGINFO("Killing child sideload process.\n");
1596 kill(sideload_child_pid, SIGTERM);
1597 int status;
1598 LOGINFO("Waiting for child sideload process to exit.\n");
1599 waitpid(sideload_child_pid, &status, 0);
1600 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001601 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1602 return 0;
1603}
1604
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001605int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001606{
1607 operation_start("OpenRecoveryScript");
1608 if (simulate) {
1609 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001610 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001611 } else {
that10ae24f2015-12-26 20:53:51 +01001612 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001613 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001614 }
1615 return 0;
1616}
1617
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001618int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001619{
1620 int op_status = 0;
1621
1622 operation_start("Install SuperSU");
1623 if (simulate) {
1624 simulate_progress_bar();
1625 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001626 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001627 }
1628
1629 operation_end(op_status);
1630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
1635 int op_status = 0;
1636
1637 operation_start("Fixing Superuser Permissions");
1638 if (simulate) {
1639 simulate_progress_bar();
1640 } else {
1641 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1642 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1643 }
1644
1645 operation_end(op_status);
1646 return 0;
1647}
1648
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001649int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001650{
1651 int op_status = 0;
1652
1653 operation_start("Try Restore Decrypt");
1654 if (simulate) {
1655 simulate_progress_bar();
1656 } else {
1657 string Restore_Path, Filename, Password;
1658 DataManager::GetValue("tw_restore", Restore_Path);
1659 Restore_Path += "/";
1660 DataManager::GetValue("tw_restore_password", Password);
1661 TWFunc::SetPerformanceMode(true);
1662 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1663 op_status = 0; // success
1664 else
1665 op_status = 1; // fail
1666 TWFunc::SetPerformanceMode(false);
1667 }
1668
1669 operation_end(op_status);
1670 return 0;
1671}
1672
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001673int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001674{
1675 int op_status = 0;
1676
1677 operation_start("Repair Partition");
1678 if (simulate) {
1679 simulate_progress_bar();
1680 } else {
1681 string part_path;
1682 DataManager::GetValue("tw_partition_mount_point", part_path);
1683 if (PartitionManager.Repair_By_Path(part_path, true)) {
1684 op_status = 0; // success
1685 } else {
that3f7b1ac2014-12-30 11:30:13 +01001686 op_status = 1; // fail
1687 }
1688 }
1689
1690 operation_end(op_status);
1691 return 0;
1692}
1693
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001694int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001695{
1696 int op_status = 0;
1697
1698 operation_start("Resize Partition");
1699 if (simulate) {
1700 simulate_progress_bar();
1701 } else {
1702 string part_path;
1703 DataManager::GetValue("tw_partition_mount_point", part_path);
1704 if (PartitionManager.Resize_By_Path(part_path, true)) {
1705 op_status = 0; // success
1706 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001707 op_status = 1; // fail
1708 }
1709 }
1710
1711 operation_end(op_status);
1712 return 0;
1713}
1714
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001715int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001716{
1717 int op_status = 0;
1718
1719 operation_start("Change File System");
1720 if (simulate) {
1721 simulate_progress_bar();
1722 } else {
1723 string part_path, file_system;
1724 DataManager::GetValue("tw_partition_mount_point", part_path);
1725 DataManager::GetValue("tw_action_new_file_system", file_system);
1726 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1727 op_status = 0; // success
1728 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001730 op_status = 1; // fail
1731 }
1732 }
1733 PartitionManager.Update_System_Details();
1734 operation_end(op_status);
1735 return 0;
1736}
1737
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001738int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001739{
1740 int op_status = 0;
1741
1742 operation_start("Start MTP");
1743 if (PartitionManager.Enable_MTP())
1744 op_status = 0; // success
1745 else
1746 op_status = 1; // fail
1747
1748 operation_end(op_status);
1749 return 0;
1750}
1751
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001752int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001753{
1754 int op_status = 0;
1755
1756 operation_start("Stop MTP");
1757 if (PartitionManager.Disable_MTP())
1758 op_status = 0; // success
1759 else
1760 op_status = 1; // fail
1761
1762 operation_end(op_status);
1763 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001764}
1765
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001766int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001767{
1768 int op_status = 0;
1769
1770 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001771 string path, filename;
1772 DataManager::GetValue("tw_zip_location", path);
1773 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001774 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001775 op_status = 0; // success
1776 else
1777 op_status = 1; // fail
1778
1779 operation_end(op_status);
1780 return 0;
1781}
1782
that10ae24f2015-12-26 20:53:51 +01001783int GUIAction::twcmd(std::string arg)
1784{
1785 operation_start("TWRP CLI Command");
1786 if (simulate)
1787 simulate_progress_bar();
1788 else
1789 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1790 operation_end(0);
1791 return 0;
1792}
1793
Dees_Troy51a0e822012-09-05 15:24:24 -04001794int GUIAction::getKeyByName(std::string key)
1795{
that8834a0f2016-01-05 23:29:30 +01001796 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001797 else if (key == "menu") return KEY_MENU;
1798 else if (key == "back") return KEY_BACK;
1799 else if (key == "search") return KEY_SEARCH;
1800 else if (key == "voldown") return KEY_VOLUMEDOWN;
1801 else if (key == "volup") return KEY_VOLUMEUP;
1802 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001803 int ret_val;
1804 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1805 if (!ret_val)
1806 return KEY_POWER;
1807 else
1808 return ret_val;
1809 }
1810
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001811 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001812}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001813
1814int GUIAction::checkpartitionlifetimewrites(std::string arg)
1815{
1816 int op_status = 0;
1817 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1818
1819 operation_start("Check Partition Lifetime Writes");
1820 if (sys) {
1821 if (sys->Check_Lifetime_Writes() != 0)
1822 DataManager::SetValue("tw_lifetime_writes", 1);
1823 else
1824 DataManager::SetValue("tw_lifetime_writes", 0);
1825 op_status = 0; // success
1826 } else {
1827 DataManager::SetValue("tw_lifetime_writes", 1);
1828 op_status = 1; // fail
1829 }
1830
1831 operation_end(op_status);
1832 return 0;
1833}
1834
1835int GUIAction::mountsystemtoggle(std::string arg)
1836{
1837 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001838 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001839 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001840
1841 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001842 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001843 op_status = 1; // fail
1844 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001845 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001846 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001847 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001848 DataManager::SetValue("tw_mount_system_ro", 0);
1849 Part->Change_Mount_Read_Only(false);
1850 } else {
1851 DataManager::SetValue("tw_mount_system_ro", 1);
1852 Part->Change_Mount_Read_Only(true);
1853 }
1854 if (remount_system) {
1855 Part->Mount(true);
1856 }
1857 op_status = 0; // success
1858 } else {
1859 op_status = 1; // fail
1860 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001861 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1862 if (Part) {
1863 if (arg == "0") {
1864 Part->Change_Mount_Read_Only(false);
1865 } else {
1866 Part->Change_Mount_Read_Only(true);
1867 }
1868 if (remount_vendor) {
1869 Part->Mount(true);
1870 }
1871 op_status = 0; // success
1872 } else {
1873 op_status = 1; // fail
1874 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001875 }
1876
1877 operation_end(op_status);
1878 return 0;
1879}
Ethan Yonker74db1572015-10-28 12:44:49 -05001880
1881int GUIAction::setlanguage(std::string arg __unused)
1882{
1883 int op_status = 0;
1884
1885 operation_start("Set Language");
1886 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1887 PageManager::RequestReload();
1888 op_status = 0; // success
1889
1890 operation_end(op_status);
1891 return 0;
1892}
Ethan Yonker1b190162016-12-05 15:25:19 -06001893
Matt Mower9472ba12016-01-20 18:12:47 -06001894int GUIAction::togglebacklight(std::string arg __unused)
1895{
1896 blankTimer.toggleBlank();
1897 return 0;
1898}
1899
Ethan Yonker1b190162016-12-05 15:25:19 -06001900int GUIAction::setbootslot(std::string arg)
1901{
1902 operation_start("Set Boot Slot");
1903 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001904 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001905 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001906 simulate_progress_bar();
1907 operation_end(0);
1908 return 0;
1909}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001910
1911int GUIAction::checkforapp(std::string arg __unused)
1912{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001913 operation_start("Check for TWRP App");
1914 if (!simulate)
1915 {
1916 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1917 int sdkver = 0;
1918 if (!sdkverstr.empty()) {
1919 sdkver = atoi(sdkverstr.c_str());
1920 }
1921 if (sdkver <= 13) {
1922 if (sdkver == 0)
1923 LOGINFO("Unable to read sdk version from build prop\n");
1924 else
1925 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001926 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 -06001927 goto exit;
1928 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001929 if (TWFunc::Is_TWRP_App_In_System()) {
1930 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1931 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001932 }
1933 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001934 const char parent_path[] = "/data/app";
1935 const char app_prefix[] = "me.twrp.twrpapp-";
1936 DIR *d = opendir(parent_path);
1937 if (d) {
1938 struct dirent *p;
1939 while ((p = readdir(d))) {
1940 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1941 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001943 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001944 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 -06001945 goto exit;
1946 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001947 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001948 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001949 } else {
1950 LOGINFO("Data partition cannot be mounted during app check\n");
1951 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 -06001952 }
1953 } else
1954 simulate_progress_bar();
1955 LOGINFO("App not installed\n");
1956 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1957exit:
1958 operation_end(0);
1959 return 0;
1960}
1961
1962int GUIAction::installapp(std::string arg __unused)
1963{
1964 int op_status = 1;
1965 operation_start("Install TWRP App");
1966 if (!simulate)
1967 {
1968 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1969 if (PartitionManager.Mount_By_Path("/data", true)) {
1970 string install_path = "/data/app";
1971 string context = "u:object_r:apk_data_file:s0";
1972 if (!TWFunc::Path_Exists(install_path)) {
1973 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1974 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1975 goto exit;
1976 }
1977 if (chown(install_path.c_str(), 1000, 1000)) {
1978 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1979 goto exit;
1980 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001981 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001982 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1983 goto exit;
1984 }
1985 }
1986 install_path += "/me.twrp.twrpapp-1";
1987 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1988 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1989 goto exit;
1990 }
1991 if (chown(install_path.c_str(), 1000, 1000)) {
1992 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1993 goto exit;
1994 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001995 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001996 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1997 goto exit;
1998 }
1999 install_path += "/base.apk";
2000 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2001 LOGERR("Error copying apk file\n");
2002 goto exit;
2003 }
2004 if (chown(install_path.c_str(), 1000, 1000)) {
2005 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2006 goto exit;
2007 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002008 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002009 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2010 goto exit;
2011 }
2012 sync();
2013 sync();
2014 }
2015 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002016 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2017 string base_path = PartitionManager.Get_Android_Root_Path();
2018 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002019 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2020 string install_path = base_path + "/priv-app";
2021 string context = "u:object_r:system_file:s0";
2022 if (!TWFunc::Path_Exists(install_path))
2023 install_path = base_path + "/app";
2024 if (TWFunc::Path_Exists(install_path)) {
2025 install_path += "/twrpapp";
2026 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2027 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002028 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002029 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2030 goto exit;
2031 }
2032 install_path += "/me.twrp.twrpapp.apk";
2033 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2034 LOGERR("Error copying apk file\n");
2035 goto exit;
2036 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002037 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002038 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2039 goto exit;
2040 }
2041 sync();
2042 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002043 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002044 op_status = 0;
2045 } else {
2046 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2047 }
2048 }
2049 }
2050 }
2051 } else
2052 simulate_progress_bar();
2053exit:
2054 operation_end(0);
2055 return 0;
2056}
Ethan Yonker53796e72019-01-11 22:49:52 -06002057
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002058int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2059{
2060 int op_status = 1;
2061 operation_start("Uninstall TWRP System App");
2062 if (!simulate)
2063 {
2064 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2065 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2066 if (!Part) {
2067 LOGERR("Unabled to find system partition.\n");
2068 goto exit;
2069 }
2070 if (!Part->UnMount(true)) {
2071 goto exit;
2072 }
2073 if (Mount_System_RO > 0) {
2074 DataManager::SetValue("tw_mount_system_ro", 0);
2075 Part->Change_Mount_Read_Only(false);
2076 }
2077 if (Part->Mount(true)) {
2078 string base_path = PartitionManager.Get_Android_Root_Path();
2079 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2080 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2081 string uninstall_path = base_path + "/priv-app";
2082 if (!TWFunc::Path_Exists(uninstall_path))
2083 uninstall_path = base_path + "/app";
2084 uninstall_path += "/twrpapp";
2085 if (TWFunc::Path_Exists(uninstall_path)) {
2086 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2087 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2088 sync();
2089 op_status = 0;
2090 DataManager::SetValue("tw_app_installed_in_system", 0);
2091 DataManager::SetValue("tw_app_install_status", 0);
2092 } else {
2093 LOGERR("Unable to remove TWRP app from system.\n");
2094 }
2095 } else {
2096 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2097 }
2098 }
2099 Part->UnMount(true);
2100 if (Mount_System_RO > 0) {
2101 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2102 Part->Change_Mount_Read_Only(true);
2103 }
2104 } else
2105 simulate_progress_bar();
2106exit:
2107 operation_end(0);
2108 return 0;
2109}
2110
Ethan Yonker53796e72019-01-11 22:49:52 -06002111int GUIAction::repackimage(std::string arg __unused)
2112{
2113 int op_status = 1;
2114 operation_start("Repack Image");
2115 if (!simulate)
2116 {
2117 std::string path = DataManager::GetStrValue("tw_filename");
2118 Repack_Options_struct Repack_Options;
2119 Repack_Options.Disable_Verity = false;
2120 Repack_Options.Disable_Force_Encrypt = false;
2121 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2122 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2123 Repack_Options.Type = REPLACE_KERNEL;
2124 else
2125 Repack_Options.Type = REPLACE_RAMDISK;
2126 if (!PartitionManager.Repack_Images(path, Repack_Options))
2127 goto exit;
2128 } else
2129 simulate_progress_bar();
2130 op_status = 0;
2131exit:
2132 operation_end(op_status);
2133 return 0;
2134}
2135
2136int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2137{
2138 int op_status = 1;
2139 operation_start("Repack Image");
2140 if (!simulate)
2141 {
2142 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2143 LOGERR("Image repacking tool not present in this TWRP build!");
2144 goto exit;
2145 }
2146 DataManager::SetProgress(0);
2147 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2148 if (part)
2149 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2150 else {
2151 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2152 goto exit;
2153 }
2154 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2155 goto exit;
2156 DataManager::SetProgress(.25);
2157 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
2158 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
2159 if (TWFunc::Exec_Cmd(command) != 0) {
2160 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2161 goto exit;
2162 }
2163 std::string header_path = REPACK_ORIG_DIR;
2164 header_path += "header";
2165 if (TWFunc::Path_Exists(header_path)) {
2166 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";
2167 if (TWFunc::Exec_Cmd(command) != 0) {
2168 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2169 goto exit;
2170 }
2171 }
2172 DataManager::SetProgress(.5);
2173 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
2174 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --repack " REPACK_ORIG_DIR "boot.img";
2175 if (TWFunc::Exec_Cmd(command) != 0) {
2176 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2177 goto exit;
2178 }
2179 DataManager::SetProgress(.75);
2180 std::string path = REPACK_ORIG_DIR;
2181 std::string file = "new-boot.img";
2182 DataManager::SetValue("tw_flash_partition", "/boot;");
2183 if (!PartitionManager.Flash_Image(path, file)) {
2184 LOGINFO("Error flashing new image\n");
2185 goto exit;
2186 }
2187 DataManager::SetProgress(1);
2188 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2189 } else
2190 simulate_progress_bar();
2191 op_status = 0;
2192exit:
2193 operation_end(op_status);
2194 return 0;
2195}