blob: fef4762873e38896a482eb49252ec9f43f8e24cd [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
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>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.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 Yonkerfbb43532015-12-28 21:54:50 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.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;
569 DataManager::GetValue("tw_restore", Restore_Name);
570 PartitionManager.Set_Restore_Files(Restore_Name);
571 return 0;
572}
573
574int GUIAction::set(std::string arg)
575{
576 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200577 {
that3f7b1ac2014-12-30 11:30:13 +0100578 string varName = arg.substr(0, arg.find('='));
579 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400580
that3f7b1ac2014-12-30 11:30:13 +0100581 DataManager::GetValue(value, value);
582 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 }
that3f7b1ac2014-12-30 11:30:13 +0100584 else
585 DataManager::SetValue(arg, "1");
586 return 0;
587}
Dees_Troy51a0e822012-09-05 15:24:24 -0400588
that3f7b1ac2014-12-30 11:30:13 +0100589int GUIAction::clear(std::string arg)
590{
591 DataManager::SetValue(arg, "0");
592 return 0;
593}
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
that3f7b1ac2014-12-30 11:30:13 +0100595int GUIAction::mount(std::string arg)
596{
597 if (arg == "usb") {
598 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100600 PartitionManager.usb_storage_enable();
601 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500602 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100603 } else if (!simulate) {
604 PartitionManager.Mount_By_Path(arg, true);
605 PartitionManager.Add_MTP_Storage(arg);
606 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500607 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100608 return 0;
609}
610
611int GUIAction::unmount(std::string arg)
612{
613 if (arg == "usb") {
614 if (!simulate)
615 PartitionManager.usb_storage_disable();
616 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100618 DataManager::SetValue(TW_ACTION_BUSY, 0);
619 } else if (!simulate) {
620 PartitionManager.UnMount_By_Path(arg, true);
621 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500622 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100623 return 0;
624}
625
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500626int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100627{
628 operation_start("Restore Defaults");
629 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500630 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100631 else {
632 DataManager::ResetDefaults();
633 PartitionManager.Update_System_Details();
634 PartitionManager.Mount_Current_Storage(true);
635 }
636 operation_end(0);
637 return 0;
638}
639
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500640int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100641{
642 operation_start("Copy Log");
643 if (!simulate)
644 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400645 string dst, curr_storage;
646 int copy_kernel_log = 0;
647
648 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100649 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400650 curr_storage = DataManager::GetCurrentStoragePath();
651 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100652 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
653 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400654 if (copy_kernel_log)
655 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100656 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400657 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100658 } else
659 simulate_progress_bar();
660 operation_end(0);
661 return 0;
662}
663
664
665int GUIAction::compute(std::string arg)
666{
667 if (arg.find("+") != string::npos)
668 {
669 string varName = arg.substr(0, arg.find('+'));
670 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
671 int amount_to_add = atoi(string_to_add.c_str());
672 int value;
673
674 DataManager::GetValue(varName, value);
675 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 return 0;
677 }
that3f7b1ac2014-12-30 11:30:13 +0100678 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 {
that3f7b1ac2014-12-30 11:30:13 +0100680 string varName = arg.substr(0, arg.find('-'));
681 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
682 int amount_to_subtract = atoi(string_to_subtract.c_str());
683 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400684
that3f7b1ac2014-12-30 11:30:13 +0100685 DataManager::GetValue(varName, value);
686 value -= amount_to_subtract;
687 if (value <= 0)
688 value = 0;
689 DataManager::SetValue(varName, value);
690 return 0;
691 }
692 if (arg.find("*") != string::npos)
693 {
694 string varName = arg.substr(0, arg.find('*'));
695 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
696 int multiply_by = atoi(multiply_by_str.c_str());
697 int value;
698
699 DataManager::GetValue(varName, value);
700 DataManager::SetValue(varName, value*multiply_by);
701 return 0;
702 }
703 if (arg.find("/") != string::npos)
704 {
705 string varName = arg.substr(0, arg.find('/'));
706 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
707 int divide_by = atoi(divide_by_str.c_str());
708 int value;
709
Matt Mowera8a89d12016-12-30 18:10:37 -0600710 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100711 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100713 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 }
715 return 0;
716 }
that3f7b1ac2014-12-30 11:30:13 +0100717 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
718 return -1;
719}
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500721int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100722{
723 string SelectedZone;
724 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
725 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
726 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
727
728 int dst;
729 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
730
731 string offset;
732 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
733
734 string NewTimeZone = Zone;
735 if (offset != "0")
736 NewTimeZone += ":" + offset;
737
738 if (dst != 0)
739 NewTimeZone += DSTZone;
740
741 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
742 DataManager::update_tz_environment_variables();
743 return 0;
744}
745
746int GUIAction::overlay(std::string arg)
747{
748 return gui_changeOverlay(arg);
749}
750
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500751int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100752{
753 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500754 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 return 0;
756 }
that3f7b1ac2014-12-30 11:30:13 +0100757 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
758 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
759 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400760 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100761 }
762 return 0;
763}
764
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500765int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100766{
767 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500768 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100770 } else {
771 zip_queue_index--;
772 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 }
that3f7b1ac2014-12-30 11:30:13 +0100774 return 0;
775}
Dees_Troy51a0e822012-09-05 15:24:24 -0400776
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500777int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100778{
779 zip_queue_index = 0;
780 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
781 return 0;
782}
Dees_Troy51a0e822012-09-05 15:24:24 -0400783
that3f7b1ac2014-12-30 11:30:13 +0100784int GUIAction::sleep(std::string arg)
785{
786 operation_start("Sleep");
787 usleep(atoi(arg.c_str()));
788 operation_end(0);
789 return 0;
790}
Dees Troyb21cc642013-09-10 17:36:41 +0000791
Matt Mower9a2a2052016-05-31 21:31:22 -0500792int GUIAction::sleepcounter(std::string arg)
793{
794 operation_start("SleepCounter");
795 // Ensure user notices countdown in case it needs to be cancelled
796 blankTimer.resetTimerAndUnblank();
797 int total = atoi(arg.c_str());
798 for (int t = total; t > 0; t--) {
799 int progress = (int)(((float)(total-t)/(float)total)*100.0);
800 DataManager::SetValue("ui_progress", progress);
801 ::sleep(1);
802 DataManager::SetValue("tw_sleep", t-1);
803 }
804 DataManager::SetValue("ui_progress", 100);
805 operation_end(0);
806 return 0;
807}
808
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500809int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100810{
811 operation_start("AppendDateToBackupName");
812 string Backup_Name;
813 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
814 Backup_Name += TWFunc::Get_Current_Date();
815 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
816 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
817 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500818 PageManager::NotifyKey(KEY_END, true);
819 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100820 operation_end(0);
821 return 0;
822}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500823
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500824int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100825{
826 operation_start("GenerateBackupName");
827 TWFunc::Auto_Generate_Backup_Name();
828 operation_end(0);
829 return 0;
830}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500831
Ethan Yonker483e9f42016-01-11 22:21:18 -0600832int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100833{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600834 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100835 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500836
Ethan Yonker483e9f42016-01-11 22:21:18 -0600837 if (arg.empty())
838 arg = "tw_wipe_list";
839 DataManager::GetValue(arg, List);
840 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
841 if (!List.empty()) {
842 size_t start_pos = 0, end_pos = List.find(";", start_pos);
843 while (end_pos != string::npos && start_pos < List.size()) {
844 part_path = List.substr(start_pos, end_pos - start_pos);
845 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
846 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100847 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400848 } else {
that3f7b1ac2014-12-30 11:30:13 +0100849 count++;
850 }
851 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100853 }
854 DataManager::SetValue("tw_check_partition_list", count);
855 } else {
856 DataManager::SetValue("tw_check_partition_list", 0);
857 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600858 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100859}
Dees_Troy51a0e822012-09-05 15:24:24 -0400860
Ethan Yonker483e9f42016-01-11 22:21:18 -0600861int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100862{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400864
Ethan Yonker483e9f42016-01-11 22:21:18 -0600865 if (arg.empty())
866 arg = "tw_wipe_list";
867 DataManager::GetValue(arg, List);
868 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
869 if (!List.empty()) {
870 size_t start_pos = 0, end_pos = List.find(";", start_pos);
871 part_path = List;
872 while (end_pos != string::npos && start_pos < List.size()) {
873 part_path = List.substr(start_pos, end_pos - start_pos);
874 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
875 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100876 // Do nothing
877 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600878 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100879 break;
880 }
881 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600882 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100883 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600884 if (!part_path.empty()) {
885 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100886 if (Part) {
887 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500888
that3f7b1ac2014-12-30 11:30:13 +0100889 DataManager::SetValue("tw_partition_name", Part->Display_Name);
890 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
891 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
892 DataManager::SetValue("tw_partition_size", Part->Size / mb);
893 DataManager::SetValue("tw_partition_used", Part->Used / mb);
894 DataManager::SetValue("tw_partition_free", Part->Free / mb);
895 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
896 DataManager::SetValue("tw_partition_removable", Part->Removable);
897 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
898
899 if (Part->Can_Repair())
900 DataManager::SetValue("tw_partition_can_repair", 1);
901 else
902 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500903 if (Part->Can_Resize())
904 DataManager::SetValue("tw_partition_can_resize", 1);
905 else
906 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600907 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100908 DataManager::SetValue("tw_partition_vfat", 1);
909 else
910 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600911 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100912 DataManager::SetValue("tw_partition_exfat", 1);
913 else
914 DataManager::SetValue("tw_partition_exfat", 0);
915 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
916 DataManager::SetValue("tw_partition_f2fs", 1);
917 else
918 DataManager::SetValue("tw_partition_f2fs", 0);
919 if (TWFunc::Path_Exists("/sbin/mke2fs"))
920 DataManager::SetValue("tw_partition_ext", 1);
921 else
922 DataManager::SetValue("tw_partition_ext", 0);
923 return 0;
924 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600925 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100926 }
927 }
928 }
929 DataManager::SetValue("tw_partition_name", "");
930 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600931 // Set this to 0 to prevent trying to partition this device, just in case
932 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100933 return 0;
934}
935
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500936int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100937{
938 time_t tm;
939 char path[256];
940 int path_len;
941 uid_t uid = -1;
942 gid_t gid = -1;
943
944 struct passwd *pwd = getpwnam("media_rw");
Matt Mowera8a89d12016-12-30 18:10:37 -0600945 if (pwd) {
that3f7b1ac2014-12-30 11:30:13 +0100946 uid = pwd->pw_uid;
947 gid = pwd->pw_gid;
948 }
949
950 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600951 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100952 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
953 } else {
954 strcpy(path, "/tmp/");
955 }
956
Matt Mowera8a89d12016-12-30 18:10:37 -0600957 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100958 return 0;
959
960 tm = time(NULL);
961 path_len = strlen(path);
962
963 // Screenshot_2014-01-01-18-21-38.png
964 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
965
966 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600967 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100968 chmod(path, 0666);
969 chown(path, uid, gid);
970
that677b13f2015-12-26 23:57:31 +0100971 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100972
973 // blink to notify that the screenshow was taken
974 gr_color(255, 255, 255, 255);
975 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
976 gr_flip();
977 gui_forceRender();
978 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500979 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100980 }
981 return 0;
982}
983
984int GUIAction::setbrightness(std::string arg)
985{
986 return TWFunc::Set_Brightness(arg);
987}
988
989int GUIAction::fileexists(std::string arg)
990{
991 struct stat st;
992 string newpath = arg + "/.";
993
994 operation_start("FileExists");
995 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
996 operation_end(0);
997 else
998 operation_end(1);
999 return 0;
1000}
1001
thatcc8ddca2015-01-03 01:59:36 +01001002void GUIAction::reinject_after_flash()
1003{
1004 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001005 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001006 if (simulate) {
1007 simulate_progress_bar();
1008 } else {
1009 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1010 if (Boot == NULL || Boot->Current_File_System != "emmc")
1011 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1012 else {
1013 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1014 TWFunc::Exec_Cmd(injectcmd);
1015 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001016 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001017 }
1018 }
1019}
1020
that3f7b1ac2014-12-30 11:30:13 +01001021int GUIAction::flash(std::string arg)
1022{
1023 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001024 // We're going to jump to this page first, like a loading page
1025 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001026 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001027 string zip_path = zip_queue[i];
1028 size_t slashpos = zip_path.find_last_of('/');
1029 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001030 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001031 DataManager::SetValue("tw_filename", zip_path);
1032 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001033 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1034
1035 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001036 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001037 TWFunc::SetPerformanceMode(false);
1038 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001039 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001040 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001041 break;
that3f7b1ac2014-12-30 11:30:13 +01001042 }
1043 }
1044 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001045
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001046 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001047 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001048 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001049 }
that3f7b1ac2014-12-30 11:30:13 +01001050
thatcc8ddca2015-01-03 01:59:36 +01001051 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001052 PartitionManager.Update_System_Details();
1053 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001054 // 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 -06001055 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001056 return 0;
1057}
1058
1059int GUIAction::wipe(std::string arg)
1060{
1061 operation_start("Format");
1062 DataManager::SetValue("tw_partition", arg);
1063
1064 int ret_val = false;
1065
1066 if (simulate) {
1067 simulate_progress_bar();
1068 } else {
1069 if (arg == "data")
1070 ret_val = PartitionManager.Factory_Reset();
1071 else if (arg == "battery")
1072 ret_val = PartitionManager.Wipe_Battery_Stats();
1073 else if (arg == "rotate")
1074 ret_val = PartitionManager.Wipe_Rotate_Data();
1075 else if (arg == "dalvik")
1076 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1077 else if (arg == "DATAMEDIA") {
1078 ret_val = PartitionManager.Format_Data();
1079 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001080 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001081
1082 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1083 if (has_datamedia) {
1084 ret_val = PartitionManager.Wipe_Media_From_Data();
1085 } else {
1086 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1087 }
1088 } else if (arg == "EXTERNAL") {
1089 string External_Path;
1090
1091 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1092 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1093 } else if (arg == "ANDROIDSECURE") {
1094 ret_val = PartitionManager.Wipe_Android_Secure();
1095 } else if (arg == "LIST") {
1096 string Wipe_List, wipe_path;
1097 bool skip = false;
1098 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001099
1100 DataManager::GetValue("tw_wipe_list", Wipe_List);
1101 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1102 if (!Wipe_List.empty()) {
1103 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1104 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1105 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1106 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1107 if (wipe_path == "/and-sec") {
1108 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001109 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001110 ret_val = false;
1111 break;
1112 } else {
1113 skip = true;
1114 }
1115 } else if (wipe_path == "DALVIK") {
1116 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001117 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001118 ret_val = false;
1119 break;
1120 } else {
1121 skip = true;
1122 }
1123 } else if (wipe_path == "INTERNAL") {
1124 if (!PartitionManager.Wipe_Media_From_Data()) {
1125 ret_val = false;
1126 break;
1127 } else {
1128 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 }
1130 }
that3f7b1ac2014-12-30 11:30:13 +01001131 if (!skip) {
1132 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001133 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001134 ret_val = false;
1135 break;
1136 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1137 arg = wipe_path;
1138 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 } else {
that3f7b1ac2014-12-30 11:30:13 +01001140 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 }
that3f7b1ac2014-12-30 11:30:13 +01001142 start_pos = end_pos + 1;
1143 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001144 }
1145 }
that3f7b1ac2014-12-30 11:30:13 +01001146 } else
1147 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001148#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001149 if (arg == DataManager::GetSettingsStoragePath()) {
1150 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1151 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001152
that3f7b1ac2014-12-30 11:30:13 +01001153 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1154 LOGINFO("Making TWRP folder and saving settings.\n");
1155 Storage_Path += "/TWRP";
1156 mkdir(Storage_Path.c_str(), 0777);
1157 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001158 } else {
that3f7b1ac2014-12-30 11:30:13 +01001159 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1160 }
1161 }
1162#endif
1163 }
1164 PartitionManager.Update_System_Details();
1165 if (ret_val)
1166 ret_val = 0; // 0 is success
1167 else
1168 ret_val = 1; // 1 is failure
1169 operation_end(ret_val);
1170 return 0;
1171}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001172
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001173int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001174{
1175 operation_start("Refreshing Sizes");
1176 if (simulate) {
1177 simulate_progress_bar();
1178 } else
1179 PartitionManager.Update_System_Details();
1180 operation_end(0);
1181 return 0;
1182}
1183
1184int GUIAction::nandroid(std::string arg)
1185{
that3f7b1ac2014-12-30 11:30:13 +01001186 if (simulate) {
that73a52952015-01-28 23:07:34 +01001187 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001188 DataManager::SetValue("tw_partition", "Simulation");
1189 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001190 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001191 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001192 operation_start("Nandroid");
1193 int ret = 0;
1194
that3f7b1ac2014-12-30 11:30:13 +01001195 if (arg == "backup") {
1196 string Backup_Name;
1197 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001198 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1199 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001200 ret = PartitionManager.Run_Backup(false);
1201 } else {
that3f7b1ac2014-12-30 11:30:13 +01001202 operation_end(1);
1203 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001204 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001205 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001206 } else if (arg == "restore") {
1207 string Restore_Name;
1208 DataManager::GetValue("tw_restore", Restore_Name);
1209 ret = PartitionManager.Run_Restore(Restore_Name);
1210 } else {
1211 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001212 return -1;
1213 }
1214 DataManager::SetValue("tw_encrypt_backup", 0);
1215 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001216 if (ret == false)
1217 ret = 1; // 1 for failure
1218 else
1219 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001220 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001221 }
1222 else {
1223 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001224 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001225 ret = 0;
1226 }
that73a52952015-01-28 23:07:34 +01001227 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001228 return ret;
1229 }
1230 return 0;
1231}
1232
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001233int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001234 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001235 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001236 }
1237 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001238 int op_status = PartitionManager.Cancel_Backup();
1239 if (op_status != 0)
1240 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001241 }
1242
1243 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001244}
Dees_Troy51a0e822012-09-05 15:24:24 -04001245
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001246int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001247{
that73a52952015-01-28 23:07:34 +01001248 int op_status = 0;
1249
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001250 operation_start("Fix Contexts");
1251 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001252 if (simulate) {
1253 simulate_progress_bar();
1254 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001255 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001256 if (op_status != 0)
1257 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001258 }
that73a52952015-01-28 23:07:34 +01001259 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001260 return 0;
1261}
1262
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001263int GUIAction::fixpermissions(std::string arg)
1264{
1265 return fixcontexts(arg);
1266}
1267
that3f7b1ac2014-12-30 11:30:13 +01001268int GUIAction::dd(std::string arg)
1269{
1270 operation_start("imaging");
1271
1272 if (simulate) {
1273 simulate_progress_bar();
1274 } else {
1275 string cmd = "dd " + arg;
1276 TWFunc::Exec_Cmd(cmd);
1277 }
1278 operation_end(0);
1279 return 0;
1280}
1281
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001282int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001283{
1284 operation_start("Partition SD Card");
1285 int ret_val = 0;
1286
1287 if (simulate) {
1288 simulate_progress_bar();
1289 } else {
1290 int allow_partition;
1291 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1292 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001293 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001294 } else {
1295 if (!PartitionManager.Partition_SDCard())
1296 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001297 }
that3f7b1ac2014-12-30 11:30:13 +01001298 }
1299 operation_end(ret_val);
1300 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001301
that3f7b1ac2014-12-30 11:30:13 +01001302}
Dees_Troy51a0e822012-09-05 15:24:24 -04001303
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001304int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001305{
1306 operation_start("Install HTC Dumlock");
1307 if (simulate) {
1308 simulate_progress_bar();
1309 } else
1310 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001311
that3f7b1ac2014-12-30 11:30:13 +01001312 operation_end(0);
1313 return 0;
1314}
Dees_Troy51a0e822012-09-05 15:24:24 -04001315
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001316int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001317{
1318 operation_start("HTC Dumlock Restore Boot");
1319 if (simulate) {
1320 simulate_progress_bar();
1321 } else
1322 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001323
that3f7b1ac2014-12-30 11:30:13 +01001324 operation_end(0);
1325 return 0;
1326}
Dees_Troy51a0e822012-09-05 15:24:24 -04001327
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001328int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001329{
1330 operation_start("HTC Dumlock Reflash Recovery");
1331 if (simulate) {
1332 simulate_progress_bar();
1333 } else
1334 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001335
that3f7b1ac2014-12-30 11:30:13 +01001336 operation_end(0);
1337 return 0;
1338}
Dees_Troy51a0e822012-09-05 15:24:24 -04001339
that3f7b1ac2014-12-30 11:30:13 +01001340int GUIAction::cmd(std::string arg)
1341{
1342 int op_status = 0;
1343
1344 operation_start("Command");
1345 LOGINFO("Running command: '%s'\n", arg.c_str());
1346 if (simulate) {
1347 simulate_progress_bar();
1348 } else {
1349 op_status = TWFunc::Exec_Cmd(arg);
1350 if (op_status != 0)
1351 op_status = 1;
1352 }
1353
1354 operation_end(op_status);
1355 return 0;
1356}
1357
1358int GUIAction::terminalcommand(std::string arg)
1359{
1360 int op_status = 0;
1361 string cmdpath, command;
1362
1363 DataManager::GetValue("tw_terminal_location", cmdpath);
1364 operation_start("CommandOutput");
1365 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1366 if (simulate) {
1367 simulate_progress_bar();
1368 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001369 } else if (arg == "exit") {
1370 LOGINFO("Exiting terminal\n");
1371 operation_end(op_status);
1372 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001373 } else {
1374 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1375 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001376 DataManager::SetValue("tw_terminal_state", 1);
1377 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001378 FILE* fp;
1379 char line[512];
1380
1381 fp = popen(command.c_str(), "r");
1382 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001383 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001384 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001385 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001386 struct timeval timeout;
1387 fd_set fdset;
1388
Matt Mowera8a89d12016-12-30 18:10:37 -06001389 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001390 {
1391 FD_ZERO(&fdset);
1392 FD_SET(fd, &fdset);
1393 timeout.tv_sec = 0;
1394 timeout.tv_usec = 400000;
1395 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1396 if (has_data == 0) {
1397 // Timeout reached
1398 DataManager::GetValue("tw_terminal_state", check);
1399 if (check == 0) {
1400 keep_going = 0;
1401 }
1402 } else if (has_data < 0) {
1403 // End of execution
1404 keep_going = 0;
1405 } else {
1406 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001407 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001408 gui_print("%s", line); // Display output
1409 else
1410 keep_going = 0; // Done executing
1411 }
1412 }
1413 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001414 }
thatc6085482015-01-09 22:12:43 +01001415 DataManager::SetValue("tw_operation_status", 0);
1416 DataManager::SetValue("tw_operation_state", 1);
1417 DataManager::SetValue("tw_terminal_state", 0);
1418 DataManager::SetValue("tw_background_thread_running", 0);
1419 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001420 }
1421 return 0;
1422}
1423
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001424int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001425{
that3f7b1ac2014-12-30 11:30:13 +01001426 LOGINFO("Sending kill command...\n");
1427 operation_start("KillCommand");
1428 DataManager::SetValue("tw_operation_status", 0);
1429 DataManager::SetValue("tw_operation_state", 1);
1430 DataManager::SetValue("tw_terminal_state", 0);
1431 DataManager::SetValue("tw_background_thread_running", 0);
1432 DataManager::SetValue(TW_ACTION_BUSY, 0);
1433 return 0;
1434}
1435
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001436int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001437{
1438 int op_status = 0;
1439 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001440 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001441 if (simulate) {
1442 simulate_progress_bar();
1443 } else {
1444 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001445 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001446 }
1447
1448 operation_end(op_status);
1449 return 0;
1450}
1451
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001452int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001453{
1454 int op_status = 0;
1455
1456 operation_start("CheckBackupName");
1457 if (simulate) {
1458 simulate_progress_bar();
1459 } else {
1460 op_status = PartitionManager.Check_Backup_Name(true);
1461 if (op_status != 0)
1462 op_status = 1;
1463 }
1464
1465 operation_end(op_status);
1466 return 0;
1467}
1468
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001469int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001470{
1471 int op_status = 0;
1472
1473 operation_start("Decrypt");
1474 if (simulate) {
1475 simulate_progress_bar();
1476 } else {
1477 string Password;
1478 DataManager::GetValue("tw_crypto_password", Password);
1479 op_status = PartitionManager.Decrypt_Device(Password);
1480 if (op_status != 0)
1481 op_status = 1;
1482 else {
that3f7b1ac2014-12-30 11:30:13 +01001483
1484 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1485
Ethan Yonkercf50da52015-01-12 21:59:07 -06001486 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001487
Ethan Yonkercf50da52015-01-12 21:59:07 -06001488 // Check for a custom theme and load it if exists
1489 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1490 if (has_datamedia != 0) {
1491 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001492 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001493 } else {
1494 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001495 }
1496 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001497 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001498 }
1499 }
1500
1501 operation_end(op_status);
1502 return 0;
1503}
1504
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001505int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001506{
1507 operation_start("Sideload");
1508 if (simulate) {
1509 simulate_progress_bar();
1510 operation_end(0);
1511 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001512 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001513 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1514
1515 // wait for the adb connection
1516 int ret = apply_from_adb("/", &sideload_child_pid);
1517 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1518
1519 if (ret != 0) {
1520 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001521 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001522 ret = 1; // failure
1523 } else {
1524 int wipe_cache = 0;
1525 int wipe_dalvik = 0;
1526 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1527
1528 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1529 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1530 PartitionManager.Wipe_By_Path("/cache");
1531 if (wipe_dalvik)
1532 PartitionManager.Wipe_Dalvik_Cache();
1533 } else {
1534 ret = 1; // failure
1535 }
thatcc8ddca2015-01-03 01:59:36 +01001536 }
thatc6085482015-01-09 22:12:43 +01001537 if (sideload_child_pid) {
1538 LOGINFO("Signaling child sideload process to exit.\n");
1539 struct stat st;
1540 // Calling stat() on this magic filename signals the minadbd
1541 // subprocess to shut down.
1542 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1543 int status;
1544 LOGINFO("Waiting for child sideload process to exit.\n");
1545 waitpid(sideload_child_pid, &status, 0);
1546 }
Dees Troy28a85d22015-04-28 02:03:16 +00001547 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001548 TWFunc::Toggle_MTP(mtp_was_enabled);
1549 reinject_after_flash();
1550 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001551 }
that3f7b1ac2014-12-30 11:30:13 +01001552 return 0;
1553}
1554
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001555int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001556{
that3f7b1ac2014-12-30 11:30:13 +01001557 struct stat st;
1558 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001560 LOGINFO("Signaling child sideload process to exit.\n");
1561 // Calling stat() on this magic filename signals the minadbd
1562 // subprocess to shut down.
1563 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1564 if (!sideload_child_pid) {
1565 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001566 return 0;
1567 }
thatcc8ddca2015-01-03 01:59:36 +01001568 ::sleep(1);
1569 LOGINFO("Killing child sideload process.\n");
1570 kill(sideload_child_pid, SIGTERM);
1571 int status;
1572 LOGINFO("Waiting for child sideload process to exit.\n");
1573 waitpid(sideload_child_pid, &status, 0);
1574 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001575 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1576 return 0;
1577}
1578
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001579int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001580{
1581 operation_start("OpenRecoveryScript");
1582 if (simulate) {
1583 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001584 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001585 } else {
that10ae24f2015-12-26 20:53:51 +01001586 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001587 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001588 }
1589 return 0;
1590}
1591
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001592int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001593{
1594 int op_status = 0;
1595
1596 operation_start("Install SuperSU");
1597 if (simulate) {
1598 simulate_progress_bar();
1599 } else {
1600 if (!TWFunc::Install_SuperSU())
1601 op_status = 1;
1602 }
1603
1604 operation_end(op_status);
1605 return 0;
1606}
1607
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001608int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001609{
1610 int op_status = 0;
1611
1612 operation_start("Fixing Superuser Permissions");
1613 if (simulate) {
1614 simulate_progress_bar();
1615 } else {
1616 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1617 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1618 }
1619
1620 operation_end(op_status);
1621 return 0;
1622}
1623
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001624int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001625{
1626 int op_status = 0;
1627
1628 operation_start("Try Restore Decrypt");
1629 if (simulate) {
1630 simulate_progress_bar();
1631 } else {
1632 string Restore_Path, Filename, Password;
1633 DataManager::GetValue("tw_restore", Restore_Path);
1634 Restore_Path += "/";
1635 DataManager::GetValue("tw_restore_password", Password);
1636 TWFunc::SetPerformanceMode(true);
1637 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1638 op_status = 0; // success
1639 else
1640 op_status = 1; // fail
1641 TWFunc::SetPerformanceMode(false);
1642 }
1643
1644 operation_end(op_status);
1645 return 0;
1646}
1647
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001648int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001649{
1650 int op_status = 0;
1651
1652 operation_start("Repair Partition");
1653 if (simulate) {
1654 simulate_progress_bar();
1655 } else {
1656 string part_path;
1657 DataManager::GetValue("tw_partition_mount_point", part_path);
1658 if (PartitionManager.Repair_By_Path(part_path, true)) {
1659 op_status = 0; // success
1660 } else {
that3f7b1ac2014-12-30 11:30:13 +01001661 op_status = 1; // fail
1662 }
1663 }
1664
1665 operation_end(op_status);
1666 return 0;
1667}
1668
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001669int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001670{
1671 int op_status = 0;
1672
1673 operation_start("Resize Partition");
1674 if (simulate) {
1675 simulate_progress_bar();
1676 } else {
1677 string part_path;
1678 DataManager::GetValue("tw_partition_mount_point", part_path);
1679 if (PartitionManager.Resize_By_Path(part_path, true)) {
1680 op_status = 0; // success
1681 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001682 op_status = 1; // fail
1683 }
1684 }
1685
1686 operation_end(op_status);
1687 return 0;
1688}
1689
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001690int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001691{
1692 int op_status = 0;
1693
1694 operation_start("Change File System");
1695 if (simulate) {
1696 simulate_progress_bar();
1697 } else {
1698 string part_path, file_system;
1699 DataManager::GetValue("tw_partition_mount_point", part_path);
1700 DataManager::GetValue("tw_action_new_file_system", file_system);
1701 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1702 op_status = 0; // success
1703 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001704 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001705 op_status = 1; // fail
1706 }
1707 }
1708 PartitionManager.Update_System_Details();
1709 operation_end(op_status);
1710 return 0;
1711}
1712
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001713int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001714{
1715 int op_status = 0;
1716
1717 operation_start("Start MTP");
1718 if (PartitionManager.Enable_MTP())
1719 op_status = 0; // success
1720 else
1721 op_status = 1; // fail
1722
1723 operation_end(op_status);
1724 return 0;
1725}
1726
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001727int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001728{
1729 int op_status = 0;
1730
1731 operation_start("Stop MTP");
1732 if (PartitionManager.Disable_MTP())
1733 op_status = 0; // success
1734 else
1735 op_status = 1; // fail
1736
1737 operation_end(op_status);
1738 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001739}
1740
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001741int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001742{
1743 int op_status = 0;
1744
1745 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001746 string path, filename;
1747 DataManager::GetValue("tw_zip_location", path);
1748 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001749 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001750 op_status = 0; // success
1751 else
1752 op_status = 1; // fail
1753
1754 operation_end(op_status);
1755 return 0;
1756}
1757
that10ae24f2015-12-26 20:53:51 +01001758int GUIAction::twcmd(std::string arg)
1759{
1760 operation_start("TWRP CLI Command");
1761 if (simulate)
1762 simulate_progress_bar();
1763 else
1764 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1765 operation_end(0);
1766 return 0;
1767}
1768
Dees_Troy51a0e822012-09-05 15:24:24 -04001769int GUIAction::getKeyByName(std::string key)
1770{
that8834a0f2016-01-05 23:29:30 +01001771 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001772 else if (key == "menu") return KEY_MENU;
1773 else if (key == "back") return KEY_BACK;
1774 else if (key == "search") return KEY_SEARCH;
1775 else if (key == "voldown") return KEY_VOLUMEDOWN;
1776 else if (key == "volup") return KEY_VOLUMEUP;
1777 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001778 int ret_val;
1779 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1780 if (!ret_val)
1781 return KEY_POWER;
1782 else
1783 return ret_val;
1784 }
1785
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001786 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001787}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001788
1789int GUIAction::checkpartitionlifetimewrites(std::string arg)
1790{
1791 int op_status = 0;
1792 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1793
1794 operation_start("Check Partition Lifetime Writes");
1795 if (sys) {
1796 if (sys->Check_Lifetime_Writes() != 0)
1797 DataManager::SetValue("tw_lifetime_writes", 1);
1798 else
1799 DataManager::SetValue("tw_lifetime_writes", 0);
1800 op_status = 0; // success
1801 } else {
1802 DataManager::SetValue("tw_lifetime_writes", 1);
1803 op_status = 1; // fail
1804 }
1805
1806 operation_end(op_status);
1807 return 0;
1808}
1809
1810int GUIAction::mountsystemtoggle(std::string arg)
1811{
1812 int op_status = 0;
1813 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001814 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001815
1816 operation_start("Toggle System Mount");
1817 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1818 op_status = 1; // fail
1819 } else {
1820 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1821 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001822 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001823 DataManager::SetValue("tw_mount_system_ro", 0);
1824 Part->Change_Mount_Read_Only(false);
1825 } else {
1826 DataManager::SetValue("tw_mount_system_ro", 1);
1827 Part->Change_Mount_Read_Only(true);
1828 }
1829 if (remount_system) {
1830 Part->Mount(true);
1831 }
1832 op_status = 0; // success
1833 } else {
1834 op_status = 1; // fail
1835 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001836 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1837 if (Part) {
1838 if (arg == "0") {
1839 Part->Change_Mount_Read_Only(false);
1840 } else {
1841 Part->Change_Mount_Read_Only(true);
1842 }
1843 if (remount_vendor) {
1844 Part->Mount(true);
1845 }
1846 op_status = 0; // success
1847 } else {
1848 op_status = 1; // fail
1849 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001850 }
1851
1852 operation_end(op_status);
1853 return 0;
1854}
Ethan Yonker74db1572015-10-28 12:44:49 -05001855
1856int GUIAction::setlanguage(std::string arg __unused)
1857{
1858 int op_status = 0;
1859
1860 operation_start("Set Language");
1861 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1862 PageManager::RequestReload();
1863 op_status = 0; // success
1864
1865 operation_end(op_status);
1866 return 0;
1867}
Ethan Yonker1b190162016-12-05 15:25:19 -06001868
Matt Mower9472ba12016-01-20 18:12:47 -06001869int GUIAction::togglebacklight(std::string arg __unused)
1870{
1871 blankTimer.toggleBlank();
1872 return 0;
1873}
1874
Ethan Yonker1b190162016-12-05 15:25:19 -06001875int GUIAction::setbootslot(std::string arg)
1876{
1877 operation_start("Set Boot Slot");
1878 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001879 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001880 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001881 simulate_progress_bar();
1882 operation_end(0);
1883 return 0;
1884}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001885
1886int GUIAction::checkforapp(std::string arg __unused)
1887{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001888 operation_start("Check for TWRP App");
1889 if (!simulate)
1890 {
1891 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1892 int sdkver = 0;
1893 if (!sdkverstr.empty()) {
1894 sdkver = atoi(sdkverstr.c_str());
1895 }
1896 if (sdkver <= 13) {
1897 if (sdkver == 0)
1898 LOGINFO("Unable to read sdk version from build prop\n");
1899 else
1900 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
1901 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1902 goto exit;
1903 }
1904 if (PartitionManager.Mount_By_Path("/system", false)) {
1905 string base_path = "/system";
1906 if (TWFunc::Path_Exists("/system/system"))
1907 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1908 string install_path = base_path + "/priv-app";
1909 if (!TWFunc::Path_Exists(install_path))
1910 install_path = base_path + "/app";
1911 install_path += "/twrpapp";
1912 if (TWFunc::Path_Exists(install_path)) {
1913 LOGINFO("App found at '%s'\n", install_path.c_str());
1914 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1915 goto exit;
1916 }
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001917 }
1918 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001919 string parent_path = "/data/app";
1920 DIR *d = opendir("/data/app");
1921 struct dirent *p;
Ethan Yonker4767caf2017-01-11 10:45:04 -06001922 size_t len = strlen("me.twrp.twrpapp-");
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001923 while ((p = readdir(d))) {
1924 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
1925 continue;
1926 if (p->d_type == DT_DIR && strlen(p->d_name) >= len && strncmp(p->d_name, "me.twrp.twrpapp-", len) == 0) {
1927 LOGINFO("App found at %s/%s\n", parent_path.c_str(), p->d_name);
1928 closedir(d);
1929 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1930 goto exit;
1931 }
1932 }
1933 closedir(d);
1934 }
1935 } else
1936 simulate_progress_bar();
1937 LOGINFO("App not installed\n");
1938 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1939exit:
1940 operation_end(0);
1941 return 0;
1942}
1943
1944int GUIAction::installapp(std::string arg __unused)
1945{
1946 int op_status = 1;
1947 operation_start("Install TWRP App");
1948 if (!simulate)
1949 {
1950 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1951 if (PartitionManager.Mount_By_Path("/data", true)) {
1952 string install_path = "/data/app";
1953 string context = "u:object_r:apk_data_file:s0";
1954 if (!TWFunc::Path_Exists(install_path)) {
1955 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1956 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1957 goto exit;
1958 }
1959 if (chown(install_path.c_str(), 1000, 1000)) {
1960 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1961 goto exit;
1962 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001963 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001964 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1965 goto exit;
1966 }
1967 }
1968 install_path += "/me.twrp.twrpapp-1";
1969 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1970 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1971 goto exit;
1972 }
1973 if (chown(install_path.c_str(), 1000, 1000)) {
1974 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1975 goto exit;
1976 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001977 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001978 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1979 goto exit;
1980 }
1981 install_path += "/base.apk";
1982 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1983 LOGERR("Error copying apk file\n");
1984 goto exit;
1985 }
1986 if (chown(install_path.c_str(), 1000, 1000)) {
1987 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1988 goto exit;
1989 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001990 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001991 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1992 goto exit;
1993 }
1994 sync();
1995 sync();
1996 }
1997 } else {
1998 if (PartitionManager.Mount_By_Path("/system", true)) {
1999 string base_path = "/system";
2000 if (TWFunc::Path_Exists("/system/system"))
2001 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2002 string install_path = base_path + "/priv-app";
2003 string context = "u:object_r:system_file:s0";
2004 if (!TWFunc::Path_Exists(install_path))
2005 install_path = base_path + "/app";
2006 if (TWFunc::Path_Exists(install_path)) {
2007 install_path += "/twrpapp";
2008 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2009 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002010 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002011 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2012 goto exit;
2013 }
2014 install_path += "/me.twrp.twrpapp.apk";
2015 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2016 LOGERR("Error copying apk file\n");
2017 goto exit;
2018 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002019 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002020 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2021 goto exit;
2022 }
2023 sync();
2024 sync();
2025 PartitionManager.UnMount_By_Path("/system", true);
2026 op_status = 0;
2027 } else {
2028 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2029 }
2030 }
2031 }
2032 }
2033 } else
2034 simulate_progress_bar();
2035exit:
2036 operation_end(0);
2037 return 0;
2038}