blob: a96e15ac990b9ca9b5157289db537955f88924d8 [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 {
642 string dst;
643 PartitionManager.Mount_Current_Storage(true);
644 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
645 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
646 tw_set_default_metadata(dst.c_str());
647 sync();
Ethan Yonker74db1572015-10-28 12:44:49 -0500648 gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
that3f7b1ac2014-12-30 11:30:13 +0100649 } else
650 simulate_progress_bar();
651 operation_end(0);
652 return 0;
653}
654
655
656int GUIAction::compute(std::string arg)
657{
658 if (arg.find("+") != string::npos)
659 {
660 string varName = arg.substr(0, arg.find('+'));
661 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
662 int amount_to_add = atoi(string_to_add.c_str());
663 int value;
664
665 DataManager::GetValue(varName, value);
666 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 return 0;
668 }
that3f7b1ac2014-12-30 11:30:13 +0100669 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 {
that3f7b1ac2014-12-30 11:30:13 +0100671 string varName = arg.substr(0, arg.find('-'));
672 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
673 int amount_to_subtract = atoi(string_to_subtract.c_str());
674 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400675
that3f7b1ac2014-12-30 11:30:13 +0100676 DataManager::GetValue(varName, value);
677 value -= amount_to_subtract;
678 if (value <= 0)
679 value = 0;
680 DataManager::SetValue(varName, value);
681 return 0;
682 }
683 if (arg.find("*") != string::npos)
684 {
685 string varName = arg.substr(0, arg.find('*'));
686 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
687 int multiply_by = atoi(multiply_by_str.c_str());
688 int value;
689
690 DataManager::GetValue(varName, value);
691 DataManager::SetValue(varName, value*multiply_by);
692 return 0;
693 }
694 if (arg.find("/") != string::npos)
695 {
696 string varName = arg.substr(0, arg.find('/'));
697 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
698 int divide_by = atoi(divide_by_str.c_str());
699 int value;
700
701 if(divide_by != 0)
702 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100704 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 }
706 return 0;
707 }
that3f7b1ac2014-12-30 11:30:13 +0100708 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
709 return -1;
710}
Dees_Troy51a0e822012-09-05 15:24:24 -0400711
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500712int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100713{
714 string SelectedZone;
715 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
716 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
717 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
718
719 int dst;
720 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
721
722 string offset;
723 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
724
725 string NewTimeZone = Zone;
726 if (offset != "0")
727 NewTimeZone += ":" + offset;
728
729 if (dst != 0)
730 NewTimeZone += DSTZone;
731
732 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
733 DataManager::update_tz_environment_variables();
734 return 0;
735}
736
737int GUIAction::overlay(std::string arg)
738{
739 return gui_changeOverlay(arg);
740}
741
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500742int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100743{
744 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500745 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 return 0;
747 }
that3f7b1ac2014-12-30 11:30:13 +0100748 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
749 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
750 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100752 }
753 return 0;
754}
755
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500756int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100757{
758 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500759 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400760 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100761 } else {
762 zip_queue_index--;
763 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 }
that3f7b1ac2014-12-30 11:30:13 +0100765 return 0;
766}
Dees_Troy51a0e822012-09-05 15:24:24 -0400767
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500768int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100769{
770 zip_queue_index = 0;
771 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
772 return 0;
773}
Dees_Troy51a0e822012-09-05 15:24:24 -0400774
that3f7b1ac2014-12-30 11:30:13 +0100775int GUIAction::sleep(std::string arg)
776{
777 operation_start("Sleep");
778 usleep(atoi(arg.c_str()));
779 operation_end(0);
780 return 0;
781}
Dees Troyb21cc642013-09-10 17:36:41 +0000782
Matt Mower9a2a2052016-05-31 21:31:22 -0500783int GUIAction::sleepcounter(std::string arg)
784{
785 operation_start("SleepCounter");
786 // Ensure user notices countdown in case it needs to be cancelled
787 blankTimer.resetTimerAndUnblank();
788 int total = atoi(arg.c_str());
789 for (int t = total; t > 0; t--) {
790 int progress = (int)(((float)(total-t)/(float)total)*100.0);
791 DataManager::SetValue("ui_progress", progress);
792 ::sleep(1);
793 DataManager::SetValue("tw_sleep", t-1);
794 }
795 DataManager::SetValue("ui_progress", 100);
796 operation_end(0);
797 return 0;
798}
799
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500800int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100801{
802 operation_start("AppendDateToBackupName");
803 string Backup_Name;
804 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
805 Backup_Name += TWFunc::Get_Current_Date();
806 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
807 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
808 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500809 PageManager::NotifyKey(KEY_END, true);
810 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100811 operation_end(0);
812 return 0;
813}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500814
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500815int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100816{
817 operation_start("GenerateBackupName");
818 TWFunc::Auto_Generate_Backup_Name();
819 operation_end(0);
820 return 0;
821}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500822
Ethan Yonker483e9f42016-01-11 22:21:18 -0600823int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100824{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600825 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100826 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500827
Ethan Yonker483e9f42016-01-11 22:21:18 -0600828 if (arg.empty())
829 arg = "tw_wipe_list";
830 DataManager::GetValue(arg, List);
831 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
832 if (!List.empty()) {
833 size_t start_pos = 0, end_pos = List.find(";", start_pos);
834 while (end_pos != string::npos && start_pos < List.size()) {
835 part_path = List.substr(start_pos, end_pos - start_pos);
836 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
837 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100838 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400839 } else {
that3f7b1ac2014-12-30 11:30:13 +0100840 count++;
841 }
842 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600843 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100844 }
845 DataManager::SetValue("tw_check_partition_list", count);
846 } else {
847 DataManager::SetValue("tw_check_partition_list", 0);
848 }
849 return 0;
850}
Dees_Troy51a0e822012-09-05 15:24:24 -0400851
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100853{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600854 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100855 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400856
Ethan Yonker483e9f42016-01-11 22:21:18 -0600857 if (arg.empty())
858 arg = "tw_wipe_list";
859 DataManager::GetValue(arg, List);
860 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
861 if (!List.empty()) {
862 size_t start_pos = 0, end_pos = List.find(";", start_pos);
863 part_path = List;
864 while (end_pos != string::npos && start_pos < List.size()) {
865 part_path = List.substr(start_pos, end_pos - start_pos);
866 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
867 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100868 // Do nothing
869 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100871 break;
872 }
873 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600874 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100875 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600876 if (!part_path.empty()) {
877 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100878 if (Part) {
879 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500880
that3f7b1ac2014-12-30 11:30:13 +0100881 DataManager::SetValue("tw_partition_name", Part->Display_Name);
882 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
883 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
884 DataManager::SetValue("tw_partition_size", Part->Size / mb);
885 DataManager::SetValue("tw_partition_used", Part->Used / mb);
886 DataManager::SetValue("tw_partition_free", Part->Free / mb);
887 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
888 DataManager::SetValue("tw_partition_removable", Part->Removable);
889 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
890
891 if (Part->Can_Repair())
892 DataManager::SetValue("tw_partition_can_repair", 1);
893 else
894 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500895 if (Part->Can_Resize())
896 DataManager::SetValue("tw_partition_can_resize", 1);
897 else
898 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600899 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100900 DataManager::SetValue("tw_partition_vfat", 1);
901 else
902 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600903 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100904 DataManager::SetValue("tw_partition_exfat", 1);
905 else
906 DataManager::SetValue("tw_partition_exfat", 0);
907 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
908 DataManager::SetValue("tw_partition_f2fs", 1);
909 else
910 DataManager::SetValue("tw_partition_f2fs", 0);
911 if (TWFunc::Path_Exists("/sbin/mke2fs"))
912 DataManager::SetValue("tw_partition_ext", 1);
913 else
914 DataManager::SetValue("tw_partition_ext", 0);
915 return 0;
916 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600917 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100918 }
919 }
920 }
921 DataManager::SetValue("tw_partition_name", "");
922 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600923 // Set this to 0 to prevent trying to partition this device, just in case
924 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100925 return 0;
926}
927
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500928int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100929{
930 time_t tm;
931 char path[256];
932 int path_len;
933 uid_t uid = -1;
934 gid_t gid = -1;
935
936 struct passwd *pwd = getpwnam("media_rw");
937 if(pwd) {
938 uid = pwd->pw_uid;
939 gid = pwd->pw_gid;
940 }
941
942 const std::string storage = DataManager::GetCurrentStoragePath();
943 if(PartitionManager.Is_Mounted_By_Path(storage)) {
944 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
945 } else {
946 strcpy(path, "/tmp/");
947 }
948
Xuefer579e9072016-01-22 13:35:50 +0800949 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100950 return 0;
951
952 tm = time(NULL);
953 path_len = strlen(path);
954
955 // Screenshot_2014-01-01-18-21-38.png
956 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
957
958 int res = gr_save_screenshot(path);
959 if(res == 0) {
960 chmod(path, 0666);
961 chown(path, uid, gid);
962
that677b13f2015-12-26 23:57:31 +0100963 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100964
965 // blink to notify that the screenshow was taken
966 gr_color(255, 255, 255, 255);
967 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
968 gr_flip();
969 gui_forceRender();
970 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500971 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100972 }
973 return 0;
974}
975
976int GUIAction::setbrightness(std::string arg)
977{
978 return TWFunc::Set_Brightness(arg);
979}
980
981int GUIAction::fileexists(std::string arg)
982{
983 struct stat st;
984 string newpath = arg + "/.";
985
986 operation_start("FileExists");
987 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
988 operation_end(0);
989 else
990 operation_end(1);
991 return 0;
992}
993
thatcc8ddca2015-01-03 01:59:36 +0100994void GUIAction::reinject_after_flash()
995{
996 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500997 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +0100998 if (simulate) {
999 simulate_progress_bar();
1000 } else {
1001 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1002 if (Boot == NULL || Boot->Current_File_System != "emmc")
1003 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1004 else {
1005 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1006 TWFunc::Exec_Cmd(injectcmd);
1007 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001008 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001009 }
1010 }
1011}
1012
that3f7b1ac2014-12-30 11:30:13 +01001013int GUIAction::flash(std::string arg)
1014{
1015 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001016 // We're going to jump to this page first, like a loading page
1017 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001018 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001019 string zip_path = zip_queue[i];
1020 size_t slashpos = zip_path.find_last_of('/');
1021 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001022 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001023 DataManager::SetValue("tw_filename", zip_path);
1024 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001025 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1026
1027 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001028 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001029 TWFunc::SetPerformanceMode(false);
1030 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001031 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001032 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001033 break;
that3f7b1ac2014-12-30 11:30:13 +01001034 }
1035 }
1036 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001037
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001038 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001039 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001040 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001041 }
that3f7b1ac2014-12-30 11:30:13 +01001042
thatcc8ddca2015-01-03 01:59:36 +01001043 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001044 PartitionManager.Update_System_Details();
1045 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001046 // 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 -06001047 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001048 return 0;
1049}
1050
1051int GUIAction::wipe(std::string arg)
1052{
1053 operation_start("Format");
1054 DataManager::SetValue("tw_partition", arg);
1055
1056 int ret_val = false;
1057
1058 if (simulate) {
1059 simulate_progress_bar();
1060 } else {
1061 if (arg == "data")
1062 ret_val = PartitionManager.Factory_Reset();
1063 else if (arg == "battery")
1064 ret_val = PartitionManager.Wipe_Battery_Stats();
1065 else if (arg == "rotate")
1066 ret_val = PartitionManager.Wipe_Rotate_Data();
1067 else if (arg == "dalvik")
1068 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1069 else if (arg == "DATAMEDIA") {
1070 ret_val = PartitionManager.Format_Data();
1071 } else if (arg == "INTERNAL") {
1072 int has_datamedia, dual_storage;
1073
1074 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1075 if (has_datamedia) {
1076 ret_val = PartitionManager.Wipe_Media_From_Data();
1077 } else {
1078 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1079 }
1080 } else if (arg == "EXTERNAL") {
1081 string External_Path;
1082
1083 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1084 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1085 } else if (arg == "ANDROIDSECURE") {
1086 ret_val = PartitionManager.Wipe_Android_Secure();
1087 } else if (arg == "LIST") {
1088 string Wipe_List, wipe_path;
1089 bool skip = false;
1090 ret_val = true;
1091 TWPartition* wipe_part = NULL;
1092
1093 DataManager::GetValue("tw_wipe_list", Wipe_List);
1094 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1095 if (!Wipe_List.empty()) {
1096 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1097 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1098 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1099 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1100 if (wipe_path == "/and-sec") {
1101 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001102 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001103 ret_val = false;
1104 break;
1105 } else {
1106 skip = true;
1107 }
1108 } else if (wipe_path == "DALVIK") {
1109 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001110 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001111 ret_val = false;
1112 break;
1113 } else {
1114 skip = true;
1115 }
1116 } else if (wipe_path == "INTERNAL") {
1117 if (!PartitionManager.Wipe_Media_From_Data()) {
1118 ret_val = false;
1119 break;
1120 } else {
1121 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001122 }
1123 }
that3f7b1ac2014-12-30 11:30:13 +01001124 if (!skip) {
1125 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001126 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001127 ret_val = false;
1128 break;
1129 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1130 arg = wipe_path;
1131 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001132 } else {
that3f7b1ac2014-12-30 11:30:13 +01001133 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001134 }
that3f7b1ac2014-12-30 11:30:13 +01001135 start_pos = end_pos + 1;
1136 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001137 }
1138 }
that3f7b1ac2014-12-30 11:30:13 +01001139 } else
1140 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001141#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001142 if (arg == DataManager::GetSettingsStoragePath()) {
1143 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1144 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001145
that3f7b1ac2014-12-30 11:30:13 +01001146 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1147 LOGINFO("Making TWRP folder and saving settings.\n");
1148 Storage_Path += "/TWRP";
1149 mkdir(Storage_Path.c_str(), 0777);
1150 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001151 } else {
that3f7b1ac2014-12-30 11:30:13 +01001152 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1153 }
1154 }
1155#endif
1156 }
1157 PartitionManager.Update_System_Details();
1158 if (ret_val)
1159 ret_val = 0; // 0 is success
1160 else
1161 ret_val = 1; // 1 is failure
1162 operation_end(ret_val);
1163 return 0;
1164}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001165
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001166int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001167{
1168 operation_start("Refreshing Sizes");
1169 if (simulate) {
1170 simulate_progress_bar();
1171 } else
1172 PartitionManager.Update_System_Details();
1173 operation_end(0);
1174 return 0;
1175}
1176
1177int GUIAction::nandroid(std::string arg)
1178{
that3f7b1ac2014-12-30 11:30:13 +01001179 if (simulate) {
that73a52952015-01-28 23:07:34 +01001180 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001181 DataManager::SetValue("tw_partition", "Simulation");
1182 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001183 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001184 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001185 operation_start("Nandroid");
1186 int ret = 0;
1187
that3f7b1ac2014-12-30 11:30:13 +01001188 if (arg == "backup") {
1189 string Backup_Name;
1190 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001191 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1192 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 -05001193 ret = PartitionManager.Run_Backup(false);
1194 } else {
that3f7b1ac2014-12-30 11:30:13 +01001195 operation_end(1);
1196 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001197 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001198 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001199 } else if (arg == "restore") {
1200 string Restore_Name;
1201 DataManager::GetValue("tw_restore", Restore_Name);
1202 ret = PartitionManager.Run_Restore(Restore_Name);
1203 } else {
1204 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001205 return -1;
1206 }
1207 DataManager::SetValue("tw_encrypt_backup", 0);
1208 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001209 if (ret == false)
1210 ret = 1; // 1 for failure
1211 else
1212 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001213 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001214 }
1215 else {
1216 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001217 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001218 ret = 0;
1219 }
that73a52952015-01-28 23:07:34 +01001220 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001221 return ret;
1222 }
1223 return 0;
1224}
1225
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001226int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001227 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001228 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001229 }
1230 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001231 int op_status = PartitionManager.Cancel_Backup();
1232 if (op_status != 0)
1233 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001234 }
1235
1236 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001237}
Dees_Troy51a0e822012-09-05 15:24:24 -04001238
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001239int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001240{
that73a52952015-01-28 23:07:34 +01001241 int op_status = 0;
1242
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001243 operation_start("Fix Contexts");
1244 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001245 if (simulate) {
1246 simulate_progress_bar();
1247 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001248 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001249 if (op_status != 0)
1250 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001251 }
that73a52952015-01-28 23:07:34 +01001252 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001253 return 0;
1254}
1255
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001256int GUIAction::fixpermissions(std::string arg)
1257{
1258 return fixcontexts(arg);
1259}
1260
that3f7b1ac2014-12-30 11:30:13 +01001261int GUIAction::dd(std::string arg)
1262{
1263 operation_start("imaging");
1264
1265 if (simulate) {
1266 simulate_progress_bar();
1267 } else {
1268 string cmd = "dd " + arg;
1269 TWFunc::Exec_Cmd(cmd);
1270 }
1271 operation_end(0);
1272 return 0;
1273}
1274
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001275int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001276{
1277 operation_start("Partition SD Card");
1278 int ret_val = 0;
1279
1280 if (simulate) {
1281 simulate_progress_bar();
1282 } else {
1283 int allow_partition;
1284 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1285 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001286 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001287 } else {
1288 if (!PartitionManager.Partition_SDCard())
1289 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001290 }
that3f7b1ac2014-12-30 11:30:13 +01001291 }
1292 operation_end(ret_val);
1293 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001294
that3f7b1ac2014-12-30 11:30:13 +01001295}
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001297int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001298{
1299 operation_start("Install HTC Dumlock");
1300 if (simulate) {
1301 simulate_progress_bar();
1302 } else
1303 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001304
that3f7b1ac2014-12-30 11:30:13 +01001305 operation_end(0);
1306 return 0;
1307}
Dees_Troy51a0e822012-09-05 15:24:24 -04001308
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001309int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001310{
1311 operation_start("HTC Dumlock Restore Boot");
1312 if (simulate) {
1313 simulate_progress_bar();
1314 } else
1315 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001316
that3f7b1ac2014-12-30 11:30:13 +01001317 operation_end(0);
1318 return 0;
1319}
Dees_Troy51a0e822012-09-05 15:24:24 -04001320
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001321int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001322{
1323 operation_start("HTC Dumlock Reflash Recovery");
1324 if (simulate) {
1325 simulate_progress_bar();
1326 } else
1327 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001328
that3f7b1ac2014-12-30 11:30:13 +01001329 operation_end(0);
1330 return 0;
1331}
Dees_Troy51a0e822012-09-05 15:24:24 -04001332
that3f7b1ac2014-12-30 11:30:13 +01001333int GUIAction::cmd(std::string arg)
1334{
1335 int op_status = 0;
1336
1337 operation_start("Command");
1338 LOGINFO("Running command: '%s'\n", arg.c_str());
1339 if (simulate) {
1340 simulate_progress_bar();
1341 } else {
1342 op_status = TWFunc::Exec_Cmd(arg);
1343 if (op_status != 0)
1344 op_status = 1;
1345 }
1346
1347 operation_end(op_status);
1348 return 0;
1349}
1350
1351int GUIAction::terminalcommand(std::string arg)
1352{
1353 int op_status = 0;
1354 string cmdpath, command;
1355
1356 DataManager::GetValue("tw_terminal_location", cmdpath);
1357 operation_start("CommandOutput");
1358 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1359 if (simulate) {
1360 simulate_progress_bar();
1361 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001362 } else if (arg == "exit") {
1363 LOGINFO("Exiting terminal\n");
1364 operation_end(op_status);
1365 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001366 } else {
1367 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1368 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001369 DataManager::SetValue("tw_terminal_state", 1);
1370 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001371 FILE* fp;
1372 char line[512];
1373
1374 fp = popen(command.c_str(), "r");
1375 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001376 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001377 } else {
1378 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1379 struct timeval timeout;
1380 fd_set fdset;
1381
1382 while(keep_going)
1383 {
1384 FD_ZERO(&fdset);
1385 FD_SET(fd, &fdset);
1386 timeout.tv_sec = 0;
1387 timeout.tv_usec = 400000;
1388 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1389 if (has_data == 0) {
1390 // Timeout reached
1391 DataManager::GetValue("tw_terminal_state", check);
1392 if (check == 0) {
1393 keep_going = 0;
1394 }
1395 } else if (has_data < 0) {
1396 // End of execution
1397 keep_going = 0;
1398 } else {
1399 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001400 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001401 gui_print("%s", line); // Display output
1402 else
1403 keep_going = 0; // Done executing
1404 }
1405 }
1406 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001407 }
thatc6085482015-01-09 22:12:43 +01001408 DataManager::SetValue("tw_operation_status", 0);
1409 DataManager::SetValue("tw_operation_state", 1);
1410 DataManager::SetValue("tw_terminal_state", 0);
1411 DataManager::SetValue("tw_background_thread_running", 0);
1412 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001413 }
1414 return 0;
1415}
1416
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001417int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001418{
1419 int op_status = 0;
1420
1421 LOGINFO("Sending kill command...\n");
1422 operation_start("KillCommand");
1423 DataManager::SetValue("tw_operation_status", 0);
1424 DataManager::SetValue("tw_operation_state", 1);
1425 DataManager::SetValue("tw_terminal_state", 0);
1426 DataManager::SetValue("tw_background_thread_running", 0);
1427 DataManager::SetValue(TW_ACTION_BUSY, 0);
1428 return 0;
1429}
1430
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001431int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001432{
1433 int op_status = 0;
1434 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001435 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001436 if (simulate) {
1437 simulate_progress_bar();
1438 } else {
1439 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001440 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001441 }
1442
1443 operation_end(op_status);
1444 return 0;
1445}
1446
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001447int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001448{
1449 int op_status = 0;
1450
1451 operation_start("CheckBackupName");
1452 if (simulate) {
1453 simulate_progress_bar();
1454 } else {
1455 op_status = PartitionManager.Check_Backup_Name(true);
1456 if (op_status != 0)
1457 op_status = 1;
1458 }
1459
1460 operation_end(op_status);
1461 return 0;
1462}
1463
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001464int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001465{
1466 int op_status = 0;
1467
1468 operation_start("Decrypt");
1469 if (simulate) {
1470 simulate_progress_bar();
1471 } else {
1472 string Password;
1473 DataManager::GetValue("tw_crypto_password", Password);
1474 op_status = PartitionManager.Decrypt_Device(Password);
1475 if (op_status != 0)
1476 op_status = 1;
1477 else {
that3f7b1ac2014-12-30 11:30:13 +01001478
1479 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1480
Ethan Yonkercf50da52015-01-12 21:59:07 -06001481 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001482
Ethan Yonkercf50da52015-01-12 21:59:07 -06001483 // Check for a custom theme and load it if exists
1484 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1485 if (has_datamedia != 0) {
1486 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001487 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001488 } else {
1489 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001490 }
1491 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001492 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001493 }
1494 }
1495
1496 operation_end(op_status);
1497 return 0;
1498}
1499
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001500int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001501{
1502 operation_start("Sideload");
1503 if (simulate) {
1504 simulate_progress_bar();
1505 operation_end(0);
1506 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001507 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001508 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1509
1510 // wait for the adb connection
1511 int ret = apply_from_adb("/", &sideload_child_pid);
1512 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1513
1514 if (ret != 0) {
1515 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001517 ret = 1; // failure
1518 } else {
1519 int wipe_cache = 0;
1520 int wipe_dalvik = 0;
1521 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1522
1523 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1524 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1525 PartitionManager.Wipe_By_Path("/cache");
1526 if (wipe_dalvik)
1527 PartitionManager.Wipe_Dalvik_Cache();
1528 } else {
1529 ret = 1; // failure
1530 }
thatcc8ddca2015-01-03 01:59:36 +01001531 }
thatc6085482015-01-09 22:12:43 +01001532 if (sideload_child_pid) {
1533 LOGINFO("Signaling child sideload process to exit.\n");
1534 struct stat st;
1535 // Calling stat() on this magic filename signals the minadbd
1536 // subprocess to shut down.
1537 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1538 int status;
1539 LOGINFO("Waiting for child sideload process to exit.\n");
1540 waitpid(sideload_child_pid, &status, 0);
1541 }
Dees Troy28a85d22015-04-28 02:03:16 +00001542 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001543 TWFunc::Toggle_MTP(mtp_was_enabled);
1544 reinject_after_flash();
1545 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001546 }
that3f7b1ac2014-12-30 11:30:13 +01001547 return 0;
1548}
1549
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001550int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001551{
that3f7b1ac2014-12-30 11:30:13 +01001552 struct stat st;
1553 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001554 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001555 LOGINFO("Signaling child sideload process to exit.\n");
1556 // Calling stat() on this magic filename signals the minadbd
1557 // subprocess to shut down.
1558 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1559 if (!sideload_child_pid) {
1560 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001561 return 0;
1562 }
thatcc8ddca2015-01-03 01:59:36 +01001563 ::sleep(1);
1564 LOGINFO("Killing child sideload process.\n");
1565 kill(sideload_child_pid, SIGTERM);
1566 int status;
1567 LOGINFO("Waiting for child sideload process to exit.\n");
1568 waitpid(sideload_child_pid, &status, 0);
1569 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001570 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1571 return 0;
1572}
1573
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001574int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001575{
1576 operation_start("OpenRecoveryScript");
1577 if (simulate) {
1578 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001579 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001580 } else {
that10ae24f2015-12-26 20:53:51 +01001581 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001582 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001583 }
1584 return 0;
1585}
1586
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001587int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001588{
1589 int op_status = 0;
1590
1591 operation_start("Install SuperSU");
1592 if (simulate) {
1593 simulate_progress_bar();
1594 } else {
1595 if (!TWFunc::Install_SuperSU())
1596 op_status = 1;
1597 }
1598
1599 operation_end(op_status);
1600 return 0;
1601}
1602
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001603int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001604{
1605 int op_status = 0;
1606
1607 operation_start("Fixing Superuser Permissions");
1608 if (simulate) {
1609 simulate_progress_bar();
1610 } else {
1611 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1612 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1613 }
1614
1615 operation_end(op_status);
1616 return 0;
1617}
1618
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001619int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001620{
1621 int op_status = 0;
1622
1623 operation_start("Try Restore Decrypt");
1624 if (simulate) {
1625 simulate_progress_bar();
1626 } else {
1627 string Restore_Path, Filename, Password;
1628 DataManager::GetValue("tw_restore", Restore_Path);
1629 Restore_Path += "/";
1630 DataManager::GetValue("tw_restore_password", Password);
1631 TWFunc::SetPerformanceMode(true);
1632 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1633 op_status = 0; // success
1634 else
1635 op_status = 1; // fail
1636 TWFunc::SetPerformanceMode(false);
1637 }
1638
1639 operation_end(op_status);
1640 return 0;
1641}
1642
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001643int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001644{
1645 int op_status = 0;
1646
1647 operation_start("Repair Partition");
1648 if (simulate) {
1649 simulate_progress_bar();
1650 } else {
1651 string part_path;
1652 DataManager::GetValue("tw_partition_mount_point", part_path);
1653 if (PartitionManager.Repair_By_Path(part_path, true)) {
1654 op_status = 0; // success
1655 } else {
that3f7b1ac2014-12-30 11:30:13 +01001656 op_status = 1; // fail
1657 }
1658 }
1659
1660 operation_end(op_status);
1661 return 0;
1662}
1663
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001664int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001665{
1666 int op_status = 0;
1667
1668 operation_start("Resize Partition");
1669 if (simulate) {
1670 simulate_progress_bar();
1671 } else {
1672 string part_path;
1673 DataManager::GetValue("tw_partition_mount_point", part_path);
1674 if (PartitionManager.Resize_By_Path(part_path, true)) {
1675 op_status = 0; // success
1676 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001677 op_status = 1; // fail
1678 }
1679 }
1680
1681 operation_end(op_status);
1682 return 0;
1683}
1684
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001685int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001686{
1687 int op_status = 0;
1688
1689 operation_start("Change File System");
1690 if (simulate) {
1691 simulate_progress_bar();
1692 } else {
1693 string part_path, file_system;
1694 DataManager::GetValue("tw_partition_mount_point", part_path);
1695 DataManager::GetValue("tw_action_new_file_system", file_system);
1696 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1697 op_status = 0; // success
1698 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001699 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001700 op_status = 1; // fail
1701 }
1702 }
1703 PartitionManager.Update_System_Details();
1704 operation_end(op_status);
1705 return 0;
1706}
1707
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001708int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001709{
1710 int op_status = 0;
1711
1712 operation_start("Start MTP");
1713 if (PartitionManager.Enable_MTP())
1714 op_status = 0; // success
1715 else
1716 op_status = 1; // fail
1717
1718 operation_end(op_status);
1719 return 0;
1720}
1721
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001722int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001723{
1724 int op_status = 0;
1725
1726 operation_start("Stop MTP");
1727 if (PartitionManager.Disable_MTP())
1728 op_status = 0; // success
1729 else
1730 op_status = 1; // fail
1731
1732 operation_end(op_status);
1733 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001734}
1735
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001736int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001737{
1738 int op_status = 0;
1739
1740 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001741 string path, filename;
1742 DataManager::GetValue("tw_zip_location", path);
1743 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001744 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001745 op_status = 0; // success
1746 else
1747 op_status = 1; // fail
1748
1749 operation_end(op_status);
1750 return 0;
1751}
1752
that10ae24f2015-12-26 20:53:51 +01001753int GUIAction::twcmd(std::string arg)
1754{
1755 operation_start("TWRP CLI Command");
1756 if (simulate)
1757 simulate_progress_bar();
1758 else
1759 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1760 operation_end(0);
1761 return 0;
1762}
1763
Dees_Troy51a0e822012-09-05 15:24:24 -04001764int GUIAction::getKeyByName(std::string key)
1765{
that8834a0f2016-01-05 23:29:30 +01001766 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001767 else if (key == "menu") return KEY_MENU;
1768 else if (key == "back") return KEY_BACK;
1769 else if (key == "search") return KEY_SEARCH;
1770 else if (key == "voldown") return KEY_VOLUMEDOWN;
1771 else if (key == "volup") return KEY_VOLUMEUP;
1772 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001773 int ret_val;
1774 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1775 if (!ret_val)
1776 return KEY_POWER;
1777 else
1778 return ret_val;
1779 }
1780
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001781 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001782}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001783
1784int GUIAction::checkpartitionlifetimewrites(std::string arg)
1785{
1786 int op_status = 0;
1787 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1788
1789 operation_start("Check Partition Lifetime Writes");
1790 if (sys) {
1791 if (sys->Check_Lifetime_Writes() != 0)
1792 DataManager::SetValue("tw_lifetime_writes", 1);
1793 else
1794 DataManager::SetValue("tw_lifetime_writes", 0);
1795 op_status = 0; // success
1796 } else {
1797 DataManager::SetValue("tw_lifetime_writes", 1);
1798 op_status = 1; // fail
1799 }
1800
1801 operation_end(op_status);
1802 return 0;
1803}
1804
1805int GUIAction::mountsystemtoggle(std::string arg)
1806{
1807 int op_status = 0;
1808 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001809 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001810
1811 operation_start("Toggle System Mount");
1812 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1813 op_status = 1; // fail
1814 } else {
1815 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1816 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001817 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001818 DataManager::SetValue("tw_mount_system_ro", 0);
1819 Part->Change_Mount_Read_Only(false);
1820 } else {
1821 DataManager::SetValue("tw_mount_system_ro", 1);
1822 Part->Change_Mount_Read_Only(true);
1823 }
1824 if (remount_system) {
1825 Part->Mount(true);
1826 }
1827 op_status = 0; // success
1828 } else {
1829 op_status = 1; // fail
1830 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001831 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1832 if (Part) {
1833 if (arg == "0") {
1834 Part->Change_Mount_Read_Only(false);
1835 } else {
1836 Part->Change_Mount_Read_Only(true);
1837 }
1838 if (remount_vendor) {
1839 Part->Mount(true);
1840 }
1841 op_status = 0; // success
1842 } else {
1843 op_status = 1; // fail
1844 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001845 }
1846
1847 operation_end(op_status);
1848 return 0;
1849}
Ethan Yonker74db1572015-10-28 12:44:49 -05001850
1851int GUIAction::setlanguage(std::string arg __unused)
1852{
1853 int op_status = 0;
1854
1855 operation_start("Set Language");
1856 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1857 PageManager::RequestReload();
1858 op_status = 0; // success
1859
1860 operation_end(op_status);
1861 return 0;
1862}