blob: 11d9580fb3c16f6b10f4d2a7bf233b279979ed79 [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);
Dees_Troy51a0e822012-09-05 15:24:24 -0400493}
494
that3f7b1ac2014-12-30 11:30:13 +0100495void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400496{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000497 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue("ui_progress", 100);
500 if (simulate) {
501 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
502 if (simulate_fail != 0)
503 DataManager::SetValue("tw_operation_status", 1);
504 else
505 DataManager::SetValue("tw_operation_status", 0);
506 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 }
510 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500512 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 }
514 DataManager::SetValue("tw_operation_state", 1);
515 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500516 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200517 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000518 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400519
520#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000521 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600522 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400523#endif
524
that3f7b1ac2014-12-30 11:30:13 +0100525 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400526}
527
that3f7b1ac2014-12-30 11:30:13 +0100528int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400529{
that3f7b1ac2014-12-30 11:30:13 +0100530 sync();
531 DataManager::SetValue("tw_gui_done", 1);
532 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400533
that3f7b1ac2014-12-30 11:30:13 +0100534 return 0;
535}
Dees_Troy51a0e822012-09-05 15:24:24 -0400536
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500537int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100538{
that3f7b1ac2014-12-30 11:30:13 +0100539 gui_changePage("main");
540 return 0;
541}
Dees_Troy51a0e822012-09-05 15:24:24 -0400542
that3f7b1ac2014-12-30 11:30:13 +0100543int GUIAction::key(std::string arg)
544{
545 const int key = getKeyByName(arg);
546 PageManager::NotifyKey(key, true);
547 PageManager::NotifyKey(key, false);
548 return 0;
549}
550
551int GUIAction::page(std::string arg)
552{
LuK133762326f42015-09-30 19:57:46 +0200553 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100554 std::string page_name = gui_parse_text(arg);
555 return gui_changePage(page_name);
556}
557
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500558int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100559{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500560 PageManager::RequestReload();
561 // The actual reload is handled in pages.cpp in PageManager::RunReload()
562 // The reload will occur on the next Update or Render call and will
563 // be performed in the rendoer thread instead of the action thread
564 // to prevent crashing which could occur when we start deleting
565 // GUI resources in the action thread while we attempt to render
566 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100567 return 0;
568}
Dees_Troy51a0e822012-09-05 15:24:24 -0400569
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500570int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100571{
572 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400573
that3f7b1ac2014-12-30 11:30:13 +0100574 DataManager::GetValue("tw_restore", Restore_Name);
575 PartitionManager.Set_Restore_Files(Restore_Name);
576 return 0;
577}
578
579int GUIAction::set(std::string arg)
580{
581 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200582 {
that3f7b1ac2014-12-30 11:30:13 +0100583 string varName = arg.substr(0, arg.find('='));
584 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
that3f7b1ac2014-12-30 11:30:13 +0100586 DataManager::GetValue(value, value);
587 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 }
that3f7b1ac2014-12-30 11:30:13 +0100589 else
590 DataManager::SetValue(arg, "1");
591 return 0;
592}
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
that3f7b1ac2014-12-30 11:30:13 +0100594int GUIAction::clear(std::string arg)
595{
596 DataManager::SetValue(arg, "0");
597 return 0;
598}
Dees_Troy51a0e822012-09-05 15:24:24 -0400599
that3f7b1ac2014-12-30 11:30:13 +0100600int GUIAction::mount(std::string arg)
601{
602 if (arg == "usb") {
603 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100605 PartitionManager.usb_storage_enable();
606 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500607 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100608 } else if (!simulate) {
609 PartitionManager.Mount_By_Path(arg, true);
610 PartitionManager.Add_MTP_Storage(arg);
611 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500612 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100613 return 0;
614}
615
616int GUIAction::unmount(std::string arg)
617{
618 if (arg == "usb") {
619 if (!simulate)
620 PartitionManager.usb_storage_disable();
621 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500622 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100623 DataManager::SetValue(TW_ACTION_BUSY, 0);
624 } else if (!simulate) {
625 PartitionManager.UnMount_By_Path(arg, true);
626 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500627 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100628 return 0;
629}
630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500631int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100632{
633 operation_start("Restore Defaults");
634 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500635 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100636 else {
637 DataManager::ResetDefaults();
638 PartitionManager.Update_System_Details();
639 PartitionManager.Mount_Current_Storage(true);
640 }
641 operation_end(0);
642 return 0;
643}
644
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500645int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100646{
647 operation_start("Copy Log");
648 if (!simulate)
649 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400650 string dst, curr_storage;
651 int copy_kernel_log = 0;
652
653 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100654 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400655 curr_storage = DataManager::GetCurrentStoragePath();
656 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100657 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
658 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400659 if (copy_kernel_log)
660 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100661 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400662 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100663 } else
664 simulate_progress_bar();
665 operation_end(0);
666 return 0;
667}
668
669
670int GUIAction::compute(std::string arg)
671{
672 if (arg.find("+") != string::npos)
673 {
674 string varName = arg.substr(0, arg.find('+'));
675 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
676 int amount_to_add = atoi(string_to_add.c_str());
677 int value;
678
679 DataManager::GetValue(varName, value);
680 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 return 0;
682 }
that3f7b1ac2014-12-30 11:30:13 +0100683 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400684 {
that3f7b1ac2014-12-30 11:30:13 +0100685 string varName = arg.substr(0, arg.find('-'));
686 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
687 int amount_to_subtract = atoi(string_to_subtract.c_str());
688 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400689
that3f7b1ac2014-12-30 11:30:13 +0100690 DataManager::GetValue(varName, value);
691 value -= amount_to_subtract;
692 if (value <= 0)
693 value = 0;
694 DataManager::SetValue(varName, value);
695 return 0;
696 }
697 if (arg.find("*") != string::npos)
698 {
699 string varName = arg.substr(0, arg.find('*'));
700 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
701 int multiply_by = atoi(multiply_by_str.c_str());
702 int value;
703
704 DataManager::GetValue(varName, value);
705 DataManager::SetValue(varName, value*multiply_by);
706 return 0;
707 }
708 if (arg.find("/") != string::npos)
709 {
710 string varName = arg.substr(0, arg.find('/'));
711 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
712 int divide_by = atoi(divide_by_str.c_str());
713 int value;
714
Matt Mowera8a89d12016-12-30 18:10:37 -0600715 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100716 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100718 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 }
720 return 0;
721 }
that3f7b1ac2014-12-30 11:30:13 +0100722 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
723 return -1;
724}
Dees_Troy51a0e822012-09-05 15:24:24 -0400725
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500726int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100727{
728 string SelectedZone;
729 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
730 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
731 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
732
733 int dst;
734 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
735
736 string offset;
737 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
738
739 string NewTimeZone = Zone;
740 if (offset != "0")
741 NewTimeZone += ":" + offset;
742
743 if (dst != 0)
744 NewTimeZone += DSTZone;
745
746 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
747 DataManager::update_tz_environment_variables();
748 return 0;
749}
750
751int GUIAction::overlay(std::string arg)
752{
753 return gui_changeOverlay(arg);
754}
755
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500756int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100757{
758 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500759 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400760 return 0;
761 }
that3f7b1ac2014-12-30 11:30:13 +0100762 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
763 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
764 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100766 }
767 return 0;
768}
769
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500770int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100771{
772 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500773 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100775 } else {
776 zip_queue_index--;
777 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 }
that3f7b1ac2014-12-30 11:30:13 +0100779 return 0;
780}
Dees_Troy51a0e822012-09-05 15:24:24 -0400781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500782int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100783{
784 zip_queue_index = 0;
785 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
786 return 0;
787}
Dees_Troy51a0e822012-09-05 15:24:24 -0400788
that3f7b1ac2014-12-30 11:30:13 +0100789int GUIAction::sleep(std::string arg)
790{
791 operation_start("Sleep");
792 usleep(atoi(arg.c_str()));
793 operation_end(0);
794 return 0;
795}
Dees Troyb21cc642013-09-10 17:36:41 +0000796
Matt Mower9a2a2052016-05-31 21:31:22 -0500797int GUIAction::sleepcounter(std::string arg)
798{
799 operation_start("SleepCounter");
800 // Ensure user notices countdown in case it needs to be cancelled
801 blankTimer.resetTimerAndUnblank();
802 int total = atoi(arg.c_str());
803 for (int t = total; t > 0; t--) {
804 int progress = (int)(((float)(total-t)/(float)total)*100.0);
805 DataManager::SetValue("ui_progress", progress);
806 ::sleep(1);
807 DataManager::SetValue("tw_sleep", t-1);
808 }
809 DataManager::SetValue("ui_progress", 100);
810 operation_end(0);
811 return 0;
812}
813
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500814int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100815{
816 operation_start("AppendDateToBackupName");
817 string Backup_Name;
818 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
819 Backup_Name += TWFunc::Get_Current_Date();
820 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
821 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
822 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500823 PageManager::NotifyKey(KEY_END, true);
824 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100825 operation_end(0);
826 return 0;
827}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500828
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500829int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100830{
831 operation_start("GenerateBackupName");
832 TWFunc::Auto_Generate_Backup_Name();
833 operation_end(0);
834 return 0;
835}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500836
Ethan Yonker483e9f42016-01-11 22:21:18 -0600837int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100838{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600839 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100840 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500841
Ethan Yonker483e9f42016-01-11 22:21:18 -0600842 if (arg.empty())
843 arg = "tw_wipe_list";
844 DataManager::GetValue(arg, List);
845 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
846 if (!List.empty()) {
847 size_t start_pos = 0, end_pos = List.find(";", start_pos);
848 while (end_pos != string::npos && start_pos < List.size()) {
849 part_path = List.substr(start_pos, end_pos - start_pos);
850 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
851 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100852 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 } else {
that3f7b1ac2014-12-30 11:30:13 +0100854 count++;
855 }
856 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600857 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100858 }
859 DataManager::SetValue("tw_check_partition_list", count);
860 } else {
861 DataManager::SetValue("tw_check_partition_list", 0);
862 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600863 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100864}
Dees_Troy51a0e822012-09-05 15:24:24 -0400865
Ethan Yonker483e9f42016-01-11 22:21:18 -0600866int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100867{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600868 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400869
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 if (arg.empty())
871 arg = "tw_wipe_list";
872 DataManager::GetValue(arg, List);
873 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
874 if (!List.empty()) {
875 size_t start_pos = 0, end_pos = List.find(";", start_pos);
876 part_path = List;
877 while (end_pos != string::npos && start_pos < List.size()) {
878 part_path = List.substr(start_pos, end_pos - start_pos);
879 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
880 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100881 // Do nothing
882 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100884 break;
885 }
886 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600887 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100888 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 if (!part_path.empty()) {
890 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100891 if (Part) {
892 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500893
that3f7b1ac2014-12-30 11:30:13 +0100894 DataManager::SetValue("tw_partition_name", Part->Display_Name);
895 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
896 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
897 DataManager::SetValue("tw_partition_size", Part->Size / mb);
898 DataManager::SetValue("tw_partition_used", Part->Used / mb);
899 DataManager::SetValue("tw_partition_free", Part->Free / mb);
900 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
901 DataManager::SetValue("tw_partition_removable", Part->Removable);
902 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
903
904 if (Part->Can_Repair())
905 DataManager::SetValue("tw_partition_can_repair", 1);
906 else
907 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500908 if (Part->Can_Resize())
909 DataManager::SetValue("tw_partition_can_resize", 1);
910 else
911 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600912 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100913 DataManager::SetValue("tw_partition_vfat", 1);
914 else
915 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600916 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100917 DataManager::SetValue("tw_partition_exfat", 1);
918 else
919 DataManager::SetValue("tw_partition_exfat", 0);
920 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
921 DataManager::SetValue("tw_partition_f2fs", 1);
922 else
923 DataManager::SetValue("tw_partition_f2fs", 0);
924 if (TWFunc::Path_Exists("/sbin/mke2fs"))
925 DataManager::SetValue("tw_partition_ext", 1);
926 else
927 DataManager::SetValue("tw_partition_ext", 0);
928 return 0;
929 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600930 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100931 }
932 }
933 }
934 DataManager::SetValue("tw_partition_name", "");
935 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600936 // Set this to 0 to prevent trying to partition this device, just in case
937 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100938 return 0;
939}
940
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500941int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100942{
943 time_t tm;
944 char path[256];
945 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600946 uid_t uid = AID_MEDIA_RW;
947 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100948
949 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600950 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100951 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
952 } else {
953 strcpy(path, "/tmp/");
954 }
955
Matt Mowera8a89d12016-12-30 18:10:37 -0600956 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100957 return 0;
958
959 tm = time(NULL);
960 path_len = strlen(path);
961
962 // Screenshot_2014-01-01-18-21-38.png
963 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
964
965 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600966 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100967 chmod(path, 0666);
968 chown(path, uid, gid);
969
that677b13f2015-12-26 23:57:31 +0100970 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100971
972 // blink to notify that the screenshow was taken
973 gr_color(255, 255, 255, 255);
974 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
975 gr_flip();
976 gui_forceRender();
977 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500978 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100979 }
980 return 0;
981}
982
983int GUIAction::setbrightness(std::string arg)
984{
985 return TWFunc::Set_Brightness(arg);
986}
987
988int GUIAction::fileexists(std::string arg)
989{
990 struct stat st;
991 string newpath = arg + "/.";
992
993 operation_start("FileExists");
994 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
995 operation_end(0);
996 else
997 operation_end(1);
998 return 0;
999}
1000
thatcc8ddca2015-01-03 01:59:36 +01001001void GUIAction::reinject_after_flash()
1002{
1003 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001004 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001005 if (simulate) {
1006 simulate_progress_bar();
1007 } else {
1008 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1009 if (Boot == NULL || Boot->Current_File_System != "emmc")
1010 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1011 else {
1012 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1013 TWFunc::Exec_Cmd(injectcmd);
1014 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001015 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001016 }
1017 }
1018}
1019
that3f7b1ac2014-12-30 11:30:13 +01001020int GUIAction::flash(std::string arg)
1021{
1022 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001023 // We're going to jump to this page first, like a loading page
1024 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001025 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001026 string zip_path = zip_queue[i];
1027 size_t slashpos = zip_path.find_last_of('/');
1028 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001029 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001030 DataManager::SetValue("tw_filename", zip_path);
1031 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001032 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1033
1034 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001035 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001036 TWFunc::SetPerformanceMode(false);
1037 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001038 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001039 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001040 break;
that3f7b1ac2014-12-30 11:30:13 +01001041 }
1042 }
1043 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001044
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001045 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001046 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001047 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001048 }
that3f7b1ac2014-12-30 11:30:13 +01001049
thatcc8ddca2015-01-03 01:59:36 +01001050 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001051 PartitionManager.Update_System_Details();
1052 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001053 // 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 -06001054 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001055 return 0;
1056}
1057
1058int GUIAction::wipe(std::string arg)
1059{
1060 operation_start("Format");
1061 DataManager::SetValue("tw_partition", arg);
1062
1063 int ret_val = false;
1064
1065 if (simulate) {
1066 simulate_progress_bar();
1067 } else {
1068 if (arg == "data")
1069 ret_val = PartitionManager.Factory_Reset();
1070 else if (arg == "battery")
1071 ret_val = PartitionManager.Wipe_Battery_Stats();
1072 else if (arg == "rotate")
1073 ret_val = PartitionManager.Wipe_Rotate_Data();
1074 else if (arg == "dalvik")
1075 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1076 else if (arg == "DATAMEDIA") {
1077 ret_val = PartitionManager.Format_Data();
1078 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001079 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001080
1081 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1082 if (has_datamedia) {
1083 ret_val = PartitionManager.Wipe_Media_From_Data();
1084 } else {
1085 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1086 }
1087 } else if (arg == "EXTERNAL") {
1088 string External_Path;
1089
1090 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1091 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1092 } else if (arg == "ANDROIDSECURE") {
1093 ret_val = PartitionManager.Wipe_Android_Secure();
1094 } else if (arg == "LIST") {
1095 string Wipe_List, wipe_path;
1096 bool skip = false;
1097 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001098
1099 DataManager::GetValue("tw_wipe_list", Wipe_List);
1100 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1101 if (!Wipe_List.empty()) {
1102 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1103 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1104 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1105 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1106 if (wipe_path == "/and-sec") {
1107 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001108 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001109 ret_val = false;
1110 break;
1111 } else {
1112 skip = true;
1113 }
1114 } else if (wipe_path == "DALVIK") {
1115 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001116 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001117 ret_val = false;
1118 break;
1119 } else {
1120 skip = true;
1121 }
1122 } else if (wipe_path == "INTERNAL") {
1123 if (!PartitionManager.Wipe_Media_From_Data()) {
1124 ret_val = false;
1125 break;
1126 } else {
1127 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001128 }
1129 }
that3f7b1ac2014-12-30 11:30:13 +01001130 if (!skip) {
1131 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001132 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001133 ret_val = false;
1134 break;
1135 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1136 arg = wipe_path;
1137 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 } else {
that3f7b1ac2014-12-30 11:30:13 +01001139 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 }
that3f7b1ac2014-12-30 11:30:13 +01001141 start_pos = end_pos + 1;
1142 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001143 }
1144 }
that3f7b1ac2014-12-30 11:30:13 +01001145 } else
1146 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001147#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001148 if (arg == DataManager::GetSettingsStoragePath()) {
1149 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1150 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001151
that3f7b1ac2014-12-30 11:30:13 +01001152 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1153 LOGINFO("Making TWRP folder and saving settings.\n");
1154 Storage_Path += "/TWRP";
1155 mkdir(Storage_Path.c_str(), 0777);
1156 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001157 } else {
that3f7b1ac2014-12-30 11:30:13 +01001158 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1159 }
1160 }
1161#endif
1162 }
1163 PartitionManager.Update_System_Details();
1164 if (ret_val)
1165 ret_val = 0; // 0 is success
1166 else
1167 ret_val = 1; // 1 is failure
1168 operation_end(ret_val);
1169 return 0;
1170}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001171
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001172int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001173{
1174 operation_start("Refreshing Sizes");
1175 if (simulate) {
1176 simulate_progress_bar();
1177 } else
1178 PartitionManager.Update_System_Details();
1179 operation_end(0);
1180 return 0;
1181}
1182
1183int GUIAction::nandroid(std::string arg)
1184{
that3f7b1ac2014-12-30 11:30:13 +01001185 if (simulate) {
that73a52952015-01-28 23:07:34 +01001186 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001187 DataManager::SetValue("tw_partition", "Simulation");
1188 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001189 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001190 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 operation_start("Nandroid");
1192 int ret = 0;
1193
that3f7b1ac2014-12-30 11:30:13 +01001194 if (arg == "backup") {
1195 string Backup_Name;
1196 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001197 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1198 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 -05001199 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001200 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1201 if (!PartitionManager.stop_backup.get_value()) {
1202 if (ret == false)
1203 ret = 1; // 1 for failure
1204 else
1205 ret = 0; // 0 for success
1206 DataManager::SetValue("tw_cancel_backup", 0);
1207 } else {
1208 DataManager::SetValue("tw_cancel_backup", 1);
1209 gui_msg("backup_cancel=Backup Cancelled");
1210 ret = 0;
1211 }
bigbiffce8f83c2015-12-12 18:30:21 -05001212 } else {
that3f7b1ac2014-12-30 11:30:13 +01001213 operation_end(1);
1214 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001215 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001216 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001217 } else if (arg == "restore") {
1218 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001219 int gui_adb_backup;
1220
that3f7b1ac2014-12-30 11:30:13 +01001221 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001222 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1223 if (gui_adb_backup) {
1224 DataManager::SetValue("tw_operation_state", 1);
1225 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1226 ret = 0; // success
1227 else
1228 ret = 1; // failure
1229 DataManager::SetValue("tw_enable_adb_backup", 0);
1230 ret = 0; // assume success???
1231 } else {
1232 if (PartitionManager.Run_Restore(Restore_Name))
1233 ret = 0; // success
1234 else
1235 ret = 1; // failure
1236 }
that3f7b1ac2014-12-30 11:30:13 +01001237 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001238 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001239 return -1;
1240 }
that73a52952015-01-28 23:07:34 +01001241 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001242 return ret;
1243 }
1244 return 0;
1245}
1246
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001247int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001248 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001249 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001250 }
1251 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001252 int op_status = PartitionManager.Cancel_Backup();
1253 if (op_status != 0)
1254 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001255 }
1256
1257 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001258}
Dees_Troy51a0e822012-09-05 15:24:24 -04001259
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001260int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001261{
that73a52952015-01-28 23:07:34 +01001262 int op_status = 0;
1263
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001264 operation_start("Fix Contexts");
1265 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001266 if (simulate) {
1267 simulate_progress_bar();
1268 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001269 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001270 if (op_status != 0)
1271 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001272 }
that73a52952015-01-28 23:07:34 +01001273 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001274 return 0;
1275}
1276
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001277int GUIAction::fixpermissions(std::string arg)
1278{
1279 return fixcontexts(arg);
1280}
1281
that3f7b1ac2014-12-30 11:30:13 +01001282int GUIAction::dd(std::string arg)
1283{
1284 operation_start("imaging");
1285
1286 if (simulate) {
1287 simulate_progress_bar();
1288 } else {
1289 string cmd = "dd " + arg;
1290 TWFunc::Exec_Cmd(cmd);
1291 }
1292 operation_end(0);
1293 return 0;
1294}
1295
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001296int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001297{
1298 operation_start("Partition SD Card");
1299 int ret_val = 0;
1300
1301 if (simulate) {
1302 simulate_progress_bar();
1303 } else {
1304 int allow_partition;
1305 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1306 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001307 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001308 } else {
1309 if (!PartitionManager.Partition_SDCard())
1310 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001311 }
that3f7b1ac2014-12-30 11:30:13 +01001312 }
1313 operation_end(ret_val);
1314 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001315
that3f7b1ac2014-12-30 11:30:13 +01001316}
Dees_Troy51a0e822012-09-05 15:24:24 -04001317
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001318int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001319{
1320 operation_start("Install HTC Dumlock");
1321 if (simulate) {
1322 simulate_progress_bar();
1323 } else
1324 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001325
that3f7b1ac2014-12-30 11:30:13 +01001326 operation_end(0);
1327 return 0;
1328}
Dees_Troy51a0e822012-09-05 15:24:24 -04001329
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001330int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001331{
1332 operation_start("HTC Dumlock Restore Boot");
1333 if (simulate) {
1334 simulate_progress_bar();
1335 } else
1336 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001337
that3f7b1ac2014-12-30 11:30:13 +01001338 operation_end(0);
1339 return 0;
1340}
Dees_Troy51a0e822012-09-05 15:24:24 -04001341
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001342int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001343{
1344 operation_start("HTC Dumlock Reflash Recovery");
1345 if (simulate) {
1346 simulate_progress_bar();
1347 } else
1348 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001349
that3f7b1ac2014-12-30 11:30:13 +01001350 operation_end(0);
1351 return 0;
1352}
Dees_Troy51a0e822012-09-05 15:24:24 -04001353
that3f7b1ac2014-12-30 11:30:13 +01001354int GUIAction::cmd(std::string arg)
1355{
1356 int op_status = 0;
1357
1358 operation_start("Command");
1359 LOGINFO("Running command: '%s'\n", arg.c_str());
1360 if (simulate) {
1361 simulate_progress_bar();
1362 } else {
1363 op_status = TWFunc::Exec_Cmd(arg);
1364 if (op_status != 0)
1365 op_status = 1;
1366 }
1367
1368 operation_end(op_status);
1369 return 0;
1370}
1371
1372int GUIAction::terminalcommand(std::string arg)
1373{
1374 int op_status = 0;
1375 string cmdpath, command;
1376
1377 DataManager::GetValue("tw_terminal_location", cmdpath);
1378 operation_start("CommandOutput");
1379 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1380 if (simulate) {
1381 simulate_progress_bar();
1382 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001383 } else if (arg == "exit") {
1384 LOGINFO("Exiting terminal\n");
1385 operation_end(op_status);
1386 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001387 } else {
1388 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1389 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001390 DataManager::SetValue("tw_terminal_state", 1);
1391 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001392 FILE* fp;
1393 char line[512];
1394
1395 fp = popen(command.c_str(), "r");
1396 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001397 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001398 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001399 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001400 struct timeval timeout;
1401 fd_set fdset;
1402
Matt Mowera8a89d12016-12-30 18:10:37 -06001403 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001404 {
1405 FD_ZERO(&fdset);
1406 FD_SET(fd, &fdset);
1407 timeout.tv_sec = 0;
1408 timeout.tv_usec = 400000;
1409 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1410 if (has_data == 0) {
1411 // Timeout reached
1412 DataManager::GetValue("tw_terminal_state", check);
1413 if (check == 0) {
1414 keep_going = 0;
1415 }
1416 } else if (has_data < 0) {
1417 // End of execution
1418 keep_going = 0;
1419 } else {
1420 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001421 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001422 gui_print("%s", line); // Display output
1423 else
1424 keep_going = 0; // Done executing
1425 }
1426 }
1427 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001428 }
thatc6085482015-01-09 22:12:43 +01001429 DataManager::SetValue("tw_operation_status", 0);
1430 DataManager::SetValue("tw_operation_state", 1);
1431 DataManager::SetValue("tw_terminal_state", 0);
1432 DataManager::SetValue("tw_background_thread_running", 0);
1433 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001434 }
1435 return 0;
1436}
1437
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001438int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001439{
that3f7b1ac2014-12-30 11:30:13 +01001440 LOGINFO("Sending kill command...\n");
1441 operation_start("KillCommand");
1442 DataManager::SetValue("tw_operation_status", 0);
1443 DataManager::SetValue("tw_operation_state", 1);
1444 DataManager::SetValue("tw_terminal_state", 0);
1445 DataManager::SetValue("tw_background_thread_running", 0);
1446 DataManager::SetValue(TW_ACTION_BUSY, 0);
1447 return 0;
1448}
1449
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001450int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001451{
1452 int op_status = 0;
1453 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001454 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001455 if (simulate) {
1456 simulate_progress_bar();
1457 } else {
1458 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001459 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001460 }
1461
1462 operation_end(op_status);
1463 return 0;
1464}
1465
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001466int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001467{
1468 int op_status = 0;
1469
1470 operation_start("CheckBackupName");
1471 if (simulate) {
1472 simulate_progress_bar();
1473 } else {
1474 op_status = PartitionManager.Check_Backup_Name(true);
1475 if (op_status != 0)
1476 op_status = 1;
1477 }
1478
1479 operation_end(op_status);
1480 return 0;
1481}
1482
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001483int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001484{
1485 int op_status = 0;
1486
1487 operation_start("Decrypt");
1488 if (simulate) {
1489 simulate_progress_bar();
1490 } else {
1491 string Password;
1492 DataManager::GetValue("tw_crypto_password", Password);
1493 op_status = PartitionManager.Decrypt_Device(Password);
1494 if (op_status != 0)
1495 op_status = 1;
1496 else {
that3f7b1ac2014-12-30 11:30:13 +01001497
1498 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1499
Ethan Yonkercf50da52015-01-12 21:59:07 -06001500 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001501
Ethan Yonkercf50da52015-01-12 21:59:07 -06001502 // Check for a custom theme and load it if exists
1503 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1504 if (has_datamedia != 0) {
1505 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001506 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001507 } else {
1508 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001509 }
1510 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001511 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001512 }
1513 }
1514
1515 operation_end(op_status);
1516 return 0;
1517}
1518
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001519int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001520{
1521 operation_start("Sideload");
1522 if (simulate) {
1523 simulate_progress_bar();
1524 operation_end(0);
1525 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001527 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1528
1529 // wait for the adb connection
1530 int ret = apply_from_adb("/", &sideload_child_pid);
1531 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1532
1533 if (ret != 0) {
1534 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001535 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001536 ret = 1; // failure
1537 } else {
1538 int wipe_cache = 0;
1539 int wipe_dalvik = 0;
1540 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1541
1542 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1543 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1544 PartitionManager.Wipe_By_Path("/cache");
1545 if (wipe_dalvik)
1546 PartitionManager.Wipe_Dalvik_Cache();
1547 } else {
1548 ret = 1; // failure
1549 }
thatcc8ddca2015-01-03 01:59:36 +01001550 }
thatc6085482015-01-09 22:12:43 +01001551 if (sideload_child_pid) {
1552 LOGINFO("Signaling child sideload process to exit.\n");
1553 struct stat st;
1554 // Calling stat() on this magic filename signals the minadbd
1555 // subprocess to shut down.
1556 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1557 int status;
1558 LOGINFO("Waiting for child sideload process to exit.\n");
1559 waitpid(sideload_child_pid, &status, 0);
1560 }
Dees Troy28a85d22015-04-28 02:03:16 +00001561 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001562 TWFunc::Toggle_MTP(mtp_was_enabled);
1563 reinject_after_flash();
1564 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001565 }
that3f7b1ac2014-12-30 11:30:13 +01001566 return 0;
1567}
1568
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001569int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001570{
that3f7b1ac2014-12-30 11:30:13 +01001571 struct stat st;
1572 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001573 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001574 LOGINFO("Signaling child sideload process to exit.\n");
1575 // Calling stat() on this magic filename signals the minadbd
1576 // subprocess to shut down.
1577 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1578 if (!sideload_child_pid) {
1579 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001580 return 0;
1581 }
thatcc8ddca2015-01-03 01:59:36 +01001582 ::sleep(1);
1583 LOGINFO("Killing child sideload process.\n");
1584 kill(sideload_child_pid, SIGTERM);
1585 int status;
1586 LOGINFO("Waiting for child sideload process to exit.\n");
1587 waitpid(sideload_child_pid, &status, 0);
1588 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001589 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1590 return 0;
1591}
1592
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001593int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001594{
1595 operation_start("OpenRecoveryScript");
1596 if (simulate) {
1597 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001598 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001599 } else {
that10ae24f2015-12-26 20:53:51 +01001600 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001601 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001602 }
1603 return 0;
1604}
1605
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001606int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001607{
1608 int op_status = 0;
1609
1610 operation_start("Install SuperSU");
1611 if (simulate) {
1612 simulate_progress_bar();
1613 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001614 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001615 }
1616
1617 operation_end(op_status);
1618 return 0;
1619}
1620
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001621int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001622{
1623 int op_status = 0;
1624
1625 operation_start("Fixing Superuser Permissions");
1626 if (simulate) {
1627 simulate_progress_bar();
1628 } else {
1629 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1630 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1631 }
1632
1633 operation_end(op_status);
1634 return 0;
1635}
1636
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001637int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001638{
1639 int op_status = 0;
1640
1641 operation_start("Try Restore Decrypt");
1642 if (simulate) {
1643 simulate_progress_bar();
1644 } else {
1645 string Restore_Path, Filename, Password;
1646 DataManager::GetValue("tw_restore", Restore_Path);
1647 Restore_Path += "/";
1648 DataManager::GetValue("tw_restore_password", Password);
1649 TWFunc::SetPerformanceMode(true);
1650 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1651 op_status = 0; // success
1652 else
1653 op_status = 1; // fail
1654 TWFunc::SetPerformanceMode(false);
1655 }
1656
1657 operation_end(op_status);
1658 return 0;
1659}
1660
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001661int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001662{
1663 int op_status = 0;
1664
1665 operation_start("Repair Partition");
1666 if (simulate) {
1667 simulate_progress_bar();
1668 } else {
1669 string part_path;
1670 DataManager::GetValue("tw_partition_mount_point", part_path);
1671 if (PartitionManager.Repair_By_Path(part_path, true)) {
1672 op_status = 0; // success
1673 } else {
that3f7b1ac2014-12-30 11:30:13 +01001674 op_status = 1; // fail
1675 }
1676 }
1677
1678 operation_end(op_status);
1679 return 0;
1680}
1681
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001682int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001683{
1684 int op_status = 0;
1685
1686 operation_start("Resize Partition");
1687 if (simulate) {
1688 simulate_progress_bar();
1689 } else {
1690 string part_path;
1691 DataManager::GetValue("tw_partition_mount_point", part_path);
1692 if (PartitionManager.Resize_By_Path(part_path, true)) {
1693 op_status = 0; // success
1694 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001695 op_status = 1; // fail
1696 }
1697 }
1698
1699 operation_end(op_status);
1700 return 0;
1701}
1702
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001703int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001704{
1705 int op_status = 0;
1706
1707 operation_start("Change File System");
1708 if (simulate) {
1709 simulate_progress_bar();
1710 } else {
1711 string part_path, file_system;
1712 DataManager::GetValue("tw_partition_mount_point", part_path);
1713 DataManager::GetValue("tw_action_new_file_system", file_system);
1714 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1715 op_status = 0; // success
1716 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001718 op_status = 1; // fail
1719 }
1720 }
1721 PartitionManager.Update_System_Details();
1722 operation_end(op_status);
1723 return 0;
1724}
1725
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001726int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001727{
1728 int op_status = 0;
1729
1730 operation_start("Start MTP");
1731 if (PartitionManager.Enable_MTP())
1732 op_status = 0; // success
1733 else
1734 op_status = 1; // fail
1735
1736 operation_end(op_status);
1737 return 0;
1738}
1739
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001740int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001741{
1742 int op_status = 0;
1743
1744 operation_start("Stop MTP");
1745 if (PartitionManager.Disable_MTP())
1746 op_status = 0; // success
1747 else
1748 op_status = 1; // fail
1749
1750 operation_end(op_status);
1751 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001752}
1753
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001754int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001755{
1756 int op_status = 0;
1757
1758 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001759 string path, filename;
1760 DataManager::GetValue("tw_zip_location", path);
1761 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001762 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001763 op_status = 0; // success
1764 else
1765 op_status = 1; // fail
1766
1767 operation_end(op_status);
1768 return 0;
1769}
1770
that10ae24f2015-12-26 20:53:51 +01001771int GUIAction::twcmd(std::string arg)
1772{
1773 operation_start("TWRP CLI Command");
1774 if (simulate)
1775 simulate_progress_bar();
1776 else
1777 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1778 operation_end(0);
1779 return 0;
1780}
1781
Dees_Troy51a0e822012-09-05 15:24:24 -04001782int GUIAction::getKeyByName(std::string key)
1783{
that8834a0f2016-01-05 23:29:30 +01001784 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001785 else if (key == "menu") return KEY_MENU;
1786 else if (key == "back") return KEY_BACK;
1787 else if (key == "search") return KEY_SEARCH;
1788 else if (key == "voldown") return KEY_VOLUMEDOWN;
1789 else if (key == "volup") return KEY_VOLUMEUP;
1790 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001791 int ret_val;
1792 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1793 if (!ret_val)
1794 return KEY_POWER;
1795 else
1796 return ret_val;
1797 }
1798
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001799 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001800}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001801
1802int GUIAction::checkpartitionlifetimewrites(std::string arg)
1803{
1804 int op_status = 0;
1805 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1806
1807 operation_start("Check Partition Lifetime Writes");
1808 if (sys) {
1809 if (sys->Check_Lifetime_Writes() != 0)
1810 DataManager::SetValue("tw_lifetime_writes", 1);
1811 else
1812 DataManager::SetValue("tw_lifetime_writes", 0);
1813 op_status = 0; // success
1814 } else {
1815 DataManager::SetValue("tw_lifetime_writes", 1);
1816 op_status = 1; // fail
1817 }
1818
1819 operation_end(op_status);
1820 return 0;
1821}
1822
1823int GUIAction::mountsystemtoggle(std::string arg)
1824{
1825 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001826 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001827 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001828
1829 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001830 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001831 op_status = 1; // fail
1832 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001833 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001834 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001835 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001836 DataManager::SetValue("tw_mount_system_ro", 0);
1837 Part->Change_Mount_Read_Only(false);
1838 } else {
1839 DataManager::SetValue("tw_mount_system_ro", 1);
1840 Part->Change_Mount_Read_Only(true);
1841 }
1842 if (remount_system) {
1843 Part->Mount(true);
1844 }
1845 op_status = 0; // success
1846 } else {
1847 op_status = 1; // fail
1848 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001849 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1850 if (Part) {
1851 if (arg == "0") {
1852 Part->Change_Mount_Read_Only(false);
1853 } else {
1854 Part->Change_Mount_Read_Only(true);
1855 }
1856 if (remount_vendor) {
1857 Part->Mount(true);
1858 }
1859 op_status = 0; // success
1860 } else {
1861 op_status = 1; // fail
1862 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001863 }
1864
1865 operation_end(op_status);
1866 return 0;
1867}
Ethan Yonker74db1572015-10-28 12:44:49 -05001868
1869int GUIAction::setlanguage(std::string arg __unused)
1870{
1871 int op_status = 0;
1872
1873 operation_start("Set Language");
1874 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1875 PageManager::RequestReload();
1876 op_status = 0; // success
1877
1878 operation_end(op_status);
1879 return 0;
1880}
Ethan Yonker1b190162016-12-05 15:25:19 -06001881
Matt Mower9472ba12016-01-20 18:12:47 -06001882int GUIAction::togglebacklight(std::string arg __unused)
1883{
1884 blankTimer.toggleBlank();
1885 return 0;
1886}
1887
Ethan Yonker1b190162016-12-05 15:25:19 -06001888int GUIAction::setbootslot(std::string arg)
1889{
1890 operation_start("Set Boot Slot");
1891 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001892 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001893 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001894 simulate_progress_bar();
1895 operation_end(0);
1896 return 0;
1897}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001898
1899int GUIAction::checkforapp(std::string arg __unused)
1900{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001901 operation_start("Check for TWRP App");
1902 if (!simulate)
1903 {
1904 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1905 int sdkver = 0;
1906 if (!sdkverstr.empty()) {
1907 sdkver = atoi(sdkverstr.c_str());
1908 }
1909 if (sdkver <= 13) {
1910 if (sdkver == 0)
1911 LOGINFO("Unable to read sdk version from build prop\n");
1912 else
1913 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001914 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 -06001915 goto exit;
1916 }
Captain Throwback9d6feb52018-07-27 10:05:24 -04001917 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
1918 string base_path = PartitionManager.Get_Android_Root_Path();
1919 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001920 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1921 string install_path = base_path + "/priv-app";
1922 if (!TWFunc::Path_Exists(install_path))
1923 install_path = base_path + "/app";
1924 install_path += "/twrpapp";
1925 if (TWFunc::Path_Exists(install_path)) {
1926 LOGINFO("App found at '%s'\n", install_path.c_str());
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001927 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 -06001928 goto exit;
1929 }
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001930 }
1931 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001932 const char parent_path[] = "/data/app";
1933 const char app_prefix[] = "me.twrp.twrpapp-";
1934 DIR *d = opendir(parent_path);
1935 if (d) {
1936 struct dirent *p;
1937 while ((p = readdir(d))) {
1938 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1939 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001940 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001941 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001942 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 -06001943 goto exit;
1944 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001945 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001946 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001947 } else {
1948 LOGINFO("Data partition cannot be mounted during app check\n");
1949 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 -06001950 }
1951 } else
1952 simulate_progress_bar();
1953 LOGINFO("App not installed\n");
1954 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1955exit:
1956 operation_end(0);
1957 return 0;
1958}
1959
1960int GUIAction::installapp(std::string arg __unused)
1961{
1962 int op_status = 1;
1963 operation_start("Install TWRP App");
1964 if (!simulate)
1965 {
1966 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1967 if (PartitionManager.Mount_By_Path("/data", true)) {
1968 string install_path = "/data/app";
1969 string context = "u:object_r:apk_data_file:s0";
1970 if (!TWFunc::Path_Exists(install_path)) {
1971 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1972 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1973 goto exit;
1974 }
1975 if (chown(install_path.c_str(), 1000, 1000)) {
1976 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1977 goto exit;
1978 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001979 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001980 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1981 goto exit;
1982 }
1983 }
1984 install_path += "/me.twrp.twrpapp-1";
1985 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1986 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
1989 if (chown(install_path.c_str(), 1000, 1000)) {
1990 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001993 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001994 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1995 goto exit;
1996 }
1997 install_path += "/base.apk";
1998 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1999 LOGERR("Error copying apk file\n");
2000 goto exit;
2001 }
2002 if (chown(install_path.c_str(), 1000, 1000)) {
2003 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002006 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002007 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2008 goto exit;
2009 }
2010 sync();
2011 sync();
2012 }
2013 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002014 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2015 string base_path = PartitionManager.Get_Android_Root_Path();
2016 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002017 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2018 string install_path = base_path + "/priv-app";
2019 string context = "u:object_r:system_file:s0";
2020 if (!TWFunc::Path_Exists(install_path))
2021 install_path = base_path + "/app";
2022 if (TWFunc::Path_Exists(install_path)) {
2023 install_path += "/twrpapp";
2024 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2025 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002026 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002027 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2028 goto exit;
2029 }
2030 install_path += "/me.twrp.twrpapp.apk";
2031 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2032 LOGERR("Error copying apk file\n");
2033 goto exit;
2034 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002035 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002036 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2037 goto exit;
2038 }
2039 sync();
2040 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002041 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002042 op_status = 0;
2043 } else {
2044 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2045 }
2046 }
2047 }
2048 }
2049 } else
2050 simulate_progress_bar();
2051exit:
2052 operation_end(0);
2053 return 0;
2054}