blob: 223d75e25b4263911ab7e09fa99b37a4f7779fdf [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);
that3f7b1ac2014-12-30 11:30:13 +0100231 }
232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200233 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600234 actions = FindNode(node, "actions");
235 if (actions) child = FindNode(actions, "action");
236 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 while (child)
241 {
242 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 attr = child->first_attribute("function");
245 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 action.mFunction = attr->value();
248 action.mArg = child->value();
249 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 child = child->next_sibling("action");
252 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600255 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (child)
257 {
258 attr = child->first_attribute("key");
259 if (attr)
260 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100261 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
262 for(size_t i = 0; i < keys.size(); ++i)
263 {
264 const int key = getKeyByName(keys[i]);
265 mKeys[key] = false;
266 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 }
268 else
269 {
270 attr = child->first_attribute("x");
271 if (!attr) return;
272 mActionX = atol(attr->value());
273 attr = child->first_attribute("y");
274 if (!attr) return;
275 mActionY = atol(attr->value());
276 attr = child->first_attribute("w");
277 if (!attr) return;
278 mActionW = atol(attr->value());
279 attr = child->first_attribute("h");
280 if (!attr) return;
281 mActionH = atol(attr->value());
282 }
283 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400284}
285
Matt Mower25036b72016-06-24 12:30:18 -0500286int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400287{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 if (state == TOUCH_RELEASE)
289 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400290
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100294int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400295{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100296 std::map<int, bool>::iterator itr = mKeys.find(key);
297 if(itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100298 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100299
300 bool prevState = itr->second;
301 itr->second = down;
302
303 // If there is only one key for this action, wait for key up so it
304 // doesn't trigger with multi-key actions.
305 // Else, check if all buttons are pressed, then consume their release events
306 // so they don't trigger one-button actions and reset mKeys pressed status
307 if(mKeys.size() == 1) {
thatd4725ca2016-01-19 00:15:21 +0100308 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100309 doActions();
thatd4725ca2016-01-19 00:15:21 +0100310 return 0;
311 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312 } else if(down) {
313 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
314 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100315 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100316 }
317
318 // Passed, all req buttons are pressed, reset them and consume release events
319 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
320 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
321 kb->ConsumeKeyRelease(itr->first);
322 itr->second = false;
323 }
324
325 doActions();
thatd4725ca2016-01-19 00:15:21 +0100326 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100327 }
328
thatd4725ca2016-01-19 00:15:21 +0100329 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400330}
331
Vojtech Bocek07220562014-02-08 02:05:33 +0100332int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400333{
Vojtech Bocek07220562014-02-08 02:05:33 +0100334 GUIObject::NotifyVarChange(varName, value);
335
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100336 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100338 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400340
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200341 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
344void GUIAction::simulate_progress_bar(void)
345{
Ethan Yonker74db1572015-10-28 12:44:49 -0500346 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400347 for (int i = 0; i < 5; i++)
348 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500349 if (PartitionManager.stop_backup.get_value()) {
350 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600351 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500352 DataManager::SetValue("ui_progress", 0);
353 PartitionManager.stop_backup.set_value(0);
354 return;
355 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 usleep(500000);
357 DataManager::SetValue("ui_progress", i * 20);
358 }
359}
360
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600361int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400362{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
365 DataManager::SetValue("ui_progress", 0);
366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 if (filename.empty())
368 {
369 LOGERR("No file specified.\n");
370 return -1;
371 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
Ethan Yonker9598c072016-01-15 21:54:34 -0600373 if (!TWFunc::Path_Exists(filename)) {
374 if (!PartitionManager.Mount_By_Path(filename, true)) {
375 return -1;
376 }
377 if (!TWFunc::Path_Exists(filename)) {
378 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
379 return -1;
380 }
381 }
Dees_Troy657c3092012-09-10 20:32:10 -0400382
Dees_Troy51a0e822012-09-05 15:24:24 -0400383 if (simulate) {
384 simulate_progress_bar();
385 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400386 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400387
388 // Now, check if we need to ensure TWRP remains installed...
389 struct stat st;
390 if (stat("/sbin/installTwrp", &st) == 0)
391 {
392 DataManager::SetValue("tw_operation", "Configuring TWRP");
393 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500394 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200395 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400396 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500397 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 }
399 }
400 }
401
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 // Done
403 DataManager::SetValue("ui_progress", 100);
404 DataManager::SetValue("ui_progress", 0);
405 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400406}
407
that73a52952015-01-28 23:07:34 +0100408GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100409{
that73a52952015-01-28 23:07:34 +0100410 string func = gui_parse_text(action.mFunction);
411 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
412 if (needsThread) {
413 if (func == "cancelbackup")
414 return THREAD_CANCEL;
415 else
416 return THREAD_ACTION;
417 }
418 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100419}
420
Dees_Troy51a0e822012-09-05 15:24:24 -0400421int GUIAction::doActions()
422{
that3f7b1ac2014-12-30 11:30:13 +0100423 if (mActions.size() < 1)
424 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425
that73a52952015-01-28 23:07:34 +0100426 // Determine in which thread to run the actions.
427 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
428 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100429 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100430 for (it = mActions.begin(); it != mActions.end(); ++it) {
431 ThreadType tt = getThreadType(*it);
432 if (tt == THREAD_NONE)
433 continue;
434 if (threadType == THREAD_NONE)
435 threadType = tt;
436 else if (threadType != tt) {
437 LOGERR("Can't mix normal and cancel actions in the same list.\n"
438 "Running the whole batch in the cancel thread.\n");
439 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100440 break;
441 }
that7d3b54f2015-01-09 22:52:51 +0100442 }
that73a52952015-01-28 23:07:34 +0100443
444 // Now run the actions in the desired thread.
445 switch (threadType) {
446 case THREAD_ACTION:
447 action_thread.threadActions(this);
448 break;
449
450 case THREAD_CANCEL:
451 cancel_thread.threadActions(this);
452 break;
453
454 default: {
455 // no iterators here because theme reloading might kill our object
456 const size_t cnt = mActions.size();
457 for (size_t i = 0; i < cnt; ++i)
458 doAction(mActions[i]);
459 }
thatc6085482015-01-09 22:12:43 +0100460 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200462 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400463}
464
that3f7b1ac2014-12-30 11:30:13 +0100465int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400466{
that3f7b1ac2014-12-30 11:30:13 +0100467 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
that3f7b1ac2014-12-30 11:30:13 +0100469 std::string function = gui_parse_text(action.mFunction);
470 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
that3f7b1ac2014-12-30 11:30:13 +0100472 // find function and execute it
473 mapFunc::const_iterator funcitr = mf.find(function);
474 if (funcitr != mf.end())
475 return (this->*funcitr->second)(arg);
476
477 LOGERR("Unknown action '%s'\n", function.c_str());
478 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
481void GUIAction::operation_start(const string operation_name)
482{
that3f7b1ac2014-12-30 11:30:13 +0100483 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000484 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 DataManager::SetValue(TW_ACTION_BUSY, 1);
486 DataManager::SetValue("ui_progress", 0);
487 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600489 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400490}
491
that3f7b1ac2014-12-30 11:30:13 +0100492void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400493{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000494 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue("ui_progress", 100);
497 if (simulate) {
498 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
499 if (simulate_fail != 0)
500 DataManager::SetValue("tw_operation_status", 1);
501 else
502 DataManager::SetValue("tw_operation_status", 0);
503 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500504 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500506 }
507 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 }
511 DataManager::SetValue("tw_operation_state", 1);
512 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500513 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200514 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000515 time(&Stop);
516 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600517 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100518 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400519}
520
that3f7b1ac2014-12-30 11:30:13 +0100521int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400522{
that3f7b1ac2014-12-30 11:30:13 +0100523 sync();
524 DataManager::SetValue("tw_gui_done", 1);
525 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400526
that3f7b1ac2014-12-30 11:30:13 +0100527 return 0;
528}
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500530int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100531{
that3f7b1ac2014-12-30 11:30:13 +0100532 gui_changePage("main");
533 return 0;
534}
Dees_Troy51a0e822012-09-05 15:24:24 -0400535
that3f7b1ac2014-12-30 11:30:13 +0100536int GUIAction::key(std::string arg)
537{
538 const int key = getKeyByName(arg);
539 PageManager::NotifyKey(key, true);
540 PageManager::NotifyKey(key, false);
541 return 0;
542}
543
544int GUIAction::page(std::string arg)
545{
LuK133762326f42015-09-30 19:57:46 +0200546 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100547 std::string page_name = gui_parse_text(arg);
548 return gui_changePage(page_name);
549}
550
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500551int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100552{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500553 PageManager::RequestReload();
554 // The actual reload is handled in pages.cpp in PageManager::RunReload()
555 // The reload will occur on the next Update or Render call and will
556 // be performed in the rendoer thread instead of the action thread
557 // to prevent crashing which could occur when we start deleting
558 // GUI resources in the action thread while we attempt to render
559 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100560 return 0;
561}
Dees_Troy51a0e822012-09-05 15:24:24 -0400562
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500563int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100564{
565 string Restore_Name;
566 DataManager::GetValue("tw_restore", Restore_Name);
567 PartitionManager.Set_Restore_Files(Restore_Name);
568 return 0;
569}
570
571int GUIAction::set(std::string arg)
572{
573 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200574 {
that3f7b1ac2014-12-30 11:30:13 +0100575 string varName = arg.substr(0, arg.find('='));
576 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400577
that3f7b1ac2014-12-30 11:30:13 +0100578 DataManager::GetValue(value, value);
579 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200580 }
that3f7b1ac2014-12-30 11:30:13 +0100581 else
582 DataManager::SetValue(arg, "1");
583 return 0;
584}
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
that3f7b1ac2014-12-30 11:30:13 +0100586int GUIAction::clear(std::string arg)
587{
588 DataManager::SetValue(arg, "0");
589 return 0;
590}
Dees_Troy51a0e822012-09-05 15:24:24 -0400591
that3f7b1ac2014-12-30 11:30:13 +0100592int GUIAction::mount(std::string arg)
593{
594 if (arg == "usb") {
595 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400596 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100597 PartitionManager.usb_storage_enable();
598 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500599 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100600 } else if (!simulate) {
601 PartitionManager.Mount_By_Path(arg, true);
602 PartitionManager.Add_MTP_Storage(arg);
603 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500604 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100605 return 0;
606}
607
608int GUIAction::unmount(std::string arg)
609{
610 if (arg == "usb") {
611 if (!simulate)
612 PartitionManager.usb_storage_disable();
613 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500614 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100615 DataManager::SetValue(TW_ACTION_BUSY, 0);
616 } else if (!simulate) {
617 PartitionManager.UnMount_By_Path(arg, true);
618 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500619 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100620 return 0;
621}
622
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500623int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100624{
625 operation_start("Restore Defaults");
626 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500627 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100628 else {
629 DataManager::ResetDefaults();
630 PartitionManager.Update_System_Details();
631 PartitionManager.Mount_Current_Storage(true);
632 }
633 operation_end(0);
634 return 0;
635}
636
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500637int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100638{
639 operation_start("Copy Log");
640 if (!simulate)
641 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400642 string dst, curr_storage;
643 int copy_kernel_log = 0;
644
645 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100646 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400647 curr_storage = DataManager::GetCurrentStoragePath();
648 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100649 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
650 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400651 if (copy_kernel_log)
652 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100653 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400654 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100655 } else
656 simulate_progress_bar();
657 operation_end(0);
658 return 0;
659}
660
661
662int GUIAction::compute(std::string arg)
663{
664 if (arg.find("+") != string::npos)
665 {
666 string varName = arg.substr(0, arg.find('+'));
667 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
668 int amount_to_add = atoi(string_to_add.c_str());
669 int value;
670
671 DataManager::GetValue(varName, value);
672 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 return 0;
674 }
that3f7b1ac2014-12-30 11:30:13 +0100675 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 {
that3f7b1ac2014-12-30 11:30:13 +0100677 string varName = arg.substr(0, arg.find('-'));
678 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
679 int amount_to_subtract = atoi(string_to_subtract.c_str());
680 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400681
that3f7b1ac2014-12-30 11:30:13 +0100682 DataManager::GetValue(varName, value);
683 value -= amount_to_subtract;
684 if (value <= 0)
685 value = 0;
686 DataManager::SetValue(varName, value);
687 return 0;
688 }
689 if (arg.find("*") != string::npos)
690 {
691 string varName = arg.substr(0, arg.find('*'));
692 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
693 int multiply_by = atoi(multiply_by_str.c_str());
694 int value;
695
696 DataManager::GetValue(varName, value);
697 DataManager::SetValue(varName, value*multiply_by);
698 return 0;
699 }
700 if (arg.find("/") != string::npos)
701 {
702 string varName = arg.substr(0, arg.find('/'));
703 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
704 int divide_by = atoi(divide_by_str.c_str());
705 int value;
706
707 if(divide_by != 0)
708 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100710 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 }
712 return 0;
713 }
that3f7b1ac2014-12-30 11:30:13 +0100714 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
715 return -1;
716}
Dees_Troy51a0e822012-09-05 15:24:24 -0400717
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500718int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100719{
720 string SelectedZone;
721 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
722 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
723 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
724
725 int dst;
726 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
727
728 string offset;
729 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
730
731 string NewTimeZone = Zone;
732 if (offset != "0")
733 NewTimeZone += ":" + offset;
734
735 if (dst != 0)
736 NewTimeZone += DSTZone;
737
738 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
739 DataManager::update_tz_environment_variables();
740 return 0;
741}
742
743int GUIAction::overlay(std::string arg)
744{
745 return gui_changeOverlay(arg);
746}
747
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500748int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100749{
750 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500751 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 return 0;
753 }
that3f7b1ac2014-12-30 11:30:13 +0100754 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
755 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
756 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400757 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100758 }
759 return 0;
760}
761
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500762int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100763{
764 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500765 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100767 } else {
768 zip_queue_index--;
769 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 }
that3f7b1ac2014-12-30 11:30:13 +0100771 return 0;
772}
Dees_Troy51a0e822012-09-05 15:24:24 -0400773
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500774int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100775{
776 zip_queue_index = 0;
777 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
778 return 0;
779}
Dees_Troy51a0e822012-09-05 15:24:24 -0400780
that3f7b1ac2014-12-30 11:30:13 +0100781int GUIAction::sleep(std::string arg)
782{
783 operation_start("Sleep");
784 usleep(atoi(arg.c_str()));
785 operation_end(0);
786 return 0;
787}
Dees Troyb21cc642013-09-10 17:36:41 +0000788
Matt Mower9a2a2052016-05-31 21:31:22 -0500789int GUIAction::sleepcounter(std::string arg)
790{
791 operation_start("SleepCounter");
792 // Ensure user notices countdown in case it needs to be cancelled
793 blankTimer.resetTimerAndUnblank();
794 int total = atoi(arg.c_str());
795 for (int t = total; t > 0; t--) {
796 int progress = (int)(((float)(total-t)/(float)total)*100.0);
797 DataManager::SetValue("ui_progress", progress);
798 ::sleep(1);
799 DataManager::SetValue("tw_sleep", t-1);
800 }
801 DataManager::SetValue("ui_progress", 100);
802 operation_end(0);
803 return 0;
804}
805
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500806int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100807{
808 operation_start("AppendDateToBackupName");
809 string Backup_Name;
810 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
811 Backup_Name += TWFunc::Get_Current_Date();
812 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
813 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
814 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500815 PageManager::NotifyKey(KEY_END, true);
816 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100817 operation_end(0);
818 return 0;
819}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500820
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500821int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100822{
823 operation_start("GenerateBackupName");
824 TWFunc::Auto_Generate_Backup_Name();
825 operation_end(0);
826 return 0;
827}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500828
Ethan Yonker483e9f42016-01-11 22:21:18 -0600829int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100830{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600831 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100832 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500833
Ethan Yonker483e9f42016-01-11 22:21:18 -0600834 if (arg.empty())
835 arg = "tw_wipe_list";
836 DataManager::GetValue(arg, List);
837 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
838 if (!List.empty()) {
839 size_t start_pos = 0, end_pos = List.find(";", start_pos);
840 while (end_pos != string::npos && start_pos < List.size()) {
841 part_path = List.substr(start_pos, end_pos - start_pos);
842 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
843 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100844 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400845 } else {
that3f7b1ac2014-12-30 11:30:13 +0100846 count++;
847 }
848 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600849 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100850 }
851 DataManager::SetValue("tw_check_partition_list", count);
852 } else {
853 DataManager::SetValue("tw_check_partition_list", 0);
854 }
855 return 0;
856}
Dees_Troy51a0e822012-09-05 15:24:24 -0400857
Ethan Yonker483e9f42016-01-11 22:21:18 -0600858int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100859{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600860 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100861 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400862
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863 if (arg.empty())
864 arg = "tw_wipe_list";
865 DataManager::GetValue(arg, List);
866 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
867 if (!List.empty()) {
868 size_t start_pos = 0, end_pos = List.find(";", start_pos);
869 part_path = List;
870 while (end_pos != string::npos && start_pos < List.size()) {
871 part_path = List.substr(start_pos, end_pos - start_pos);
872 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
873 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100874 // Do nothing
875 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600876 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100877 break;
878 }
879 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600880 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100881 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600882 if (!part_path.empty()) {
883 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100884 if (Part) {
885 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500886
that3f7b1ac2014-12-30 11:30:13 +0100887 DataManager::SetValue("tw_partition_name", Part->Display_Name);
888 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
889 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
890 DataManager::SetValue("tw_partition_size", Part->Size / mb);
891 DataManager::SetValue("tw_partition_used", Part->Used / mb);
892 DataManager::SetValue("tw_partition_free", Part->Free / mb);
893 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
894 DataManager::SetValue("tw_partition_removable", Part->Removable);
895 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
896
897 if (Part->Can_Repair())
898 DataManager::SetValue("tw_partition_can_repair", 1);
899 else
900 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500901 if (Part->Can_Resize())
902 DataManager::SetValue("tw_partition_can_resize", 1);
903 else
904 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600905 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100906 DataManager::SetValue("tw_partition_vfat", 1);
907 else
908 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600909 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100910 DataManager::SetValue("tw_partition_exfat", 1);
911 else
912 DataManager::SetValue("tw_partition_exfat", 0);
913 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
914 DataManager::SetValue("tw_partition_f2fs", 1);
915 else
916 DataManager::SetValue("tw_partition_f2fs", 0);
917 if (TWFunc::Path_Exists("/sbin/mke2fs"))
918 DataManager::SetValue("tw_partition_ext", 1);
919 else
920 DataManager::SetValue("tw_partition_ext", 0);
921 return 0;
922 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600923 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100924 }
925 }
926 }
927 DataManager::SetValue("tw_partition_name", "");
928 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600929 // Set this to 0 to prevent trying to partition this device, just in case
930 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100931 return 0;
932}
933
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500934int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100935{
936 time_t tm;
937 char path[256];
938 int path_len;
939 uid_t uid = -1;
940 gid_t gid = -1;
941
942 struct passwd *pwd = getpwnam("media_rw");
943 if(pwd) {
944 uid = pwd->pw_uid;
945 gid = pwd->pw_gid;
946 }
947
948 const std::string storage = DataManager::GetCurrentStoragePath();
949 if(PartitionManager.Is_Mounted_By_Path(storage)) {
950 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
951 } else {
952 strcpy(path, "/tmp/");
953 }
954
Xuefer579e9072016-01-22 13:35:50 +0800955 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100956 return 0;
957
958 tm = time(NULL);
959 path_len = strlen(path);
960
961 // Screenshot_2014-01-01-18-21-38.png
962 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
963
964 int res = gr_save_screenshot(path);
965 if(res == 0) {
966 chmod(path, 0666);
967 chown(path, uid, gid);
968
that677b13f2015-12-26 23:57:31 +0100969 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100970
971 // blink to notify that the screenshow was taken
972 gr_color(255, 255, 255, 255);
973 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
974 gr_flip();
975 gui_forceRender();
976 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500977 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100978 }
979 return 0;
980}
981
982int GUIAction::setbrightness(std::string arg)
983{
984 return TWFunc::Set_Brightness(arg);
985}
986
987int GUIAction::fileexists(std::string arg)
988{
989 struct stat st;
990 string newpath = arg + "/.";
991
992 operation_start("FileExists");
993 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
994 operation_end(0);
995 else
996 operation_end(1);
997 return 0;
998}
999
thatcc8ddca2015-01-03 01:59:36 +01001000void GUIAction::reinject_after_flash()
1001{
1002 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001003 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001004 if (simulate) {
1005 simulate_progress_bar();
1006 } else {
1007 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1008 if (Boot == NULL || Boot->Current_File_System != "emmc")
1009 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1010 else {
1011 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1012 TWFunc::Exec_Cmd(injectcmd);
1013 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001014 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001015 }
1016 }
1017}
1018
that3f7b1ac2014-12-30 11:30:13 +01001019int GUIAction::flash(std::string arg)
1020{
1021 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001022 // We're going to jump to this page first, like a loading page
1023 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001024 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001025 string zip_path = zip_queue[i];
1026 size_t slashpos = zip_path.find_last_of('/');
1027 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001028 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001029 DataManager::SetValue("tw_filename", zip_path);
1030 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001031 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1032
1033 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001034 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001035 TWFunc::SetPerformanceMode(false);
1036 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001037 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001038 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001039 break;
that3f7b1ac2014-12-30 11:30:13 +01001040 }
1041 }
1042 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001043
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001044 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001045 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001046 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001047 }
that3f7b1ac2014-12-30 11:30:13 +01001048
thatcc8ddca2015-01-03 01:59:36 +01001049 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001050 PartitionManager.Update_System_Details();
1051 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001052 // 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 -06001053 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001054 return 0;
1055}
1056
1057int GUIAction::wipe(std::string arg)
1058{
1059 operation_start("Format");
1060 DataManager::SetValue("tw_partition", arg);
1061
1062 int ret_val = false;
1063
1064 if (simulate) {
1065 simulate_progress_bar();
1066 } else {
1067 if (arg == "data")
1068 ret_val = PartitionManager.Factory_Reset();
1069 else if (arg == "battery")
1070 ret_val = PartitionManager.Wipe_Battery_Stats();
1071 else if (arg == "rotate")
1072 ret_val = PartitionManager.Wipe_Rotate_Data();
1073 else if (arg == "dalvik")
1074 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1075 else if (arg == "DATAMEDIA") {
1076 ret_val = PartitionManager.Format_Data();
1077 } else if (arg == "INTERNAL") {
1078 int has_datamedia, dual_storage;
1079
1080 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1081 if (has_datamedia) {
1082 ret_val = PartitionManager.Wipe_Media_From_Data();
1083 } else {
1084 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1085 }
1086 } else if (arg == "EXTERNAL") {
1087 string External_Path;
1088
1089 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1090 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1091 } else if (arg == "ANDROIDSECURE") {
1092 ret_val = PartitionManager.Wipe_Android_Secure();
1093 } else if (arg == "LIST") {
1094 string Wipe_List, wipe_path;
1095 bool skip = false;
1096 ret_val = true;
1097 TWPartition* wipe_part = NULL;
1098
1099 DataManager::GetValue("tw_wipe_list", Wipe_List);
1100 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1101 if (!Wipe_List.empty()) {
1102 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1103 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1104 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1105 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1106 if (wipe_path == "/and-sec") {
1107 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001108 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001109 ret_val = false;
1110 break;
1111 } else {
1112 skip = true;
1113 }
1114 } else if (wipe_path == "DALVIK") {
1115 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001116 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001117 ret_val = false;
1118 break;
1119 } else {
1120 skip = true;
1121 }
1122 } else if (wipe_path == "INTERNAL") {
1123 if (!PartitionManager.Wipe_Media_From_Data()) {
1124 ret_val = false;
1125 break;
1126 } else {
1127 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001128 }
1129 }
that3f7b1ac2014-12-30 11:30:13 +01001130 if (!skip) {
1131 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001132 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001133 ret_val = false;
1134 break;
1135 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1136 arg = wipe_path;
1137 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 } else {
that3f7b1ac2014-12-30 11:30:13 +01001139 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 }
that3f7b1ac2014-12-30 11:30:13 +01001141 start_pos = end_pos + 1;
1142 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001143 }
1144 }
that3f7b1ac2014-12-30 11:30:13 +01001145 } else
1146 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001147#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001148 if (arg == DataManager::GetSettingsStoragePath()) {
1149 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1150 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001151
that3f7b1ac2014-12-30 11:30:13 +01001152 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1153 LOGINFO("Making TWRP folder and saving settings.\n");
1154 Storage_Path += "/TWRP";
1155 mkdir(Storage_Path.c_str(), 0777);
1156 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001157 } else {
that3f7b1ac2014-12-30 11:30:13 +01001158 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1159 }
1160 }
1161#endif
1162 }
1163 PartitionManager.Update_System_Details();
1164 if (ret_val)
1165 ret_val = 0; // 0 is success
1166 else
1167 ret_val = 1; // 1 is failure
1168 operation_end(ret_val);
1169 return 0;
1170}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001171
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001172int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001173{
1174 operation_start("Refreshing Sizes");
1175 if (simulate) {
1176 simulate_progress_bar();
1177 } else
1178 PartitionManager.Update_System_Details();
1179 operation_end(0);
1180 return 0;
1181}
1182
1183int GUIAction::nandroid(std::string arg)
1184{
that3f7b1ac2014-12-30 11:30:13 +01001185 if (simulate) {
that73a52952015-01-28 23:07:34 +01001186 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001187 DataManager::SetValue("tw_partition", "Simulation");
1188 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001189 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001190 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 operation_start("Nandroid");
1192 int ret = 0;
1193
that3f7b1ac2014-12-30 11:30:13 +01001194 if (arg == "backup") {
1195 string Backup_Name;
1196 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001197 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1198 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001199 ret = PartitionManager.Run_Backup(false);
1200 } else {
that3f7b1ac2014-12-30 11:30:13 +01001201 operation_end(1);
1202 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001203 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001204 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001205 } else if (arg == "restore") {
1206 string Restore_Name;
1207 DataManager::GetValue("tw_restore", Restore_Name);
1208 ret = PartitionManager.Run_Restore(Restore_Name);
1209 } else {
1210 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001211 return -1;
1212 }
1213 DataManager::SetValue("tw_encrypt_backup", 0);
1214 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 if (ret == false)
1216 ret = 1; // 1 for failure
1217 else
1218 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001219 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001220 }
1221 else {
1222 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001223 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001224 ret = 0;
1225 }
that73a52952015-01-28 23:07:34 +01001226 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001227 return ret;
1228 }
1229 return 0;
1230}
1231
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001232int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001233 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001234 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001235 }
1236 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001237 int op_status = PartitionManager.Cancel_Backup();
1238 if (op_status != 0)
1239 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001240 }
1241
1242 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001243}
Dees_Troy51a0e822012-09-05 15:24:24 -04001244
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001245int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001246{
that73a52952015-01-28 23:07:34 +01001247 int op_status = 0;
1248
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001249 operation_start("Fix Contexts");
1250 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001251 if (simulate) {
1252 simulate_progress_bar();
1253 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001254 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001255 if (op_status != 0)
1256 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001257 }
that73a52952015-01-28 23:07:34 +01001258 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001259 return 0;
1260}
1261
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001262int GUIAction::fixpermissions(std::string arg)
1263{
1264 return fixcontexts(arg);
1265}
1266
that3f7b1ac2014-12-30 11:30:13 +01001267int GUIAction::dd(std::string arg)
1268{
1269 operation_start("imaging");
1270
1271 if (simulate) {
1272 simulate_progress_bar();
1273 } else {
1274 string cmd = "dd " + arg;
1275 TWFunc::Exec_Cmd(cmd);
1276 }
1277 operation_end(0);
1278 return 0;
1279}
1280
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001281int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001282{
1283 operation_start("Partition SD Card");
1284 int ret_val = 0;
1285
1286 if (simulate) {
1287 simulate_progress_bar();
1288 } else {
1289 int allow_partition;
1290 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1291 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001293 } else {
1294 if (!PartitionManager.Partition_SDCard())
1295 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001296 }
that3f7b1ac2014-12-30 11:30:13 +01001297 }
1298 operation_end(ret_val);
1299 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
that3f7b1ac2014-12-30 11:30:13 +01001301}
Dees_Troy51a0e822012-09-05 15:24:24 -04001302
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001303int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001304{
1305 operation_start("Install HTC Dumlock");
1306 if (simulate) {
1307 simulate_progress_bar();
1308 } else
1309 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001310
that3f7b1ac2014-12-30 11:30:13 +01001311 operation_end(0);
1312 return 0;
1313}
Dees_Troy51a0e822012-09-05 15:24:24 -04001314
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001315int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001316{
1317 operation_start("HTC Dumlock Restore Boot");
1318 if (simulate) {
1319 simulate_progress_bar();
1320 } else
1321 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001322
that3f7b1ac2014-12-30 11:30:13 +01001323 operation_end(0);
1324 return 0;
1325}
Dees_Troy51a0e822012-09-05 15:24:24 -04001326
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001327int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001328{
1329 operation_start("HTC Dumlock Reflash Recovery");
1330 if (simulate) {
1331 simulate_progress_bar();
1332 } else
1333 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001334
that3f7b1ac2014-12-30 11:30:13 +01001335 operation_end(0);
1336 return 0;
1337}
Dees_Troy51a0e822012-09-05 15:24:24 -04001338
that3f7b1ac2014-12-30 11:30:13 +01001339int GUIAction::cmd(std::string arg)
1340{
1341 int op_status = 0;
1342
1343 operation_start("Command");
1344 LOGINFO("Running command: '%s'\n", arg.c_str());
1345 if (simulate) {
1346 simulate_progress_bar();
1347 } else {
1348 op_status = TWFunc::Exec_Cmd(arg);
1349 if (op_status != 0)
1350 op_status = 1;
1351 }
1352
1353 operation_end(op_status);
1354 return 0;
1355}
1356
1357int GUIAction::terminalcommand(std::string arg)
1358{
1359 int op_status = 0;
1360 string cmdpath, command;
1361
1362 DataManager::GetValue("tw_terminal_location", cmdpath);
1363 operation_start("CommandOutput");
1364 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1365 if (simulate) {
1366 simulate_progress_bar();
1367 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001368 } else if (arg == "exit") {
1369 LOGINFO("Exiting terminal\n");
1370 operation_end(op_status);
1371 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001372 } else {
1373 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1374 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001375 DataManager::SetValue("tw_terminal_state", 1);
1376 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001377 FILE* fp;
1378 char line[512];
1379
1380 fp = popen(command.c_str(), "r");
1381 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001382 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001383 } else {
1384 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1385 struct timeval timeout;
1386 fd_set fdset;
1387
1388 while(keep_going)
1389 {
1390 FD_ZERO(&fdset);
1391 FD_SET(fd, &fdset);
1392 timeout.tv_sec = 0;
1393 timeout.tv_usec = 400000;
1394 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1395 if (has_data == 0) {
1396 // Timeout reached
1397 DataManager::GetValue("tw_terminal_state", check);
1398 if (check == 0) {
1399 keep_going = 0;
1400 }
1401 } else if (has_data < 0) {
1402 // End of execution
1403 keep_going = 0;
1404 } else {
1405 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001406 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001407 gui_print("%s", line); // Display output
1408 else
1409 keep_going = 0; // Done executing
1410 }
1411 }
1412 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001413 }
thatc6085482015-01-09 22:12:43 +01001414 DataManager::SetValue("tw_operation_status", 0);
1415 DataManager::SetValue("tw_operation_state", 1);
1416 DataManager::SetValue("tw_terminal_state", 0);
1417 DataManager::SetValue("tw_background_thread_running", 0);
1418 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001419 }
1420 return 0;
1421}
1422
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001423int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001424{
1425 int op_status = 0;
1426
1427 LOGINFO("Sending kill command...\n");
1428 operation_start("KillCommand");
1429 DataManager::SetValue("tw_operation_status", 0);
1430 DataManager::SetValue("tw_operation_state", 1);
1431 DataManager::SetValue("tw_terminal_state", 0);
1432 DataManager::SetValue("tw_background_thread_running", 0);
1433 DataManager::SetValue(TW_ACTION_BUSY, 0);
1434 return 0;
1435}
1436
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001437int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001438{
1439 int op_status = 0;
1440 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001442 if (simulate) {
1443 simulate_progress_bar();
1444 } else {
1445 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001446 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001447 }
1448
1449 operation_end(op_status);
1450 return 0;
1451}
1452
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001453int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001454{
1455 int op_status = 0;
1456
1457 operation_start("CheckBackupName");
1458 if (simulate) {
1459 simulate_progress_bar();
1460 } else {
1461 op_status = PartitionManager.Check_Backup_Name(true);
1462 if (op_status != 0)
1463 op_status = 1;
1464 }
1465
1466 operation_end(op_status);
1467 return 0;
1468}
1469
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001470int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001471{
1472 int op_status = 0;
1473
1474 operation_start("Decrypt");
1475 if (simulate) {
1476 simulate_progress_bar();
1477 } else {
1478 string Password;
1479 DataManager::GetValue("tw_crypto_password", Password);
1480 op_status = PartitionManager.Decrypt_Device(Password);
1481 if (op_status != 0)
1482 op_status = 1;
1483 else {
that3f7b1ac2014-12-30 11:30:13 +01001484
1485 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1486
Ethan Yonkercf50da52015-01-12 21:59:07 -06001487 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001488
Ethan Yonkercf50da52015-01-12 21:59:07 -06001489 // Check for a custom theme and load it if exists
1490 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1491 if (has_datamedia != 0) {
1492 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001493 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001494 } else {
1495 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001496 }
1497 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001498 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001499 }
1500 }
1501
1502 operation_end(op_status);
1503 return 0;
1504}
1505
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001506int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001507{
1508 operation_start("Sideload");
1509 if (simulate) {
1510 simulate_progress_bar();
1511 operation_end(0);
1512 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001513 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001514 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1515
1516 // wait for the adb connection
1517 int ret = apply_from_adb("/", &sideload_child_pid);
1518 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1519
1520 if (ret != 0) {
1521 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001522 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001523 ret = 1; // failure
1524 } else {
1525 int wipe_cache = 0;
1526 int wipe_dalvik = 0;
1527 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1528
1529 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1530 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1531 PartitionManager.Wipe_By_Path("/cache");
1532 if (wipe_dalvik)
1533 PartitionManager.Wipe_Dalvik_Cache();
1534 } else {
1535 ret = 1; // failure
1536 }
thatcc8ddca2015-01-03 01:59:36 +01001537 }
thatc6085482015-01-09 22:12:43 +01001538 if (sideload_child_pid) {
1539 LOGINFO("Signaling child sideload process to exit.\n");
1540 struct stat st;
1541 // Calling stat() on this magic filename signals the minadbd
1542 // subprocess to shut down.
1543 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1544 int status;
1545 LOGINFO("Waiting for child sideload process to exit.\n");
1546 waitpid(sideload_child_pid, &status, 0);
1547 }
Dees Troy28a85d22015-04-28 02:03:16 +00001548 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001549 TWFunc::Toggle_MTP(mtp_was_enabled);
1550 reinject_after_flash();
1551 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001552 }
that3f7b1ac2014-12-30 11:30:13 +01001553 return 0;
1554}
1555
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001556int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001557{
that3f7b1ac2014-12-30 11:30:13 +01001558 struct stat st;
1559 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001560 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001561 LOGINFO("Signaling child sideload process to exit.\n");
1562 // Calling stat() on this magic filename signals the minadbd
1563 // subprocess to shut down.
1564 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1565 if (!sideload_child_pid) {
1566 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001567 return 0;
1568 }
thatcc8ddca2015-01-03 01:59:36 +01001569 ::sleep(1);
1570 LOGINFO("Killing child sideload process.\n");
1571 kill(sideload_child_pid, SIGTERM);
1572 int status;
1573 LOGINFO("Waiting for child sideload process to exit.\n");
1574 waitpid(sideload_child_pid, &status, 0);
1575 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001576 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1577 return 0;
1578}
1579
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001580int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001581{
1582 operation_start("OpenRecoveryScript");
1583 if (simulate) {
1584 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001585 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001586 } else {
that10ae24f2015-12-26 20:53:51 +01001587 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001588 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001589 }
1590 return 0;
1591}
1592
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001593int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001594{
1595 int op_status = 0;
1596
1597 operation_start("Install SuperSU");
1598 if (simulate) {
1599 simulate_progress_bar();
1600 } else {
1601 if (!TWFunc::Install_SuperSU())
1602 op_status = 1;
1603 }
1604
1605 operation_end(op_status);
1606 return 0;
1607}
1608
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001609int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001610{
1611 int op_status = 0;
1612
1613 operation_start("Fixing Superuser Permissions");
1614 if (simulate) {
1615 simulate_progress_bar();
1616 } else {
1617 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1618 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1619 }
1620
1621 operation_end(op_status);
1622 return 0;
1623}
1624
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001625int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001626{
1627 int op_status = 0;
1628
1629 operation_start("Try Restore Decrypt");
1630 if (simulate) {
1631 simulate_progress_bar();
1632 } else {
1633 string Restore_Path, Filename, Password;
1634 DataManager::GetValue("tw_restore", Restore_Path);
1635 Restore_Path += "/";
1636 DataManager::GetValue("tw_restore_password", Password);
1637 TWFunc::SetPerformanceMode(true);
1638 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1639 op_status = 0; // success
1640 else
1641 op_status = 1; // fail
1642 TWFunc::SetPerformanceMode(false);
1643 }
1644
1645 operation_end(op_status);
1646 return 0;
1647}
1648
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001649int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001650{
1651 int op_status = 0;
1652
1653 operation_start("Repair Partition");
1654 if (simulate) {
1655 simulate_progress_bar();
1656 } else {
1657 string part_path;
1658 DataManager::GetValue("tw_partition_mount_point", part_path);
1659 if (PartitionManager.Repair_By_Path(part_path, true)) {
1660 op_status = 0; // success
1661 } else {
that3f7b1ac2014-12-30 11:30:13 +01001662 op_status = 1; // fail
1663 }
1664 }
1665
1666 operation_end(op_status);
1667 return 0;
1668}
1669
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001670int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001671{
1672 int op_status = 0;
1673
1674 operation_start("Resize Partition");
1675 if (simulate) {
1676 simulate_progress_bar();
1677 } else {
1678 string part_path;
1679 DataManager::GetValue("tw_partition_mount_point", part_path);
1680 if (PartitionManager.Resize_By_Path(part_path, true)) {
1681 op_status = 0; // success
1682 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001683 op_status = 1; // fail
1684 }
1685 }
1686
1687 operation_end(op_status);
1688 return 0;
1689}
1690
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001691int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001692{
1693 int op_status = 0;
1694
1695 operation_start("Change File System");
1696 if (simulate) {
1697 simulate_progress_bar();
1698 } else {
1699 string part_path, file_system;
1700 DataManager::GetValue("tw_partition_mount_point", part_path);
1701 DataManager::GetValue("tw_action_new_file_system", file_system);
1702 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1703 op_status = 0; // success
1704 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001705 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001706 op_status = 1; // fail
1707 }
1708 }
1709 PartitionManager.Update_System_Details();
1710 operation_end(op_status);
1711 return 0;
1712}
1713
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001714int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001715{
1716 int op_status = 0;
1717
1718 operation_start("Start MTP");
1719 if (PartitionManager.Enable_MTP())
1720 op_status = 0; // success
1721 else
1722 op_status = 1; // fail
1723
1724 operation_end(op_status);
1725 return 0;
1726}
1727
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001728int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001729{
1730 int op_status = 0;
1731
1732 operation_start("Stop MTP");
1733 if (PartitionManager.Disable_MTP())
1734 op_status = 0; // success
1735 else
1736 op_status = 1; // fail
1737
1738 operation_end(op_status);
1739 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001740}
1741
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001742int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001743{
1744 int op_status = 0;
1745
1746 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001747 string path, filename;
1748 DataManager::GetValue("tw_zip_location", path);
1749 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001750 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001751 op_status = 0; // success
1752 else
1753 op_status = 1; // fail
1754
1755 operation_end(op_status);
1756 return 0;
1757}
1758
that10ae24f2015-12-26 20:53:51 +01001759int GUIAction::twcmd(std::string arg)
1760{
1761 operation_start("TWRP CLI Command");
1762 if (simulate)
1763 simulate_progress_bar();
1764 else
1765 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1766 operation_end(0);
1767 return 0;
1768}
1769
Dees_Troy51a0e822012-09-05 15:24:24 -04001770int GUIAction::getKeyByName(std::string key)
1771{
that8834a0f2016-01-05 23:29:30 +01001772 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001773 else if (key == "menu") return KEY_MENU;
1774 else if (key == "back") return KEY_BACK;
1775 else if (key == "search") return KEY_SEARCH;
1776 else if (key == "voldown") return KEY_VOLUMEDOWN;
1777 else if (key == "volup") return KEY_VOLUMEUP;
1778 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001779 int ret_val;
1780 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1781 if (!ret_val)
1782 return KEY_POWER;
1783 else
1784 return ret_val;
1785 }
1786
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001787 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001788}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001789
1790int GUIAction::checkpartitionlifetimewrites(std::string arg)
1791{
1792 int op_status = 0;
1793 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1794
1795 operation_start("Check Partition Lifetime Writes");
1796 if (sys) {
1797 if (sys->Check_Lifetime_Writes() != 0)
1798 DataManager::SetValue("tw_lifetime_writes", 1);
1799 else
1800 DataManager::SetValue("tw_lifetime_writes", 0);
1801 op_status = 0; // success
1802 } else {
1803 DataManager::SetValue("tw_lifetime_writes", 1);
1804 op_status = 1; // fail
1805 }
1806
1807 operation_end(op_status);
1808 return 0;
1809}
1810
1811int GUIAction::mountsystemtoggle(std::string arg)
1812{
1813 int op_status = 0;
1814 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001815 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001816
1817 operation_start("Toggle System Mount");
1818 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1819 op_status = 1; // fail
1820 } else {
1821 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1822 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001823 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001824 DataManager::SetValue("tw_mount_system_ro", 0);
1825 Part->Change_Mount_Read_Only(false);
1826 } else {
1827 DataManager::SetValue("tw_mount_system_ro", 1);
1828 Part->Change_Mount_Read_Only(true);
1829 }
1830 if (remount_system) {
1831 Part->Mount(true);
1832 }
1833 op_status = 0; // success
1834 } else {
1835 op_status = 1; // fail
1836 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001837 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1838 if (Part) {
1839 if (arg == "0") {
1840 Part->Change_Mount_Read_Only(false);
1841 } else {
1842 Part->Change_Mount_Read_Only(true);
1843 }
1844 if (remount_vendor) {
1845 Part->Mount(true);
1846 }
1847 op_status = 0; // success
1848 } else {
1849 op_status = 1; // fail
1850 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001851 }
1852
1853 operation_end(op_status);
1854 return 0;
1855}
Ethan Yonker74db1572015-10-28 12:44:49 -05001856
1857int GUIAction::setlanguage(std::string arg __unused)
1858{
1859 int op_status = 0;
1860
1861 operation_start("Set Language");
1862 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1863 PageManager::RequestReload();
1864 op_status = 0; // success
1865
1866 operation_end(op_status);
1867 return 0;
1868}