blob: 8600186ee31faff8a4524beb85aa0a3c832a4527 [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;
65static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010067
that73a52952015-01-28 23:07:34 +010068static void *ActionThread_work_wrapper(void *data);
69
70class ActionThread
71{
72public:
73 ActionThread();
74 ~ActionThread();
75
76 void threadActions(GUIAction *act);
77 void run(void *data);
78private:
79 friend void *ActionThread_work_wrapper(void*);
80 struct ThreadData
81 {
82 ActionThread *this_;
83 GUIAction *act;
84 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
85 };
86
87 pthread_t m_thread;
88 bool m_thread_running;
89 pthread_mutex_t m_act_lock;
90};
91
92static ActionThread action_thread; // for all kinds of longer running actions
93static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010094
95static void *ActionThread_work_wrapper(void *data)
96{
that73a52952015-01-28 23:07:34 +010097 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010098 return NULL;
99}
100
101ActionThread::ActionThread()
102{
103 m_thread_running = false;
104 pthread_mutex_init(&m_act_lock, NULL);
105}
106
107ActionThread::~ActionThread()
108{
109 pthread_mutex_lock(&m_act_lock);
110 if(m_thread_running) {
111 pthread_mutex_unlock(&m_act_lock);
112 pthread_join(m_thread, NULL);
113 } else {
114 pthread_mutex_unlock(&m_act_lock);
115 }
116 pthread_mutex_destroy(&m_act_lock);
117}
118
that7d3b54f2015-01-09 22:52:51 +0100119void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100120{
121 pthread_mutex_lock(&m_act_lock);
122 if (m_thread_running) {
123 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100124 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
125 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100126 } else {
127 m_thread_running = true;
128 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100129 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100130 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
131 }
132}
133
134void ActionThread::run(void *data)
135{
136 ThreadData *d = (ThreadData*)data;
137 GUIAction* act = d->act;
138
139 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100140 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100141 act->doAction(*it);
142
143 pthread_mutex_lock(&m_act_lock);
144 m_thread_running = false;
145 pthread_mutex_unlock(&m_act_lock);
146 delete d;
147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 xml_node<>* child;
153 xml_node<>* actions;
154 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
that3f7b1ac2014-12-30 11:30:13 +0100158 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100159#define ADD_ACTION(n) mf[#n] = &GUIAction::n
160#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100161 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100162 ADD_ACTION(reboot);
163 ADD_ACTION(home);
164 ADD_ACTION(key);
165 ADD_ACTION(page);
166 ADD_ACTION(reload);
167 ADD_ACTION(readBackup);
168 ADD_ACTION(set);
169 ADD_ACTION(clear);
170 ADD_ACTION(mount);
171 ADD_ACTION(unmount);
172 ADD_ACTION_EX("umount", unmount);
173 ADD_ACTION(restoredefaultsettings);
174 ADD_ACTION(copylog);
175 ADD_ACTION(compute);
176 ADD_ACTION_EX("addsubtract", compute);
177 ADD_ACTION(setguitimezone);
178 ADD_ACTION(overlay);
179 ADD_ACTION(queuezip);
180 ADD_ACTION(cancelzip);
181 ADD_ACTION(queueclear);
182 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500183 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
thatc6085482015-01-09 22:12:43 +0100201
202 // remember actions that run in the caller thread
203 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
204 setActionsRunningInCallerThread.insert(it->first);
205
206 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100207 ADD_ACTION(flash);
208 ADD_ACTION(wipe);
209 ADD_ACTION(refreshsizes);
210 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600211 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100212 ADD_ACTION(fixpermissions);
213 ADD_ACTION(dd);
214 ADD_ACTION(partitionsd);
215 ADD_ACTION(installhtcdumlock);
216 ADD_ACTION(htcdumlockrestoreboot);
217 ADD_ACTION(htcdumlockreflashrecovery);
218 ADD_ACTION(cmd);
219 ADD_ACTION(terminalcommand);
220 ADD_ACTION(reinjecttwrp);
221 ADD_ACTION(decrypt);
222 ADD_ACTION(adbsideload);
223 ADD_ACTION(openrecoveryscript);
224 ADD_ACTION(installsu);
225 ADD_ACTION(decrypt_backup);
226 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500227 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100228 ADD_ACTION(changefilesystem);
229 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100230 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600231 ADD_ACTION(setbootslot);
that3f7b1ac2014-12-30 11:30:13 +0100232 }
233
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600235 actions = FindNode(node, "actions");
236 if (actions) child = FindNode(actions, "action");
237 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 while (child)
242 {
243 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 attr = child->first_attribute("function");
246 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 action.mFunction = attr->value();
249 action.mArg = child->value();
250 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 child = child->next_sibling("action");
253 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600256 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 if (child)
258 {
259 attr = child->first_attribute("key");
260 if (attr)
261 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100262 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
263 for(size_t i = 0; i < keys.size(); ++i)
264 {
265 const int key = getKeyByName(keys[i]);
266 mKeys[key] = false;
267 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 }
269 else
270 {
271 attr = child->first_attribute("x");
272 if (!attr) return;
273 mActionX = atol(attr->value());
274 attr = child->first_attribute("y");
275 if (!attr) return;
276 mActionY = atol(attr->value());
277 attr = child->first_attribute("w");
278 if (!attr) return;
279 mActionW = atol(attr->value());
280 attr = child->first_attribute("h");
281 if (!attr) return;
282 mActionH = atol(attr->value());
283 }
284 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400285}
286
Matt Mower25036b72016-06-24 12:30:18 -0500287int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400288{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 if (state == TOUCH_RELEASE)
290 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400291
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293}
294
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100295int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400296{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100297 std::map<int, bool>::iterator itr = mKeys.find(key);
298 if(itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100299 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100300
301 bool prevState = itr->second;
302 itr->second = down;
303
304 // If there is only one key for this action, wait for key up so it
305 // doesn't trigger with multi-key actions.
306 // Else, check if all buttons are pressed, then consume their release events
307 // so they don't trigger one-button actions and reset mKeys pressed status
308 if(mKeys.size() == 1) {
thatd4725ca2016-01-19 00:15:21 +0100309 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310 doActions();
thatd4725ca2016-01-19 00:15:21 +0100311 return 0;
312 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313 } else if(down) {
314 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
315 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100316 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100317 }
318
319 // Passed, all req buttons are pressed, reset them and consume release events
320 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
321 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
322 kb->ConsumeKeyRelease(itr->first);
323 itr->second = false;
324 }
325
326 doActions();
thatd4725ca2016-01-19 00:15:21 +0100327 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100328 }
329
thatd4725ca2016-01-19 00:15:21 +0100330 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400331}
332
Vojtech Bocek07220562014-02-08 02:05:33 +0100333int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400334{
Vojtech Bocek07220562014-02-08 02:05:33 +0100335 GUIObject::NotifyVarChange(varName, value);
336
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100337 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100339 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400343}
344
345void GUIAction::simulate_progress_bar(void)
346{
Ethan Yonker74db1572015-10-28 12:44:49 -0500347 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400348 for (int i = 0; i < 5; i++)
349 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500350 if (PartitionManager.stop_backup.get_value()) {
351 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600352 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500353 DataManager::SetValue("ui_progress", 0);
354 PartitionManager.stop_backup.set_value(0);
355 return;
356 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 usleep(500000);
358 DataManager::SetValue("ui_progress", i * 20);
359 }
360}
361
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600362int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400363{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
366 DataManager::SetValue("ui_progress", 0);
367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 if (filename.empty())
369 {
370 LOGERR("No file specified.\n");
371 return -1;
372 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400373
Ethan Yonker9598c072016-01-15 21:54:34 -0600374 if (!TWFunc::Path_Exists(filename)) {
375 if (!PartitionManager.Mount_By_Path(filename, true)) {
376 return -1;
377 }
378 if (!TWFunc::Path_Exists(filename)) {
379 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
380 return -1;
381 }
382 }
Dees_Troy657c3092012-09-10 20:32:10 -0400383
Dees_Troy51a0e822012-09-05 15:24:24 -0400384 if (simulate) {
385 simulate_progress_bar();
386 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400387 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400388
389 // Now, check if we need to ensure TWRP remains installed...
390 struct stat st;
391 if (stat("/sbin/installTwrp", &st) == 0)
392 {
393 DataManager::SetValue("tw_operation", "Configuring TWRP");
394 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500395 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200396 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500398 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 }
400 }
401 }
402
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 // Done
404 DataManager::SetValue("ui_progress", 100);
405 DataManager::SetValue("ui_progress", 0);
406 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400407}
408
that73a52952015-01-28 23:07:34 +0100409GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100410{
that73a52952015-01-28 23:07:34 +0100411 string func = gui_parse_text(action.mFunction);
412 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
413 if (needsThread) {
414 if (func == "cancelbackup")
415 return THREAD_CANCEL;
416 else
417 return THREAD_ACTION;
418 }
419 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100420}
421
Dees_Troy51a0e822012-09-05 15:24:24 -0400422int GUIAction::doActions()
423{
that3f7b1ac2014-12-30 11:30:13 +0100424 if (mActions.size() < 1)
425 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426
that73a52952015-01-28 23:07:34 +0100427 // Determine in which thread to run the actions.
428 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
429 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100430 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100431 for (it = mActions.begin(); it != mActions.end(); ++it) {
432 ThreadType tt = getThreadType(*it);
433 if (tt == THREAD_NONE)
434 continue;
435 if (threadType == THREAD_NONE)
436 threadType = tt;
437 else if (threadType != tt) {
438 LOGERR("Can't mix normal and cancel actions in the same list.\n"
439 "Running the whole batch in the cancel thread.\n");
440 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100441 break;
442 }
that7d3b54f2015-01-09 22:52:51 +0100443 }
that73a52952015-01-28 23:07:34 +0100444
445 // Now run the actions in the desired thread.
446 switch (threadType) {
447 case THREAD_ACTION:
448 action_thread.threadActions(this);
449 break;
450
451 case THREAD_CANCEL:
452 cancel_thread.threadActions(this);
453 break;
454
455 default: {
456 // no iterators here because theme reloading might kill our object
457 const size_t cnt = mActions.size();
458 for (size_t i = 0; i < cnt; ++i)
459 doAction(mActions[i]);
460 }
thatc6085482015-01-09 22:12:43 +0100461 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400462
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200463 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400464}
465
that3f7b1ac2014-12-30 11:30:13 +0100466int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400467{
that3f7b1ac2014-12-30 11:30:13 +0100468 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400469
that3f7b1ac2014-12-30 11:30:13 +0100470 std::string function = gui_parse_text(action.mFunction);
471 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400472
that3f7b1ac2014-12-30 11:30:13 +0100473 // find function and execute it
474 mapFunc::const_iterator funcitr = mf.find(function);
475 if (funcitr != mf.end())
476 return (this->*funcitr->second)(arg);
477
478 LOGERR("Unknown action '%s'\n", function.c_str());
479 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
482void GUIAction::operation_start(const string operation_name)
483{
that3f7b1ac2014-12-30 11:30:13 +0100484 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000485 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 DataManager::SetValue(TW_ACTION_BUSY, 1);
487 DataManager::SetValue("ui_progress", 0);
488 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600490 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491}
492
that3f7b1ac2014-12-30 11:30:13 +0100493void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400494{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000495 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue("ui_progress", 100);
498 if (simulate) {
499 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
500 if (simulate_fail != 0)
501 DataManager::SetValue("tw_operation_status", 1);
502 else
503 DataManager::SetValue("tw_operation_status", 0);
504 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500505 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 }
508 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500510 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 }
512 DataManager::SetValue("tw_operation_state", 1);
513 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500514 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200515 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000516 time(&Stop);
517 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600518 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100519 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400520}
521
that3f7b1ac2014-12-30 11:30:13 +0100522int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400523{
that3f7b1ac2014-12-30 11:30:13 +0100524 sync();
525 DataManager::SetValue("tw_gui_done", 1);
526 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400527
that3f7b1ac2014-12-30 11:30:13 +0100528 return 0;
529}
Dees_Troy51a0e822012-09-05 15:24:24 -0400530
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500531int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100532{
that3f7b1ac2014-12-30 11:30:13 +0100533 gui_changePage("main");
534 return 0;
535}
Dees_Troy51a0e822012-09-05 15:24:24 -0400536
that3f7b1ac2014-12-30 11:30:13 +0100537int GUIAction::key(std::string arg)
538{
539 const int key = getKeyByName(arg);
540 PageManager::NotifyKey(key, true);
541 PageManager::NotifyKey(key, false);
542 return 0;
543}
544
545int GUIAction::page(std::string arg)
546{
LuK133762326f42015-09-30 19:57:46 +0200547 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100548 std::string page_name = gui_parse_text(arg);
549 return gui_changePage(page_name);
550}
551
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500552int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100553{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500554 PageManager::RequestReload();
555 // The actual reload is handled in pages.cpp in PageManager::RunReload()
556 // The reload will occur on the next Update or Render call and will
557 // be performed in the rendoer thread instead of the action thread
558 // to prevent crashing which could occur when we start deleting
559 // GUI resources in the action thread while we attempt to render
560 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100561 return 0;
562}
Dees_Troy51a0e822012-09-05 15:24:24 -0400563
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500564int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100565{
566 string Restore_Name;
567 DataManager::GetValue("tw_restore", Restore_Name);
568 PartitionManager.Set_Restore_Files(Restore_Name);
569 return 0;
570}
571
572int GUIAction::set(std::string arg)
573{
574 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200575 {
that3f7b1ac2014-12-30 11:30:13 +0100576 string varName = arg.substr(0, arg.find('='));
577 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400578
that3f7b1ac2014-12-30 11:30:13 +0100579 DataManager::GetValue(value, value);
580 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200581 }
that3f7b1ac2014-12-30 11:30:13 +0100582 else
583 DataManager::SetValue(arg, "1");
584 return 0;
585}
Dees_Troy51a0e822012-09-05 15:24:24 -0400586
that3f7b1ac2014-12-30 11:30:13 +0100587int GUIAction::clear(std::string arg)
588{
589 DataManager::SetValue(arg, "0");
590 return 0;
591}
Dees_Troy51a0e822012-09-05 15:24:24 -0400592
that3f7b1ac2014-12-30 11:30:13 +0100593int GUIAction::mount(std::string arg)
594{
595 if (arg == "usb") {
596 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100598 PartitionManager.usb_storage_enable();
599 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500600 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100601 } else if (!simulate) {
602 PartitionManager.Mount_By_Path(arg, true);
603 PartitionManager.Add_MTP_Storage(arg);
604 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500605 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100606 return 0;
607}
608
609int GUIAction::unmount(std::string arg)
610{
611 if (arg == "usb") {
612 if (!simulate)
613 PartitionManager.usb_storage_disable();
614 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500615 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100616 DataManager::SetValue(TW_ACTION_BUSY, 0);
617 } else if (!simulate) {
618 PartitionManager.UnMount_By_Path(arg, true);
619 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500620 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100621 return 0;
622}
623
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500624int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100625{
626 operation_start("Restore Defaults");
627 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500628 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100629 else {
630 DataManager::ResetDefaults();
631 PartitionManager.Update_System_Details();
632 PartitionManager.Mount_Current_Storage(true);
633 }
634 operation_end(0);
635 return 0;
636}
637
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500638int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100639{
640 operation_start("Copy Log");
641 if (!simulate)
642 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400643 string dst, curr_storage;
644 int copy_kernel_log = 0;
645
646 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100647 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400648 curr_storage = DataManager::GetCurrentStoragePath();
649 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100650 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
651 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400652 if (copy_kernel_log)
653 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100654 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400655 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100656 } else
657 simulate_progress_bar();
658 operation_end(0);
659 return 0;
660}
661
662
663int GUIAction::compute(std::string arg)
664{
665 if (arg.find("+") != string::npos)
666 {
667 string varName = arg.substr(0, arg.find('+'));
668 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
669 int amount_to_add = atoi(string_to_add.c_str());
670 int value;
671
672 DataManager::GetValue(varName, value);
673 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 return 0;
675 }
that3f7b1ac2014-12-30 11:30:13 +0100676 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 {
that3f7b1ac2014-12-30 11:30:13 +0100678 string varName = arg.substr(0, arg.find('-'));
679 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
680 int amount_to_subtract = atoi(string_to_subtract.c_str());
681 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400682
that3f7b1ac2014-12-30 11:30:13 +0100683 DataManager::GetValue(varName, value);
684 value -= amount_to_subtract;
685 if (value <= 0)
686 value = 0;
687 DataManager::SetValue(varName, value);
688 return 0;
689 }
690 if (arg.find("*") != string::npos)
691 {
692 string varName = arg.substr(0, arg.find('*'));
693 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
694 int multiply_by = atoi(multiply_by_str.c_str());
695 int value;
696
697 DataManager::GetValue(varName, value);
698 DataManager::SetValue(varName, value*multiply_by);
699 return 0;
700 }
701 if (arg.find("/") != string::npos)
702 {
703 string varName = arg.substr(0, arg.find('/'));
704 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
705 int divide_by = atoi(divide_by_str.c_str());
706 int value;
707
708 if(divide_by != 0)
709 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100711 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 }
713 return 0;
714 }
that3f7b1ac2014-12-30 11:30:13 +0100715 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
716 return -1;
717}
Dees_Troy51a0e822012-09-05 15:24:24 -0400718
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500719int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100720{
721 string SelectedZone;
722 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
723 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
724 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
725
726 int dst;
727 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
728
729 string offset;
730 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
731
732 string NewTimeZone = Zone;
733 if (offset != "0")
734 NewTimeZone += ":" + offset;
735
736 if (dst != 0)
737 NewTimeZone += DSTZone;
738
739 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
740 DataManager::update_tz_environment_variables();
741 return 0;
742}
743
744int GUIAction::overlay(std::string arg)
745{
746 return gui_changeOverlay(arg);
747}
748
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500749int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100750{
751 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500752 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400753 return 0;
754 }
that3f7b1ac2014-12-30 11:30:13 +0100755 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
756 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
757 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100759 }
760 return 0;
761}
762
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500763int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100764{
765 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500766 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100768 } else {
769 zip_queue_index--;
770 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 }
that3f7b1ac2014-12-30 11:30:13 +0100772 return 0;
773}
Dees_Troy51a0e822012-09-05 15:24:24 -0400774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500775int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100776{
777 zip_queue_index = 0;
778 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
779 return 0;
780}
Dees_Troy51a0e822012-09-05 15:24:24 -0400781
that3f7b1ac2014-12-30 11:30:13 +0100782int GUIAction::sleep(std::string arg)
783{
784 operation_start("Sleep");
785 usleep(atoi(arg.c_str()));
786 operation_end(0);
787 return 0;
788}
Dees Troyb21cc642013-09-10 17:36:41 +0000789
Matt Mower9a2a2052016-05-31 21:31:22 -0500790int GUIAction::sleepcounter(std::string arg)
791{
792 operation_start("SleepCounter");
793 // Ensure user notices countdown in case it needs to be cancelled
794 blankTimer.resetTimerAndUnblank();
795 int total = atoi(arg.c_str());
796 for (int t = total; t > 0; t--) {
797 int progress = (int)(((float)(total-t)/(float)total)*100.0);
798 DataManager::SetValue("ui_progress", progress);
799 ::sleep(1);
800 DataManager::SetValue("tw_sleep", t-1);
801 }
802 DataManager::SetValue("ui_progress", 100);
803 operation_end(0);
804 return 0;
805}
806
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500807int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100808{
809 operation_start("AppendDateToBackupName");
810 string Backup_Name;
811 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
812 Backup_Name += TWFunc::Get_Current_Date();
813 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
814 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
815 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500816 PageManager::NotifyKey(KEY_END, true);
817 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100818 operation_end(0);
819 return 0;
820}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500821
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500822int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100823{
824 operation_start("GenerateBackupName");
825 TWFunc::Auto_Generate_Backup_Name();
826 operation_end(0);
827 return 0;
828}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500829
Ethan Yonker483e9f42016-01-11 22:21:18 -0600830int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100831{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600832 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100833 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500834
Ethan Yonker483e9f42016-01-11 22:21:18 -0600835 if (arg.empty())
836 arg = "tw_wipe_list";
837 DataManager::GetValue(arg, List);
838 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
839 if (!List.empty()) {
840 size_t start_pos = 0, end_pos = List.find(";", start_pos);
841 while (end_pos != string::npos && start_pos < List.size()) {
842 part_path = List.substr(start_pos, end_pos - start_pos);
843 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
844 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100845 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400846 } else {
that3f7b1ac2014-12-30 11:30:13 +0100847 count++;
848 }
849 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600850 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100851 }
852 DataManager::SetValue("tw_check_partition_list", count);
853 } else {
854 DataManager::SetValue("tw_check_partition_list", 0);
855 }
856 return 0;
857}
Dees_Troy51a0e822012-09-05 15:24:24 -0400858
Ethan Yonker483e9f42016-01-11 22:21:18 -0600859int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100860{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600861 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100862 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400863
Ethan Yonker483e9f42016-01-11 22:21:18 -0600864 if (arg.empty())
865 arg = "tw_wipe_list";
866 DataManager::GetValue(arg, List);
867 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
868 if (!List.empty()) {
869 size_t start_pos = 0, end_pos = List.find(";", start_pos);
870 part_path = List;
871 while (end_pos != string::npos && start_pos < List.size()) {
872 part_path = List.substr(start_pos, end_pos - start_pos);
873 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
874 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100875 // Do nothing
876 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600877 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100878 break;
879 }
880 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100882 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 if (!part_path.empty()) {
884 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100885 if (Part) {
886 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500887
that3f7b1ac2014-12-30 11:30:13 +0100888 DataManager::SetValue("tw_partition_name", Part->Display_Name);
889 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
890 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
891 DataManager::SetValue("tw_partition_size", Part->Size / mb);
892 DataManager::SetValue("tw_partition_used", Part->Used / mb);
893 DataManager::SetValue("tw_partition_free", Part->Free / mb);
894 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
895 DataManager::SetValue("tw_partition_removable", Part->Removable);
896 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
897
898 if (Part->Can_Repair())
899 DataManager::SetValue("tw_partition_can_repair", 1);
900 else
901 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500902 if (Part->Can_Resize())
903 DataManager::SetValue("tw_partition_can_resize", 1);
904 else
905 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600906 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100907 DataManager::SetValue("tw_partition_vfat", 1);
908 else
909 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600910 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100911 DataManager::SetValue("tw_partition_exfat", 1);
912 else
913 DataManager::SetValue("tw_partition_exfat", 0);
914 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
915 DataManager::SetValue("tw_partition_f2fs", 1);
916 else
917 DataManager::SetValue("tw_partition_f2fs", 0);
918 if (TWFunc::Path_Exists("/sbin/mke2fs"))
919 DataManager::SetValue("tw_partition_ext", 1);
920 else
921 DataManager::SetValue("tw_partition_ext", 0);
922 return 0;
923 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600924 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100925 }
926 }
927 }
928 DataManager::SetValue("tw_partition_name", "");
929 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600930 // Set this to 0 to prevent trying to partition this device, just in case
931 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100932 return 0;
933}
934
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500935int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100936{
937 time_t tm;
938 char path[256];
939 int path_len;
940 uid_t uid = -1;
941 gid_t gid = -1;
942
943 struct passwd *pwd = getpwnam("media_rw");
944 if(pwd) {
945 uid = pwd->pw_uid;
946 gid = pwd->pw_gid;
947 }
948
949 const std::string storage = DataManager::GetCurrentStoragePath();
950 if(PartitionManager.Is_Mounted_By_Path(storage)) {
951 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
952 } else {
953 strcpy(path, "/tmp/");
954 }
955
Xuefer579e9072016-01-22 13:35:50 +0800956 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100957 return 0;
958
959 tm = time(NULL);
960 path_len = strlen(path);
961
962 // Screenshot_2014-01-01-18-21-38.png
963 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
964
965 int res = gr_save_screenshot(path);
966 if(res == 0) {
967 chmod(path, 0666);
968 chown(path, uid, gid);
969
that677b13f2015-12-26 23:57:31 +0100970 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100971
972 // blink to notify that the screenshow was taken
973 gr_color(255, 255, 255, 255);
974 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
975 gr_flip();
976 gui_forceRender();
977 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500978 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100979 }
980 return 0;
981}
982
983int GUIAction::setbrightness(std::string arg)
984{
985 return TWFunc::Set_Brightness(arg);
986}
987
988int GUIAction::fileexists(std::string arg)
989{
990 struct stat st;
991 string newpath = arg + "/.";
992
993 operation_start("FileExists");
994 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
995 operation_end(0);
996 else
997 operation_end(1);
998 return 0;
999}
1000
thatcc8ddca2015-01-03 01:59:36 +01001001void GUIAction::reinject_after_flash()
1002{
1003 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001004 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001005 if (simulate) {
1006 simulate_progress_bar();
1007 } else {
1008 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1009 if (Boot == NULL || Boot->Current_File_System != "emmc")
1010 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1011 else {
1012 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1013 TWFunc::Exec_Cmd(injectcmd);
1014 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001015 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001016 }
1017 }
1018}
1019
that3f7b1ac2014-12-30 11:30:13 +01001020int GUIAction::flash(std::string arg)
1021{
1022 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001023 // We're going to jump to this page first, like a loading page
1024 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001025 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001026 string zip_path = zip_queue[i];
1027 size_t slashpos = zip_path.find_last_of('/');
1028 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001029 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001030 DataManager::SetValue("tw_filename", zip_path);
1031 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001032 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1033
1034 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001035 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001036 TWFunc::SetPerformanceMode(false);
1037 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001038 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001039 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001040 break;
that3f7b1ac2014-12-30 11:30:13 +01001041 }
1042 }
1043 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001044
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001045 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001046 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001047 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001048 }
that3f7b1ac2014-12-30 11:30:13 +01001049
thatcc8ddca2015-01-03 01:59:36 +01001050 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001051 PartitionManager.Update_System_Details();
1052 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001053 // This needs to be after the operation_end call so we change pages before we change variables that we display on the screen
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001054 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001055 return 0;
1056}
1057
1058int GUIAction::wipe(std::string arg)
1059{
1060 operation_start("Format");
1061 DataManager::SetValue("tw_partition", arg);
1062
1063 int ret_val = false;
1064
1065 if (simulate) {
1066 simulate_progress_bar();
1067 } else {
1068 if (arg == "data")
1069 ret_val = PartitionManager.Factory_Reset();
1070 else if (arg == "battery")
1071 ret_val = PartitionManager.Wipe_Battery_Stats();
1072 else if (arg == "rotate")
1073 ret_val = PartitionManager.Wipe_Rotate_Data();
1074 else if (arg == "dalvik")
1075 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1076 else if (arg == "DATAMEDIA") {
1077 ret_val = PartitionManager.Format_Data();
1078 } else if (arg == "INTERNAL") {
1079 int has_datamedia, dual_storage;
1080
1081 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1082 if (has_datamedia) {
1083 ret_val = PartitionManager.Wipe_Media_From_Data();
1084 } else {
1085 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1086 }
1087 } else if (arg == "EXTERNAL") {
1088 string External_Path;
1089
1090 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1091 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1092 } else if (arg == "ANDROIDSECURE") {
1093 ret_val = PartitionManager.Wipe_Android_Secure();
1094 } else if (arg == "LIST") {
1095 string Wipe_List, wipe_path;
1096 bool skip = false;
1097 ret_val = true;
1098 TWPartition* wipe_part = NULL;
1099
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 {
1385 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1386 struct timeval timeout;
1387 fd_set fdset;
1388
1389 while(keep_going)
1390 {
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
xiaolue738da52015-02-22 20:49:35 +08001407 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{
1426 int op_status = 0;
1427
1428 LOGINFO("Sending kill command...\n");
1429 operation_start("KillCommand");
1430 DataManager::SetValue("tw_operation_status", 0);
1431 DataManager::SetValue("tw_operation_state", 1);
1432 DataManager::SetValue("tw_terminal_state", 0);
1433 DataManager::SetValue("tw_background_thread_running", 0);
1434 DataManager::SetValue(TW_ACTION_BUSY, 0);
1435 return 0;
1436}
1437
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001438int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001439{
1440 int op_status = 0;
1441 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001442 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001443 if (simulate) {
1444 simulate_progress_bar();
1445 } else {
1446 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001447 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001448 }
1449
1450 operation_end(op_status);
1451 return 0;
1452}
1453
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001454int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001455{
1456 int op_status = 0;
1457
1458 operation_start("CheckBackupName");
1459 if (simulate) {
1460 simulate_progress_bar();
1461 } else {
1462 op_status = PartitionManager.Check_Backup_Name(true);
1463 if (op_status != 0)
1464 op_status = 1;
1465 }
1466
1467 operation_end(op_status);
1468 return 0;
1469}
1470
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001471int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001472{
1473 int op_status = 0;
1474
1475 operation_start("Decrypt");
1476 if (simulate) {
1477 simulate_progress_bar();
1478 } else {
1479 string Password;
1480 DataManager::GetValue("tw_crypto_password", Password);
1481 op_status = PartitionManager.Decrypt_Device(Password);
1482 if (op_status != 0)
1483 op_status = 1;
1484 else {
that3f7b1ac2014-12-30 11:30:13 +01001485
1486 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1487
Ethan Yonkercf50da52015-01-12 21:59:07 -06001488 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001489
Ethan Yonkercf50da52015-01-12 21:59:07 -06001490 // Check for a custom theme and load it if exists
1491 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1492 if (has_datamedia != 0) {
1493 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001494 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001495 } else {
1496 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001497 }
1498 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001499 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001500 }
1501 }
1502
1503 operation_end(op_status);
1504 return 0;
1505}
1506
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001507int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001508{
1509 operation_start("Sideload");
1510 if (simulate) {
1511 simulate_progress_bar();
1512 operation_end(0);
1513 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001514 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001515 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1516
1517 // wait for the adb connection
1518 int ret = apply_from_adb("/", &sideload_child_pid);
1519 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1520
1521 if (ret != 0) {
1522 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001523 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001524 ret = 1; // failure
1525 } else {
1526 int wipe_cache = 0;
1527 int wipe_dalvik = 0;
1528 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1529
1530 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1531 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1532 PartitionManager.Wipe_By_Path("/cache");
1533 if (wipe_dalvik)
1534 PartitionManager.Wipe_Dalvik_Cache();
1535 } else {
1536 ret = 1; // failure
1537 }
thatcc8ddca2015-01-03 01:59:36 +01001538 }
thatc6085482015-01-09 22:12:43 +01001539 if (sideload_child_pid) {
1540 LOGINFO("Signaling child sideload process to exit.\n");
1541 struct stat st;
1542 // Calling stat() on this magic filename signals the minadbd
1543 // subprocess to shut down.
1544 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1545 int status;
1546 LOGINFO("Waiting for child sideload process to exit.\n");
1547 waitpid(sideload_child_pid, &status, 0);
1548 }
Dees Troy28a85d22015-04-28 02:03:16 +00001549 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001550 TWFunc::Toggle_MTP(mtp_was_enabled);
1551 reinject_after_flash();
1552 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001553 }
that3f7b1ac2014-12-30 11:30:13 +01001554 return 0;
1555}
1556
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001557int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001558{
that3f7b1ac2014-12-30 11:30:13 +01001559 struct stat st;
1560 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001562 LOGINFO("Signaling child sideload process to exit.\n");
1563 // Calling stat() on this magic filename signals the minadbd
1564 // subprocess to shut down.
1565 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1566 if (!sideload_child_pid) {
1567 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001568 return 0;
1569 }
thatcc8ddca2015-01-03 01:59:36 +01001570 ::sleep(1);
1571 LOGINFO("Killing child sideload process.\n");
1572 kill(sideload_child_pid, SIGTERM);
1573 int status;
1574 LOGINFO("Waiting for child sideload process to exit.\n");
1575 waitpid(sideload_child_pid, &status, 0);
1576 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001577 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1578 return 0;
1579}
1580
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001581int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001582{
1583 operation_start("OpenRecoveryScript");
1584 if (simulate) {
1585 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001586 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001587 } else {
that10ae24f2015-12-26 20:53:51 +01001588 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001589 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001590 }
1591 return 0;
1592}
1593
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001594int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001595{
1596 int op_status = 0;
1597
1598 operation_start("Install SuperSU");
1599 if (simulate) {
1600 simulate_progress_bar();
1601 } else {
1602 if (!TWFunc::Install_SuperSU())
1603 op_status = 1;
1604 }
1605
1606 operation_end(op_status);
1607 return 0;
1608}
1609
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001610int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001611{
1612 int op_status = 0;
1613
1614 operation_start("Fixing Superuser Permissions");
1615 if (simulate) {
1616 simulate_progress_bar();
1617 } else {
1618 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1619 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1620 }
1621
1622 operation_end(op_status);
1623 return 0;
1624}
1625
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001626int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001627{
1628 int op_status = 0;
1629
1630 operation_start("Try Restore Decrypt");
1631 if (simulate) {
1632 simulate_progress_bar();
1633 } else {
1634 string Restore_Path, Filename, Password;
1635 DataManager::GetValue("tw_restore", Restore_Path);
1636 Restore_Path += "/";
1637 DataManager::GetValue("tw_restore_password", Password);
1638 TWFunc::SetPerformanceMode(true);
1639 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1640 op_status = 0; // success
1641 else
1642 op_status = 1; // fail
1643 TWFunc::SetPerformanceMode(false);
1644 }
1645
1646 operation_end(op_status);
1647 return 0;
1648}
1649
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001650int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001651{
1652 int op_status = 0;
1653
1654 operation_start("Repair Partition");
1655 if (simulate) {
1656 simulate_progress_bar();
1657 } else {
1658 string part_path;
1659 DataManager::GetValue("tw_partition_mount_point", part_path);
1660 if (PartitionManager.Repair_By_Path(part_path, true)) {
1661 op_status = 0; // success
1662 } else {
that3f7b1ac2014-12-30 11:30:13 +01001663 op_status = 1; // fail
1664 }
1665 }
1666
1667 operation_end(op_status);
1668 return 0;
1669}
1670
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001671int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001672{
1673 int op_status = 0;
1674
1675 operation_start("Resize Partition");
1676 if (simulate) {
1677 simulate_progress_bar();
1678 } else {
1679 string part_path;
1680 DataManager::GetValue("tw_partition_mount_point", part_path);
1681 if (PartitionManager.Resize_By_Path(part_path, true)) {
1682 op_status = 0; // success
1683 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001684 op_status = 1; // fail
1685 }
1686 }
1687
1688 operation_end(op_status);
1689 return 0;
1690}
1691
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001692int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001693{
1694 int op_status = 0;
1695
1696 operation_start("Change File System");
1697 if (simulate) {
1698 simulate_progress_bar();
1699 } else {
1700 string part_path, file_system;
1701 DataManager::GetValue("tw_partition_mount_point", part_path);
1702 DataManager::GetValue("tw_action_new_file_system", file_system);
1703 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1704 op_status = 0; // success
1705 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001706 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001707 op_status = 1; // fail
1708 }
1709 }
1710 PartitionManager.Update_System_Details();
1711 operation_end(op_status);
1712 return 0;
1713}
1714
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001715int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001716{
1717 int op_status = 0;
1718
1719 operation_start("Start MTP");
1720 if (PartitionManager.Enable_MTP())
1721 op_status = 0; // success
1722 else
1723 op_status = 1; // fail
1724
1725 operation_end(op_status);
1726 return 0;
1727}
1728
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001729int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001730{
1731 int op_status = 0;
1732
1733 operation_start("Stop MTP");
1734 if (PartitionManager.Disable_MTP())
1735 op_status = 0; // success
1736 else
1737 op_status = 1; // fail
1738
1739 operation_end(op_status);
1740 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001741}
1742
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001743int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001744{
1745 int op_status = 0;
1746
1747 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001748 string path, filename;
1749 DataManager::GetValue("tw_zip_location", path);
1750 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001751 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001752 op_status = 0; // success
1753 else
1754 op_status = 1; // fail
1755
1756 operation_end(op_status);
1757 return 0;
1758}
1759
that10ae24f2015-12-26 20:53:51 +01001760int GUIAction::twcmd(std::string arg)
1761{
1762 operation_start("TWRP CLI Command");
1763 if (simulate)
1764 simulate_progress_bar();
1765 else
1766 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1767 operation_end(0);
1768 return 0;
1769}
1770
Dees_Troy51a0e822012-09-05 15:24:24 -04001771int GUIAction::getKeyByName(std::string key)
1772{
that8834a0f2016-01-05 23:29:30 +01001773 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001774 else if (key == "menu") return KEY_MENU;
1775 else if (key == "back") return KEY_BACK;
1776 else if (key == "search") return KEY_SEARCH;
1777 else if (key == "voldown") return KEY_VOLUMEDOWN;
1778 else if (key == "volup") return KEY_VOLUMEUP;
1779 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001780 int ret_val;
1781 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1782 if (!ret_val)
1783 return KEY_POWER;
1784 else
1785 return ret_val;
1786 }
1787
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001788 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001789}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001790
1791int GUIAction::checkpartitionlifetimewrites(std::string arg)
1792{
1793 int op_status = 0;
1794 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1795
1796 operation_start("Check Partition Lifetime Writes");
1797 if (sys) {
1798 if (sys->Check_Lifetime_Writes() != 0)
1799 DataManager::SetValue("tw_lifetime_writes", 1);
1800 else
1801 DataManager::SetValue("tw_lifetime_writes", 0);
1802 op_status = 0; // success
1803 } else {
1804 DataManager::SetValue("tw_lifetime_writes", 1);
1805 op_status = 1; // fail
1806 }
1807
1808 operation_end(op_status);
1809 return 0;
1810}
1811
1812int GUIAction::mountsystemtoggle(std::string arg)
1813{
1814 int op_status = 0;
1815 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001816 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001817
1818 operation_start("Toggle System Mount");
1819 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1820 op_status = 1; // fail
1821 } else {
1822 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1823 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001824 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001825 DataManager::SetValue("tw_mount_system_ro", 0);
1826 Part->Change_Mount_Read_Only(false);
1827 } else {
1828 DataManager::SetValue("tw_mount_system_ro", 1);
1829 Part->Change_Mount_Read_Only(true);
1830 }
1831 if (remount_system) {
1832 Part->Mount(true);
1833 }
1834 op_status = 0; // success
1835 } else {
1836 op_status = 1; // fail
1837 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001838 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1839 if (Part) {
1840 if (arg == "0") {
1841 Part->Change_Mount_Read_Only(false);
1842 } else {
1843 Part->Change_Mount_Read_Only(true);
1844 }
1845 if (remount_vendor) {
1846 Part->Mount(true);
1847 }
1848 op_status = 0; // success
1849 } else {
1850 op_status = 1; // fail
1851 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001852 }
1853
1854 operation_end(op_status);
1855 return 0;
1856}
Ethan Yonker74db1572015-10-28 12:44:49 -05001857
1858int GUIAction::setlanguage(std::string arg __unused)
1859{
1860 int op_status = 0;
1861
1862 operation_start("Set Language");
1863 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1864 PageManager::RequestReload();
1865 op_status = 0; // success
1866
1867 operation_end(op_status);
1868 return 0;
1869}
Ethan Yonker1b190162016-12-05 15:25:19 -06001870
1871int GUIAction::setbootslot(std::string arg)
1872{
1873 operation_start("Set Boot Slot");
1874 if (!simulate)
1875 {
1876 PartitionManager.Set_Active_Slot(arg);
1877 } else
1878 simulate_progress_bar();
1879 operation_end(0);
1880 return 0;
1881}