blob: 4b644a9b46e8ab442ba0846d8d2de0429df36bde [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);
that3f7b1ac2014-12-30 11:30:13 +0100234 }
235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600237 actions = FindNode(node, "actions");
238 if (actions) child = FindNode(actions, "action");
239 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 while (child)
244 {
245 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 attr = child->first_attribute("function");
248 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 action.mFunction = attr->value();
251 action.mArg = child->value();
252 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 child = child->next_sibling("action");
255 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600258 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 if (child)
260 {
261 attr = child->first_attribute("key");
262 if (attr)
263 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100264 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600265 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100266 {
267 const int key = getKeyByName(keys[i]);
268 mKeys[key] = false;
269 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 }
271 else
272 {
273 attr = child->first_attribute("x");
274 if (!attr) return;
275 mActionX = atol(attr->value());
276 attr = child->first_attribute("y");
277 if (!attr) return;
278 mActionY = atol(attr->value());
279 attr = child->first_attribute("w");
280 if (!attr) return;
281 mActionW = atol(attr->value());
282 attr = child->first_attribute("h");
283 if (!attr) return;
284 mActionH = atol(attr->value());
285 }
286 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400287}
288
Matt Mower25036b72016-06-24 12:30:18 -0500289int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400290{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 if (state == TOUCH_RELEASE)
292 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295}
296
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100297int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400298{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100299 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600300 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100301 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100302
303 bool prevState = itr->second;
304 itr->second = down;
305
306 // If there is only one key for this action, wait for key up so it
307 // doesn't trigger with multi-key actions.
308 // Else, check if all buttons are pressed, then consume their release events
309 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600310 if (mKeys.size() == 1) {
311 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312 doActions();
thatd4725ca2016-01-19 00:15:21 +0100313 return 0;
314 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600315 } else if (down) {
316 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
317 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100318 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100319 }
320
321 // Passed, all req buttons are pressed, reset them and consume release events
322 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600323 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100324 kb->ConsumeKeyRelease(itr->first);
325 itr->second = false;
326 }
327
328 doActions();
thatd4725ca2016-01-19 00:15:21 +0100329 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100330 }
331
thatd4725ca2016-01-19 00:15:21 +0100332 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
Vojtech Bocek07220562014-02-08 02:05:33 +0100335int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400336{
Vojtech Bocek07220562014-02-08 02:05:33 +0100337 GUIObject::NotifyVarChange(varName, value);
338
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100339 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600341 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400343
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
347void GUIAction::simulate_progress_bar(void)
348{
Ethan Yonker74db1572015-10-28 12:44:49 -0500349 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400350 for (int i = 0; i < 5; i++)
351 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500352 if (PartitionManager.stop_backup.get_value()) {
353 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600354 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500355 DataManager::SetValue("ui_progress", 0);
356 PartitionManager.stop_backup.set_value(0);
357 return;
358 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 usleep(500000);
360 DataManager::SetValue("ui_progress", i * 20);
361 }
362}
363
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600364int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400365{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
368 DataManager::SetValue("ui_progress", 0);
369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 if (filename.empty())
371 {
372 LOGERR("No file specified.\n");
373 return -1;
374 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Ethan Yonker9598c072016-01-15 21:54:34 -0600376 if (!TWFunc::Path_Exists(filename)) {
377 if (!PartitionManager.Mount_By_Path(filename, true)) {
378 return -1;
379 }
380 if (!TWFunc::Path_Exists(filename)) {
381 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
382 return -1;
383 }
384 }
Dees_Troy657c3092012-09-10 20:32:10 -0400385
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 if (simulate) {
387 simulate_progress_bar();
388 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400389 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400390
391 // Now, check if we need to ensure TWRP remains installed...
392 struct stat st;
393 if (stat("/sbin/installTwrp", &st) == 0)
394 {
395 DataManager::SetValue("tw_operation", "Configuring TWRP");
396 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500397 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200398 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500400 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 }
402 }
403 }
404
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 // Done
406 DataManager::SetValue("ui_progress", 100);
407 DataManager::SetValue("ui_progress", 0);
408 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409}
410
that73a52952015-01-28 23:07:34 +0100411GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100412{
that73a52952015-01-28 23:07:34 +0100413 string func = gui_parse_text(action.mFunction);
414 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
415 if (needsThread) {
416 if (func == "cancelbackup")
417 return THREAD_CANCEL;
418 else
419 return THREAD_ACTION;
420 }
421 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100422}
423
Dees_Troy51a0e822012-09-05 15:24:24 -0400424int GUIAction::doActions()
425{
that3f7b1ac2014-12-30 11:30:13 +0100426 if (mActions.size() < 1)
427 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400428
that73a52952015-01-28 23:07:34 +0100429 // Determine in which thread to run the actions.
430 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
431 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100432 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100433 for (it = mActions.begin(); it != mActions.end(); ++it) {
434 ThreadType tt = getThreadType(*it);
435 if (tt == THREAD_NONE)
436 continue;
437 if (threadType == THREAD_NONE)
438 threadType = tt;
439 else if (threadType != tt) {
440 LOGERR("Can't mix normal and cancel actions in the same list.\n"
441 "Running the whole batch in the cancel thread.\n");
442 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100443 break;
444 }
that7d3b54f2015-01-09 22:52:51 +0100445 }
that73a52952015-01-28 23:07:34 +0100446
447 // Now run the actions in the desired thread.
448 switch (threadType) {
449 case THREAD_ACTION:
450 action_thread.threadActions(this);
451 break;
452
453 case THREAD_CANCEL:
454 cancel_thread.threadActions(this);
455 break;
456
457 default: {
458 // no iterators here because theme reloading might kill our object
459 const size_t cnt = mActions.size();
460 for (size_t i = 0; i < cnt; ++i)
461 doAction(mActions[i]);
462 }
thatc6085482015-01-09 22:12:43 +0100463 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466}
467
that3f7b1ac2014-12-30 11:30:13 +0100468int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400469{
that3f7b1ac2014-12-30 11:30:13 +0100470 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
that3f7b1ac2014-12-30 11:30:13 +0100472 std::string function = gui_parse_text(action.mFunction);
473 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474
that3f7b1ac2014-12-30 11:30:13 +0100475 // find function and execute it
476 mapFunc::const_iterator funcitr = mf.find(function);
477 if (funcitr != mf.end())
478 return (this->*funcitr->second)(arg);
479
480 LOGERR("Unknown action '%s'\n", function.c_str());
481 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482}
483
484void GUIAction::operation_start(const string operation_name)
485{
that3f7b1ac2014-12-30 11:30:13 +0100486 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000487 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 DataManager::SetValue(TW_ACTION_BUSY, 1);
489 DataManager::SetValue("ui_progress", 0);
490 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600492 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500493 bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR;
494 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495}
496
that3f7b1ac2014-12-30 11:30:13 +0100497void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400498{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000499 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue("ui_progress", 100);
502 if (simulate) {
503 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
504 if (simulate_fail != 0)
505 DataManager::SetValue("tw_operation_status", 1);
506 else
507 DataManager::SetValue("tw_operation_status", 0);
508 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500511 }
512 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500514 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 }
516 DataManager::SetValue("tw_operation_state", 1);
517 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500518 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200519 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000520 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400521
522#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000523 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600524 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400525#endif
526
that3f7b1ac2014-12-30 11:30:13 +0100527 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400528}
529
that3f7b1ac2014-12-30 11:30:13 +0100530int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400531{
that3f7b1ac2014-12-30 11:30:13 +0100532 sync();
533 DataManager::SetValue("tw_gui_done", 1);
534 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400535
that3f7b1ac2014-12-30 11:30:13 +0100536 return 0;
537}
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500539int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100540{
that3f7b1ac2014-12-30 11:30:13 +0100541 gui_changePage("main");
542 return 0;
543}
Dees_Troy51a0e822012-09-05 15:24:24 -0400544
that3f7b1ac2014-12-30 11:30:13 +0100545int GUIAction::key(std::string arg)
546{
547 const int key = getKeyByName(arg);
548 PageManager::NotifyKey(key, true);
549 PageManager::NotifyKey(key, false);
550 return 0;
551}
552
553int GUIAction::page(std::string arg)
554{
LuK133762326f42015-09-30 19:57:46 +0200555 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100556 std::string page_name = gui_parse_text(arg);
557 return gui_changePage(page_name);
558}
559
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500560int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100561{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500562 PageManager::RequestReload();
563 // The actual reload is handled in pages.cpp in PageManager::RunReload()
564 // The reload will occur on the next Update or Render call and will
565 // be performed in the rendoer thread instead of the action thread
566 // to prevent crashing which could occur when we start deleting
567 // GUI resources in the action thread while we attempt to render
568 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100569 return 0;
570}
Dees_Troy51a0e822012-09-05 15:24:24 -0400571
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500572int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100573{
574 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400575
that3f7b1ac2014-12-30 11:30:13 +0100576 DataManager::GetValue("tw_restore", Restore_Name);
577 PartitionManager.Set_Restore_Files(Restore_Name);
578 return 0;
579}
580
581int GUIAction::set(std::string arg)
582{
583 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200584 {
that3f7b1ac2014-12-30 11:30:13 +0100585 string varName = arg.substr(0, arg.find('='));
586 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587
that3f7b1ac2014-12-30 11:30:13 +0100588 DataManager::GetValue(value, value);
589 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200590 }
that3f7b1ac2014-12-30 11:30:13 +0100591 else
592 DataManager::SetValue(arg, "1");
593 return 0;
594}
Dees_Troy51a0e822012-09-05 15:24:24 -0400595
that3f7b1ac2014-12-30 11:30:13 +0100596int GUIAction::clear(std::string arg)
597{
598 DataManager::SetValue(arg, "0");
599 return 0;
600}
Dees_Troy51a0e822012-09-05 15:24:24 -0400601
that3f7b1ac2014-12-30 11:30:13 +0100602int GUIAction::mount(std::string arg)
603{
604 if (arg == "usb") {
605 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100607 PartitionManager.usb_storage_enable();
608 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500609 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100610 } else if (!simulate) {
611 PartitionManager.Mount_By_Path(arg, true);
612 PartitionManager.Add_MTP_Storage(arg);
613 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500614 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100615 return 0;
616}
617
618int GUIAction::unmount(std::string arg)
619{
620 if (arg == "usb") {
621 if (!simulate)
622 PartitionManager.usb_storage_disable();
623 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500624 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100625 DataManager::SetValue(TW_ACTION_BUSY, 0);
626 } else if (!simulate) {
627 PartitionManager.UnMount_By_Path(arg, true);
628 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500629 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100630 return 0;
631}
632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500633int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100634{
635 operation_start("Restore Defaults");
636 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500637 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100638 else {
639 DataManager::ResetDefaults();
640 PartitionManager.Update_System_Details();
641 PartitionManager.Mount_Current_Storage(true);
642 }
643 operation_end(0);
644 return 0;
645}
646
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500647int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100648{
649 operation_start("Copy Log");
650 if (!simulate)
651 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400652 string dst, curr_storage;
653 int copy_kernel_log = 0;
654
655 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100656 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400657 curr_storage = DataManager::GetCurrentStoragePath();
658 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100659 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
660 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400661 if (copy_kernel_log)
662 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100663 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400664 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100665 } else
666 simulate_progress_bar();
667 operation_end(0);
668 return 0;
669}
670
671
672int GUIAction::compute(std::string arg)
673{
674 if (arg.find("+") != string::npos)
675 {
676 string varName = arg.substr(0, arg.find('+'));
677 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
678 int amount_to_add = atoi(string_to_add.c_str());
679 int value;
680
681 DataManager::GetValue(varName, value);
682 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 return 0;
684 }
that3f7b1ac2014-12-30 11:30:13 +0100685 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400686 {
that3f7b1ac2014-12-30 11:30:13 +0100687 string varName = arg.substr(0, arg.find('-'));
688 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
689 int amount_to_subtract = atoi(string_to_subtract.c_str());
690 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400691
that3f7b1ac2014-12-30 11:30:13 +0100692 DataManager::GetValue(varName, value);
693 value -= amount_to_subtract;
694 if (value <= 0)
695 value = 0;
696 DataManager::SetValue(varName, value);
697 return 0;
698 }
699 if (arg.find("*") != string::npos)
700 {
701 string varName = arg.substr(0, arg.find('*'));
702 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
703 int multiply_by = atoi(multiply_by_str.c_str());
704 int value;
705
706 DataManager::GetValue(varName, value);
707 DataManager::SetValue(varName, value*multiply_by);
708 return 0;
709 }
710 if (arg.find("/") != string::npos)
711 {
712 string varName = arg.substr(0, arg.find('/'));
713 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
714 int divide_by = atoi(divide_by_str.c_str());
715 int value;
716
Matt Mowera8a89d12016-12-30 18:10:37 -0600717 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100718 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100720 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400721 }
722 return 0;
723 }
that3f7b1ac2014-12-30 11:30:13 +0100724 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
725 return -1;
726}
Dees_Troy51a0e822012-09-05 15:24:24 -0400727
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500728int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100729{
730 string SelectedZone;
731 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
732 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
733 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
734
735 int dst;
736 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
737
738 string offset;
739 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
740
741 string NewTimeZone = Zone;
742 if (offset != "0")
743 NewTimeZone += ":" + offset;
744
745 if (dst != 0)
746 NewTimeZone += DSTZone;
747
748 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
749 DataManager::update_tz_environment_variables();
750 return 0;
751}
752
753int GUIAction::overlay(std::string arg)
754{
755 return gui_changeOverlay(arg);
756}
757
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500758int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100759{
760 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500761 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 return 0;
763 }
that3f7b1ac2014-12-30 11:30:13 +0100764 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
765 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
766 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100768 }
769 return 0;
770}
771
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500772int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100773{
774 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100777 } else {
778 zip_queue_index--;
779 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 }
that3f7b1ac2014-12-30 11:30:13 +0100781 return 0;
782}
Dees_Troy51a0e822012-09-05 15:24:24 -0400783
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500784int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100785{
786 zip_queue_index = 0;
787 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
788 return 0;
789}
Dees_Troy51a0e822012-09-05 15:24:24 -0400790
that3f7b1ac2014-12-30 11:30:13 +0100791int GUIAction::sleep(std::string arg)
792{
793 operation_start("Sleep");
794 usleep(atoi(arg.c_str()));
795 operation_end(0);
796 return 0;
797}
Dees Troyb21cc642013-09-10 17:36:41 +0000798
Matt Mower9a2a2052016-05-31 21:31:22 -0500799int GUIAction::sleepcounter(std::string arg)
800{
801 operation_start("SleepCounter");
802 // Ensure user notices countdown in case it needs to be cancelled
803 blankTimer.resetTimerAndUnblank();
804 int total = atoi(arg.c_str());
805 for (int t = total; t > 0; t--) {
806 int progress = (int)(((float)(total-t)/(float)total)*100.0);
807 DataManager::SetValue("ui_progress", progress);
808 ::sleep(1);
809 DataManager::SetValue("tw_sleep", t-1);
810 }
811 DataManager::SetValue("ui_progress", 100);
812 operation_end(0);
813 return 0;
814}
815
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500816int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100817{
818 operation_start("AppendDateToBackupName");
819 string Backup_Name;
820 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
821 Backup_Name += TWFunc::Get_Current_Date();
822 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
823 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
824 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500825 PageManager::NotifyKey(KEY_END, true);
826 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100827 operation_end(0);
828 return 0;
829}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500830
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500831int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100832{
833 operation_start("GenerateBackupName");
834 TWFunc::Auto_Generate_Backup_Name();
835 operation_end(0);
836 return 0;
837}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500838
Ethan Yonker483e9f42016-01-11 22:21:18 -0600839int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100840{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600841 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100842 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500843
Ethan Yonker483e9f42016-01-11 22:21:18 -0600844 if (arg.empty())
845 arg = "tw_wipe_list";
846 DataManager::GetValue(arg, List);
847 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
848 if (!List.empty()) {
849 size_t start_pos = 0, end_pos = List.find(";", start_pos);
850 while (end_pos != string::npos && start_pos < List.size()) {
851 part_path = List.substr(start_pos, end_pos - start_pos);
852 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
853 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100854 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400855 } else {
that3f7b1ac2014-12-30 11:30:13 +0100856 count++;
857 }
858 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600859 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100860 }
861 DataManager::SetValue("tw_check_partition_list", count);
862 } else {
863 DataManager::SetValue("tw_check_partition_list", 0);
864 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600865 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100866}
Dees_Troy51a0e822012-09-05 15:24:24 -0400867
Ethan Yonker483e9f42016-01-11 22:21:18 -0600868int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100869{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400871
Ethan Yonker483e9f42016-01-11 22:21:18 -0600872 if (arg.empty())
873 arg = "tw_wipe_list";
874 DataManager::GetValue(arg, List);
875 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
876 if (!List.empty()) {
877 size_t start_pos = 0, end_pos = List.find(";", start_pos);
878 part_path = List;
879 while (end_pos != string::npos && start_pos < List.size()) {
880 part_path = List.substr(start_pos, end_pos - start_pos);
881 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
882 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100883 // Do nothing
884 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600885 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100886 break;
887 }
888 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100890 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600891 if (!part_path.empty()) {
892 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100893 if (Part) {
894 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500895
that3f7b1ac2014-12-30 11:30:13 +0100896 DataManager::SetValue("tw_partition_name", Part->Display_Name);
897 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
898 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
899 DataManager::SetValue("tw_partition_size", Part->Size / mb);
900 DataManager::SetValue("tw_partition_used", Part->Used / mb);
901 DataManager::SetValue("tw_partition_free", Part->Free / mb);
902 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
903 DataManager::SetValue("tw_partition_removable", Part->Removable);
904 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
905
906 if (Part->Can_Repair())
907 DataManager::SetValue("tw_partition_can_repair", 1);
908 else
909 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500910 if (Part->Can_Resize())
911 DataManager::SetValue("tw_partition_can_resize", 1);
912 else
913 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600914 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100915 DataManager::SetValue("tw_partition_vfat", 1);
916 else
917 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600918 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100919 DataManager::SetValue("tw_partition_exfat", 1);
920 else
921 DataManager::SetValue("tw_partition_exfat", 0);
922 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
923 DataManager::SetValue("tw_partition_f2fs", 1);
924 else
925 DataManager::SetValue("tw_partition_f2fs", 0);
926 if (TWFunc::Path_Exists("/sbin/mke2fs"))
927 DataManager::SetValue("tw_partition_ext", 1);
928 else
929 DataManager::SetValue("tw_partition_ext", 0);
930 return 0;
931 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600932 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100933 }
934 }
935 }
936 DataManager::SetValue("tw_partition_name", "");
937 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600938 // Set this to 0 to prevent trying to partition this device, just in case
939 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100940 return 0;
941}
942
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500943int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100944{
945 time_t tm;
946 char path[256];
947 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600948 uid_t uid = AID_MEDIA_RW;
949 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100950
951 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600952 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100953 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
954 } else {
955 strcpy(path, "/tmp/");
956 }
957
Matt Mowera8a89d12016-12-30 18:10:37 -0600958 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100959 return 0;
960
961 tm = time(NULL);
962 path_len = strlen(path);
963
964 // Screenshot_2014-01-01-18-21-38.png
965 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
966
967 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600968 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100969 chmod(path, 0666);
970 chown(path, uid, gid);
971
that677b13f2015-12-26 23:57:31 +0100972 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100973
974 // blink to notify that the screenshow was taken
975 gr_color(255, 255, 255, 255);
976 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
977 gr_flip();
978 gui_forceRender();
979 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500980 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100981 }
982 return 0;
983}
984
985int GUIAction::setbrightness(std::string arg)
986{
987 return TWFunc::Set_Brightness(arg);
988}
989
990int GUIAction::fileexists(std::string arg)
991{
992 struct stat st;
993 string newpath = arg + "/.";
994
995 operation_start("FileExists");
996 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
997 operation_end(0);
998 else
999 operation_end(1);
1000 return 0;
1001}
1002
thatcc8ddca2015-01-03 01:59:36 +01001003void GUIAction::reinject_after_flash()
1004{
1005 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001006 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001007 if (simulate) {
1008 simulate_progress_bar();
1009 } else {
1010 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1011 if (Boot == NULL || Boot->Current_File_System != "emmc")
1012 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1013 else {
1014 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1015 TWFunc::Exec_Cmd(injectcmd);
1016 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001017 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001018 }
1019 }
1020}
1021
that3f7b1ac2014-12-30 11:30:13 +01001022int GUIAction::flash(std::string arg)
1023{
1024 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001025 // We're going to jump to this page first, like a loading page
1026 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001027 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001028 string zip_path = zip_queue[i];
1029 size_t slashpos = zip_path.find_last_of('/');
1030 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001031 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001032 DataManager::SetValue("tw_filename", zip_path);
1033 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001034 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1035
1036 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001037 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001038 TWFunc::SetPerformanceMode(false);
1039 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001040 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001041 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001042 break;
that3f7b1ac2014-12-30 11:30:13 +01001043 }
1044 }
1045 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001046
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001047 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001048 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001049 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001050 }
that3f7b1ac2014-12-30 11:30:13 +01001051
thatcc8ddca2015-01-03 01:59:36 +01001052 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001053 PartitionManager.Update_System_Details();
1054 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001055 // 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 -06001056 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001057 return 0;
1058}
1059
1060int GUIAction::wipe(std::string arg)
1061{
1062 operation_start("Format");
1063 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001064 int ret_val = false;
1065
1066 if (simulate) {
1067 simulate_progress_bar();
1068 } else {
1069 if (arg == "data")
1070 ret_val = PartitionManager.Factory_Reset();
1071 else if (arg == "battery")
1072 ret_val = PartitionManager.Wipe_Battery_Stats();
1073 else if (arg == "rotate")
1074 ret_val = PartitionManager.Wipe_Rotate_Data();
1075 else if (arg == "dalvik")
1076 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1077 else if (arg == "DATAMEDIA") {
1078 ret_val = PartitionManager.Format_Data();
1079 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001080 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001081
1082 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1083 if (has_datamedia) {
1084 ret_val = PartitionManager.Wipe_Media_From_Data();
1085 } else {
1086 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1087 }
1088 } else if (arg == "EXTERNAL") {
1089 string External_Path;
1090
1091 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1092 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1093 } else if (arg == "ANDROIDSECURE") {
1094 ret_val = PartitionManager.Wipe_Android_Secure();
1095 } else if (arg == "LIST") {
1096 string Wipe_List, wipe_path;
1097 bool skip = false;
1098 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001099
1100 DataManager::GetValue("tw_wipe_list", Wipe_List);
1101 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1102 if (!Wipe_List.empty()) {
1103 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1104 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1105 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1106 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1107 if (wipe_path == "/and-sec") {
1108 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001109 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001110 ret_val = false;
1111 break;
1112 } else {
1113 skip = true;
1114 }
1115 } else if (wipe_path == "DALVIK") {
1116 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001117 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001118 ret_val = false;
1119 break;
1120 } else {
1121 skip = true;
1122 }
1123 } else if (wipe_path == "INTERNAL") {
1124 if (!PartitionManager.Wipe_Media_From_Data()) {
1125 ret_val = false;
1126 break;
1127 } else {
1128 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 }
1130 }
that3f7b1ac2014-12-30 11:30:13 +01001131 if (!skip) {
1132 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001133 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001134 ret_val = false;
1135 break;
1136 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1137 arg = wipe_path;
1138 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 } else {
that3f7b1ac2014-12-30 11:30:13 +01001140 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 }
that3f7b1ac2014-12-30 11:30:13 +01001142 start_pos = end_pos + 1;
1143 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001144 }
1145 }
that3f7b1ac2014-12-30 11:30:13 +01001146 } else
1147 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001148#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001149 if (arg == DataManager::GetSettingsStoragePath()) {
1150 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1151 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001152
that3f7b1ac2014-12-30 11:30:13 +01001153 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1154 LOGINFO("Making TWRP folder and saving settings.\n");
1155 Storage_Path += "/TWRP";
1156 mkdir(Storage_Path.c_str(), 0777);
1157 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001158 } else {
that3f7b1ac2014-12-30 11:30:13 +01001159 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1160 }
1161 }
1162#endif
1163 }
1164 PartitionManager.Update_System_Details();
1165 if (ret_val)
1166 ret_val = 0; // 0 is success
1167 else
1168 ret_val = 1; // 1 is failure
1169 operation_end(ret_val);
1170 return 0;
1171}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001172
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001173int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001174{
1175 operation_start("Refreshing Sizes");
1176 if (simulate) {
1177 simulate_progress_bar();
1178 } else
1179 PartitionManager.Update_System_Details();
1180 operation_end(0);
1181 return 0;
1182}
1183
1184int GUIAction::nandroid(std::string arg)
1185{
that3f7b1ac2014-12-30 11:30:13 +01001186 if (simulate) {
that73a52952015-01-28 23:07:34 +01001187 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001188 DataManager::SetValue("tw_partition", "Simulation");
1189 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001190 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001191 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001192 operation_start("Nandroid");
1193 int ret = 0;
1194
that3f7b1ac2014-12-30 11:30:13 +01001195 if (arg == "backup") {
1196 string Backup_Name;
1197 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001198 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1199 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001200 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001201 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1202 if (!PartitionManager.stop_backup.get_value()) {
1203 if (ret == false)
1204 ret = 1; // 1 for failure
1205 else
1206 ret = 0; // 0 for success
1207 DataManager::SetValue("tw_cancel_backup", 0);
1208 } else {
1209 DataManager::SetValue("tw_cancel_backup", 1);
1210 gui_msg("backup_cancel=Backup Cancelled");
1211 ret = 0;
1212 }
bigbiffce8f83c2015-12-12 18:30:21 -05001213 } else {
that3f7b1ac2014-12-30 11:30:13 +01001214 operation_end(1);
1215 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001216 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001217 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001218 } else if (arg == "restore") {
1219 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001220 int gui_adb_backup;
1221
that3f7b1ac2014-12-30 11:30:13 +01001222 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001223 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1224 if (gui_adb_backup) {
1225 DataManager::SetValue("tw_operation_state", 1);
1226 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1227 ret = 0; // success
1228 else
1229 ret = 1; // failure
1230 DataManager::SetValue("tw_enable_adb_backup", 0);
1231 ret = 0; // assume success???
1232 } else {
1233 if (PartitionManager.Run_Restore(Restore_Name))
1234 ret = 0; // success
1235 else
1236 ret = 1; // failure
1237 }
that3f7b1ac2014-12-30 11:30:13 +01001238 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001239 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001240 return -1;
1241 }
that73a52952015-01-28 23:07:34 +01001242 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001243 return ret;
1244 }
1245 return 0;
1246}
1247
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001248int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001249 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001250 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001251 }
1252 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001253 int op_status = PartitionManager.Cancel_Backup();
1254 if (op_status != 0)
1255 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001256 }
1257
1258 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001259}
Dees_Troy51a0e822012-09-05 15:24:24 -04001260
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001261int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001262{
that73a52952015-01-28 23:07:34 +01001263 int op_status = 0;
1264
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001265 operation_start("Fix Contexts");
1266 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001267 if (simulate) {
1268 simulate_progress_bar();
1269 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001270 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001271 if (op_status != 0)
1272 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001273 }
that73a52952015-01-28 23:07:34 +01001274 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001275 return 0;
1276}
1277
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001278int GUIAction::fixpermissions(std::string arg)
1279{
1280 return fixcontexts(arg);
1281}
1282
that3f7b1ac2014-12-30 11:30:13 +01001283int GUIAction::dd(std::string arg)
1284{
1285 operation_start("imaging");
1286
1287 if (simulate) {
1288 simulate_progress_bar();
1289 } else {
1290 string cmd = "dd " + arg;
1291 TWFunc::Exec_Cmd(cmd);
1292 }
1293 operation_end(0);
1294 return 0;
1295}
1296
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001297int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001298{
1299 operation_start("Partition SD Card");
1300 int ret_val = 0;
1301
1302 if (simulate) {
1303 simulate_progress_bar();
1304 } else {
1305 int allow_partition;
1306 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1307 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001308 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001309 } else {
1310 if (!PartitionManager.Partition_SDCard())
1311 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001312 }
that3f7b1ac2014-12-30 11:30:13 +01001313 }
1314 operation_end(ret_val);
1315 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001316
that3f7b1ac2014-12-30 11:30:13 +01001317}
Dees_Troy51a0e822012-09-05 15:24:24 -04001318
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001319int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001320{
1321 operation_start("Install HTC Dumlock");
1322 if (simulate) {
1323 simulate_progress_bar();
1324 } else
1325 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001326
that3f7b1ac2014-12-30 11:30:13 +01001327 operation_end(0);
1328 return 0;
1329}
Dees_Troy51a0e822012-09-05 15:24:24 -04001330
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001331int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001332{
1333 operation_start("HTC Dumlock Restore Boot");
1334 if (simulate) {
1335 simulate_progress_bar();
1336 } else
1337 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001338
that3f7b1ac2014-12-30 11:30:13 +01001339 operation_end(0);
1340 return 0;
1341}
Dees_Troy51a0e822012-09-05 15:24:24 -04001342
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001343int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001344{
1345 operation_start("HTC Dumlock Reflash Recovery");
1346 if (simulate) {
1347 simulate_progress_bar();
1348 } else
1349 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001350
that3f7b1ac2014-12-30 11:30:13 +01001351 operation_end(0);
1352 return 0;
1353}
Dees_Troy51a0e822012-09-05 15:24:24 -04001354
that3f7b1ac2014-12-30 11:30:13 +01001355int GUIAction::cmd(std::string arg)
1356{
1357 int op_status = 0;
1358
1359 operation_start("Command");
1360 LOGINFO("Running command: '%s'\n", arg.c_str());
1361 if (simulate) {
1362 simulate_progress_bar();
1363 } else {
1364 op_status = TWFunc::Exec_Cmd(arg);
1365 if (op_status != 0)
1366 op_status = 1;
1367 }
1368
1369 operation_end(op_status);
1370 return 0;
1371}
1372
1373int GUIAction::terminalcommand(std::string arg)
1374{
1375 int op_status = 0;
1376 string cmdpath, command;
1377
1378 DataManager::GetValue("tw_terminal_location", cmdpath);
1379 operation_start("CommandOutput");
1380 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1381 if (simulate) {
1382 simulate_progress_bar();
1383 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001384 } else if (arg == "exit") {
1385 LOGINFO("Exiting terminal\n");
1386 operation_end(op_status);
1387 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001388 } else {
1389 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1390 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001391 DataManager::SetValue("tw_terminal_state", 1);
1392 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001393 FILE* fp;
1394 char line[512];
1395
1396 fp = popen(command.c_str(), "r");
1397 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001398 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001399 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001400 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001401 struct timeval timeout;
1402 fd_set fdset;
1403
Matt Mowera8a89d12016-12-30 18:10:37 -06001404 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001405 {
1406 FD_ZERO(&fdset);
1407 FD_SET(fd, &fdset);
1408 timeout.tv_sec = 0;
1409 timeout.tv_usec = 400000;
1410 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1411 if (has_data == 0) {
1412 // Timeout reached
1413 DataManager::GetValue("tw_terminal_state", check);
1414 if (check == 0) {
1415 keep_going = 0;
1416 }
1417 } else if (has_data < 0) {
1418 // End of execution
1419 keep_going = 0;
1420 } else {
1421 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001422 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001423 gui_print("%s", line); // Display output
1424 else
1425 keep_going = 0; // Done executing
1426 }
1427 }
1428 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001429 }
thatc6085482015-01-09 22:12:43 +01001430 DataManager::SetValue("tw_operation_status", 0);
1431 DataManager::SetValue("tw_operation_state", 1);
1432 DataManager::SetValue("tw_terminal_state", 0);
1433 DataManager::SetValue("tw_background_thread_running", 0);
1434 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001435 }
1436 return 0;
1437}
1438
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001439int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001440{
that3f7b1ac2014-12-30 11:30:13 +01001441 LOGINFO("Sending kill command...\n");
1442 operation_start("KillCommand");
1443 DataManager::SetValue("tw_operation_status", 0);
1444 DataManager::SetValue("tw_operation_state", 1);
1445 DataManager::SetValue("tw_terminal_state", 0);
1446 DataManager::SetValue("tw_background_thread_running", 0);
1447 DataManager::SetValue(TW_ACTION_BUSY, 0);
1448 return 0;
1449}
1450
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001451int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001452{
1453 int op_status = 0;
1454 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001455 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001456 if (simulate) {
1457 simulate_progress_bar();
1458 } else {
1459 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001461 }
1462
1463 operation_end(op_status);
1464 return 0;
1465}
1466
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001467int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001468{
1469 int op_status = 0;
1470
1471 operation_start("CheckBackupName");
1472 if (simulate) {
1473 simulate_progress_bar();
1474 } else {
1475 op_status = PartitionManager.Check_Backup_Name(true);
1476 if (op_status != 0)
1477 op_status = 1;
1478 }
1479
1480 operation_end(op_status);
1481 return 0;
1482}
1483
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001484int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001485{
1486 int op_status = 0;
1487
1488 operation_start("Decrypt");
1489 if (simulate) {
1490 simulate_progress_bar();
1491 } else {
1492 string Password;
1493 DataManager::GetValue("tw_crypto_password", Password);
1494 op_status = PartitionManager.Decrypt_Device(Password);
1495 if (op_status != 0)
1496 op_status = 1;
1497 else {
that3f7b1ac2014-12-30 11:30:13 +01001498
1499 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1500
Ethan Yonkercf50da52015-01-12 21:59:07 -06001501 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001502
Ethan Yonkercf50da52015-01-12 21:59:07 -06001503 // Check for a custom theme and load it if exists
1504 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1505 if (has_datamedia != 0) {
1506 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001507 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001508 } else {
1509 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001510 }
1511 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001512 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001513 }
1514 }
1515
1516 operation_end(op_status);
1517 return 0;
1518}
1519
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001520int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001521{
1522 operation_start("Sideload");
1523 if (simulate) {
1524 simulate_progress_bar();
1525 operation_end(0);
1526 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001527 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001528 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1529
1530 // wait for the adb connection
1531 int ret = apply_from_adb("/", &sideload_child_pid);
1532 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1533
1534 if (ret != 0) {
1535 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001537 ret = 1; // failure
1538 } else {
1539 int wipe_cache = 0;
1540 int wipe_dalvik = 0;
1541 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1542
1543 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1544 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1545 PartitionManager.Wipe_By_Path("/cache");
1546 if (wipe_dalvik)
1547 PartitionManager.Wipe_Dalvik_Cache();
1548 } else {
1549 ret = 1; // failure
1550 }
thatcc8ddca2015-01-03 01:59:36 +01001551 }
thatc6085482015-01-09 22:12:43 +01001552 if (sideload_child_pid) {
1553 LOGINFO("Signaling child sideload process to exit.\n");
1554 struct stat st;
1555 // Calling stat() on this magic filename signals the minadbd
1556 // subprocess to shut down.
1557 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1558 int status;
1559 LOGINFO("Waiting for child sideload process to exit.\n");
1560 waitpid(sideload_child_pid, &status, 0);
1561 }
Dees Troy28a85d22015-04-28 02:03:16 +00001562 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001563 TWFunc::Toggle_MTP(mtp_was_enabled);
1564 reinject_after_flash();
1565 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001566 }
that3f7b1ac2014-12-30 11:30:13 +01001567 return 0;
1568}
1569
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001570int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001571{
that3f7b1ac2014-12-30 11:30:13 +01001572 struct stat st;
1573 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001574 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001575 LOGINFO("Signaling child sideload process to exit.\n");
1576 // Calling stat() on this magic filename signals the minadbd
1577 // subprocess to shut down.
1578 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1579 if (!sideload_child_pid) {
1580 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001581 return 0;
1582 }
thatcc8ddca2015-01-03 01:59:36 +01001583 ::sleep(1);
1584 LOGINFO("Killing child sideload process.\n");
1585 kill(sideload_child_pid, SIGTERM);
1586 int status;
1587 LOGINFO("Waiting for child sideload process to exit.\n");
1588 waitpid(sideload_child_pid, &status, 0);
1589 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001590 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1591 return 0;
1592}
1593
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001594int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001595{
1596 operation_start("OpenRecoveryScript");
1597 if (simulate) {
1598 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001599 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001600 } else {
that10ae24f2015-12-26 20:53:51 +01001601 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001602 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001603 }
1604 return 0;
1605}
1606
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001607int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001608{
1609 int op_status = 0;
1610
1611 operation_start("Install SuperSU");
1612 if (simulate) {
1613 simulate_progress_bar();
1614 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001615 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001616 }
1617
1618 operation_end(op_status);
1619 return 0;
1620}
1621
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001622int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001623{
1624 int op_status = 0;
1625
1626 operation_start("Fixing Superuser Permissions");
1627 if (simulate) {
1628 simulate_progress_bar();
1629 } else {
1630 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1631 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1632 }
1633
1634 operation_end(op_status);
1635 return 0;
1636}
1637
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001638int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001639{
1640 int op_status = 0;
1641
1642 operation_start("Try Restore Decrypt");
1643 if (simulate) {
1644 simulate_progress_bar();
1645 } else {
1646 string Restore_Path, Filename, Password;
1647 DataManager::GetValue("tw_restore", Restore_Path);
1648 Restore_Path += "/";
1649 DataManager::GetValue("tw_restore_password", Password);
1650 TWFunc::SetPerformanceMode(true);
1651 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1652 op_status = 0; // success
1653 else
1654 op_status = 1; // fail
1655 TWFunc::SetPerformanceMode(false);
1656 }
1657
1658 operation_end(op_status);
1659 return 0;
1660}
1661
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001662int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001663{
1664 int op_status = 0;
1665
1666 operation_start("Repair Partition");
1667 if (simulate) {
1668 simulate_progress_bar();
1669 } else {
1670 string part_path;
1671 DataManager::GetValue("tw_partition_mount_point", part_path);
1672 if (PartitionManager.Repair_By_Path(part_path, true)) {
1673 op_status = 0; // success
1674 } else {
that3f7b1ac2014-12-30 11:30:13 +01001675 op_status = 1; // fail
1676 }
1677 }
1678
1679 operation_end(op_status);
1680 return 0;
1681}
1682
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001683int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001684{
1685 int op_status = 0;
1686
1687 operation_start("Resize Partition");
1688 if (simulate) {
1689 simulate_progress_bar();
1690 } else {
1691 string part_path;
1692 DataManager::GetValue("tw_partition_mount_point", part_path);
1693 if (PartitionManager.Resize_By_Path(part_path, true)) {
1694 op_status = 0; // success
1695 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001696 op_status = 1; // fail
1697 }
1698 }
1699
1700 operation_end(op_status);
1701 return 0;
1702}
1703
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001704int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001705{
1706 int op_status = 0;
1707
1708 operation_start("Change File System");
1709 if (simulate) {
1710 simulate_progress_bar();
1711 } else {
1712 string part_path, file_system;
1713 DataManager::GetValue("tw_partition_mount_point", part_path);
1714 DataManager::GetValue("tw_action_new_file_system", file_system);
1715 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1716 op_status = 0; // success
1717 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001718 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001719 op_status = 1; // fail
1720 }
1721 }
1722 PartitionManager.Update_System_Details();
1723 operation_end(op_status);
1724 return 0;
1725}
1726
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001727int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001728{
1729 int op_status = 0;
1730
1731 operation_start("Start MTP");
1732 if (PartitionManager.Enable_MTP())
1733 op_status = 0; // success
1734 else
1735 op_status = 1; // fail
1736
1737 operation_end(op_status);
1738 return 0;
1739}
1740
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001741int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001742{
1743 int op_status = 0;
1744
1745 operation_start("Stop MTP");
1746 if (PartitionManager.Disable_MTP())
1747 op_status = 0; // success
1748 else
1749 op_status = 1; // fail
1750
1751 operation_end(op_status);
1752 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001753}
1754
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001755int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001756{
1757 int op_status = 0;
1758
1759 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001760 string path, filename;
1761 DataManager::GetValue("tw_zip_location", path);
1762 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001763 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001764 op_status = 0; // success
1765 else
1766 op_status = 1; // fail
1767
1768 operation_end(op_status);
1769 return 0;
1770}
1771
that10ae24f2015-12-26 20:53:51 +01001772int GUIAction::twcmd(std::string arg)
1773{
1774 operation_start("TWRP CLI Command");
1775 if (simulate)
1776 simulate_progress_bar();
1777 else
1778 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1779 operation_end(0);
1780 return 0;
1781}
1782
Dees_Troy51a0e822012-09-05 15:24:24 -04001783int GUIAction::getKeyByName(std::string key)
1784{
that8834a0f2016-01-05 23:29:30 +01001785 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001786 else if (key == "menu") return KEY_MENU;
1787 else if (key == "back") return KEY_BACK;
1788 else if (key == "search") return KEY_SEARCH;
1789 else if (key == "voldown") return KEY_VOLUMEDOWN;
1790 else if (key == "volup") return KEY_VOLUMEUP;
1791 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001792 int ret_val;
1793 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1794 if (!ret_val)
1795 return KEY_POWER;
1796 else
1797 return ret_val;
1798 }
1799
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001800 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001801}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001802
1803int GUIAction::checkpartitionlifetimewrites(std::string arg)
1804{
1805 int op_status = 0;
1806 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1807
1808 operation_start("Check Partition Lifetime Writes");
1809 if (sys) {
1810 if (sys->Check_Lifetime_Writes() != 0)
1811 DataManager::SetValue("tw_lifetime_writes", 1);
1812 else
1813 DataManager::SetValue("tw_lifetime_writes", 0);
1814 op_status = 0; // success
1815 } else {
1816 DataManager::SetValue("tw_lifetime_writes", 1);
1817 op_status = 1; // fail
1818 }
1819
1820 operation_end(op_status);
1821 return 0;
1822}
1823
1824int GUIAction::mountsystemtoggle(std::string arg)
1825{
1826 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001827 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001828 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001829
1830 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001831 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001832 op_status = 1; // fail
1833 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001834 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001835 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001836 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001837 DataManager::SetValue("tw_mount_system_ro", 0);
1838 Part->Change_Mount_Read_Only(false);
1839 } else {
1840 DataManager::SetValue("tw_mount_system_ro", 1);
1841 Part->Change_Mount_Read_Only(true);
1842 }
1843 if (remount_system) {
1844 Part->Mount(true);
1845 }
1846 op_status = 0; // success
1847 } else {
1848 op_status = 1; // fail
1849 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001850 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1851 if (Part) {
1852 if (arg == "0") {
1853 Part->Change_Mount_Read_Only(false);
1854 } else {
1855 Part->Change_Mount_Read_Only(true);
1856 }
1857 if (remount_vendor) {
1858 Part->Mount(true);
1859 }
1860 op_status = 0; // success
1861 } else {
1862 op_status = 1; // fail
1863 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001864 }
1865
1866 operation_end(op_status);
1867 return 0;
1868}
Ethan Yonker74db1572015-10-28 12:44:49 -05001869
1870int GUIAction::setlanguage(std::string arg __unused)
1871{
1872 int op_status = 0;
1873
1874 operation_start("Set Language");
1875 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1876 PageManager::RequestReload();
1877 op_status = 0; // success
1878
1879 operation_end(op_status);
1880 return 0;
1881}
Ethan Yonker1b190162016-12-05 15:25:19 -06001882
Matt Mower9472ba12016-01-20 18:12:47 -06001883int GUIAction::togglebacklight(std::string arg __unused)
1884{
1885 blankTimer.toggleBlank();
1886 return 0;
1887}
1888
Ethan Yonker1b190162016-12-05 15:25:19 -06001889int GUIAction::setbootslot(std::string arg)
1890{
1891 operation_start("Set Boot Slot");
1892 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001893 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001894 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001895 simulate_progress_bar();
1896 operation_end(0);
1897 return 0;
1898}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001899
1900int GUIAction::checkforapp(std::string arg __unused)
1901{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001902 operation_start("Check for TWRP App");
1903 if (!simulate)
1904 {
1905 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1906 int sdkver = 0;
1907 if (!sdkverstr.empty()) {
1908 sdkver = atoi(sdkverstr.c_str());
1909 }
1910 if (sdkver <= 13) {
1911 if (sdkver == 0)
1912 LOGINFO("Unable to read sdk version from build prop\n");
1913 else
1914 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001915 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 -06001916 goto exit;
1917 }
Captain Throwback9d6feb52018-07-27 10:05:24 -04001918 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
1919 string base_path = PartitionManager.Get_Android_Root_Path();
1920 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001921 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1922 string install_path = base_path + "/priv-app";
1923 if (!TWFunc::Path_Exists(install_path))
1924 install_path = base_path + "/app";
1925 install_path += "/twrpapp";
1926 if (TWFunc::Path_Exists(install_path)) {
1927 LOGINFO("App found at '%s'\n", install_path.c_str());
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001928 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 -06001929 goto exit;
1930 }
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001931 }
1932 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001933 const char parent_path[] = "/data/app";
1934 const char app_prefix[] = "me.twrp.twrpapp-";
1935 DIR *d = opendir(parent_path);
1936 if (d) {
1937 struct dirent *p;
1938 while ((p = readdir(d))) {
1939 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1940 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001941 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001942 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001943 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 -06001944 goto exit;
1945 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001946 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001947 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001948 } else {
1949 LOGINFO("Data partition cannot be mounted during app check\n");
1950 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 -06001951 }
1952 } else
1953 simulate_progress_bar();
1954 LOGINFO("App not installed\n");
1955 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1956exit:
1957 operation_end(0);
1958 return 0;
1959}
1960
1961int GUIAction::installapp(std::string arg __unused)
1962{
1963 int op_status = 1;
1964 operation_start("Install TWRP App");
1965 if (!simulate)
1966 {
1967 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1968 if (PartitionManager.Mount_By_Path("/data", true)) {
1969 string install_path = "/data/app";
1970 string context = "u:object_r:apk_data_file:s0";
1971 if (!TWFunc::Path_Exists(install_path)) {
1972 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1973 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1974 goto exit;
1975 }
1976 if (chown(install_path.c_str(), 1000, 1000)) {
1977 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1978 goto exit;
1979 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001980 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001981 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1982 goto exit;
1983 }
1984 }
1985 install_path += "/me.twrp.twrpapp-1";
1986 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1987 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1988 goto exit;
1989 }
1990 if (chown(install_path.c_str(), 1000, 1000)) {
1991 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1992 goto exit;
1993 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001994 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001995 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1996 goto exit;
1997 }
1998 install_path += "/base.apk";
1999 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2000 LOGERR("Error copying apk file\n");
2001 goto exit;
2002 }
2003 if (chown(install_path.c_str(), 1000, 1000)) {
2004 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2005 goto exit;
2006 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002007 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002008 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2009 goto exit;
2010 }
2011 sync();
2012 sync();
2013 }
2014 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002015 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2016 string base_path = PartitionManager.Get_Android_Root_Path();
2017 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002018 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2019 string install_path = base_path + "/priv-app";
2020 string context = "u:object_r:system_file:s0";
2021 if (!TWFunc::Path_Exists(install_path))
2022 install_path = base_path + "/app";
2023 if (TWFunc::Path_Exists(install_path)) {
2024 install_path += "/twrpapp";
2025 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2026 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002027 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002028 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2029 goto exit;
2030 }
2031 install_path += "/me.twrp.twrpapp.apk";
2032 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2033 LOGERR("Error copying apk file\n");
2034 goto exit;
2035 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002036 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002037 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2038 goto exit;
2039 }
2040 sync();
2041 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002042 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002043 op_status = 0;
2044 } else {
2045 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2046 }
2047 }
2048 }
2049 }
2050 } else
2051 simulate_progress_bar();
2052exit:
2053 operation_end(0);
2054 return 0;
2055}