blob: 46f4575d77f525ad12f195f3a8eace9fb705a465 [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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500283int GUIAction::NotifyTouch(TOUCH_STATE state __unused, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400284{
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500521int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100522{
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
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500542int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100543{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500544 PageManager::RequestReload();
545 // The actual reload is handled in pages.cpp in PageManager::RunReload()
546 // The reload will occur on the next Update or Render call and will
547 // be performed in the rendoer thread instead of the action thread
548 // to prevent crashing which could occur when we start deleting
549 // GUI resources in the action thread while we attempt to render
550 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100551 return 0;
552}
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
556 string Restore_Name;
557 DataManager::GetValue("tw_restore", Restore_Name);
558 PartitionManager.Set_Restore_Files(Restore_Name);
559 return 0;
560}
561
562int GUIAction::set(std::string arg)
563{
564 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200565 {
that3f7b1ac2014-12-30 11:30:13 +0100566 string varName = arg.substr(0, arg.find('='));
567 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400568
that3f7b1ac2014-12-30 11:30:13 +0100569 DataManager::GetValue(value, value);
570 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200571 }
that3f7b1ac2014-12-30 11:30:13 +0100572 else
573 DataManager::SetValue(arg, "1");
574 return 0;
575}
Dees_Troy51a0e822012-09-05 15:24:24 -0400576
that3f7b1ac2014-12-30 11:30:13 +0100577int GUIAction::clear(std::string arg)
578{
579 DataManager::SetValue(arg, "0");
580 return 0;
581}
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
that3f7b1ac2014-12-30 11:30:13 +0100583int GUIAction::mount(std::string arg)
584{
585 if (arg == "usb") {
586 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100588 PartitionManager.usb_storage_enable();
589 else
590 gui_print("Simulating actions...\n");
591 } else if (!simulate) {
592 PartitionManager.Mount_By_Path(arg, true);
593 PartitionManager.Add_MTP_Storage(arg);
594 } else
595 gui_print("Simulating actions...\n");
596 return 0;
597}
598
599int GUIAction::unmount(std::string arg)
600{
601 if (arg == "usb") {
602 if (!simulate)
603 PartitionManager.usb_storage_disable();
604 else
605 gui_print("Simulating actions...\n");
606 DataManager::SetValue(TW_ACTION_BUSY, 0);
607 } else if (!simulate) {
608 PartitionManager.UnMount_By_Path(arg, true);
609 } else
610 gui_print("Simulating actions...\n");
611 return 0;
612}
613
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500614int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100615{
616 operation_start("Restore Defaults");
617 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
618 gui_print("Simulating actions...\n");
619 else {
620 DataManager::ResetDefaults();
621 PartitionManager.Update_System_Details();
622 PartitionManager.Mount_Current_Storage(true);
623 }
624 operation_end(0);
625 return 0;
626}
627
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500628int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100629{
630 operation_start("Copy Log");
631 if (!simulate)
632 {
633 string dst;
634 PartitionManager.Mount_Current_Storage(true);
635 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
636 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
637 tw_set_default_metadata(dst.c_str());
638 sync();
639 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
640 } else
641 simulate_progress_bar();
642 operation_end(0);
643 return 0;
644}
645
646
647int GUIAction::compute(std::string arg)
648{
649 if (arg.find("+") != string::npos)
650 {
651 string varName = arg.substr(0, arg.find('+'));
652 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
653 int amount_to_add = atoi(string_to_add.c_str());
654 int value;
655
656 DataManager::GetValue(varName, value);
657 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400658 return 0;
659 }
that3f7b1ac2014-12-30 11:30:13 +0100660 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 {
that3f7b1ac2014-12-30 11:30:13 +0100662 string varName = arg.substr(0, arg.find('-'));
663 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
664 int amount_to_subtract = atoi(string_to_subtract.c_str());
665 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400666
that3f7b1ac2014-12-30 11:30:13 +0100667 DataManager::GetValue(varName, value);
668 value -= amount_to_subtract;
669 if (value <= 0)
670 value = 0;
671 DataManager::SetValue(varName, value);
672 return 0;
673 }
674 if (arg.find("*") != string::npos)
675 {
676 string varName = arg.substr(0, arg.find('*'));
677 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
678 int multiply_by = atoi(multiply_by_str.c_str());
679 int value;
680
681 DataManager::GetValue(varName, value);
682 DataManager::SetValue(varName, value*multiply_by);
683 return 0;
684 }
685 if (arg.find("/") != string::npos)
686 {
687 string varName = arg.substr(0, arg.find('/'));
688 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
689 int divide_by = atoi(divide_by_str.c_str());
690 int value;
691
692 if(divide_by != 0)
693 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100695 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 }
697 return 0;
698 }
that3f7b1ac2014-12-30 11:30:13 +0100699 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
700 return -1;
701}
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500703int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100704{
705 string SelectedZone;
706 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
707 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
708 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
709
710 int dst;
711 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
712
713 string offset;
714 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
715
716 string NewTimeZone = Zone;
717 if (offset != "0")
718 NewTimeZone += ":" + offset;
719
720 if (dst != 0)
721 NewTimeZone += DSTZone;
722
723 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
724 DataManager::update_tz_environment_variables();
725 return 0;
726}
727
728int GUIAction::overlay(std::string arg)
729{
730 return gui_changeOverlay(arg);
731}
732
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500733int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100734{
735 if (zip_queue_index >= 10) {
736 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 return 0;
738 }
that3f7b1ac2014-12-30 11:30:13 +0100739 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
740 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
741 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100743 }
744 return 0;
745}
746
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500747int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100748{
749 if (zip_queue_index <= 0) {
750 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100752 } else {
753 zip_queue_index--;
754 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 }
that3f7b1ac2014-12-30 11:30:13 +0100756 return 0;
757}
Dees_Troy51a0e822012-09-05 15:24:24 -0400758
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500759int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100760{
761 zip_queue_index = 0;
762 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
763 return 0;
764}
Dees_Troy51a0e822012-09-05 15:24:24 -0400765
that3f7b1ac2014-12-30 11:30:13 +0100766int GUIAction::sleep(std::string arg)
767{
768 operation_start("Sleep");
769 usleep(atoi(arg.c_str()));
770 operation_end(0);
771 return 0;
772}
Dees Troyb21cc642013-09-10 17:36:41 +0000773
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500774int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100775{
776 operation_start("AppendDateToBackupName");
777 string Backup_Name;
778 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
779 Backup_Name += TWFunc::Get_Current_Date();
780 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
781 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
782 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
783 operation_end(0);
784 return 0;
785}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500786
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500787int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100788{
789 operation_start("GenerateBackupName");
790 TWFunc::Auto_Generate_Backup_Name();
791 operation_end(0);
792 return 0;
793}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500795int GUIAction::checkpartitionlist(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100796{
797 string Wipe_List, wipe_path;
798 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500799
that3f7b1ac2014-12-30 11:30:13 +0100800 DataManager::GetValue("tw_wipe_list", Wipe_List);
801 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
802 if (!Wipe_List.empty()) {
803 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
804 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
805 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
806 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
807 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
808 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 } else {
that3f7b1ac2014-12-30 11:30:13 +0100810 count++;
811 }
812 start_pos = end_pos + 1;
813 end_pos = Wipe_List.find(";", start_pos);
814 }
815 DataManager::SetValue("tw_check_partition_list", count);
816 } else {
817 DataManager::SetValue("tw_check_partition_list", 0);
818 }
819 return 0;
820}
Dees_Troy51a0e822012-09-05 15:24:24 -0400821
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500822int GUIAction::getpartitiondetails(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100823{
824 string Wipe_List, wipe_path;
825 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826
that3f7b1ac2014-12-30 11:30:13 +0100827 DataManager::GetValue("tw_wipe_list", Wipe_List);
828 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
829 if (!Wipe_List.empty()) {
830 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
831 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
832 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
833 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
834 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
835 // Do nothing
836 } else {
837 DataManager::SetValue("tw_partition_path", wipe_path);
838 break;
839 }
840 start_pos = end_pos + 1;
841 end_pos = Wipe_List.find(";", start_pos);
842 }
843 if (!wipe_path.empty()) {
844 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
845 if (Part) {
846 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500847
that3f7b1ac2014-12-30 11:30:13 +0100848 DataManager::SetValue("tw_partition_name", Part->Display_Name);
849 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
850 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
851 DataManager::SetValue("tw_partition_size", Part->Size / mb);
852 DataManager::SetValue("tw_partition_used", Part->Used / mb);
853 DataManager::SetValue("tw_partition_free", Part->Free / mb);
854 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
855 DataManager::SetValue("tw_partition_removable", Part->Removable);
856 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
857
858 if (Part->Can_Repair())
859 DataManager::SetValue("tw_partition_can_repair", 1);
860 else
861 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500862 if (Part->Can_Resize())
863 DataManager::SetValue("tw_partition_can_resize", 1);
864 else
865 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600866 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100867 DataManager::SetValue("tw_partition_vfat", 1);
868 else
869 DataManager::SetValue("tw_partition_vfat", 0);
870 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
871 DataManager::SetValue("tw_partition_exfat", 1);
872 else
873 DataManager::SetValue("tw_partition_exfat", 0);
874 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
875 DataManager::SetValue("tw_partition_f2fs", 1);
876 else
877 DataManager::SetValue("tw_partition_f2fs", 0);
878 if (TWFunc::Path_Exists("/sbin/mke2fs"))
879 DataManager::SetValue("tw_partition_ext", 1);
880 else
881 DataManager::SetValue("tw_partition_ext", 0);
882 return 0;
883 } else {
884 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
885 }
886 }
887 }
888 DataManager::SetValue("tw_partition_name", "");
889 DataManager::SetValue("tw_partition_file_system", "");
890 return 0;
891}
892
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500893int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100894{
895 time_t tm;
896 char path[256];
897 int path_len;
898 uid_t uid = -1;
899 gid_t gid = -1;
900
901 struct passwd *pwd = getpwnam("media_rw");
902 if(pwd) {
903 uid = pwd->pw_uid;
904 gid = pwd->pw_gid;
905 }
906
907 const std::string storage = DataManager::GetCurrentStoragePath();
908 if(PartitionManager.Is_Mounted_By_Path(storage)) {
909 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
910 } else {
911 strcpy(path, "/tmp/");
912 }
913
914 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
915 return 0;
916
917 tm = time(NULL);
918 path_len = strlen(path);
919
920 // Screenshot_2014-01-01-18-21-38.png
921 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
922
923 int res = gr_save_screenshot(path);
924 if(res == 0) {
925 chmod(path, 0666);
926 chown(path, uid, gid);
927
928 gui_print("Screenshot was saved to %s\n", path);
929
930 // blink to notify that the screenshow was taken
931 gr_color(255, 255, 255, 255);
932 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
933 gr_flip();
934 gui_forceRender();
935 } else {
936 LOGERR("Failed to take a screenshot!\n");
937 }
938 return 0;
939}
940
941int GUIAction::setbrightness(std::string arg)
942{
943 return TWFunc::Set_Brightness(arg);
944}
945
946int GUIAction::fileexists(std::string arg)
947{
948 struct stat st;
949 string newpath = arg + "/.";
950
951 operation_start("FileExists");
952 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
953 operation_end(0);
954 else
955 operation_end(1);
956 return 0;
957}
958
thatcc8ddca2015-01-03 01:59:36 +0100959void GUIAction::reinject_after_flash()
960{
961 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
thatcc8ddca2015-01-03 01:59:36 +0100962 gui_print("Injecting TWRP into boot image...\n");
963 if (simulate) {
964 simulate_progress_bar();
965 } else {
966 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
967 if (Boot == NULL || Boot->Current_File_System != "emmc")
968 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
969 else {
970 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
971 TWFunc::Exec_Cmd(injectcmd);
972 }
973 gui_print("TWRP injection complete.\n");
974 }
975 }
976}
977
that3f7b1ac2014-12-30 11:30:13 +0100978int GUIAction::flash(std::string arg)
979{
980 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600981 // We're going to jump to this page first, like a loading page
982 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100983 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +0200984 string zip_path = zip_queue[i];
985 size_t slashpos = zip_path.find_last_of('/');
986 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +0100987 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +0200988 DataManager::SetValue("tw_filename", zip_path);
989 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +0100990 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
991
992 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +0200993 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100994 TWFunc::SetPerformanceMode(false);
995 if (ret_val != 0) {
thatc7b631b2015-06-01 23:36:57 +0200996 gui_print("Error flashing zip '%s'\n", zip_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100997 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600998 break;
that3f7b1ac2014-12-30 11:30:13 +0100999 }
1000 }
1001 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001002
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001003 if (wipe_cache) {
1004 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +01001005 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001006 }
that3f7b1ac2014-12-30 11:30:13 +01001007
thatcc8ddca2015-01-03 01:59:36 +01001008 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001009 PartitionManager.Update_System_Details();
1010 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001011 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001012 return 0;
1013}
1014
1015int GUIAction::wipe(std::string arg)
1016{
1017 operation_start("Format");
1018 DataManager::SetValue("tw_partition", arg);
1019
1020 int ret_val = false;
1021
1022 if (simulate) {
1023 simulate_progress_bar();
1024 } else {
1025 if (arg == "data")
1026 ret_val = PartitionManager.Factory_Reset();
1027 else if (arg == "battery")
1028 ret_val = PartitionManager.Wipe_Battery_Stats();
1029 else if (arg == "rotate")
1030 ret_val = PartitionManager.Wipe_Rotate_Data();
1031 else if (arg == "dalvik")
1032 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1033 else if (arg == "DATAMEDIA") {
1034 ret_val = PartitionManager.Format_Data();
1035 } else if (arg == "INTERNAL") {
1036 int has_datamedia, dual_storage;
1037
1038 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1039 if (has_datamedia) {
1040 ret_val = PartitionManager.Wipe_Media_From_Data();
1041 } else {
1042 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1043 }
1044 } else if (arg == "EXTERNAL") {
1045 string External_Path;
1046
1047 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1048 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1049 } else if (arg == "ANDROIDSECURE") {
1050 ret_val = PartitionManager.Wipe_Android_Secure();
1051 } else if (arg == "LIST") {
1052 string Wipe_List, wipe_path;
1053 bool skip = false;
1054 ret_val = true;
1055 TWPartition* wipe_part = NULL;
1056
1057 DataManager::GetValue("tw_wipe_list", Wipe_List);
1058 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1059 if (!Wipe_List.empty()) {
1060 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1061 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1062 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1063 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1064 if (wipe_path == "/and-sec") {
1065 if (!PartitionManager.Wipe_Android_Secure()) {
1066 LOGERR("Unable to wipe android secure\n");
1067 ret_val = false;
1068 break;
1069 } else {
1070 skip = true;
1071 }
1072 } else if (wipe_path == "DALVIK") {
1073 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1074 LOGERR("Failed to wipe dalvik\n");
1075 ret_val = false;
1076 break;
1077 } else {
1078 skip = true;
1079 }
1080 } else if (wipe_path == "INTERNAL") {
1081 if (!PartitionManager.Wipe_Media_From_Data()) {
1082 ret_val = false;
1083 break;
1084 } else {
1085 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001086 }
1087 }
that3f7b1ac2014-12-30 11:30:13 +01001088 if (!skip) {
1089 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1090 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1091 ret_val = false;
1092 break;
1093 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1094 arg = wipe_path;
1095 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 } else {
that3f7b1ac2014-12-30 11:30:13 +01001097 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 }
that3f7b1ac2014-12-30 11:30:13 +01001099 start_pos = end_pos + 1;
1100 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001101 }
1102 }
that3f7b1ac2014-12-30 11:30:13 +01001103 } else
1104 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001105#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001106 if (arg == DataManager::GetSettingsStoragePath()) {
1107 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1108 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001109
that3f7b1ac2014-12-30 11:30:13 +01001110 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1111 LOGINFO("Making TWRP folder and saving settings.\n");
1112 Storage_Path += "/TWRP";
1113 mkdir(Storage_Path.c_str(), 0777);
1114 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 } else {
that3f7b1ac2014-12-30 11:30:13 +01001116 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1117 }
1118 }
1119#endif
1120 }
1121 PartitionManager.Update_System_Details();
1122 if (ret_val)
1123 ret_val = 0; // 0 is success
1124 else
1125 ret_val = 1; // 1 is failure
1126 operation_end(ret_val);
1127 return 0;
1128}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001129
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001130int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001131{
1132 operation_start("Refreshing Sizes");
1133 if (simulate) {
1134 simulate_progress_bar();
1135 } else
1136 PartitionManager.Update_System_Details();
1137 operation_end(0);
1138 return 0;
1139}
1140
1141int GUIAction::nandroid(std::string arg)
1142{
that3f7b1ac2014-12-30 11:30:13 +01001143 if (simulate) {
that73a52952015-01-28 23:07:34 +01001144 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001145 DataManager::SetValue("tw_partition", "Simulation");
1146 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001147 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001148 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001149 operation_start("Nandroid");
1150 int ret = 0;
1151
that3f7b1ac2014-12-30 11:30:13 +01001152 if (arg == "backup") {
1153 string Backup_Name;
1154 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1155 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1156 ret = PartitionManager.Run_Backup();
1157 }
1158 else {
1159 operation_end(1);
1160 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001161 }
1162 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1163 } else if (arg == "restore") {
1164 string Restore_Name;
1165 DataManager::GetValue("tw_restore", Restore_Name);
1166 ret = PartitionManager.Run_Restore(Restore_Name);
1167 } else {
1168 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001169 return -1;
1170 }
1171 DataManager::SetValue("tw_encrypt_backup", 0);
1172 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001173 if (ret == false)
1174 ret = 1; // 1 for failure
1175 else
1176 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001177 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001178 }
1179 else {
1180 DataManager::SetValue("tw_cancel_backup", 1);
1181 gui_print("Backup Canceled.\n");
1182 ret = 0;
1183 }
that73a52952015-01-28 23:07:34 +01001184 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001185 return ret;
1186 }
1187 return 0;
1188}
1189
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001190int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001192 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001193 }
1194 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001195 int op_status = PartitionManager.Cancel_Backup();
1196 if (op_status != 0)
1197 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001198 }
1199
1200 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001201}
Dees_Troy51a0e822012-09-05 15:24:24 -04001202
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001203int GUIAction::fixpermissions(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001204{
that73a52952015-01-28 23:07:34 +01001205 int op_status = 0;
1206
that3f7b1ac2014-12-30 11:30:13 +01001207 operation_start("Fix Permissions");
1208 LOGINFO("fix permissions started!\n");
1209 if (simulate) {
1210 simulate_progress_bar();
1211 } else {
that73a52952015-01-28 23:07:34 +01001212 op_status = PartitionManager.Fix_Permissions();
that3f7b1ac2014-12-30 11:30:13 +01001213 if (op_status != 0)
1214 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001215 }
that73a52952015-01-28 23:07:34 +01001216 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001217 return 0;
1218}
1219
1220int GUIAction::dd(std::string arg)
1221{
1222 operation_start("imaging");
1223
1224 if (simulate) {
1225 simulate_progress_bar();
1226 } else {
1227 string cmd = "dd " + arg;
1228 TWFunc::Exec_Cmd(cmd);
1229 }
1230 operation_end(0);
1231 return 0;
1232}
1233
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001234int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001235{
1236 operation_start("Partition SD Card");
1237 int ret_val = 0;
1238
1239 if (simulate) {
1240 simulate_progress_bar();
1241 } else {
1242 int allow_partition;
1243 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1244 if (allow_partition == 0) {
1245 gui_print("This device does not have a real SD Card!\nAborting!\n");
1246 } else {
1247 if (!PartitionManager.Partition_SDCard())
1248 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001249 }
that3f7b1ac2014-12-30 11:30:13 +01001250 }
1251 operation_end(ret_val);
1252 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001253
that3f7b1ac2014-12-30 11:30:13 +01001254}
Dees_Troy51a0e822012-09-05 15:24:24 -04001255
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001256int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001257{
1258 operation_start("Install HTC Dumlock");
1259 if (simulate) {
1260 simulate_progress_bar();
1261 } else
1262 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001263
that3f7b1ac2014-12-30 11:30:13 +01001264 operation_end(0);
1265 return 0;
1266}
Dees_Troy51a0e822012-09-05 15:24:24 -04001267
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001268int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001269{
1270 operation_start("HTC Dumlock Restore Boot");
1271 if (simulate) {
1272 simulate_progress_bar();
1273 } else
1274 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001275
that3f7b1ac2014-12-30 11:30:13 +01001276 operation_end(0);
1277 return 0;
1278}
Dees_Troy51a0e822012-09-05 15:24:24 -04001279
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001280int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001281{
1282 operation_start("HTC Dumlock Reflash Recovery");
1283 if (simulate) {
1284 simulate_progress_bar();
1285 } else
1286 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001287
that3f7b1ac2014-12-30 11:30:13 +01001288 operation_end(0);
1289 return 0;
1290}
Dees_Troy51a0e822012-09-05 15:24:24 -04001291
that3f7b1ac2014-12-30 11:30:13 +01001292int GUIAction::cmd(std::string arg)
1293{
1294 int op_status = 0;
1295
1296 operation_start("Command");
1297 LOGINFO("Running command: '%s'\n", arg.c_str());
1298 if (simulate) {
1299 simulate_progress_bar();
1300 } else {
1301 op_status = TWFunc::Exec_Cmd(arg);
1302 if (op_status != 0)
1303 op_status = 1;
1304 }
1305
1306 operation_end(op_status);
1307 return 0;
1308}
1309
1310int GUIAction::terminalcommand(std::string arg)
1311{
1312 int op_status = 0;
1313 string cmdpath, command;
1314
1315 DataManager::GetValue("tw_terminal_location", cmdpath);
1316 operation_start("CommandOutput");
1317 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1318 if (simulate) {
1319 simulate_progress_bar();
1320 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001321 } else if (arg == "exit") {
1322 LOGINFO("Exiting terminal\n");
1323 operation_end(op_status);
1324 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001325 } else {
1326 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1327 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001328 DataManager::SetValue("tw_terminal_state", 1);
1329 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001330 FILE* fp;
1331 char line[512];
1332
1333 fp = popen(command.c_str(), "r");
1334 if (fp == NULL) {
1335 LOGERR("Error opening command to run.\n");
1336 } else {
1337 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1338 struct timeval timeout;
1339 fd_set fdset;
1340
1341 while(keep_going)
1342 {
1343 FD_ZERO(&fdset);
1344 FD_SET(fd, &fdset);
1345 timeout.tv_sec = 0;
1346 timeout.tv_usec = 400000;
1347 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1348 if (has_data == 0) {
1349 // Timeout reached
1350 DataManager::GetValue("tw_terminal_state", check);
1351 if (check == 0) {
1352 keep_going = 0;
1353 }
1354 } else if (has_data < 0) {
1355 // End of execution
1356 keep_going = 0;
1357 } else {
1358 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001359 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001360 gui_print("%s", line); // Display output
1361 else
1362 keep_going = 0; // Done executing
1363 }
1364 }
1365 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001366 }
thatc6085482015-01-09 22:12:43 +01001367 DataManager::SetValue("tw_operation_status", 0);
1368 DataManager::SetValue("tw_operation_state", 1);
1369 DataManager::SetValue("tw_terminal_state", 0);
1370 DataManager::SetValue("tw_background_thread_running", 0);
1371 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001372 }
1373 return 0;
1374}
1375
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001376int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001377{
1378 int op_status = 0;
1379
1380 LOGINFO("Sending kill command...\n");
1381 operation_start("KillCommand");
1382 DataManager::SetValue("tw_operation_status", 0);
1383 DataManager::SetValue("tw_operation_state", 1);
1384 DataManager::SetValue("tw_terminal_state", 0);
1385 DataManager::SetValue("tw_background_thread_running", 0);
1386 DataManager::SetValue(TW_ACTION_BUSY, 0);
1387 return 0;
1388}
1389
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001390int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001391{
1392 int op_status = 0;
1393 operation_start("ReinjectTWRP");
1394 gui_print("Injecting TWRP into boot image...\n");
1395 if (simulate) {
1396 simulate_progress_bar();
1397 } else {
1398 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1399 gui_print("TWRP injection complete.\n");
1400 }
1401
1402 operation_end(op_status);
1403 return 0;
1404}
1405
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001406int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001407{
1408 int op_status = 0;
1409
1410 operation_start("CheckBackupName");
1411 if (simulate) {
1412 simulate_progress_bar();
1413 } else {
1414 op_status = PartitionManager.Check_Backup_Name(true);
1415 if (op_status != 0)
1416 op_status = 1;
1417 }
1418
1419 operation_end(op_status);
1420 return 0;
1421}
1422
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001423int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001424{
1425 int op_status = 0;
1426
1427 operation_start("Decrypt");
1428 if (simulate) {
1429 simulate_progress_bar();
1430 } else {
1431 string Password;
1432 DataManager::GetValue("tw_crypto_password", Password);
1433 op_status = PartitionManager.Decrypt_Device(Password);
1434 if (op_status != 0)
1435 op_status = 1;
1436 else {
that3f7b1ac2014-12-30 11:30:13 +01001437
1438 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1439
Ethan Yonkercf50da52015-01-12 21:59:07 -06001440 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001441
Ethan Yonkercf50da52015-01-12 21:59:07 -06001442 // Check for a custom theme and load it if exists
1443 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1444 if (has_datamedia != 0) {
1445 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001446 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001447 } else {
1448 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001449 }
1450 }
1451 }
1452 }
1453
1454 operation_end(op_status);
1455 return 0;
1456}
1457
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001458int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001459{
1460 operation_start("Sideload");
1461 if (simulate) {
1462 simulate_progress_bar();
1463 operation_end(0);
1464 } else {
thatc6085482015-01-09 22:12:43 +01001465 gui_print("Starting ADB sideload feature...\n");
1466 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1467
1468 // wait for the adb connection
1469 int ret = apply_from_adb("/", &sideload_child_pid);
1470 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1471
1472 if (ret != 0) {
1473 if (ret == -2)
1474 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1475 ret = 1; // failure
1476 } else {
1477 int wipe_cache = 0;
1478 int wipe_dalvik = 0;
1479 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1480
1481 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1482 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1483 PartitionManager.Wipe_By_Path("/cache");
1484 if (wipe_dalvik)
1485 PartitionManager.Wipe_Dalvik_Cache();
1486 } else {
1487 ret = 1; // failure
1488 }
thatcc8ddca2015-01-03 01:59:36 +01001489 }
thatc6085482015-01-09 22:12:43 +01001490 if (sideload_child_pid) {
1491 LOGINFO("Signaling child sideload process to exit.\n");
1492 struct stat st;
1493 // Calling stat() on this magic filename signals the minadbd
1494 // subprocess to shut down.
1495 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1496 int status;
1497 LOGINFO("Waiting for child sideload process to exit.\n");
1498 waitpid(sideload_child_pid, &status, 0);
1499 }
Dees Troy28a85d22015-04-28 02:03:16 +00001500 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001501 TWFunc::Toggle_MTP(mtp_was_enabled);
1502 reinject_after_flash();
1503 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001504 }
that3f7b1ac2014-12-30 11:30:13 +01001505 return 0;
1506}
1507
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001508int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001509{
that3f7b1ac2014-12-30 11:30:13 +01001510 struct stat st;
1511 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1512 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001513 LOGINFO("Signaling child sideload process to exit.\n");
1514 // Calling stat() on this magic filename signals the minadbd
1515 // subprocess to shut down.
1516 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1517 if (!sideload_child_pid) {
1518 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001519 return 0;
1520 }
thatcc8ddca2015-01-03 01:59:36 +01001521 ::sleep(1);
1522 LOGINFO("Killing child sideload process.\n");
1523 kill(sideload_child_pid, SIGTERM);
1524 int status;
1525 LOGINFO("Waiting for child sideload process to exit.\n");
1526 waitpid(sideload_child_pid, &status, 0);
1527 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001528 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1529 return 0;
1530}
1531
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001532int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001533{
Ethan Yonkere1abe612015-06-09 11:09:32 -05001534 int op_status = 1;
1535
that3f7b1ac2014-12-30 11:30:13 +01001536 operation_start("OpenRecoveryScript");
1537 if (simulate) {
1538 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001539 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001540 } else {
thatc6085482015-01-09 22:12:43 +01001541 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1542 // that we converted to ORS commands during boot in recovery.cpp.
1543 // Run those first.
1544 int reboot = 0;
1545 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1546 gui_print("Processing AOSP recovery commands...\n");
1547 if (OpenRecoveryScript::run_script_file() == 0) {
1548 reboot = 1;
Matt Mower85161af2015-08-08 10:00:50 -05001549 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001550 }
that3f7b1ac2014-12-30 11:30:13 +01001551 }
thatc6085482015-01-09 22:12:43 +01001552 // Check for the ORS file in /cache and attempt to run those commands.
1553 if (OpenRecoveryScript::check_for_script_file()) {
1554 gui_print("Processing OpenRecoveryScript file...\n");
1555 if (OpenRecoveryScript::run_script_file() == 0) {
1556 reboot = 1;
Ethan Yonkere1abe612015-06-09 11:09:32 -05001557 op_status = 0;
thatc6085482015-01-09 22:12:43 +01001558 }
1559 }
1560 if (reboot) {
Ethan Yonker9132d912015-02-02 10:32:49 -06001561 // Disable stock recovery reflashing
1562 TWFunc::Disable_Stock_Recovery_Replace();
thatc6085482015-01-09 22:12:43 +01001563 usleep(2000000); // Sleep for 2 seconds before rebooting
1564 TWFunc::tw_reboot(rb_system);
Ethan Yonkere1abe612015-06-09 11:09:32 -05001565 usleep(5000000); // Sleep for 5 seconds to allow reboot to occur
thatc6085482015-01-09 22:12:43 +01001566 } else {
1567 DataManager::SetValue("tw_page_done", 1);
1568 }
Ethan Yonkere1abe612015-06-09 11:09:32 -05001569 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001570 }
1571 return 0;
1572}
1573
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001574int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001575{
1576 int op_status = 0;
1577
1578 operation_start("Install SuperSU");
1579 if (simulate) {
1580 simulate_progress_bar();
1581 } else {
1582 if (!TWFunc::Install_SuperSU())
1583 op_status = 1;
1584 }
1585
1586 operation_end(op_status);
1587 return 0;
1588}
1589
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001590int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001591{
1592 int op_status = 0;
1593
1594 operation_start("Fixing Superuser Permissions");
1595 if (simulate) {
1596 simulate_progress_bar();
1597 } else {
1598 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1599 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1600 }
1601
1602 operation_end(op_status);
1603 return 0;
1604}
1605
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001606int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001607{
1608 int op_status = 0;
1609
1610 operation_start("Try Restore Decrypt");
1611 if (simulate) {
1612 simulate_progress_bar();
1613 } else {
1614 string Restore_Path, Filename, Password;
1615 DataManager::GetValue("tw_restore", Restore_Path);
1616 Restore_Path += "/";
1617 DataManager::GetValue("tw_restore_password", Password);
1618 TWFunc::SetPerformanceMode(true);
1619 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1620 op_status = 0; // success
1621 else
1622 op_status = 1; // fail
1623 TWFunc::SetPerformanceMode(false);
1624 }
1625
1626 operation_end(op_status);
1627 return 0;
1628}
1629
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001630int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001631{
1632 int op_status = 0;
1633
1634 operation_start("Repair Partition");
1635 if (simulate) {
1636 simulate_progress_bar();
1637 } else {
1638 string part_path;
1639 DataManager::GetValue("tw_partition_mount_point", part_path);
1640 if (PartitionManager.Repair_By_Path(part_path, true)) {
1641 op_status = 0; // success
1642 } else {
1643 LOGERR("Error repairing file system.\n");
1644 op_status = 1; // fail
1645 }
1646 }
1647
1648 operation_end(op_status);
1649 return 0;
1650}
1651
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001652int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001653{
1654 int op_status = 0;
1655
1656 operation_start("Resize Partition");
1657 if (simulate) {
1658 simulate_progress_bar();
1659 } else {
1660 string part_path;
1661 DataManager::GetValue("tw_partition_mount_point", part_path);
1662 if (PartitionManager.Resize_By_Path(part_path, true)) {
1663 op_status = 0; // success
1664 } else {
1665 LOGERR("Error resizing file system.\n");
1666 op_status = 1; // fail
1667 }
1668 }
1669
1670 operation_end(op_status);
1671 return 0;
1672}
1673
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001674int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001675{
1676 int op_status = 0;
1677
1678 operation_start("Change File System");
1679 if (simulate) {
1680 simulate_progress_bar();
1681 } else {
1682 string part_path, file_system;
1683 DataManager::GetValue("tw_partition_mount_point", part_path);
1684 DataManager::GetValue("tw_action_new_file_system", file_system);
1685 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1686 op_status = 0; // success
1687 } else {
1688 LOGERR("Error changing file system.\n");
1689 op_status = 1; // fail
1690 }
1691 }
1692 PartitionManager.Update_System_Details();
1693 operation_end(op_status);
1694 return 0;
1695}
1696
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001697int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001698{
1699 int op_status = 0;
1700
1701 operation_start("Start MTP");
1702 if (PartitionManager.Enable_MTP())
1703 op_status = 0; // success
1704 else
1705 op_status = 1; // fail
1706
1707 operation_end(op_status);
1708 return 0;
1709}
1710
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001711int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001712{
1713 int op_status = 0;
1714
1715 operation_start("Stop MTP");
1716 if (PartitionManager.Disable_MTP())
1717 op_status = 0; // success
1718 else
1719 op_status = 1; // fail
1720
1721 operation_end(op_status);
1722 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001723}
1724
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001725int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001726{
1727 int op_status = 0;
1728
1729 operation_start("Flash Image");
1730 string path, filename, full_filename;
1731 DataManager::GetValue("tw_zip_location", path);
1732 DataManager::GetValue("tw_file", filename);
1733 full_filename = path + "/" + filename;
1734 if (PartitionManager.Flash_Image(full_filename))
1735 op_status = 0; // success
1736 else
1737 op_status = 1; // fail
1738
1739 operation_end(op_status);
1740 return 0;
1741}
1742
Dees_Troy51a0e822012-09-05 15:24:24 -04001743int GUIAction::getKeyByName(std::string key)
1744{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001745 if (key == "home") return KEY_HOME;
1746 else if (key == "menu") return KEY_MENU;
1747 else if (key == "back") return KEY_BACK;
1748 else if (key == "search") return KEY_SEARCH;
1749 else if (key == "voldown") return KEY_VOLUMEDOWN;
1750 else if (key == "volup") return KEY_VOLUMEUP;
1751 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001752 int ret_val;
1753 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1754 if (!ret_val)
1755 return KEY_POWER;
1756 else
1757 return ret_val;
1758 }
1759
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001760 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001761}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001762
1763int GUIAction::checkpartitionlifetimewrites(std::string arg)
1764{
1765 int op_status = 0;
1766 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1767
1768 operation_start("Check Partition Lifetime Writes");
1769 if (sys) {
1770 if (sys->Check_Lifetime_Writes() != 0)
1771 DataManager::SetValue("tw_lifetime_writes", 1);
1772 else
1773 DataManager::SetValue("tw_lifetime_writes", 0);
1774 op_status = 0; // success
1775 } else {
1776 DataManager::SetValue("tw_lifetime_writes", 1);
1777 op_status = 1; // fail
1778 }
1779
1780 operation_end(op_status);
1781 return 0;
1782}
1783
1784int GUIAction::mountsystemtoggle(std::string arg)
1785{
1786 int op_status = 0;
1787 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001788 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001789
1790 operation_start("Toggle System Mount");
1791 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1792 op_status = 1; // fail
1793 } else {
1794 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1795 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001796 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001797 DataManager::SetValue("tw_mount_system_ro", 0);
1798 Part->Change_Mount_Read_Only(false);
1799 } else {
1800 DataManager::SetValue("tw_mount_system_ro", 1);
1801 Part->Change_Mount_Read_Only(true);
1802 }
1803 if (remount_system) {
1804 Part->Mount(true);
1805 }
1806 op_status = 0; // success
1807 } else {
1808 op_status = 1; // fail
1809 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001810 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1811 if (Part) {
1812 if (arg == "0") {
1813 Part->Change_Mount_Read_Only(false);
1814 } else {
1815 Part->Change_Mount_Read_Only(true);
1816 }
1817 if (remount_vendor) {
1818 Part->Mount(true);
1819 }
1820 op_status = 0; // success
1821 } else {
1822 op_status = 1; // fail
1823 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001824 }
1825
1826 operation_end(op_status);
1827 return 0;
1828}