blob: fb05d77b41f911d756f76a2893695c1e497b7df0 [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);
519 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600520 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100521 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522}
523
that3f7b1ac2014-12-30 11:30:13 +0100524int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400525{
that3f7b1ac2014-12-30 11:30:13 +0100526 sync();
527 DataManager::SetValue("tw_gui_done", 1);
528 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
that3f7b1ac2014-12-30 11:30:13 +0100530 return 0;
531}
Dees_Troy51a0e822012-09-05 15:24:24 -0400532
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500533int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100534{
that3f7b1ac2014-12-30 11:30:13 +0100535 gui_changePage("main");
536 return 0;
537}
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
that3f7b1ac2014-12-30 11:30:13 +0100539int GUIAction::key(std::string arg)
540{
541 const int key = getKeyByName(arg);
542 PageManager::NotifyKey(key, true);
543 PageManager::NotifyKey(key, false);
544 return 0;
545}
546
547int GUIAction::page(std::string arg)
548{
LuK133762326f42015-09-30 19:57:46 +0200549 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100550 std::string page_name = gui_parse_text(arg);
551 return gui_changePage(page_name);
552}
553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500556 PageManager::RequestReload();
557 // The actual reload is handled in pages.cpp in PageManager::RunReload()
558 // The reload will occur on the next Update or Render call and will
559 // be performed in the rendoer thread instead of the action thread
560 // to prevent crashing which could occur when we start deleting
561 // GUI resources in the action thread while we attempt to render
562 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100563 return 0;
564}
Dees_Troy51a0e822012-09-05 15:24:24 -0400565
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500566int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100567{
568 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400569
that3f7b1ac2014-12-30 11:30:13 +0100570 DataManager::GetValue("tw_restore", Restore_Name);
571 PartitionManager.Set_Restore_Files(Restore_Name);
572 return 0;
573}
574
575int GUIAction::set(std::string arg)
576{
577 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 {
that3f7b1ac2014-12-30 11:30:13 +0100579 string varName = arg.substr(0, arg.find('='));
580 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
that3f7b1ac2014-12-30 11:30:13 +0100582 DataManager::GetValue(value, value);
583 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200584 }
that3f7b1ac2014-12-30 11:30:13 +0100585 else
586 DataManager::SetValue(arg, "1");
587 return 0;
588}
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
that3f7b1ac2014-12-30 11:30:13 +0100590int GUIAction::clear(std::string arg)
591{
592 DataManager::SetValue(arg, "0");
593 return 0;
594}
Dees_Troy51a0e822012-09-05 15:24:24 -0400595
that3f7b1ac2014-12-30 11:30:13 +0100596int GUIAction::mount(std::string arg)
597{
598 if (arg == "usb") {
599 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100601 PartitionManager.usb_storage_enable();
602 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500603 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100604 } else if (!simulate) {
605 PartitionManager.Mount_By_Path(arg, true);
606 PartitionManager.Add_MTP_Storage(arg);
607 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500608 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100609 return 0;
610}
611
612int GUIAction::unmount(std::string arg)
613{
614 if (arg == "usb") {
615 if (!simulate)
616 PartitionManager.usb_storage_disable();
617 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500618 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100619 DataManager::SetValue(TW_ACTION_BUSY, 0);
620 } else if (!simulate) {
621 PartitionManager.UnMount_By_Path(arg, true);
622 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500623 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100624 return 0;
625}
626
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500627int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100628{
629 operation_start("Restore Defaults");
630 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500631 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100632 else {
633 DataManager::ResetDefaults();
634 PartitionManager.Update_System_Details();
635 PartitionManager.Mount_Current_Storage(true);
636 }
637 operation_end(0);
638 return 0;
639}
640
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500641int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100642{
643 operation_start("Copy Log");
644 if (!simulate)
645 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400646 string dst, curr_storage;
647 int copy_kernel_log = 0;
648
649 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100650 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400651 curr_storage = DataManager::GetCurrentStoragePath();
652 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100653 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
654 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400655 if (copy_kernel_log)
656 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100657 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400658 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100659 } else
660 simulate_progress_bar();
661 operation_end(0);
662 return 0;
663}
664
665
666int GUIAction::compute(std::string arg)
667{
668 if (arg.find("+") != string::npos)
669 {
670 string varName = arg.substr(0, arg.find('+'));
671 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
672 int amount_to_add = atoi(string_to_add.c_str());
673 int value;
674
675 DataManager::GetValue(varName, value);
676 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 return 0;
678 }
that3f7b1ac2014-12-30 11:30:13 +0100679 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400680 {
that3f7b1ac2014-12-30 11:30:13 +0100681 string varName = arg.substr(0, arg.find('-'));
682 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
683 int amount_to_subtract = atoi(string_to_subtract.c_str());
684 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400685
that3f7b1ac2014-12-30 11:30:13 +0100686 DataManager::GetValue(varName, value);
687 value -= amount_to_subtract;
688 if (value <= 0)
689 value = 0;
690 DataManager::SetValue(varName, value);
691 return 0;
692 }
693 if (arg.find("*") != string::npos)
694 {
695 string varName = arg.substr(0, arg.find('*'));
696 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
697 int multiply_by = atoi(multiply_by_str.c_str());
698 int value;
699
700 DataManager::GetValue(varName, value);
701 DataManager::SetValue(varName, value*multiply_by);
702 return 0;
703 }
704 if (arg.find("/") != string::npos)
705 {
706 string varName = arg.substr(0, arg.find('/'));
707 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
708 int divide_by = atoi(divide_by_str.c_str());
709 int value;
710
Matt Mowera8a89d12016-12-30 18:10:37 -0600711 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100712 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100714 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 }
716 return 0;
717 }
that3f7b1ac2014-12-30 11:30:13 +0100718 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
719 return -1;
720}
Dees_Troy51a0e822012-09-05 15:24:24 -0400721
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500722int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100723{
724 string SelectedZone;
725 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
726 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
727 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
728
729 int dst;
730 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
731
732 string offset;
733 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
734
735 string NewTimeZone = Zone;
736 if (offset != "0")
737 NewTimeZone += ":" + offset;
738
739 if (dst != 0)
740 NewTimeZone += DSTZone;
741
742 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
743 DataManager::update_tz_environment_variables();
744 return 0;
745}
746
747int GUIAction::overlay(std::string arg)
748{
749 return gui_changeOverlay(arg);
750}
751
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500752int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100753{
754 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500755 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400756 return 0;
757 }
that3f7b1ac2014-12-30 11:30:13 +0100758 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
759 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
760 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400761 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100762 }
763 return 0;
764}
765
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500766int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100767{
768 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500769 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100771 } else {
772 zip_queue_index--;
773 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 }
that3f7b1ac2014-12-30 11:30:13 +0100775 return 0;
776}
Dees_Troy51a0e822012-09-05 15:24:24 -0400777
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500778int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100779{
780 zip_queue_index = 0;
781 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
782 return 0;
783}
Dees_Troy51a0e822012-09-05 15:24:24 -0400784
that3f7b1ac2014-12-30 11:30:13 +0100785int GUIAction::sleep(std::string arg)
786{
787 operation_start("Sleep");
788 usleep(atoi(arg.c_str()));
789 operation_end(0);
790 return 0;
791}
Dees Troyb21cc642013-09-10 17:36:41 +0000792
Matt Mower9a2a2052016-05-31 21:31:22 -0500793int GUIAction::sleepcounter(std::string arg)
794{
795 operation_start("SleepCounter");
796 // Ensure user notices countdown in case it needs to be cancelled
797 blankTimer.resetTimerAndUnblank();
798 int total = atoi(arg.c_str());
799 for (int t = total; t > 0; t--) {
800 int progress = (int)(((float)(total-t)/(float)total)*100.0);
801 DataManager::SetValue("ui_progress", progress);
802 ::sleep(1);
803 DataManager::SetValue("tw_sleep", t-1);
804 }
805 DataManager::SetValue("ui_progress", 100);
806 operation_end(0);
807 return 0;
808}
809
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500810int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100811{
812 operation_start("AppendDateToBackupName");
813 string Backup_Name;
814 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
815 Backup_Name += TWFunc::Get_Current_Date();
816 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
817 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
818 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500819 PageManager::NotifyKey(KEY_END, true);
820 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100821 operation_end(0);
822 return 0;
823}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500824
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500825int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100826{
827 operation_start("GenerateBackupName");
828 TWFunc::Auto_Generate_Backup_Name();
829 operation_end(0);
830 return 0;
831}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500832
Ethan Yonker483e9f42016-01-11 22:21:18 -0600833int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100834{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600835 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100836 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500837
Ethan Yonker483e9f42016-01-11 22:21:18 -0600838 if (arg.empty())
839 arg = "tw_wipe_list";
840 DataManager::GetValue(arg, List);
841 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
842 if (!List.empty()) {
843 size_t start_pos = 0, end_pos = List.find(";", start_pos);
844 while (end_pos != string::npos && start_pos < List.size()) {
845 part_path = List.substr(start_pos, end_pos - start_pos);
846 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
847 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100848 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400849 } else {
that3f7b1ac2014-12-30 11:30:13 +0100850 count++;
851 }
852 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600853 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100854 }
855 DataManager::SetValue("tw_check_partition_list", count);
856 } else {
857 DataManager::SetValue("tw_check_partition_list", 0);
858 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600859 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100860}
Dees_Troy51a0e822012-09-05 15:24:24 -0400861
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100863{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600864 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400865
Ethan Yonker483e9f42016-01-11 22:21:18 -0600866 if (arg.empty())
867 arg = "tw_wipe_list";
868 DataManager::GetValue(arg, List);
869 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
870 if (!List.empty()) {
871 size_t start_pos = 0, end_pos = List.find(";", start_pos);
872 part_path = List;
873 while (end_pos != string::npos && start_pos < List.size()) {
874 part_path = List.substr(start_pos, end_pos - start_pos);
875 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
876 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100877 // Do nothing
878 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600879 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100880 break;
881 }
882 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100884 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600885 if (!part_path.empty()) {
886 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100887 if (Part) {
888 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500889
that3f7b1ac2014-12-30 11:30:13 +0100890 DataManager::SetValue("tw_partition_name", Part->Display_Name);
891 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
892 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
893 DataManager::SetValue("tw_partition_size", Part->Size / mb);
894 DataManager::SetValue("tw_partition_used", Part->Used / mb);
895 DataManager::SetValue("tw_partition_free", Part->Free / mb);
896 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
897 DataManager::SetValue("tw_partition_removable", Part->Removable);
898 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
899
900 if (Part->Can_Repair())
901 DataManager::SetValue("tw_partition_can_repair", 1);
902 else
903 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500904 if (Part->Can_Resize())
905 DataManager::SetValue("tw_partition_can_resize", 1);
906 else
907 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600908 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100909 DataManager::SetValue("tw_partition_vfat", 1);
910 else
911 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600912 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100913 DataManager::SetValue("tw_partition_exfat", 1);
914 else
915 DataManager::SetValue("tw_partition_exfat", 0);
916 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
917 DataManager::SetValue("tw_partition_f2fs", 1);
918 else
919 DataManager::SetValue("tw_partition_f2fs", 0);
920 if (TWFunc::Path_Exists("/sbin/mke2fs"))
921 DataManager::SetValue("tw_partition_ext", 1);
922 else
923 DataManager::SetValue("tw_partition_ext", 0);
924 return 0;
925 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600926 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100927 }
928 }
929 }
930 DataManager::SetValue("tw_partition_name", "");
931 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600932 // Set this to 0 to prevent trying to partition this device, just in case
933 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100934 return 0;
935}
936
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500937int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100938{
939 time_t tm;
940 char path[256];
941 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600942 uid_t uid = AID_MEDIA_RW;
943 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100944
945 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600946 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100947 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
948 } else {
949 strcpy(path, "/tmp/");
950 }
951
Matt Mowera8a89d12016-12-30 18:10:37 -0600952 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100953 return 0;
954
955 tm = time(NULL);
956 path_len = strlen(path);
957
958 // Screenshot_2014-01-01-18-21-38.png
959 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
960
961 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600962 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100963 chmod(path, 0666);
964 chown(path, uid, gid);
965
that677b13f2015-12-26 23:57:31 +0100966 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100967
968 // blink to notify that the screenshow was taken
969 gr_color(255, 255, 255, 255);
970 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
971 gr_flip();
972 gui_forceRender();
973 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500974 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100975 }
976 return 0;
977}
978
979int GUIAction::setbrightness(std::string arg)
980{
981 return TWFunc::Set_Brightness(arg);
982}
983
984int GUIAction::fileexists(std::string arg)
985{
986 struct stat st;
987 string newpath = arg + "/.";
988
989 operation_start("FileExists");
990 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
991 operation_end(0);
992 else
993 operation_end(1);
994 return 0;
995}
996
thatcc8ddca2015-01-03 01:59:36 +0100997void GUIAction::reinject_after_flash()
998{
999 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001000 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001001 if (simulate) {
1002 simulate_progress_bar();
1003 } else {
1004 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1005 if (Boot == NULL || Boot->Current_File_System != "emmc")
1006 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1007 else {
1008 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1009 TWFunc::Exec_Cmd(injectcmd);
1010 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001011 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001012 }
1013 }
1014}
1015
that3f7b1ac2014-12-30 11:30:13 +01001016int GUIAction::flash(std::string arg)
1017{
1018 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001019 // We're going to jump to this page first, like a loading page
1020 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001021 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001022 string zip_path = zip_queue[i];
1023 size_t slashpos = zip_path.find_last_of('/');
1024 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001025 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001026 DataManager::SetValue("tw_filename", zip_path);
1027 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001028 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1029
1030 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001031 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001032 TWFunc::SetPerformanceMode(false);
1033 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001034 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001035 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001036 break;
that3f7b1ac2014-12-30 11:30:13 +01001037 }
1038 }
1039 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001040
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001041 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001042 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001043 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001044 }
that3f7b1ac2014-12-30 11:30:13 +01001045
thatcc8ddca2015-01-03 01:59:36 +01001046 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001047 PartitionManager.Update_System_Details();
1048 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001049 // 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 -06001050 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001051 return 0;
1052}
1053
1054int GUIAction::wipe(std::string arg)
1055{
1056 operation_start("Format");
1057 DataManager::SetValue("tw_partition", arg);
1058
1059 int ret_val = false;
1060
1061 if (simulate) {
1062 simulate_progress_bar();
1063 } else {
1064 if (arg == "data")
1065 ret_val = PartitionManager.Factory_Reset();
1066 else if (arg == "battery")
1067 ret_val = PartitionManager.Wipe_Battery_Stats();
1068 else if (arg == "rotate")
1069 ret_val = PartitionManager.Wipe_Rotate_Data();
1070 else if (arg == "dalvik")
1071 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1072 else if (arg == "DATAMEDIA") {
1073 ret_val = PartitionManager.Format_Data();
1074 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001075 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001076
1077 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1078 if (has_datamedia) {
1079 ret_val = PartitionManager.Wipe_Media_From_Data();
1080 } else {
1081 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1082 }
1083 } else if (arg == "EXTERNAL") {
1084 string External_Path;
1085
1086 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1087 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1088 } else if (arg == "ANDROIDSECURE") {
1089 ret_val = PartitionManager.Wipe_Android_Secure();
1090 } else if (arg == "LIST") {
1091 string Wipe_List, wipe_path;
1092 bool skip = false;
1093 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001094
1095 DataManager::GetValue("tw_wipe_list", Wipe_List);
1096 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1097 if (!Wipe_List.empty()) {
1098 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1099 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1100 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1101 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1102 if (wipe_path == "/and-sec") {
1103 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001104 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001105 ret_val = false;
1106 break;
1107 } else {
1108 skip = true;
1109 }
1110 } else if (wipe_path == "DALVIK") {
1111 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001112 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001113 ret_val = false;
1114 break;
1115 } else {
1116 skip = true;
1117 }
1118 } else if (wipe_path == "INTERNAL") {
1119 if (!PartitionManager.Wipe_Media_From_Data()) {
1120 ret_val = false;
1121 break;
1122 } else {
1123 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001124 }
1125 }
that3f7b1ac2014-12-30 11:30:13 +01001126 if (!skip) {
1127 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001128 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001129 ret_val = false;
1130 break;
1131 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1132 arg = wipe_path;
1133 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001134 } else {
that3f7b1ac2014-12-30 11:30:13 +01001135 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001136 }
that3f7b1ac2014-12-30 11:30:13 +01001137 start_pos = end_pos + 1;
1138 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001139 }
1140 }
that3f7b1ac2014-12-30 11:30:13 +01001141 } else
1142 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001143#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001144 if (arg == DataManager::GetSettingsStoragePath()) {
1145 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1146 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001147
that3f7b1ac2014-12-30 11:30:13 +01001148 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1149 LOGINFO("Making TWRP folder and saving settings.\n");
1150 Storage_Path += "/TWRP";
1151 mkdir(Storage_Path.c_str(), 0777);
1152 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001153 } else {
that3f7b1ac2014-12-30 11:30:13 +01001154 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1155 }
1156 }
1157#endif
1158 }
1159 PartitionManager.Update_System_Details();
1160 if (ret_val)
1161 ret_val = 0; // 0 is success
1162 else
1163 ret_val = 1; // 1 is failure
1164 operation_end(ret_val);
1165 return 0;
1166}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001167
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001168int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001169{
1170 operation_start("Refreshing Sizes");
1171 if (simulate) {
1172 simulate_progress_bar();
1173 } else
1174 PartitionManager.Update_System_Details();
1175 operation_end(0);
1176 return 0;
1177}
1178
1179int GUIAction::nandroid(std::string arg)
1180{
that3f7b1ac2014-12-30 11:30:13 +01001181 if (simulate) {
that73a52952015-01-28 23:07:34 +01001182 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001183 DataManager::SetValue("tw_partition", "Simulation");
1184 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001185 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001186 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001187 operation_start("Nandroid");
1188 int ret = 0;
1189
that3f7b1ac2014-12-30 11:30:13 +01001190 if (arg == "backup") {
1191 string Backup_Name;
1192 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001193 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1194 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 -05001195 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001196 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1197 if (!PartitionManager.stop_backup.get_value()) {
1198 if (ret == false)
1199 ret = 1; // 1 for failure
1200 else
1201 ret = 0; // 0 for success
1202 DataManager::SetValue("tw_cancel_backup", 0);
1203 } else {
1204 DataManager::SetValue("tw_cancel_backup", 1);
1205 gui_msg("backup_cancel=Backup Cancelled");
1206 ret = 0;
1207 }
bigbiffce8f83c2015-12-12 18:30:21 -05001208 } else {
that3f7b1ac2014-12-30 11:30:13 +01001209 operation_end(1);
1210 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001211 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001212 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001213 } else if (arg == "restore") {
1214 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001215 int gui_adb_backup;
1216
that3f7b1ac2014-12-30 11:30:13 +01001217 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001218 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1219 if (gui_adb_backup) {
1220 DataManager::SetValue("tw_operation_state", 1);
1221 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1222 ret = 0; // success
1223 else
1224 ret = 1; // failure
1225 DataManager::SetValue("tw_enable_adb_backup", 0);
1226 ret = 0; // assume success???
1227 } else {
1228 if (PartitionManager.Run_Restore(Restore_Name))
1229 ret = 0; // success
1230 else
1231 ret = 1; // failure
1232 }
that3f7b1ac2014-12-30 11:30:13 +01001233 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001234 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001235 return -1;
1236 }
that73a52952015-01-28 23:07:34 +01001237 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001238 return ret;
1239 }
1240 return 0;
1241}
1242
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001243int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001244 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001245 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001246 }
1247 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001248 int op_status = PartitionManager.Cancel_Backup();
1249 if (op_status != 0)
1250 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001251 }
1252
1253 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001254}
Dees_Troy51a0e822012-09-05 15:24:24 -04001255
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001256int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001257{
that73a52952015-01-28 23:07:34 +01001258 int op_status = 0;
1259
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001260 operation_start("Fix Contexts");
1261 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001262 if (simulate) {
1263 simulate_progress_bar();
1264 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001265 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001266 if (op_status != 0)
1267 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001268 }
that73a52952015-01-28 23:07:34 +01001269 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001270 return 0;
1271}
1272
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001273int GUIAction::fixpermissions(std::string arg)
1274{
1275 return fixcontexts(arg);
1276}
1277
that3f7b1ac2014-12-30 11:30:13 +01001278int GUIAction::dd(std::string arg)
1279{
1280 operation_start("imaging");
1281
1282 if (simulate) {
1283 simulate_progress_bar();
1284 } else {
1285 string cmd = "dd " + arg;
1286 TWFunc::Exec_Cmd(cmd);
1287 }
1288 operation_end(0);
1289 return 0;
1290}
1291
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001292int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001293{
1294 operation_start("Partition SD Card");
1295 int ret_val = 0;
1296
1297 if (simulate) {
1298 simulate_progress_bar();
1299 } else {
1300 int allow_partition;
1301 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1302 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001303 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001304 } else {
1305 if (!PartitionManager.Partition_SDCard())
1306 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001307 }
that3f7b1ac2014-12-30 11:30:13 +01001308 }
1309 operation_end(ret_val);
1310 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001311
that3f7b1ac2014-12-30 11:30:13 +01001312}
Dees_Troy51a0e822012-09-05 15:24:24 -04001313
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001314int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001315{
1316 operation_start("Install HTC Dumlock");
1317 if (simulate) {
1318 simulate_progress_bar();
1319 } else
1320 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001321
that3f7b1ac2014-12-30 11:30:13 +01001322 operation_end(0);
1323 return 0;
1324}
Dees_Troy51a0e822012-09-05 15:24:24 -04001325
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001326int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001327{
1328 operation_start("HTC Dumlock Restore Boot");
1329 if (simulate) {
1330 simulate_progress_bar();
1331 } else
1332 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001333
that3f7b1ac2014-12-30 11:30:13 +01001334 operation_end(0);
1335 return 0;
1336}
Dees_Troy51a0e822012-09-05 15:24:24 -04001337
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001338int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001339{
1340 operation_start("HTC Dumlock Reflash Recovery");
1341 if (simulate) {
1342 simulate_progress_bar();
1343 } else
1344 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001345
that3f7b1ac2014-12-30 11:30:13 +01001346 operation_end(0);
1347 return 0;
1348}
Dees_Troy51a0e822012-09-05 15:24:24 -04001349
that3f7b1ac2014-12-30 11:30:13 +01001350int GUIAction::cmd(std::string arg)
1351{
1352 int op_status = 0;
1353
1354 operation_start("Command");
1355 LOGINFO("Running command: '%s'\n", arg.c_str());
1356 if (simulate) {
1357 simulate_progress_bar();
1358 } else {
1359 op_status = TWFunc::Exec_Cmd(arg);
1360 if (op_status != 0)
1361 op_status = 1;
1362 }
1363
1364 operation_end(op_status);
1365 return 0;
1366}
1367
1368int GUIAction::terminalcommand(std::string arg)
1369{
1370 int op_status = 0;
1371 string cmdpath, command;
1372
1373 DataManager::GetValue("tw_terminal_location", cmdpath);
1374 operation_start("CommandOutput");
1375 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1376 if (simulate) {
1377 simulate_progress_bar();
1378 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001379 } else if (arg == "exit") {
1380 LOGINFO("Exiting terminal\n");
1381 operation_end(op_status);
1382 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001383 } else {
1384 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1385 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001386 DataManager::SetValue("tw_terminal_state", 1);
1387 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001388 FILE* fp;
1389 char line[512];
1390
1391 fp = popen(command.c_str(), "r");
1392 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001393 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001394 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001395 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001396 struct timeval timeout;
1397 fd_set fdset;
1398
Matt Mowera8a89d12016-12-30 18:10:37 -06001399 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001400 {
1401 FD_ZERO(&fdset);
1402 FD_SET(fd, &fdset);
1403 timeout.tv_sec = 0;
1404 timeout.tv_usec = 400000;
1405 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1406 if (has_data == 0) {
1407 // Timeout reached
1408 DataManager::GetValue("tw_terminal_state", check);
1409 if (check == 0) {
1410 keep_going = 0;
1411 }
1412 } else if (has_data < 0) {
1413 // End of execution
1414 keep_going = 0;
1415 } else {
1416 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001417 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001418 gui_print("%s", line); // Display output
1419 else
1420 keep_going = 0; // Done executing
1421 }
1422 }
1423 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001424 }
thatc6085482015-01-09 22:12:43 +01001425 DataManager::SetValue("tw_operation_status", 0);
1426 DataManager::SetValue("tw_operation_state", 1);
1427 DataManager::SetValue("tw_terminal_state", 0);
1428 DataManager::SetValue("tw_background_thread_running", 0);
1429 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001430 }
1431 return 0;
1432}
1433
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001434int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001435{
that3f7b1ac2014-12-30 11:30:13 +01001436 LOGINFO("Sending kill command...\n");
1437 operation_start("KillCommand");
1438 DataManager::SetValue("tw_operation_status", 0);
1439 DataManager::SetValue("tw_operation_state", 1);
1440 DataManager::SetValue("tw_terminal_state", 0);
1441 DataManager::SetValue("tw_background_thread_running", 0);
1442 DataManager::SetValue(TW_ACTION_BUSY, 0);
1443 return 0;
1444}
1445
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001446int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001447{
1448 int op_status = 0;
1449 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001450 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001451 if (simulate) {
1452 simulate_progress_bar();
1453 } else {
1454 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001455 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001456 }
1457
1458 operation_end(op_status);
1459 return 0;
1460}
1461
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001462int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001463{
1464 int op_status = 0;
1465
1466 operation_start("CheckBackupName");
1467 if (simulate) {
1468 simulate_progress_bar();
1469 } else {
1470 op_status = PartitionManager.Check_Backup_Name(true);
1471 if (op_status != 0)
1472 op_status = 1;
1473 }
1474
1475 operation_end(op_status);
1476 return 0;
1477}
1478
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001479int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001480{
1481 int op_status = 0;
1482
1483 operation_start("Decrypt");
1484 if (simulate) {
1485 simulate_progress_bar();
1486 } else {
1487 string Password;
1488 DataManager::GetValue("tw_crypto_password", Password);
1489 op_status = PartitionManager.Decrypt_Device(Password);
1490 if (op_status != 0)
1491 op_status = 1;
1492 else {
that3f7b1ac2014-12-30 11:30:13 +01001493
1494 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1495
Ethan Yonkercf50da52015-01-12 21:59:07 -06001496 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001497
Ethan Yonkercf50da52015-01-12 21:59:07 -06001498 // Check for a custom theme and load it if exists
1499 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1500 if (has_datamedia != 0) {
1501 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001502 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001503 } else {
1504 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001505 }
1506 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001507 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001508 }
1509 }
1510
1511 operation_end(op_status);
1512 return 0;
1513}
1514
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001515int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001516{
1517 operation_start("Sideload");
1518 if (simulate) {
1519 simulate_progress_bar();
1520 operation_end(0);
1521 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001522 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001523 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1524
1525 // wait for the adb connection
1526 int ret = apply_from_adb("/", &sideload_child_pid);
1527 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1528
1529 if (ret != 0) {
1530 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001531 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001532 ret = 1; // failure
1533 } else {
1534 int wipe_cache = 0;
1535 int wipe_dalvik = 0;
1536 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1537
1538 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1539 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1540 PartitionManager.Wipe_By_Path("/cache");
1541 if (wipe_dalvik)
1542 PartitionManager.Wipe_Dalvik_Cache();
1543 } else {
1544 ret = 1; // failure
1545 }
thatcc8ddca2015-01-03 01:59:36 +01001546 }
thatc6085482015-01-09 22:12:43 +01001547 if (sideload_child_pid) {
1548 LOGINFO("Signaling child sideload process to exit.\n");
1549 struct stat st;
1550 // Calling stat() on this magic filename signals the minadbd
1551 // subprocess to shut down.
1552 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1553 int status;
1554 LOGINFO("Waiting for child sideload process to exit.\n");
1555 waitpid(sideload_child_pid, &status, 0);
1556 }
Dees Troy28a85d22015-04-28 02:03:16 +00001557 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001558 TWFunc::Toggle_MTP(mtp_was_enabled);
1559 reinject_after_flash();
1560 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001561 }
that3f7b1ac2014-12-30 11:30:13 +01001562 return 0;
1563}
1564
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001565int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001566{
that3f7b1ac2014-12-30 11:30:13 +01001567 struct stat st;
1568 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001569 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001570 LOGINFO("Signaling child sideload process to exit.\n");
1571 // Calling stat() on this magic filename signals the minadbd
1572 // subprocess to shut down.
1573 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1574 if (!sideload_child_pid) {
1575 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001576 return 0;
1577 }
thatcc8ddca2015-01-03 01:59:36 +01001578 ::sleep(1);
1579 LOGINFO("Killing child sideload process.\n");
1580 kill(sideload_child_pid, SIGTERM);
1581 int status;
1582 LOGINFO("Waiting for child sideload process to exit.\n");
1583 waitpid(sideload_child_pid, &status, 0);
1584 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001585 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1586 return 0;
1587}
1588
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001589int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001590{
1591 operation_start("OpenRecoveryScript");
1592 if (simulate) {
1593 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001594 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001595 } else {
that10ae24f2015-12-26 20:53:51 +01001596 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001597 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001598 }
1599 return 0;
1600}
1601
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001602int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001603{
1604 int op_status = 0;
1605
1606 operation_start("Install SuperSU");
1607 if (simulate) {
1608 simulate_progress_bar();
1609 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001610 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001611 }
1612
1613 operation_end(op_status);
1614 return 0;
1615}
1616
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001617int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001618{
1619 int op_status = 0;
1620
1621 operation_start("Fixing Superuser Permissions");
1622 if (simulate) {
1623 simulate_progress_bar();
1624 } else {
1625 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1626 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1627 }
1628
1629 operation_end(op_status);
1630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
1635 int op_status = 0;
1636
1637 operation_start("Try Restore Decrypt");
1638 if (simulate) {
1639 simulate_progress_bar();
1640 } else {
1641 string Restore_Path, Filename, Password;
1642 DataManager::GetValue("tw_restore", Restore_Path);
1643 Restore_Path += "/";
1644 DataManager::GetValue("tw_restore_password", Password);
1645 TWFunc::SetPerformanceMode(true);
1646 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1647 op_status = 0; // success
1648 else
1649 op_status = 1; // fail
1650 TWFunc::SetPerformanceMode(false);
1651 }
1652
1653 operation_end(op_status);
1654 return 0;
1655}
1656
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001657int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001658{
1659 int op_status = 0;
1660
1661 operation_start("Repair Partition");
1662 if (simulate) {
1663 simulate_progress_bar();
1664 } else {
1665 string part_path;
1666 DataManager::GetValue("tw_partition_mount_point", part_path);
1667 if (PartitionManager.Repair_By_Path(part_path, true)) {
1668 op_status = 0; // success
1669 } else {
that3f7b1ac2014-12-30 11:30:13 +01001670 op_status = 1; // fail
1671 }
1672 }
1673
1674 operation_end(op_status);
1675 return 0;
1676}
1677
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001678int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001679{
1680 int op_status = 0;
1681
1682 operation_start("Resize Partition");
1683 if (simulate) {
1684 simulate_progress_bar();
1685 } else {
1686 string part_path;
1687 DataManager::GetValue("tw_partition_mount_point", part_path);
1688 if (PartitionManager.Resize_By_Path(part_path, true)) {
1689 op_status = 0; // success
1690 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001691 op_status = 1; // fail
1692 }
1693 }
1694
1695 operation_end(op_status);
1696 return 0;
1697}
1698
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001699int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001700{
1701 int op_status = 0;
1702
1703 operation_start("Change File System");
1704 if (simulate) {
1705 simulate_progress_bar();
1706 } else {
1707 string part_path, file_system;
1708 DataManager::GetValue("tw_partition_mount_point", part_path);
1709 DataManager::GetValue("tw_action_new_file_system", file_system);
1710 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1711 op_status = 0; // success
1712 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001713 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001714 op_status = 1; // fail
1715 }
1716 }
1717 PartitionManager.Update_System_Details();
1718 operation_end(op_status);
1719 return 0;
1720}
1721
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001722int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001723{
1724 int op_status = 0;
1725
1726 operation_start("Start MTP");
1727 if (PartitionManager.Enable_MTP())
1728 op_status = 0; // success
1729 else
1730 op_status = 1; // fail
1731
1732 operation_end(op_status);
1733 return 0;
1734}
1735
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001736int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001737{
1738 int op_status = 0;
1739
1740 operation_start("Stop MTP");
1741 if (PartitionManager.Disable_MTP())
1742 op_status = 0; // success
1743 else
1744 op_status = 1; // fail
1745
1746 operation_end(op_status);
1747 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001748}
1749
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001750int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001751{
1752 int op_status = 0;
1753
1754 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001755 string path, filename;
1756 DataManager::GetValue("tw_zip_location", path);
1757 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001758 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001759 op_status = 0; // success
1760 else
1761 op_status = 1; // fail
1762
1763 operation_end(op_status);
1764 return 0;
1765}
1766
that10ae24f2015-12-26 20:53:51 +01001767int GUIAction::twcmd(std::string arg)
1768{
1769 operation_start("TWRP CLI Command");
1770 if (simulate)
1771 simulate_progress_bar();
1772 else
1773 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1774 operation_end(0);
1775 return 0;
1776}
1777
Dees_Troy51a0e822012-09-05 15:24:24 -04001778int GUIAction::getKeyByName(std::string key)
1779{
that8834a0f2016-01-05 23:29:30 +01001780 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001781 else if (key == "menu") return KEY_MENU;
1782 else if (key == "back") return KEY_BACK;
1783 else if (key == "search") return KEY_SEARCH;
1784 else if (key == "voldown") return KEY_VOLUMEDOWN;
1785 else if (key == "volup") return KEY_VOLUMEUP;
1786 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001787 int ret_val;
1788 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1789 if (!ret_val)
1790 return KEY_POWER;
1791 else
1792 return ret_val;
1793 }
1794
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001795 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001796}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001797
1798int GUIAction::checkpartitionlifetimewrites(std::string arg)
1799{
1800 int op_status = 0;
1801 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1802
1803 operation_start("Check Partition Lifetime Writes");
1804 if (sys) {
1805 if (sys->Check_Lifetime_Writes() != 0)
1806 DataManager::SetValue("tw_lifetime_writes", 1);
1807 else
1808 DataManager::SetValue("tw_lifetime_writes", 0);
1809 op_status = 0; // success
1810 } else {
1811 DataManager::SetValue("tw_lifetime_writes", 1);
1812 op_status = 1; // fail
1813 }
1814
1815 operation_end(op_status);
1816 return 0;
1817}
1818
1819int GUIAction::mountsystemtoggle(std::string arg)
1820{
1821 int op_status = 0;
1822 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001823 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001824
1825 operation_start("Toggle System Mount");
1826 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1827 op_status = 1; // fail
1828 } else {
1829 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1830 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001831 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001832 DataManager::SetValue("tw_mount_system_ro", 0);
1833 Part->Change_Mount_Read_Only(false);
1834 } else {
1835 DataManager::SetValue("tw_mount_system_ro", 1);
1836 Part->Change_Mount_Read_Only(true);
1837 }
1838 if (remount_system) {
1839 Part->Mount(true);
1840 }
1841 op_status = 0; // success
1842 } else {
1843 op_status = 1; // fail
1844 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001845 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1846 if (Part) {
1847 if (arg == "0") {
1848 Part->Change_Mount_Read_Only(false);
1849 } else {
1850 Part->Change_Mount_Read_Only(true);
1851 }
1852 if (remount_vendor) {
1853 Part->Mount(true);
1854 }
1855 op_status = 0; // success
1856 } else {
1857 op_status = 1; // fail
1858 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001859 }
1860
1861 operation_end(op_status);
1862 return 0;
1863}
Ethan Yonker74db1572015-10-28 12:44:49 -05001864
1865int GUIAction::setlanguage(std::string arg __unused)
1866{
1867 int op_status = 0;
1868
1869 operation_start("Set Language");
1870 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1871 PageManager::RequestReload();
1872 op_status = 0; // success
1873
1874 operation_end(op_status);
1875 return 0;
1876}
Ethan Yonker1b190162016-12-05 15:25:19 -06001877
Matt Mower9472ba12016-01-20 18:12:47 -06001878int GUIAction::togglebacklight(std::string arg __unused)
1879{
1880 blankTimer.toggleBlank();
1881 return 0;
1882}
1883
Ethan Yonker1b190162016-12-05 15:25:19 -06001884int GUIAction::setbootslot(std::string arg)
1885{
1886 operation_start("Set Boot Slot");
1887 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001888 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001889 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001890 simulate_progress_bar();
1891 operation_end(0);
1892 return 0;
1893}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001894
1895int GUIAction::checkforapp(std::string arg __unused)
1896{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001897 operation_start("Check for TWRP App");
1898 if (!simulate)
1899 {
1900 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1901 int sdkver = 0;
1902 if (!sdkverstr.empty()) {
1903 sdkver = atoi(sdkverstr.c_str());
1904 }
1905 if (sdkver <= 13) {
1906 if (sdkver == 0)
1907 LOGINFO("Unable to read sdk version from build prop\n");
1908 else
1909 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001910 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 -06001911 goto exit;
1912 }
1913 if (PartitionManager.Mount_By_Path("/system", false)) {
1914 string base_path = "/system";
1915 if (TWFunc::Path_Exists("/system/system"))
1916 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1917 string install_path = base_path + "/priv-app";
1918 if (!TWFunc::Path_Exists(install_path))
1919 install_path = base_path + "/app";
1920 install_path += "/twrpapp";
1921 if (TWFunc::Path_Exists(install_path)) {
1922 LOGINFO("App found at '%s'\n", install_path.c_str());
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001923 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 -06001924 goto exit;
1925 }
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001926 }
1927 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001928 const char parent_path[] = "/data/app";
1929 const char app_prefix[] = "me.twrp.twrpapp-";
1930 DIR *d = opendir(parent_path);
1931 if (d) {
1932 struct dirent *p;
1933 while ((p = readdir(d))) {
1934 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1935 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001936 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001937 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001938 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001939 goto exit;
1940 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001941 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001943 } else {
1944 LOGINFO("Data partition cannot be mounted during app check\n");
1945 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001946 }
1947 } else
1948 simulate_progress_bar();
1949 LOGINFO("App not installed\n");
1950 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1951exit:
1952 operation_end(0);
1953 return 0;
1954}
1955
1956int GUIAction::installapp(std::string arg __unused)
1957{
1958 int op_status = 1;
1959 operation_start("Install TWRP App");
1960 if (!simulate)
1961 {
1962 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1963 if (PartitionManager.Mount_By_Path("/data", true)) {
1964 string install_path = "/data/app";
1965 string context = "u:object_r:apk_data_file:s0";
1966 if (!TWFunc::Path_Exists(install_path)) {
1967 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1968 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1969 goto exit;
1970 }
1971 if (chown(install_path.c_str(), 1000, 1000)) {
1972 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1973 goto exit;
1974 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001975 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001976 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1977 goto exit;
1978 }
1979 }
1980 install_path += "/me.twrp.twrpapp-1";
1981 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1982 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1983 goto exit;
1984 }
1985 if (chown(install_path.c_str(), 1000, 1000)) {
1986 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001989 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001990 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
1993 install_path += "/base.apk";
1994 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1995 LOGERR("Error copying apk file\n");
1996 goto exit;
1997 }
1998 if (chown(install_path.c_str(), 1000, 1000)) {
1999 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2000 goto exit;
2001 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002002 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002003 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
2006 sync();
2007 sync();
2008 }
2009 } else {
2010 if (PartitionManager.Mount_By_Path("/system", true)) {
2011 string base_path = "/system";
2012 if (TWFunc::Path_Exists("/system/system"))
2013 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2014 string install_path = base_path + "/priv-app";
2015 string context = "u:object_r:system_file:s0";
2016 if (!TWFunc::Path_Exists(install_path))
2017 install_path = base_path + "/app";
2018 if (TWFunc::Path_Exists(install_path)) {
2019 install_path += "/twrpapp";
2020 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2021 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002022 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002023 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2024 goto exit;
2025 }
2026 install_path += "/me.twrp.twrpapp.apk";
2027 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2028 LOGERR("Error copying apk file\n");
2029 goto exit;
2030 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002031 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002032 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2033 goto exit;
2034 }
2035 sync();
2036 sync();
2037 PartitionManager.UnMount_By_Path("/system", true);
2038 op_status = 0;
2039 } else {
2040 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2041 }
2042 }
2043 }
2044 }
2045 } else
2046 simulate_progress_bar();
2047exit:
2048 operation_end(0);
2049 return 0;
2050}