blob: 36f400613f48133a0b725d25c65c1a5b4703a1f6 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 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>
Dees_Troy51a0e822012-09-05 15:24:24 -040035
36#include <string>
37#include <sstream>
38#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040040#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070043#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050044#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#endif
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"
Dees_Troy43d8b002012-09-17 16:00:01 -040052#include "../minadbd/adb.h"
53
Dees_Troy32c8eb82012-09-11 15:28:06 -040054int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040055void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040056int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040057int gui_start();
58};
59
60#include "rapidxml.hpp"
61#include "objects.hpp"
62
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070063#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070065#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040066
Dees_Troy51a0e822012-09-05 15:24:24 -040067void curtainClose(void);
68
69GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 xml_node<>* child;
73 xml_node<>* actions;
74 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 mKey = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 // First, get the action
81 actions = node->first_node("actions");
82 if (actions) child = actions->first_node("action");
83 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 while (child)
88 {
89 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 attr = child->first_attribute("function");
92 if (!attr) return;
93
94 action.mFunction = attr->value();
95 action.mArg = child->value();
96 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 child = child->next_sibling("action");
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Now, let's get either the key or region
102 child = node->first_node("touch");
103 if (child)
104 {
105 attr = child->first_attribute("key");
106 if (attr)
107 {
108 std::string key = attr->value();
109
110 mKey = getKeyByName(key);
111 }
112 else
113 {
114 attr = child->first_attribute("x");
115 if (!attr) return;
116 mActionX = atol(attr->value());
117 attr = child->first_attribute("y");
118 if (!attr) return;
119 mActionY = atol(attr->value());
120 attr = child->first_attribute("w");
121 if (!attr) return;
122 mActionW = atol(attr->value());
123 attr = child->first_attribute("h");
124 if (!attr) return;
125 mActionH = atol(attr->value());
126 }
127 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
131{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (state == TOUCH_RELEASE)
133 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136}
137
138int GUIAction::NotifyKey(int key)
139{
140 if (!mKey || key != mKey)
141 return 1;
142
143 doActions();
144 return 0;
145}
146
147int GUIAction::NotifyVarChange(std::string varName, std::string value)
148{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
150 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400151
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 // This handles notifying the condition system of page start
153 if (varName.empty() && isConditionValid())
154 NotifyPageSet();
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
157 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
162void GUIAction::simulate_progress_bar(void)
163{
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400165 for (int i = 0; i < 5; i++)
166 {
167 usleep(500000);
168 DataManager::SetValue("ui_progress", i * 20);
169 }
170}
171
Dees_Troy657c3092012-09-10 20:32:10 -0400172int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400173{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400175
176 DataManager::SetValue("ui_progress", 0);
177
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200178 if (filename.empty())
179 {
180 LOGERR("No file specified.\n");
181 return -1;
182 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 // We're going to jump to this page first, like a loading page
185 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 int fd = -1;
188 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200190 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400191 return -1;
192
193 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 {
195 LOGERR("Unable to open zip file.\n");
196 return -1;
197 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400198
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 if (twrp != NULL)
202 {
203 unlink("/tmp/twrp.zip");
204 fd = creat("/tmp/twrp.zip", 0666);
205 }
206 if (fd >= 0 && twrp != NULL &&
207 mzExtractZipEntryToFile(&zip, twrp, fd) &&
208 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
209 {
210 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400211 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 gui_changePage("main");
213 }
214 else
215 {
216 // In this case, we just use the default page
217 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400218 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 }
220 if (fd >= 0)
221 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400222
223 if (simulate) {
224 simulate_progress_bar();
225 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400226 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
228 // Now, check if we need to ensure TWRP remains installed...
229 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500230 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231 if (stat("/sbin/installTwrp", &st) == 0)
232 {
233 DataManager::SetValue("tw_operation", "Configuring TWRP");
234 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000235 gui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500236 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400237 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 }
240 }
241 }
242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 // Done
244 DataManager::SetValue("ui_progress", 100);
245 DataManager::SetValue("ui_progress", 0);
246 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247}
248
249int GUIAction::doActions()
250{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 if (mActions.size() < 1) return -1;
252 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 return doAction(mActions.at(0), 0);
254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 // For multi-action, we always use a thread
256 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000257 pthread_attr_t tattr;
258
259 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000261 return -1;
262 }
263 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000265 return -1;
266 }
267 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000268 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000269 return -1;
270 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500271 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000273 return -1;
274 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500275 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000276 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000278 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000279 thread_start(this);
280 } else {
281 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 }
284 }
285 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000287 return -1;
288 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
293void* GUIAction::thread_start(void *cookie)
294{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400296
297 DataManager::SetValue(TW_ACTION_BUSY, 1);
298
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 if (ourThis->mActions.size() > 1)
300 {
301 std::vector<Action>::iterator iter;
302 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
303 ourThis->doAction(*iter, 1);
304 }
305 else
306 {
307 ourThis->doAction(ourThis->mActions.at(0), 1);
308 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400309 int check = 0;
310 DataManager::GetValue("tw_background_thread_running", check);
311 if (check == 0)
312 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400314}
315
316void GUIAction::operation_start(const string operation_name)
317{
318 DataManager::SetValue(TW_ACTION_BUSY, 1);
319 DataManager::SetValue("ui_progress", 0);
320 DataManager::SetValue("tw_operation", operation_name);
321 DataManager::SetValue("tw_operation_status", 0);
322 DataManager::SetValue("tw_operation_state", 0);
323}
324
325void GUIAction::operation_end(const int operation_status, const int simulate)
326{
327 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400328 DataManager::SetValue("ui_progress", 100);
329 if (simulate) {
330 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
331 if (simulate_fail != 0)
332 DataManager::SetValue("tw_operation_status", 1);
333 else
334 DataManager::SetValue("tw_operation_status", 0);
335 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500336 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400337 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500338 }
339 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400340 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500341 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400342 }
343 DataManager::SetValue("tw_operation_state", 1);
344 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700345#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500346 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700347#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400348}
349
350int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
351{
352 static string zip_queue[10];
353 static int zip_queue_index;
354 static pthread_t terminal_command;
355 int simulate;
356
357 std::string arg = gui_parse_text(action.mArg);
358
359 std::string function = gui_parse_text(action.mFunction);
360
361 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
362
Dees_Troy6ef66352013-02-21 08:26:57 -0600363 if (function == "reboot")
364 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
Dees_Troy6ef66352013-02-21 08:26:57 -0600367 sync();
368 DataManager::SetValue("tw_gui_done", 1);
369 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400370
Dees_Troy6ef66352013-02-21 08:26:57 -0600371 return 0;
372 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 if (function == "home")
374 {
375 PageManager::SelectPackage("TWRP");
376 gui_changePage("main");
377 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378 }
379
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 if (function == "key")
381 {
382 PageManager::NotifyKey(getKeyByName(arg));
383 return 0;
384 }
385
386 if (function == "page") {
387 std::string page_name = gui_parse_text(arg);
388 return gui_changePage(page_name);
389 }
390
391 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400392 int check = 0, ret_val = 0;
393 std::string theme_path;
394
395 operation_start("Reload Theme");
396 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000398 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 check = 1;
400 }
401
402 theme_path += "/TWRP/theme/ui.zip";
403 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
404 {
405 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000406 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
408 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000409 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 ret_val = 1;
411 }
412 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200413 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000414 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 }
416
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 if (function == "readBackup")
418 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 string Restore_Name;
420 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400421 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200422 return 0;
423 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400424
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200425 if (function == "set")
426 {
427 if (arg.find('=') != string::npos)
428 {
429 string varName = arg.substr(0, arg.find('='));
430 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400431
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200432 DataManager::GetValue(value, value);
433 DataManager::SetValue(varName, value);
434 }
435 else
436 DataManager::SetValue(arg, "1");
437 return 0;
438 }
439 if (function == "clear")
440 {
441 DataManager::SetValue(arg, "0");
442 return 0;
443 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400444
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200445 if (function == "mount")
446 {
447 if (arg == "usb")
448 {
449 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400451 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200454 }
455 else if (!simulate)
456 {
457 string cmd;
Dees_Troy51a0e822012-09-05 15:24:24 -0400458 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400459 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400461 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462 else
Dees_Troy51127312012-09-08 13:08:49 -0400463 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200464 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000465 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200466 return 0;
467 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200469 if (function == "umount" || function == "unmount")
470 {
471 if (arg == "usb")
472 {
473 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400474 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000476 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 }
479 else if (!simulate)
480 {
481 string cmd;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400483 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400485 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 else
Dees_Troy51127312012-09-08 13:08:49 -0400487 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200488 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000489 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200490 return 0;
491 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400492
493 if (function == "restoredefaultsettings")
494 {
495 operation_start("Restore Defaults");
496 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000497 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 else {
499 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400500 PartitionManager.Update_System_Details();
501 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 }
503 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000504 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 }
506
507 if (function == "copylog")
508 {
509 operation_start("Copy Log");
510 if (!simulate)
511 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500512 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400513 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500514 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
515 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000517 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 } else
519 simulate_progress_bar();
520 operation_end(0, simulate);
521 return 0;
522 }
523
524 if (function == "compute" || function == "addsubtract")
525 {
526 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200527 {
528 string varName = arg.substr(0, arg.find('+'));
529 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 int amount_to_add = atoi(string_to_add.c_str());
531 int value;
532
533 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200534 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400535 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200536 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 {
539 string varName = arg.substr(0, arg.find('-'));
540 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 int amount_to_subtract = atoi(string_to_subtract.c_str());
542 int value;
543
544 DataManager::GetValue(varName, value);
545 value -= amount_to_subtract;
546 if (value <= 0)
547 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200548 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200551 if (arg.find("*") != string::npos)
552 {
553 string varName = arg.substr(0, arg.find('*'));
554 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
555 int multiply_by = atoi(multiply_by_str.c_str());
556 int value;
557
558 DataManager::GetValue(varName, value);
559 DataManager::SetValue(varName, value*multiply_by);
560 return 0;
561 }
562 if (arg.find("/") != string::npos)
563 {
564 string varName = arg.substr(0, arg.find('/'));
565 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
566 int divide_by = atoi(divide_by_str.c_str());
567 int value;
568
569 if(divide_by != 0)
570 {
571 DataManager::GetValue(varName, value);
572 DataManager::SetValue(varName, value/divide_by);
573 }
574 return 0;
575 }
576 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
577 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400578 }
579
580 if (function == "setguitimezone")
581 {
582 string SelectedZone;
583 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
584 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
585 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
586
587 int dst;
588 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
589
590 string offset;
591 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
592
593 string NewTimeZone = Zone;
594 if (offset != "0")
595 NewTimeZone += ":" + offset;
596
597 if (dst != 0)
598 NewTimeZone += DSTZone;
599
600 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400601 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400602 return 0;
603 }
604
605 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000606 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400607 if (arg == "internal") {
608 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
609 } else if (arg == "external") {
610 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
611 }
Dees_Troy51127312012-09-08 13:08:49 -0400612 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400614 string zip_path, zip_root;
615 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
616 zip_root = TWFunc::Get_Root_Path(zip_path);
617#ifdef RECOVERY_SDCARD_ON_DATA
618 #ifndef TW_EXTERNAL_STORAGE_PATH
619 if (zip_root != "/sdcard")
620 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
621 #else
622 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
623 if (zip_root != "/emmc")
624 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
625 } else {
626 if (zip_root != "/sdcard")
627 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
628 }
629 #endif
630#else
631 if (zip_root != DataManager::GetCurrentStoragePath())
632 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
633#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 // Save the current zip location to the external variable
635 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
636 // Change the current zip location to the internal variable
637 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
638 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400639 string zip_path, zip_root;
640 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
641 zip_root = TWFunc::Get_Root_Path(zip_path);
642 if (zip_root != DataManager::GetCurrentStoragePath()) {
643 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
644 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400645 // Save the current zip location to the internal variable
646 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
647 // Change the current zip location to the external variable
648 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
649 }
650 } else {
651 // We weren't able to toggle for some reason, restore original setting
652 if (arg == "internal") {
653 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
654 } else if (arg == "external") {
655 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
656 }
657 }
658 return 0;
659 }
660
661 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200662 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400663
664 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200665 {
666 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000667 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 return 0;
669 }
670 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
671 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
672 zip_queue_index++;
673 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
674 }
675 return 0;
676 }
677
678 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200679 {
680 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000681 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 return 0;
683 } else {
684 zip_queue_index--;
685 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
686 }
687 return 0;
688 }
689
690 if (function == "queueclear")
691 {
692 zip_queue_index = 0;
693 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
694 return 0;
695 }
696
697 if (function == "sleep")
698 {
699 operation_start("Sleep");
700 usleep(atoi(arg.c_str()));
701 operation_end(0, simulate);
702 return 0;
703 }
704
Dees Troyb21cc642013-09-10 17:36:41 +0000705 if (function == "appenddatetobackupname")
706 {
707 operation_start("AppendDateToBackupName");
708 string Backup_Name;
709 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
710 Backup_Name += TWFunc::Get_Current_Date();
711 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
712 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
713 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
714 operation_end(0, simulate);
715 return 0;
716 }
717
718 if (function == "generatebackupname")
719 {
720 operation_start("GenerateBackupName");
721 TWFunc::Auto_Generate_Backup_Name();
722 operation_end(0, simulate);
723 return 0;
724 }
725
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200726 if (isThreaded)
727 {
728 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400729 {
730 struct stat st;
731 string newpath = arg + "/.";
732
733 operation_start("FileExists");
734 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
735 operation_end(0, simulate);
736 else
737 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000738 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 }
740
741 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200742 {
Dees_Troy657c3092012-09-10 20:32:10 -0400743 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400744
745 for (i=0; i<zip_queue_index; i++) {
746 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200747 DataManager::SetValue("tw_filename", zip_queue[i]);
748 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400749
Dees_Troy657c3092012-09-10 20:32:10 -0400750 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000752 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400753 i = 10; // Error flashing zip - exit queue
754 ret_val = 1;
755 }
756 }
757 zip_queue_index = 0;
758 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
759
Dees_Troy657c3092012-09-10 20:32:10 -0400760 if (wipe_cache)
761 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500762 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
764 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000765 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 if (simulate) {
767 simulate_progress_bar();
768 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400769 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
770 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500771 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400772 else {
773 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500774 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400775 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000776 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 }
778 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400779 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200781 return 0;
782 }
783 if (function == "wipe")
784 {
785 operation_start("Format");
786 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400787
Dees_Troy38bd7602012-09-14 13:33:53 -0400788 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400789
790 if (simulate) {
791 simulate_progress_bar();
792 } else {
793 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400794 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400796 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400797 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400798 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400799 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400800 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400802 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400803 } else if (arg == "INTERNAL") {
804 int has_datamedia, dual_storage;
805
806 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
807 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400808 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400810 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 }
812 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400813 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400814
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
816 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400817 } else if (arg == "ANDROIDSECURE") {
818 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500819 } else if (arg == "LIST") {
820 string Wipe_List, wipe_path;
821 bool skip = false;
822 ret_val = true;
823 TWPartition* wipe_part = NULL;
824
825 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000826 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500827 if (!Wipe_List.empty()) {
828 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
829 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
830 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 if (wipe_path == "/and-sec") {
833 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000834 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 ret_val = false;
836 break;
837 } else {
838 skip = true;
839 }
840 } else if (wipe_path == "DALVIK") {
841 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000842 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500843 ret_val = false;
844 break;
845 } else {
846 skip = true;
847 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000848 } else if (wipe_path == "INTERNAL") {
849 if (!PartitionManager.Wipe_Media_From_Data()) {
850 ret_val = false;
851 break;
852 } else {
853 skip = true;
854 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500855 }
856 if (!skip) {
857 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000858 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500859 ret_val = false;
860 break;
861 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
862 arg = wipe_path;
863 }
864 } else {
865 skip = false;
866 }
867 start_pos = end_pos + 1;
868 end_pos = Wipe_List.find(";", start_pos);
869 }
870 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400871 } else
872 ret_val = PartitionManager.Wipe_By_Path(arg);
873
874 if (arg == DataManager::GetSettingsStoragePath()) {
875 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
876 string Storage_Path = DataManager::GetSettingsStoragePath();
877
878 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000879 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400880 Storage_Path += "/TWRP";
881 mkdir(Storage_Path.c_str(), 0777);
882 DataManager::Flush();
883 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000884 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400885 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400886 }
887 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400888 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400889 if (ret_val)
890 ret_val = 0; // 0 is success
891 else
892 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200893 operation_end(ret_val, simulate);
894 return 0;
895 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400896 if (function == "refreshsizes")
897 {
898 operation_start("Refreshing Sizes");
899 if (simulate) {
900 simulate_progress_bar();
901 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400903 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000904 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400905 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200906 if (function == "nandroid")
907 {
908 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400909 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400910
911 if (simulate) {
912 DataManager::SetValue("tw_partition", "Simulation");
913 simulate_progress_bar();
914 } else {
915 if (arg == "backup") {
916 string Backup_Name;
917 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000918 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400919 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500920 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400921 else {
922 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400923 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400924 }
Dees Troyb21cc642013-09-10 17:36:41 +0000925 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -0400926 } else if (arg == "restore") {
927 string Restore_Name;
928 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400929 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400930 } else {
931 operation_end(1, simulate);
932 return -1;
933 }
934 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000935 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400936 if (ret == false)
937 ret = 1; // 1 for failure
938 else
939 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200940 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000941 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200942 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400943 if (function == "fixpermissions")
944 {
945 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200946 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400947 if (simulate) {
948 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400949 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400950 int op_status = PartitionManager.Fix_Permissions();
951 if (op_status != 0)
952 op_status = 1; // failure
953 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400954 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400955 return 0;
956 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200957 if (function == "dd")
958 {
959 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -0400960
961 if (simulate) {
962 simulate_progress_bar();
963 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500964 string result;
965 string cmd = "dd " + arg;
966 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400967 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200968 operation_end(0, simulate);
969 return 0;
970 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400971 if (function == "partitionsd")
972 {
973 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400974 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400975
976 if (simulate) {
977 simulate_progress_bar();
978 } else {
979 int allow_partition;
980 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
981 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000982 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400983 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400984 if (!PartitionManager.Partition_SDCard())
985 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400986 }
987 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400988 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400989 return 0;
990 }
991 if (function == "installhtcdumlock")
992 {
993 operation_start("Install HTC Dumlock");
994 if (simulate) {
995 simulate_progress_bar();
996 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400997 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400998
999 operation_end(0, simulate);
1000 return 0;
1001 }
1002 if (function == "htcdumlockrestoreboot")
1003 {
1004 operation_start("HTC Dumlock Restore Boot");
1005 if (simulate) {
1006 simulate_progress_bar();
1007 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001008 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001009
1010 operation_end(0, simulate);
1011 return 0;
1012 }
1013 if (function == "htcdumlockreflashrecovery")
1014 {
1015 operation_start("HTC Dumlock Reflash Recovery");
1016 if (simulate) {
1017 simulate_progress_bar();
1018 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001019 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001020
1021 operation_end(0, simulate);
1022 return 0;
1023 }
1024 if (function == "cmd")
1025 {
1026 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001027 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001028
1029 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001031 if (simulate) {
1032 simulate_progress_bar();
1033 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001034 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -04001035 if (op_status != 0)
1036 op_status = 1;
1037 }
1038
1039 operation_end(op_status, simulate);
1040 return 0;
1041 }
1042 if (function == "terminalcommand")
1043 {
1044 int op_status = 0;
1045 string cmdpath, command;
1046
1047 DataManager::GetValue("tw_terminal_location", cmdpath);
1048 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001049 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001050 if (simulate) {
1051 simulate_progress_bar();
1052 operation_end(op_status, simulate);
1053 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001054 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001055 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001056 DataManager::SetValue("tw_terminal_command_thread", command);
1057 DataManager::SetValue("tw_terminal_state", 1);
1058 DataManager::SetValue("tw_background_thread_running", 1);
1059 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1060 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001062 DataManager::SetValue("tw_terminal_state", 0);
1063 DataManager::SetValue("tw_background_thread_running", 0);
1064 operation_end(1, simulate);
1065 }
1066 }
1067 return 0;
1068 }
1069 if (function == "killterminal")
1070 {
1071 int op_status = 0;
1072
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001074 operation_start("KillCommand");
1075 DataManager::SetValue("tw_operation_status", 0);
1076 DataManager::SetValue("tw_operation_state", 1);
1077 DataManager::SetValue("tw_terminal_state", 0);
1078 DataManager::SetValue("tw_background_thread_running", 0);
1079 DataManager::SetValue(TW_ACTION_BUSY, 0);
1080 return 0;
1081 }
1082 if (function == "reinjecttwrp")
1083 {
1084 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001085 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001086 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001088 if (simulate) {
1089 simulate_progress_bar();
1090 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001091 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001093 }
1094
1095 operation_end(op_status, simulate);
1096 return 0;
1097 }
1098 if (function == "checkbackupname")
1099 {
1100 int op_status = 0;
1101
1102 operation_start("CheckBackupName");
1103 if (simulate) {
1104 simulate_progress_bar();
1105 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001106 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001107 if (op_status != 0)
1108 op_status = 1;
1109 }
1110
1111 operation_end(op_status, simulate);
1112 return 0;
1113 }
1114 if (function == "decrypt")
1115 {
1116 int op_status = 0;
1117
1118 operation_start("Decrypt");
1119 if (simulate) {
1120 simulate_progress_bar();
1121 } else {
1122 string Password;
1123 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001124 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 if (op_status != 0)
1126 op_status = 1;
1127 else {
1128 int load_theme = 1;
1129
1130 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001131
1132 if (load_theme) {
1133 int has_datamedia;
1134
1135 // Check for a custom theme and load it if exists
1136 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1137 if (has_datamedia != 0) {
1138 struct stat st;
1139 int check = 0;
1140 std::string theme_path;
1141
1142 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001143 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001145 check = 1;
1146 }
1147
1148 theme_path += "/TWRP/theme/ui.zip";
1149 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1150 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1151 {
1152 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001153 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001154 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1155 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001157 }
1158 }
1159 }
1160 }
1161 }
1162 }
1163 }
1164
1165 operation_end(op_status, simulate);
1166 return 0;
1167 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001168 if (function == "adbsideload")
1169 {
1170 int ret = 0;
1171
1172 operation_start("Sideload");
1173 if (simulate) {
1174 simulate_progress_bar();
1175 } else {
1176 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001177 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001178 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001179
1180 if (!PartitionManager.Mount_Current_Storage(true)) {
1181 operation_end(1, simulate);
1182 return 0;
1183 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001184 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1185 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001186 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001187 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001189 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 ret = apply_from_adb(Sideload_File.c_str());
1191 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001192 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001193 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001195 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1196 PartitionManager.Wipe_By_Path("/cache");
1197 if (wipe_dalvik)
1198 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001199 } else {
1200 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001201 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001202 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1203 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001205 if (simulate) {
1206 simulate_progress_bar();
1207 } else {
1208 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1209 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001210 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001211 else {
1212 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001213 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001214 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001215 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001216 }
1217 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001218 }
1219 operation_end(ret, simulate);
1220 return 0;
1221 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001222 if (function == "adbsideloadcancel")
1223 {
1224 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001226 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001227 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001228 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001229 property_get("tw_child_pid", child_prop, "error");
1230 if (strcmp(child_prop, "error") == 0) {
1231 LOGERR("Unable to get child ID from prop\n");
1232 return 0;
1233 }
1234 child_pid = atoi(child_prop);
1235 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001236 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001237 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001238 return 0;
1239 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001240 if (function == "openrecoveryscript") {
1241 operation_start("OpenRecoveryScript");
1242 if (simulate) {
1243 simulate_progress_bar();
1244 } else {
1245 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1246 // that we converted to ORS commands during boot in recovery.cpp.
1247 // Run those first.
1248 int reboot = 0;
1249 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001250 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001251 if (OpenRecoveryScript::run_script_file() == 0) {
1252 reboot = 1;
1253 }
1254 }
1255 // Check for the ORS file in /cache and attempt to run those commands.
1256 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001258 if (OpenRecoveryScript::run_script_file() == 0) {
1259 reboot = 1;
1260 }
1261 }
1262 if (reboot) {
1263 usleep(2000000); // Sleep for 2 seconds before rebooting
1264 TWFunc::tw_reboot(rb_system);
1265 } else {
1266 DataManager::SetValue("tw_page_done", 1);
1267 }
1268 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001269 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001270 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001271 if (function == "installsu")
1272 {
1273 int op_status = 0;
1274
1275 operation_start("Install SuperSU");
1276 if (simulate) {
1277 simulate_progress_bar();
1278 } else {
1279 if (!TWFunc::Install_SuperSU())
1280 op_status = 1;
1281 }
1282
1283 operation_end(op_status, simulate);
1284 return 0;
1285 }
1286 if (function == "fixsu")
1287 {
1288 int op_status = 0;
1289
1290 operation_start("Fixing Superuser Permissions");
1291 if (simulate) {
1292 simulate_progress_bar();
1293 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001294 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1295 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001296 }
1297
1298 operation_end(op_status, simulate);
1299 return 0;
1300 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001301 if (function == "decrypt_backup")
1302 {
1303 int op_status = 0;
1304
1305 operation_start("Try Restore Decrypt");
1306 if (simulate) {
1307 simulate_progress_bar();
1308 } else {
1309 string Restore_Path, Filename, Password;
1310 DataManager::GetValue("tw_restore", Restore_Path);
1311 Restore_Path += "/";
1312 DataManager::GetValue("tw_restore_password", Password);
1313 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1314 op_status = 0; // success
1315 else
1316 op_status = 1; // fail
1317 }
1318
1319 operation_end(op_status, simulate);
1320 return 0;
1321 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001322 }
1323 else
1324 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001325 pthread_t t;
1326 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001327 return 0;
1328 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001329 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001330 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001331}
1332
1333int GUIAction::getKeyByName(std::string key)
1334{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001335 if (key == "home") return KEY_HOME;
1336 else if (key == "menu") return KEY_MENU;
1337 else if (key == "back") return KEY_BACK;
1338 else if (key == "search") return KEY_SEARCH;
1339 else if (key == "voldown") return KEY_VOLUMEDOWN;
1340 else if (key == "volup") return KEY_VOLUMEUP;
1341 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001342 int ret_val;
1343 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1344 if (!ret_val)
1345 return KEY_POWER;
1346 else
1347 return ret_val;
1348 }
1349
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001350 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001351}
1352
1353void* GUIAction::command_thread(void *cookie)
1354{
1355 string command;
1356 FILE* fp;
1357 char line[512];
1358
1359 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001360 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001361 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001363 } else {
1364 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1365 struct timeval timeout;
1366 fd_set fdset;
1367
1368 while(keep_going)
1369 {
1370 FD_ZERO(&fdset);
1371 FD_SET(fd, &fdset);
1372 timeout.tv_sec = 0;
1373 timeout.tv_usec = 400000;
1374 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1375 if (has_data == 0) {
1376 // Timeout reached
1377 DataManager::GetValue("tw_terminal_state", check);
1378 if (check == 0) {
1379 keep_going = 0;
1380 }
1381 } else if (has_data < 0) {
1382 // End of execution
1383 keep_going = 0;
1384 } else {
1385 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001386 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001387 bytes_read = read(fd, line, sizeof(line));
1388 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001390 else
1391 keep_going = 0; // Done executing
1392 }
1393 }
1394 fclose(fp);
1395 }
1396 DataManager::SetValue("tw_operation_status", 0);
1397 DataManager::SetValue("tw_operation_state", 1);
1398 DataManager::SetValue("tw_terminal_state", 0);
1399 DataManager::SetValue("tw_background_thread_running", 0);
1400 DataManager::SetValue(TW_ACTION_BUSY, 0);
1401 return NULL;
1402}