blob: 0f8c7eea62443d67e42d635e421f667d50fd3ff1 [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../minuitwrp/minui.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"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060053#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
55
56#include "rapidxml.hpp"
57#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050058#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Dees_Troy51a0e822012-09-05 15:24:24 -040060void curtainClose(void);
61
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
66static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010067pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
111 if(m_thread_running) {
112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
184 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);
thatc6085482015-01-09 22:12:43 +0100200
201 // remember actions that run in the caller thread
202 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
203 setActionsRunningInCallerThread.insert(it->first);
204
205 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100206 ADD_ACTION(flash);
207 ADD_ACTION(wipe);
208 ADD_ACTION(refreshsizes);
209 ADD_ACTION(nandroid);
210 ADD_ACTION(fixpermissions);
211 ADD_ACTION(dd);
212 ADD_ACTION(partitionsd);
213 ADD_ACTION(installhtcdumlock);
214 ADD_ACTION(htcdumlockrestoreboot);
215 ADD_ACTION(htcdumlockreflashrecovery);
216 ADD_ACTION(cmd);
217 ADD_ACTION(terminalcommand);
218 ADD_ACTION(reinjecttwrp);
219 ADD_ACTION(decrypt);
220 ADD_ACTION(adbsideload);
221 ADD_ACTION(openrecoveryscript);
222 ADD_ACTION(installsu);
223 ADD_ACTION(decrypt_backup);
224 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500225 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100226 ADD_ACTION(changefilesystem);
227 ADD_ACTION(flashimage);
that3f7b1ac2014-12-30 11:30:13 +0100228 }
229
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600231 actions = FindNode(node, "actions");
232 if (actions) child = FindNode(actions, "action");
233 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 while (child)
238 {
239 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 attr = child->first_attribute("function");
242 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 action.mFunction = attr->value();
245 action.mArg = child->value();
246 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 child = child->next_sibling("action");
249 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600252 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 if (child)
254 {
255 attr = child->first_attribute("key");
256 if (attr)
257 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100258 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
259 for(size_t i = 0; i < keys.size(); ++i)
260 {
261 const int key = getKeyByName(keys[i]);
262 mKeys[key] = false;
263 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 }
265 else
266 {
267 attr = child->first_attribute("x");
268 if (!attr) return;
269 mActionX = atol(attr->value());
270 attr = child->first_attribute("y");
271 if (!attr) return;
272 mActionY = atol(attr->value());
273 attr = child->first_attribute("w");
274 if (!attr) return;
275 mActionW = atol(attr->value());
276 attr = child->first_attribute("h");
277 if (!attr) return;
278 mActionH = atol(attr->value());
279 }
280 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400281}
282
283int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
284{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 if (state == TOUCH_RELEASE)
286 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289}
290
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100291int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400292{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100293 if (mKeys.empty())
294 return 0;
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())
298 return 0;
299
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) {
308 if(!down && prevState)
309 doActions();
310 } else if(down) {
311 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
312 if(!itr->second)
313 return 0;
314 }
315
316 // Passed, all req buttons are pressed, reset them and consume release events
317 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
318 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
319 kb->ConsumeKeyRelease(itr->first);
320 itr->second = false;
321 }
322
323 doActions();
324 }
325
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 return 0;
327}
328
Vojtech Bocek07220562014-02-08 02:05:33 +0100329int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400330{
Vojtech Bocek07220562014-02-08 02:05:33 +0100331 GUIObject::NotifyVarChange(varName, value);
332
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100333 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100335 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400337
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400339}
340
341void GUIAction::simulate_progress_bar(void)
342{
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400344 for (int i = 0; i < 5; i++)
345 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500346 if (PartitionManager.stop_backup.get_value()) {
347 DataManager::SetValue("tw_cancel_backup", 1);
348 gui_print("Backup Canceled.\n");
349 DataManager::SetValue("ui_progress", 0);
350 PartitionManager.stop_backup.set_value(0);
351 return;
352 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400353 usleep(500000);
354 DataManager::SetValue("ui_progress", i * 20);
355 }
356}
357
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600358int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400359{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400361
362 DataManager::SetValue("ui_progress", 0);
363
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 if (filename.empty())
365 {
366 LOGERR("No file specified.\n");
367 return -1;
368 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400371 return -1;
372
Dees_Troy51a0e822012-09-05 15:24:24 -0400373 if (simulate) {
374 simulate_progress_bar();
375 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400376 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400377
378 // Now, check if we need to ensure TWRP remains installed...
379 struct stat st;
380 if (stat("/sbin/installTwrp", &st) == 0)
381 {
382 DataManager::SetValue("tw_operation", "Configuring TWRP");
383 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000384 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200385 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000387 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400388 }
389 }
390 }
391
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 // Done
393 DataManager::SetValue("ui_progress", 100);
394 DataManager::SetValue("ui_progress", 0);
395 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
that73a52952015-01-28 23:07:34 +0100398GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100399{
that73a52952015-01-28 23:07:34 +0100400 string func = gui_parse_text(action.mFunction);
401 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
402 if (needsThread) {
403 if (func == "cancelbackup")
404 return THREAD_CANCEL;
405 else
406 return THREAD_ACTION;
407 }
408 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100409}
410
Dees_Troy51a0e822012-09-05 15:24:24 -0400411int GUIAction::doActions()
412{
that3f7b1ac2014-12-30 11:30:13 +0100413 if (mActions.size() < 1)
414 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415
that73a52952015-01-28 23:07:34 +0100416 // Determine in which thread to run the actions.
417 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
418 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100419 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100420 for (it = mActions.begin(); it != mActions.end(); ++it) {
421 ThreadType tt = getThreadType(*it);
422 if (tt == THREAD_NONE)
423 continue;
424 if (threadType == THREAD_NONE)
425 threadType = tt;
426 else if (threadType != tt) {
427 LOGERR("Can't mix normal and cancel actions in the same list.\n"
428 "Running the whole batch in the cancel thread.\n");
429 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100430 break;
431 }
that7d3b54f2015-01-09 22:52:51 +0100432 }
that73a52952015-01-28 23:07:34 +0100433
434 // Now run the actions in the desired thread.
435 switch (threadType) {
436 case THREAD_ACTION:
437 action_thread.threadActions(this);
438 break;
439
440 case THREAD_CANCEL:
441 cancel_thread.threadActions(this);
442 break;
443
444 default: {
445 // no iterators here because theme reloading might kill our object
446 const size_t cnt = mActions.size();
447 for (size_t i = 0; i < cnt; ++i)
448 doAction(mActions[i]);
449 }
thatc6085482015-01-09 22:12:43 +0100450 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400453}
454
that3f7b1ac2014-12-30 11:30:13 +0100455int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400456{
that3f7b1ac2014-12-30 11:30:13 +0100457 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
that3f7b1ac2014-12-30 11:30:13 +0100459 std::string function = gui_parse_text(action.mFunction);
460 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
that3f7b1ac2014-12-30 11:30:13 +0100462 // find function and execute it
463 mapFunc::const_iterator funcitr = mf.find(function);
464 if (funcitr != mf.end())
465 return (this->*funcitr->second)(arg);
466
467 LOGERR("Unknown action '%s'\n", function.c_str());
468 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400469}
470
471void GUIAction::operation_start(const string operation_name)
472{
that3f7b1ac2014-12-30 11:30:13 +0100473 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000474 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 DataManager::SetValue(TW_ACTION_BUSY, 1);
476 DataManager::SetValue("ui_progress", 0);
477 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600479 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
that3f7b1ac2014-12-30 11:30:13 +0100482void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400483{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000484 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 DataManager::SetValue("ui_progress", 100);
487 if (simulate) {
488 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
489 if (simulate_fail != 0)
490 DataManager::SetValue("tw_operation_status", 1);
491 else
492 DataManager::SetValue("tw_operation_status", 0);
493 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500494 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500496 }
497 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500499 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 }
501 DataManager::SetValue("tw_operation_state", 1);
502 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500503 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000504 time(&Stop);
505 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600506 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100507 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508}
509
that3f7b1ac2014-12-30 11:30:13 +0100510int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400511{
that3f7b1ac2014-12-30 11:30:13 +0100512 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400513
that3f7b1ac2014-12-30 11:30:13 +0100514 sync();
515 DataManager::SetValue("tw_gui_done", 1);
516 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400517
that3f7b1ac2014-12-30 11:30:13 +0100518 return 0;
519}
Dees_Troy51a0e822012-09-05 15:24:24 -0400520
that3f7b1ac2014-12-30 11:30:13 +0100521int GUIAction::home(std::string arg)
522{
523 PageManager::SelectPackage("TWRP");
524 gui_changePage("main");
525 return 0;
526}
Dees_Troy51a0e822012-09-05 15:24:24 -0400527
that3f7b1ac2014-12-30 11:30:13 +0100528int GUIAction::key(std::string arg)
529{
530 const int key = getKeyByName(arg);
531 PageManager::NotifyKey(key, true);
532 PageManager::NotifyKey(key, false);
533 return 0;
534}
535
536int GUIAction::page(std::string arg)
537{
538 std::string page_name = gui_parse_text(arg);
539 return gui_changePage(page_name);
540}
541
542int GUIAction::reload(std::string arg)
543{
544 int check = 0, ret_val = 0;
545 std::string theme_path;
546
that3f7b1ac2014-12-30 11:30:13 +0100547 theme_path = DataManager::GetSettingsStoragePath();
548 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
549 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
550 check = 1;
551 }
552
553 theme_path += "/TWRP/theme/ui.zip";
554 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600555 {
that3f7b1ac2014-12-30 11:30:13 +0100556 // Loading the custom theme failed - try loading the stock theme
557 LOGINFO("Attempting to reload stock theme...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000558 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400559 {
that3f7b1ac2014-12-30 11:30:13 +0100560 LOGERR("Failed to load base packages.\n");
561 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 }
that3f7b1ac2014-12-30 11:30:13 +0100564 return 0;
565}
Dees_Troy51a0e822012-09-05 15:24:24 -0400566
that3f7b1ac2014-12-30 11:30:13 +0100567int GUIAction::readBackup(std::string arg)
568{
569 string Restore_Name;
570 DataManager::GetValue("tw_restore", Restore_Name);
571 PartitionManager.Set_Restore_Files(Restore_Name);
572 return 0;
573}
574
575int GUIAction::set(std::string arg)
576{
577 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 {
that3f7b1ac2014-12-30 11:30:13 +0100579 string varName = arg.substr(0, arg.find('='));
580 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
that3f7b1ac2014-12-30 11:30:13 +0100582 DataManager::GetValue(value, value);
583 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200584 }
that3f7b1ac2014-12-30 11:30:13 +0100585 else
586 DataManager::SetValue(arg, "1");
587 return 0;
588}
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
that3f7b1ac2014-12-30 11:30:13 +0100590int GUIAction::clear(std::string arg)
591{
592 DataManager::SetValue(arg, "0");
593 return 0;
594}
Dees_Troy51a0e822012-09-05 15:24:24 -0400595
that3f7b1ac2014-12-30 11:30:13 +0100596int GUIAction::mount(std::string arg)
597{
598 if (arg == "usb") {
599 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100601 PartitionManager.usb_storage_enable();
602 else
603 gui_print("Simulating actions...\n");
604 } else if (!simulate) {
605 PartitionManager.Mount_By_Path(arg, true);
606 PartitionManager.Add_MTP_Storage(arg);
607 } else
608 gui_print("Simulating actions...\n");
609 return 0;
610}
611
612int GUIAction::unmount(std::string arg)
613{
614 if (arg == "usb") {
615 if (!simulate)
616 PartitionManager.usb_storage_disable();
617 else
618 gui_print("Simulating actions...\n");
619 DataManager::SetValue(TW_ACTION_BUSY, 0);
620 } else if (!simulate) {
621 PartitionManager.UnMount_By_Path(arg, true);
622 } else
623 gui_print("Simulating actions...\n");
624 return 0;
625}
626
627int GUIAction::restoredefaultsettings(std::string arg)
628{
629 operation_start("Restore Defaults");
630 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
631 gui_print("Simulating actions...\n");
632 else {
633 DataManager::ResetDefaults();
634 PartitionManager.Update_System_Details();
635 PartitionManager.Mount_Current_Storage(true);
636 }
637 operation_end(0);
638 return 0;
639}
640
641int GUIAction::copylog(std::string arg)
642{
643 operation_start("Copy Log");
644 if (!simulate)
645 {
646 string dst;
647 PartitionManager.Mount_Current_Storage(true);
648 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
649 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
650 tw_set_default_metadata(dst.c_str());
651 sync();
652 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
653 } else
654 simulate_progress_bar();
655 operation_end(0);
656 return 0;
657}
658
659
660int GUIAction::compute(std::string arg)
661{
662 if (arg.find("+") != string::npos)
663 {
664 string varName = arg.substr(0, arg.find('+'));
665 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
666 int amount_to_add = atoi(string_to_add.c_str());
667 int value;
668
669 DataManager::GetValue(varName, value);
670 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 return 0;
672 }
that3f7b1ac2014-12-30 11:30:13 +0100673 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 {
that3f7b1ac2014-12-30 11:30:13 +0100675 string varName = arg.substr(0, arg.find('-'));
676 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
677 int amount_to_subtract = atoi(string_to_subtract.c_str());
678 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400679
that3f7b1ac2014-12-30 11:30:13 +0100680 DataManager::GetValue(varName, value);
681 value -= amount_to_subtract;
682 if (value <= 0)
683 value = 0;
684 DataManager::SetValue(varName, value);
685 return 0;
686 }
687 if (arg.find("*") != string::npos)
688 {
689 string varName = arg.substr(0, arg.find('*'));
690 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
691 int multiply_by = atoi(multiply_by_str.c_str());
692 int value;
693
694 DataManager::GetValue(varName, value);
695 DataManager::SetValue(varName, value*multiply_by);
696 return 0;
697 }
698 if (arg.find("/") != string::npos)
699 {
700 string varName = arg.substr(0, arg.find('/'));
701 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
702 int divide_by = atoi(divide_by_str.c_str());
703 int value;
704
705 if(divide_by != 0)
706 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100708 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 }
710 return 0;
711 }
that3f7b1ac2014-12-30 11:30:13 +0100712 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
713 return -1;
714}
Dees_Troy51a0e822012-09-05 15:24:24 -0400715
that3f7b1ac2014-12-30 11:30:13 +0100716int GUIAction::setguitimezone(std::string arg)
717{
718 string SelectedZone;
719 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
720 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
721 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
722
723 int dst;
724 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
725
726 string offset;
727 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
728
729 string NewTimeZone = Zone;
730 if (offset != "0")
731 NewTimeZone += ":" + offset;
732
733 if (dst != 0)
734 NewTimeZone += DSTZone;
735
736 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
737 DataManager::update_tz_environment_variables();
738 return 0;
739}
740
741int GUIAction::overlay(std::string arg)
742{
743 return gui_changeOverlay(arg);
744}
745
746int GUIAction::queuezip(std::string arg)
747{
748 if (zip_queue_index >= 10) {
749 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 return 0;
751 }
that3f7b1ac2014-12-30 11:30:13 +0100752 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
753 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
754 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100756 }
757 return 0;
758}
759
760int GUIAction::cancelzip(std::string arg)
761{
762 if (zip_queue_index <= 0) {
763 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100765 } else {
766 zip_queue_index--;
767 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 }
that3f7b1ac2014-12-30 11:30:13 +0100769 return 0;
770}
Dees_Troy51a0e822012-09-05 15:24:24 -0400771
that3f7b1ac2014-12-30 11:30:13 +0100772int GUIAction::queueclear(std::string arg)
773{
774 zip_queue_index = 0;
775 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
776 return 0;
777}
Dees_Troy51a0e822012-09-05 15:24:24 -0400778
that3f7b1ac2014-12-30 11:30:13 +0100779int GUIAction::sleep(std::string arg)
780{
781 operation_start("Sleep");
782 usleep(atoi(arg.c_str()));
783 operation_end(0);
784 return 0;
785}
Dees Troyb21cc642013-09-10 17:36:41 +0000786
that3f7b1ac2014-12-30 11:30:13 +0100787int GUIAction::appenddatetobackupname(std::string arg)
788{
789 operation_start("AppendDateToBackupName");
790 string Backup_Name;
791 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
792 Backup_Name += TWFunc::Get_Current_Date();
793 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
794 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
795 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
796 operation_end(0);
797 return 0;
798}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500799
that3f7b1ac2014-12-30 11:30:13 +0100800int GUIAction::generatebackupname(std::string arg)
801{
802 operation_start("GenerateBackupName");
803 TWFunc::Auto_Generate_Backup_Name();
804 operation_end(0);
805 return 0;
806}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500807
that3f7b1ac2014-12-30 11:30:13 +0100808int GUIAction::checkpartitionlist(std::string arg)
809{
810 string Wipe_List, wipe_path;
811 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500812
that3f7b1ac2014-12-30 11:30:13 +0100813 DataManager::GetValue("tw_wipe_list", Wipe_List);
814 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
815 if (!Wipe_List.empty()) {
816 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
817 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
818 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
819 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
820 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
821 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400822 } else {
that3f7b1ac2014-12-30 11:30:13 +0100823 count++;
824 }
825 start_pos = end_pos + 1;
826 end_pos = Wipe_List.find(";", start_pos);
827 }
828 DataManager::SetValue("tw_check_partition_list", count);
829 } else {
830 DataManager::SetValue("tw_check_partition_list", 0);
831 }
832 return 0;
833}
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
that3f7b1ac2014-12-30 11:30:13 +0100835int GUIAction::getpartitiondetails(std::string arg)
836{
837 string Wipe_List, wipe_path;
838 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
that3f7b1ac2014-12-30 11:30:13 +0100840 DataManager::GetValue("tw_wipe_list", Wipe_List);
841 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
842 if (!Wipe_List.empty()) {
843 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
844 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
845 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
846 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
847 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
848 // Do nothing
849 } else {
850 DataManager::SetValue("tw_partition_path", wipe_path);
851 break;
852 }
853 start_pos = end_pos + 1;
854 end_pos = Wipe_List.find(";", start_pos);
855 }
856 if (!wipe_path.empty()) {
857 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
858 if (Part) {
859 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500860
that3f7b1ac2014-12-30 11:30:13 +0100861 DataManager::SetValue("tw_partition_name", Part->Display_Name);
862 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
863 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
864 DataManager::SetValue("tw_partition_size", Part->Size / mb);
865 DataManager::SetValue("tw_partition_used", Part->Used / mb);
866 DataManager::SetValue("tw_partition_free", Part->Free / mb);
867 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
868 DataManager::SetValue("tw_partition_removable", Part->Removable);
869 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
870
871 if (Part->Can_Repair())
872 DataManager::SetValue("tw_partition_can_repair", 1);
873 else
874 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500875 if (Part->Can_Resize())
876 DataManager::SetValue("tw_partition_can_resize", 1);
877 else
878 DataManager::SetValue("tw_partition_can_resize", 0);
that3f7b1ac2014-12-30 11:30:13 +0100879 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
880 DataManager::SetValue("tw_partition_vfat", 1);
881 else
882 DataManager::SetValue("tw_partition_vfat", 0);
883 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
884 DataManager::SetValue("tw_partition_exfat", 1);
885 else
886 DataManager::SetValue("tw_partition_exfat", 0);
887 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
888 DataManager::SetValue("tw_partition_f2fs", 1);
889 else
890 DataManager::SetValue("tw_partition_f2fs", 0);
891 if (TWFunc::Path_Exists("/sbin/mke2fs"))
892 DataManager::SetValue("tw_partition_ext", 1);
893 else
894 DataManager::SetValue("tw_partition_ext", 0);
895 return 0;
896 } else {
897 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
898 }
899 }
900 }
901 DataManager::SetValue("tw_partition_name", "");
902 DataManager::SetValue("tw_partition_file_system", "");
903 return 0;
904}
905
906int GUIAction::screenshot(std::string arg)
907{
908 time_t tm;
909 char path[256];
910 int path_len;
911 uid_t uid = -1;
912 gid_t gid = -1;
913
914 struct passwd *pwd = getpwnam("media_rw");
915 if(pwd) {
916 uid = pwd->pw_uid;
917 gid = pwd->pw_gid;
918 }
919
920 const std::string storage = DataManager::GetCurrentStoragePath();
921 if(PartitionManager.Is_Mounted_By_Path(storage)) {
922 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
923 } else {
924 strcpy(path, "/tmp/");
925 }
926
927 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
928 return 0;
929
930 tm = time(NULL);
931 path_len = strlen(path);
932
933 // Screenshot_2014-01-01-18-21-38.png
934 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
935
936 int res = gr_save_screenshot(path);
937 if(res == 0) {
938 chmod(path, 0666);
939 chown(path, uid, gid);
940
941 gui_print("Screenshot was saved to %s\n", path);
942
943 // blink to notify that the screenshow was taken
944 gr_color(255, 255, 255, 255);
945 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
946 gr_flip();
947 gui_forceRender();
948 } else {
949 LOGERR("Failed to take a screenshot!\n");
950 }
951 return 0;
952}
953
954int GUIAction::setbrightness(std::string arg)
955{
956 return TWFunc::Set_Brightness(arg);
957}
958
959int GUIAction::fileexists(std::string arg)
960{
961 struct stat st;
962 string newpath = arg + "/.";
963
964 operation_start("FileExists");
965 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
966 operation_end(0);
967 else
968 operation_end(1);
969 return 0;
970}
971
thatcc8ddca2015-01-03 01:59:36 +0100972void GUIAction::reinject_after_flash()
973{
974 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
thatcc8ddca2015-01-03 01:59:36 +0100975 gui_print("Injecting TWRP into boot image...\n");
976 if (simulate) {
977 simulate_progress_bar();
978 } else {
979 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
980 if (Boot == NULL || Boot->Current_File_System != "emmc")
981 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
982 else {
983 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
984 TWFunc::Exec_Cmd(injectcmd);
985 }
986 gui_print("TWRP injection complete.\n");
987 }
988 }
989}
990
that3f7b1ac2014-12-30 11:30:13 +0100991int GUIAction::flash(std::string arg)
992{
993 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600994 // We're going to jump to this page first, like a loading page
995 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100996 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200997 string zip_path = zip_queue[i];
998 size_t slashpos = zip_path.find_last_of('/');
999 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001000 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001001 DataManager::SetValue("tw_filename", zip_path);
1002 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001003 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1004
1005 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001006 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001007 TWFunc::SetPerformanceMode(false);
1008 if (ret_val != 0) {
thatc7b631b2015-06-01 23:36:57 +02001009 gui_print("Error flashing zip '%s'\n", zip_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001010 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001011 break;
that3f7b1ac2014-12-30 11:30:13 +01001012 }
1013 }
1014 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001015
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001016 if (wipe_cache) {
1017 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001018 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001019 }
that3f7b1ac2014-12-30 11:30:13 +01001020
thatcc8ddca2015-01-03 01:59:36 +01001021 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001022 PartitionManager.Update_System_Details();
1023 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001024 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001025 return 0;
1026}
1027
1028int GUIAction::wipe(std::string arg)
1029{
1030 operation_start("Format");
1031 DataManager::SetValue("tw_partition", arg);
1032
1033 int ret_val = false;
1034
1035 if (simulate) {
1036 simulate_progress_bar();
1037 } else {
1038 if (arg == "data")
1039 ret_val = PartitionManager.Factory_Reset();
1040 else if (arg == "battery")
1041 ret_val = PartitionManager.Wipe_Battery_Stats();
1042 else if (arg == "rotate")
1043 ret_val = PartitionManager.Wipe_Rotate_Data();
1044 else if (arg == "dalvik")
1045 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1046 else if (arg == "DATAMEDIA") {
1047 ret_val = PartitionManager.Format_Data();
1048 } else if (arg == "INTERNAL") {
1049 int has_datamedia, dual_storage;
1050
1051 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1052 if (has_datamedia) {
1053 ret_val = PartitionManager.Wipe_Media_From_Data();
1054 } else {
1055 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1056 }
1057 } else if (arg == "EXTERNAL") {
1058 string External_Path;
1059
1060 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1061 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1062 } else if (arg == "ANDROIDSECURE") {
1063 ret_val = PartitionManager.Wipe_Android_Secure();
1064 } else if (arg == "LIST") {
1065 string Wipe_List, wipe_path;
1066 bool skip = false;
1067 ret_val = true;
1068 TWPartition* wipe_part = NULL;
1069
1070 DataManager::GetValue("tw_wipe_list", Wipe_List);
1071 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1072 if (!Wipe_List.empty()) {
1073 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1074 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1075 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1076 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1077 if (wipe_path == "/and-sec") {
1078 if (!PartitionManager.Wipe_Android_Secure()) {
1079 LOGERR("Unable to wipe android secure\n");
1080 ret_val = false;
1081 break;
1082 } else {
1083 skip = true;
1084 }
1085 } else if (wipe_path == "DALVIK") {
1086 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1087 LOGERR("Failed to wipe dalvik\n");
1088 ret_val = false;
1089 break;
1090 } else {
1091 skip = true;
1092 }
1093 } else if (wipe_path == "INTERNAL") {
1094 if (!PartitionManager.Wipe_Media_From_Data()) {
1095 ret_val = false;
1096 break;
1097 } else {
1098 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001099 }
1100 }
that3f7b1ac2014-12-30 11:30:13 +01001101 if (!skip) {
1102 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1103 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1104 ret_val = false;
1105 break;
1106 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1107 arg = wipe_path;
1108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 } else {
that3f7b1ac2014-12-30 11:30:13 +01001110 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001111 }
that3f7b1ac2014-12-30 11:30:13 +01001112 start_pos = end_pos + 1;
1113 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001114 }
1115 }
that3f7b1ac2014-12-30 11:30:13 +01001116 } else
1117 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001118#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001119 if (arg == DataManager::GetSettingsStoragePath()) {
1120 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1121 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001122
that3f7b1ac2014-12-30 11:30:13 +01001123 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1124 LOGINFO("Making TWRP folder and saving settings.\n");
1125 Storage_Path += "/TWRP";
1126 mkdir(Storage_Path.c_str(), 0777);
1127 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001128 } else {
that3f7b1ac2014-12-30 11:30:13 +01001129 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1130 }
1131 }
1132#endif
1133 }
1134 PartitionManager.Update_System_Details();
1135 if (ret_val)
1136 ret_val = 0; // 0 is success
1137 else
1138 ret_val = 1; // 1 is failure
1139 operation_end(ret_val);
1140 return 0;
1141}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001142
that3f7b1ac2014-12-30 11:30:13 +01001143int GUIAction::refreshsizes(std::string arg)
1144{
1145 operation_start("Refreshing Sizes");
1146 if (simulate) {
1147 simulate_progress_bar();
1148 } else
1149 PartitionManager.Update_System_Details();
1150 operation_end(0);
1151 return 0;
1152}
1153
1154int GUIAction::nandroid(std::string arg)
1155{
that3f7b1ac2014-12-30 11:30:13 +01001156 if (simulate) {
that73a52952015-01-28 23:07:34 +01001157 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001158 DataManager::SetValue("tw_partition", "Simulation");
1159 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001160 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001161 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001162 operation_start("Nandroid");
1163 int ret = 0;
1164
that3f7b1ac2014-12-30 11:30:13 +01001165 if (arg == "backup") {
1166 string Backup_Name;
1167 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1168 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1169 ret = PartitionManager.Run_Backup();
1170 }
1171 else {
1172 operation_end(1);
1173 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001174 }
1175 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1176 } else if (arg == "restore") {
1177 string Restore_Name;
1178 DataManager::GetValue("tw_restore", Restore_Name);
1179 ret = PartitionManager.Run_Restore(Restore_Name);
1180 } else {
1181 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001182 return -1;
1183 }
1184 DataManager::SetValue("tw_encrypt_backup", 0);
1185 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001186 if (ret == false)
1187 ret = 1; // 1 for failure
1188 else
1189 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001190 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 }
1192 else {
1193 DataManager::SetValue("tw_cancel_backup", 1);
1194 gui_print("Backup Canceled.\n");
1195 ret = 0;
1196 }
that73a52952015-01-28 23:07:34 +01001197 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 return ret;
1199 }
1200 return 0;
1201}
1202
1203int GUIAction::cancelbackup(std::string arg) {
1204 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001205 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001206 }
1207 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001208 int op_status = PartitionManager.Cancel_Backup();
1209 if (op_status != 0)
1210 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001211 }
1212
1213 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001214}
Dees_Troy51a0e822012-09-05 15:24:24 -04001215
that3f7b1ac2014-12-30 11:30:13 +01001216int GUIAction::fixpermissions(std::string arg)
1217{
that73a52952015-01-28 23:07:34 +01001218 int op_status = 0;
1219
that3f7b1ac2014-12-30 11:30:13 +01001220 operation_start("Fix Permissions");
1221 LOGINFO("fix permissions started!\n");
1222 if (simulate) {
1223 simulate_progress_bar();
1224 } else {
that73a52952015-01-28 23:07:34 +01001225 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001226 if (op_status != 0)
1227 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001228 }
that73a52952015-01-28 23:07:34 +01001229 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001230 return 0;
1231}
1232
1233int GUIAction::dd(std::string arg)
1234{
1235 operation_start("imaging");
1236
1237 if (simulate) {
1238 simulate_progress_bar();
1239 } else {
1240 string cmd = "dd " + arg;
1241 TWFunc::Exec_Cmd(cmd);
1242 }
1243 operation_end(0);
1244 return 0;
1245}
1246
1247int GUIAction::partitionsd(std::string arg)
1248{
1249 operation_start("Partition SD Card");
1250 int ret_val = 0;
1251
1252 if (simulate) {
1253 simulate_progress_bar();
1254 } else {
1255 int allow_partition;
1256 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1257 if (allow_partition == 0) {
1258 gui_print("This device does not have a real SD Card!\nAborting!\n");
1259 } else {
1260 if (!PartitionManager.Partition_SDCard())
1261 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001262 }
that3f7b1ac2014-12-30 11:30:13 +01001263 }
1264 operation_end(ret_val);
1265 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001266
that3f7b1ac2014-12-30 11:30:13 +01001267}
Dees_Troy51a0e822012-09-05 15:24:24 -04001268
that3f7b1ac2014-12-30 11:30:13 +01001269int GUIAction::installhtcdumlock(std::string arg)
1270{
1271 operation_start("Install HTC Dumlock");
1272 if (simulate) {
1273 simulate_progress_bar();
1274 } else
1275 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001276
that3f7b1ac2014-12-30 11:30:13 +01001277 operation_end(0);
1278 return 0;
1279}
Dees_Troy51a0e822012-09-05 15:24:24 -04001280
that3f7b1ac2014-12-30 11:30:13 +01001281int GUIAction::htcdumlockrestoreboot(std::string arg)
1282{
1283 operation_start("HTC Dumlock Restore Boot");
1284 if (simulate) {
1285 simulate_progress_bar();
1286 } else
1287 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001288
that3f7b1ac2014-12-30 11:30:13 +01001289 operation_end(0);
1290 return 0;
1291}
Dees_Troy51a0e822012-09-05 15:24:24 -04001292
that3f7b1ac2014-12-30 11:30:13 +01001293int GUIAction::htcdumlockreflashrecovery(std::string arg)
1294{
1295 operation_start("HTC Dumlock Reflash Recovery");
1296 if (simulate) {
1297 simulate_progress_bar();
1298 } else
1299 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
that3f7b1ac2014-12-30 11:30:13 +01001301 operation_end(0);
1302 return 0;
1303}
Dees_Troy51a0e822012-09-05 15:24:24 -04001304
that3f7b1ac2014-12-30 11:30:13 +01001305int GUIAction::cmd(std::string arg)
1306{
1307 int op_status = 0;
1308
1309 operation_start("Command");
1310 LOGINFO("Running command: '%s'\n", arg.c_str());
1311 if (simulate) {
1312 simulate_progress_bar();
1313 } else {
1314 op_status = TWFunc::Exec_Cmd(arg);
1315 if (op_status != 0)
1316 op_status = 1;
1317 }
1318
1319 operation_end(op_status);
1320 return 0;
1321}
1322
1323int GUIAction::terminalcommand(std::string arg)
1324{
1325 int op_status = 0;
1326 string cmdpath, command;
1327
1328 DataManager::GetValue("tw_terminal_location", cmdpath);
1329 operation_start("CommandOutput");
1330 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1331 if (simulate) {
1332 simulate_progress_bar();
1333 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001334 } else if (arg == "exit") {
1335 LOGINFO("Exiting terminal\n");
1336 operation_end(op_status);
1337 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001338 } else {
1339 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1340 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001341 DataManager::SetValue("tw_terminal_state", 1);
1342 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001343 FILE* fp;
1344 char line[512];
1345
1346 fp = popen(command.c_str(), "r");
1347 if (fp == NULL) {
1348 LOGERR("Error opening command to run.\n");
1349 } else {
1350 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1351 struct timeval timeout;
1352 fd_set fdset;
1353
1354 while(keep_going)
1355 {
1356 FD_ZERO(&fdset);
1357 FD_SET(fd, &fdset);
1358 timeout.tv_sec = 0;
1359 timeout.tv_usec = 400000;
1360 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1361 if (has_data == 0) {
1362 // Timeout reached
1363 DataManager::GetValue("tw_terminal_state", check);
1364 if (check == 0) {
1365 keep_going = 0;
1366 }
1367 } else if (has_data < 0) {
1368 // End of execution
1369 keep_going = 0;
1370 } else {
1371 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001372 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001373 gui_print("%s", line); // Display output
1374 else
1375 keep_going = 0; // Done executing
1376 }
1377 }
1378 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001379 }
thatc6085482015-01-09 22:12:43 +01001380 DataManager::SetValue("tw_operation_status", 0);
1381 DataManager::SetValue("tw_operation_state", 1);
1382 DataManager::SetValue("tw_terminal_state", 0);
1383 DataManager::SetValue("tw_background_thread_running", 0);
1384 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001385 }
1386 return 0;
1387}
1388
1389int GUIAction::killterminal(std::string arg)
1390{
1391 int op_status = 0;
1392
1393 LOGINFO("Sending kill command...\n");
1394 operation_start("KillCommand");
1395 DataManager::SetValue("tw_operation_status", 0);
1396 DataManager::SetValue("tw_operation_state", 1);
1397 DataManager::SetValue("tw_terminal_state", 0);
1398 DataManager::SetValue("tw_background_thread_running", 0);
1399 DataManager::SetValue(TW_ACTION_BUSY, 0);
1400 return 0;
1401}
1402
1403int GUIAction::reinjecttwrp(std::string arg)
1404{
1405 int op_status = 0;
1406 operation_start("ReinjectTWRP");
1407 gui_print("Injecting TWRP into boot image...\n");
1408 if (simulate) {
1409 simulate_progress_bar();
1410 } else {
1411 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1412 gui_print("TWRP injection complete.\n");
1413 }
1414
1415 operation_end(op_status);
1416 return 0;
1417}
1418
1419int GUIAction::checkbackupname(std::string arg)
1420{
1421 int op_status = 0;
1422
1423 operation_start("CheckBackupName");
1424 if (simulate) {
1425 simulate_progress_bar();
1426 } else {
1427 op_status = PartitionManager.Check_Backup_Name(true);
1428 if (op_status != 0)
1429 op_status = 1;
1430 }
1431
1432 operation_end(op_status);
1433 return 0;
1434}
1435
1436int GUIAction::decrypt(std::string arg)
1437{
1438 int op_status = 0;
1439
1440 operation_start("Decrypt");
1441 if (simulate) {
1442 simulate_progress_bar();
1443 } else {
1444 string Password;
1445 DataManager::GetValue("tw_crypto_password", Password);
1446 op_status = PartitionManager.Decrypt_Device(Password);
1447 if (op_status != 0)
1448 op_status = 1;
1449 else {
that3f7b1ac2014-12-30 11:30:13 +01001450
1451 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1452
Ethan Yonkercf50da52015-01-12 21:59:07 -06001453 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001454
Ethan Yonkercf50da52015-01-12 21:59:07 -06001455 // Check for a custom theme and load it if exists
1456 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1457 if (has_datamedia != 0) {
1458 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001459 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001460 } else {
1461 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001462 }
1463 }
1464 }
1465 }
1466
1467 operation_end(op_status);
1468 return 0;
1469}
1470
thatcc8ddca2015-01-03 01:59:36 +01001471int GUIAction::adbsideload(std::string arg)
1472{
1473 operation_start("Sideload");
1474 if (simulate) {
1475 simulate_progress_bar();
1476 operation_end(0);
1477 } else {
thatc6085482015-01-09 22:12:43 +01001478 gui_print("Starting ADB sideload feature...\n");
1479 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1480
1481 // wait for the adb connection
1482 int ret = apply_from_adb("/", &sideload_child_pid);
1483 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1484
1485 if (ret != 0) {
1486 if (ret == -2)
1487 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1488 ret = 1; // failure
1489 } else {
1490 int wipe_cache = 0;
1491 int wipe_dalvik = 0;
1492 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1493
1494 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1495 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1496 PartitionManager.Wipe_By_Path("/cache");
1497 if (wipe_dalvik)
1498 PartitionManager.Wipe_Dalvik_Cache();
1499 } else {
1500 ret = 1; // failure
1501 }
thatcc8ddca2015-01-03 01:59:36 +01001502 }
thatc6085482015-01-09 22:12:43 +01001503 if (sideload_child_pid) {
1504 LOGINFO("Signaling child sideload process to exit.\n");
1505 struct stat st;
1506 // Calling stat() on this magic filename signals the minadbd
1507 // subprocess to shut down.
1508 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1509 int status;
1510 LOGINFO("Waiting for child sideload process to exit.\n");
1511 waitpid(sideload_child_pid, &status, 0);
1512 }
Dees Troy28a85d22015-04-28 02:03:16 +00001513 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001514 TWFunc::Toggle_MTP(mtp_was_enabled);
1515 reinject_after_flash();
1516 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001517 }
that3f7b1ac2014-12-30 11:30:13 +01001518 return 0;
1519}
1520
1521int GUIAction::adbsideloadcancel(std::string arg)
1522{
that3f7b1ac2014-12-30 11:30:13 +01001523 struct stat st;
1524 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1525 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001526 LOGINFO("Signaling child sideload process to exit.\n");
1527 // Calling stat() on this magic filename signals the minadbd
1528 // subprocess to shut down.
1529 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1530 if (!sideload_child_pid) {
1531 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001532 return 0;
1533 }
thatcc8ddca2015-01-03 01:59:36 +01001534 ::sleep(1);
1535 LOGINFO("Killing child sideload process.\n");
1536 kill(sideload_child_pid, SIGTERM);
1537 int status;
1538 LOGINFO("Waiting for child sideload process to exit.\n");
1539 waitpid(sideload_child_pid, &status, 0);
1540 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001541 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1542 return 0;
1543}
1544
1545int GUIAction::openrecoveryscript(std::string arg)
1546{
Ethan Yonkere1abe612015-06-09 11:09:32 -05001547 int op_status = 1;
1548
that3f7b1ac2014-12-30 11:30:13 +01001549 operation_start("OpenRecoveryScript");
1550 if (simulate) {
1551 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001552 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001553 } else {
thatc6085482015-01-09 22:12:43 +01001554 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1555 // that we converted to ORS commands during boot in recovery.cpp.
1556 // Run those first.
1557 int reboot = 0;
1558 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1559 gui_print("Processing AOSP recovery commands...\n");
1560 if (OpenRecoveryScript::run_script_file() == 0) {
1561 reboot = 1;
Matt Mower85161af2015-08-08 10:00:50 -05001562 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001563 }
that3f7b1ac2014-12-30 11:30:13 +01001564 }
thatc6085482015-01-09 22:12:43 +01001565 // Check for the ORS file in /cache and attempt to run those commands.
1566 if (OpenRecoveryScript::check_for_script_file()) {
1567 gui_print("Processing OpenRecoveryScript file...\n");
1568 if (OpenRecoveryScript::run_script_file() == 0) {
1569 reboot = 1;
Ethan Yonkere1abe612015-06-09 11:09:32 -05001570 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001571 }
1572 }
1573 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001574 // Disable stock recovery reflashing
1575 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001576 usleep(2000000); // Sleep for 2 seconds before rebooting
1577 TWFunc::tw_reboot(rb_system);
Ethan Yonkere1abe612015-06-09 11:09:32 -05001578 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
thatc6085482015-01-09 22:12:43 +01001579 } else {
1580 DataManager::SetValue("tw_page_done", 1);
1581 }
Ethan Yonkere1abe612015-06-09 11:09:32 -05001582 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001583 }
1584 return 0;
1585}
1586
1587int GUIAction::installsu(std::string arg)
1588{
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
1603int GUIAction::fixsu(std::string arg)
1604{
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
1619int GUIAction::decrypt_backup(std::string arg)
1620{
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
1643int GUIAction::repair(std::string arg)
1644{
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 {
1656 LOGERR("Error repairing file system.\n");
1657 op_status = 1; // fail
1658 }
1659 }
1660
1661 operation_end(op_status);
1662 return 0;
1663}
1664
Ethan Yonkera2719152015-05-28 09:44:41 -05001665int GUIAction::resize(std::string arg)
1666{
1667 int op_status = 0;
1668
1669 operation_start("Resize Partition");
1670 if (simulate) {
1671 simulate_progress_bar();
1672 } else {
1673 string part_path;
1674 DataManager::GetValue("tw_partition_mount_point", part_path);
1675 if (PartitionManager.Resize_By_Path(part_path, true)) {
1676 op_status = 0; // success
1677 } else {
1678 LOGERR("Error resizing file system.\n");
1679 op_status = 1; // fail
1680 }
1681 }
1682
1683 operation_end(op_status);
1684 return 0;
1685}
1686
that3f7b1ac2014-12-30 11:30:13 +01001687int GUIAction::changefilesystem(std::string arg)
1688{
1689 int op_status = 0;
1690
1691 operation_start("Change File System");
1692 if (simulate) {
1693 simulate_progress_bar();
1694 } else {
1695 string part_path, file_system;
1696 DataManager::GetValue("tw_partition_mount_point", part_path);
1697 DataManager::GetValue("tw_action_new_file_system", file_system);
1698 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1699 op_status = 0; // success
1700 } else {
1701 LOGERR("Error changing file system.\n");
1702 op_status = 1; // fail
1703 }
1704 }
1705 PartitionManager.Update_System_Details();
1706 operation_end(op_status);
1707 return 0;
1708}
1709
1710int GUIAction::startmtp(std::string arg)
1711{
1712 int op_status = 0;
1713
1714 operation_start("Start MTP");
1715 if (PartitionManager.Enable_MTP())
1716 op_status = 0; // success
1717 else
1718 op_status = 1; // fail
1719
1720 operation_end(op_status);
1721 return 0;
1722}
1723
1724int GUIAction::stopmtp(std::string arg)
1725{
1726 int op_status = 0;
1727
1728 operation_start("Stop MTP");
1729 if (PartitionManager.Disable_MTP())
1730 op_status = 0; // success
1731 else
1732 op_status = 1; // fail
1733
1734 operation_end(op_status);
1735 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001736}
1737
Ethan Yonker96af84a2015-01-05 14:58:36 -06001738int GUIAction::flashimage(std::string arg)
1739{
1740 int op_status = 0;
1741
1742 operation_start("Flash Image");
1743 string path, filename, full_filename;
1744 DataManager::GetValue("tw_zip_location", path);
1745 DataManager::GetValue("tw_file", filename);
1746 full_filename = path + "/" + filename;
1747 if (PartitionManager.Flash_Image(full_filename))
1748 op_status = 0; // success
1749 else
1750 op_status = 1; // fail
1751
1752 operation_end(op_status);
1753 return 0;
1754}
1755
Dees_Troy51a0e822012-09-05 15:24:24 -04001756int GUIAction::getKeyByName(std::string key)
1757{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001758 if (key == "home") return KEY_HOME;
1759 else if (key == "menu") return KEY_MENU;
1760 else if (key == "back") return KEY_BACK;
1761 else if (key == "search") return KEY_SEARCH;
1762 else if (key == "voldown") return KEY_VOLUMEDOWN;
1763 else if (key == "volup") return KEY_VOLUMEUP;
1764 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001765 int ret_val;
1766 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1767 if (!ret_val)
1768 return KEY_POWER;
1769 else
1770 return ret_val;
1771 }
1772
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001773 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001774}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001775
1776int GUIAction::checkpartitionlifetimewrites(std::string arg)
1777{
1778 int op_status = 0;
1779 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1780
1781 operation_start("Check Partition Lifetime Writes");
1782 if (sys) {
1783 if (sys->Check_Lifetime_Writes() != 0)
1784 DataManager::SetValue("tw_lifetime_writes", 1);
1785 else
1786 DataManager::SetValue("tw_lifetime_writes", 0);
1787 op_status = 0; // success
1788 } else {
1789 DataManager::SetValue("tw_lifetime_writes", 1);
1790 op_status = 1; // fail
1791 }
1792
1793 operation_end(op_status);
1794 return 0;
1795}
1796
1797int GUIAction::mountsystemtoggle(std::string arg)
1798{
1799 int op_status = 0;
1800 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
1801
1802 operation_start("Toggle System Mount");
1803 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1804 op_status = 1; // fail
1805 } else {
1806 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1807 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001808 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001809 DataManager::SetValue("tw_mount_system_ro", 0);
1810 Part->Change_Mount_Read_Only(false);
1811 } else {
1812 DataManager::SetValue("tw_mount_system_ro", 1);
1813 Part->Change_Mount_Read_Only(true);
1814 }
1815 if (remount_system) {
1816 Part->Mount(true);
1817 }
1818 op_status = 0; // success
1819 } else {
1820 op_status = 1; // fail
1821 }
1822 }
1823
1824 operation_end(op_status);
1825 return 0;
1826}