blob: 3e1f793df2d9631c1712b44c6514de2f4b01e18b [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"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050043#include "blanktimer.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040044
Dees_Troy51a0e822012-09-05 15:24:24 -040045extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000046#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040047#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040049#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000050#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040051#include "../minadbd/adb.h"
52
Dees_Troy32c8eb82012-09-11 15:28:06 -040053int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040054void 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 -040055int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040056int gui_start();
57};
58
59#include "rapidxml.hpp"
60#include "objects.hpp"
61
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050062extern blanktimer blankTimer;
Dees_Troy43d8b002012-09-17 16:00:01 -040063
Dees_Troy51a0e822012-09-05 15:24:24 -040064void curtainClose(void);
65
66GUIAction::GUIAction(xml_node<>* node)
67 : Conditional(node)
68{
69 xml_node<>* child;
70 xml_node<>* actions;
71 xml_attribute<>* attr;
72
73 mKey = 0;
74
75 if (!node) return;
76
77 // First, get the action
78 actions = node->first_node("actions");
79 if (actions) child = actions->first_node("action");
80 else child = node->first_node("action");
81
82 if (!child) return;
83
84 while (child)
85 {
86 Action action;
87
88 attr = child->first_attribute("function");
89 if (!attr) return;
90
91 action.mFunction = attr->value();
92 action.mArg = child->value();
93 mActions.push_back(action);
94
95 child = child->next_sibling("action");
96 }
97
98 // Now, let's get either the key or region
99 child = node->first_node("touch");
100 if (child)
101 {
102 attr = child->first_attribute("key");
103 if (attr)
104 {
105 std::string key = attr->value();
106
107 mKey = getKeyByName(key);
108 }
109 else
110 {
111 attr = child->first_attribute("x");
112 if (!attr) return;
113 mActionX = atol(attr->value());
114 attr = child->first_attribute("y");
115 if (!attr) return;
116 mActionY = atol(attr->value());
117 attr = child->first_attribute("w");
118 if (!attr) return;
119 mActionW = atol(attr->value());
120 attr = child->first_attribute("h");
121 if (!attr) return;
122 mActionH = atol(attr->value());
123 }
124 }
125}
126
127int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
128{
129 if (state == TOUCH_RELEASE)
130 doActions();
131
132 return 0;
133}
134
135int GUIAction::NotifyKey(int key)
136{
137 if (!mKey || key != mKey)
138 return 1;
139
140 doActions();
141 return 0;
142}
143
144int GUIAction::NotifyVarChange(std::string varName, std::string value)
145{
146 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
147 doActions();
148
149 // This handles notifying the condition system of page start
150 if (varName.empty() && isConditionValid())
151 NotifyPageSet();
152
153 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
154 doActions();
155
156 return 0;
157}
158
159void GUIAction::simulate_progress_bar(void)
160{
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400162 for (int i = 0; i < 5; i++)
163 {
164 usleep(500000);
165 DataManager::SetValue("ui_progress", i * 20);
166 }
167}
168
Dees_Troy657c3092012-09-10 20:32:10 -0400169int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400170{
171 int ret_val = 0;
172
173 DataManager::SetValue("ui_progress", 0);
174
175 if (filename.empty())
176 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 LOGERR("No file specified.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400178 return -1;
179 }
180
181 // We're going to jump to this page first, like a loading page
182 gui_changePage(pageName);
183
184 int fd = -1;
185 ZipArchive zip;
186
Dees_Troy657c3092012-09-10 20:32:10 -0400187 if (!PartitionManager.Mount_By_Path(filename, true))
188 return -1;
189
190 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400191 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGERR("Unable to open zip file.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 return -1;
194 }
195
196 // Check the zip to see if it has a custom installer theme
197 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
198 if (twrp != NULL)
199 {
200 unlink("/tmp/twrp.zip");
201 fd = creat("/tmp/twrp.zip", 0666);
202 }
203 if (fd >= 0 && twrp != NULL &&
204 mzExtractZipEntryToFile(&zip, twrp, fd) &&
205 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
206 {
207 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400208 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 gui_changePage("main");
210 }
211 else
212 {
213 // In this case, we just use the default page
214 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400215 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 }
217 if (fd >= 0)
218 close(fd);
219
220 if (simulate) {
221 simulate_progress_bar();
222 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400223 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
225 // Now, check if we need to ensure TWRP remains installed...
226 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500227 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 if (stat("/sbin/installTwrp", &st) == 0)
229 {
230 DataManager::SetValue("tw_operation", "Configuring TWRP");
231 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000232 gui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500233 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400234 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000235 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 }
237 }
238 }
239
240 // Done
241 DataManager::SetValue("ui_progress", 100);
242 DataManager::SetValue("ui_progress", 0);
243 return ret_val;
244}
245
246int GUIAction::doActions()
247{
248 if (mActions.size() < 1) return -1;
249 if (mActions.size() == 1)
250 return doAction(mActions.at(0), 0);
251
252 // For multi-action, we always use a thread
253 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000254 pthread_attr_t tattr;
255
256 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000258 return -1;
259 }
260 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000262 return -1;
263 }
264 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000266 return -1;
267 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500268 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000269 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000270 return -1;
271 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500272 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000273 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000274 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000275 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000276 thread_start(this);
277 } else {
278 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000280 }
281 }
282 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000284 return -1;
285 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400286
287 return 0;
288}
289
290void* GUIAction::thread_start(void *cookie)
291{
292 GUIAction* ourThis = (GUIAction*) cookie;
293
294 DataManager::SetValue(TW_ACTION_BUSY, 1);
295
296 if (ourThis->mActions.size() > 1)
297 {
298 std::vector<Action>::iterator iter;
299 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
300 ourThis->doAction(*iter, 1);
301 }
302 else
303 {
304 ourThis->doAction(ourThis->mActions.at(0), 1);
305 }
306 int check = 0;
307 DataManager::GetValue("tw_background_thread_running", check);
308 if (check == 0)
309 DataManager::SetValue(TW_ACTION_BUSY, 0);
310 return NULL;
311}
312
313void GUIAction::operation_start(const string operation_name)
314{
315 DataManager::SetValue(TW_ACTION_BUSY, 1);
316 DataManager::SetValue("ui_progress", 0);
317 DataManager::SetValue("tw_operation", operation_name);
318 DataManager::SetValue("tw_operation_status", 0);
319 DataManager::SetValue("tw_operation_state", 0);
320}
321
322void GUIAction::operation_end(const int operation_status, const int simulate)
323{
324 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400325 DataManager::SetValue("ui_progress", 100);
326 if (simulate) {
327 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
328 if (simulate_fail != 0)
329 DataManager::SetValue("tw_operation_status", 1);
330 else
331 DataManager::SetValue("tw_operation_status", 0);
332 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400334 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500335 }
336 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400337 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500338 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 }
340 DataManager::SetValue("tw_operation_state", 1);
341 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500342 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400343}
344
345int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
346{
347 static string zip_queue[10];
348 static int zip_queue_index;
349 static pthread_t terminal_command;
350 int simulate;
351
352 std::string arg = gui_parse_text(action.mArg);
353
354 std::string function = gui_parse_text(action.mFunction);
355
356 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
357
Dees_Troy6ef66352013-02-21 08:26:57 -0600358 if (function == "reboot")
359 {
360 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400361
Dees_Troy6ef66352013-02-21 08:26:57 -0600362 sync();
363 DataManager::SetValue("tw_gui_done", 1);
364 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
Dees_Troy6ef66352013-02-21 08:26:57 -0600366 return 0;
367 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 if (function == "home")
369 {
370 PageManager::SelectPackage("TWRP");
371 gui_changePage("main");
372 return 0;
373 }
374
375 if (function == "key")
376 {
377 PageManager::NotifyKey(getKeyByName(arg));
378 return 0;
379 }
380
381 if (function == "page") {
382 std::string page_name = gui_parse_text(arg);
383 return gui_changePage(page_name);
384 }
385
386 if (function == "reload") {
387 int check = 0, ret_val = 0;
388 std::string theme_path;
389
390 operation_start("Reload Theme");
391 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000393 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400394 check = 1;
395 }
396
397 theme_path += "/TWRP/theme/ui.zip";
398 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
399 {
400 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000401 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
403 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000404 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 ret_val = 1;
406 }
407 }
408 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000409 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 }
411
412 if (function == "readBackup")
413 {
414 string Restore_Name;
415 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400416 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 return 0;
418 }
419
420 if (function == "set")
421 {
422 if (arg.find('=') != string::npos)
423 {
424 string varName = arg.substr(0, arg.find('='));
425 string value = arg.substr(arg.find('=') + 1, string::npos);
426
427 DataManager::GetValue(value, value);
428 DataManager::SetValue(varName, value);
429 }
430 else
431 DataManager::SetValue(arg, "1");
432 return 0;
433 }
434 if (function == "clear")
435 {
436 DataManager::SetValue(arg, "0");
437 return 0;
438 }
439
440 if (function == "mount")
441 {
442 if (arg == "usb")
443 {
444 DataManager::SetValue(TW_ACTION_BUSY, 1);
445 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400446 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000448 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 }
450 else if (!simulate)
451 {
452 string cmd;
453 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400454 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400455 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400456 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 else
Dees_Troy51127312012-09-08 13:08:49 -0400458 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000460 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400461 return 0;
462 }
463
464 if (function == "umount" || function == "unmount")
465 {
466 if (arg == "usb")
467 {
468 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400469 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400470 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000471 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400472 DataManager::SetValue(TW_ACTION_BUSY, 0);
473 }
474 else if (!simulate)
475 {
476 string cmd;
477 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400478 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400480 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 else
Dees_Troy51127312012-09-08 13:08:49 -0400482 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000484 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 return 0;
486 }
487
488 if (function == "restoredefaultsettings")
489 {
490 operation_start("Restore Defaults");
491 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000492 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 else {
494 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400495 PartitionManager.Update_System_Details();
496 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 }
498 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000499 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 }
501
502 if (function == "copylog")
503 {
504 operation_start("Copy Log");
505 if (!simulate)
506 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400508 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
510 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 } else
514 simulate_progress_bar();
515 operation_end(0, simulate);
516 return 0;
517 }
518
519 if (function == "compute" || function == "addsubtract")
520 {
521 if (arg.find("+") != string::npos)
522 {
523 string varName = arg.substr(0, arg.find('+'));
524 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
525 int amount_to_add = atoi(string_to_add.c_str());
526 int value;
527
528 DataManager::GetValue(varName, value);
529 DataManager::SetValue(varName, value + amount_to_add);
530 return 0;
531 }
532 if (arg.find("-") != string::npos)
533 {
534 string varName = arg.substr(0, arg.find('-'));
535 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
536 int amount_to_subtract = atoi(string_to_subtract.c_str());
537 int value;
538
539 DataManager::GetValue(varName, value);
540 value -= amount_to_subtract;
541 if (value <= 0)
542 value = 0;
543 DataManager::SetValue(varName, value);
544 return 0;
545 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200546 if (arg.find("*") != string::npos)
547 {
548 string varName = arg.substr(0, arg.find('*'));
549 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
550 int multiply_by = atoi(multiply_by_str.c_str());
551 int value;
552
553 DataManager::GetValue(varName, value);
554 DataManager::SetValue(varName, value*multiply_by);
555 return 0;
556 }
557 if (arg.find("/") != string::npos)
558 {
559 string varName = arg.substr(0, arg.find('/'));
560 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
561 int divide_by = atoi(divide_by_str.c_str());
562 int value;
563
564 if(divide_by != 0)
565 {
566 DataManager::GetValue(varName, value);
567 DataManager::SetValue(varName, value/divide_by);
568 }
569 return 0;
570 }
571 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
572 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400573 }
574
575 if (function == "setguitimezone")
576 {
577 string SelectedZone;
578 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
579 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
580 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
581
582 int dst;
583 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
584
585 string offset;
586 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
587
588 string NewTimeZone = Zone;
589 if (offset != "0")
590 NewTimeZone += ":" + offset;
591
592 if (dst != 0)
593 NewTimeZone += DSTZone;
594
595 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400596 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 return 0;
598 }
599
600 if (function == "togglestorage") {
601 if (arg == "internal") {
602 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
603 } else if (arg == "external") {
604 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
605 }
Dees_Troy51127312012-09-08 13:08:49 -0400606 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400607 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400608 string zip_path, zip_root;
609 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
610 zip_root = TWFunc::Get_Root_Path(zip_path);
611#ifdef RECOVERY_SDCARD_ON_DATA
612 #ifndef TW_EXTERNAL_STORAGE_PATH
613 if (zip_root != "/sdcard")
614 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
615 #else
616 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
617 if (zip_root != "/emmc")
618 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
619 } else {
620 if (zip_root != "/sdcard")
621 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
622 }
623 #endif
624#else
625 if (zip_root != DataManager::GetCurrentStoragePath())
626 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
627#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 // Save the current zip location to the external variable
629 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
630 // Change the current zip location to the internal variable
631 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
632 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400633 string zip_path, zip_root;
634 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
635 zip_root = TWFunc::Get_Root_Path(zip_path);
636 if (zip_root != DataManager::GetCurrentStoragePath()) {
637 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
638 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400639 // Save the current zip location to the internal variable
640 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
641 // Change the current zip location to the external variable
642 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
643 }
644 } else {
645 // We weren't able to toggle for some reason, restore original setting
646 if (arg == "internal") {
647 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
648 } else if (arg == "external") {
649 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
650 }
651 }
652 return 0;
653 }
654
655 if (function == "overlay")
656 return gui_changeOverlay(arg);
657
658 if (function == "queuezip")
659 {
660 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000661 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 return 0;
663 }
664 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
665 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
666 zip_queue_index++;
667 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
668 }
669 return 0;
670 }
671
672 if (function == "cancelzip")
673 {
674 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000675 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 return 0;
677 } else {
678 zip_queue_index--;
679 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
680 }
681 return 0;
682 }
683
684 if (function == "queueclear")
685 {
686 zip_queue_index = 0;
687 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
688 return 0;
689 }
690
691 if (function == "sleep")
692 {
693 operation_start("Sleep");
694 usleep(atoi(arg.c_str()));
695 operation_end(0, simulate);
696 return 0;
697 }
698
699 if (isThreaded)
700 {
701 if (function == "fileexists")
702 {
703 struct stat st;
704 string newpath = arg + "/.";
705
706 operation_start("FileExists");
707 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
708 operation_end(0, simulate);
709 else
710 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000711 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 }
713
714 if (function == "flash")
715 {
Dees_Troy657c3092012-09-10 20:32:10 -0400716 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400717
718 for (i=0; i<zip_queue_index; i++) {
719 operation_start("Flashing");
720 DataManager::SetValue("tw_filename", zip_queue[i]);
721 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
722
Dees_Troy657c3092012-09-10 20:32:10 -0400723 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000725 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 i = 10; // Error flashing zip - exit queue
727 ret_val = 1;
728 }
729 }
730 zip_queue_index = 0;
731 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
732
Dees_Troy657c3092012-09-10 20:32:10 -0400733 if (wipe_cache)
734 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500735 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
737 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 if (simulate) {
740 simulate_progress_bar();
741 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400742 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
743 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500744 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400745 else {
746 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 -0500747 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400748 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000749 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 }
751 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400752 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400753 operation_end(ret_val, simulate);
754 return 0;
755 }
756 if (function == "wipe")
757 {
758 operation_start("Format");
759 DataManager::SetValue("tw_partition", arg);
760
Dees_Troy38bd7602012-09-14 13:33:53 -0400761 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400762
763 if (simulate) {
764 simulate_progress_bar();
765 } else {
766 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400767 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400769 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400771 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400773 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 } else if (arg == "INTERNAL") {
777 int has_datamedia, dual_storage;
778
779 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
780 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400781 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400783 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 }
785 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400786 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400787
Dees_Troy38bd7602012-09-14 13:33:53 -0400788 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
789 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400790 } else if (arg == "ANDROIDSECURE") {
791 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500792 } else if (arg == "LIST") {
793 string Wipe_List, wipe_path;
794 bool skip = false;
795 ret_val = true;
796 TWPartition* wipe_part = NULL;
797
798 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000799 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500800 if (!Wipe_List.empty()) {
801 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
802 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
803 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500805 if (wipe_path == "/and-sec") {
806 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500808 ret_val = false;
809 break;
810 } else {
811 skip = true;
812 }
813 } else if (wipe_path == "DALVIK") {
814 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500816 ret_val = false;
817 break;
818 } else {
819 skip = true;
820 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000821 } else if (wipe_path == "INTERNAL") {
822 if (!PartitionManager.Wipe_Media_From_Data()) {
823 ret_val = false;
824 break;
825 } else {
826 skip = true;
827 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500828 }
829 if (!skip) {
830 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000831 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 ret_val = false;
833 break;
834 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
835 arg = wipe_path;
836 }
837 } else {
838 skip = false;
839 }
840 start_pos = end_pos + 1;
841 end_pos = Wipe_List.find(";", start_pos);
842 }
843 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400844 } else
845 ret_val = PartitionManager.Wipe_By_Path(arg);
846
847 if (arg == DataManager::GetSettingsStoragePath()) {
848 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
849 string Storage_Path = DataManager::GetSettingsStoragePath();
850
851 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000852 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400853 Storage_Path += "/TWRP";
854 mkdir(Storage_Path.c_str(), 0777);
855 DataManager::Flush();
856 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400858 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 }
860 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400862 if (ret_val)
863 ret_val = 0; // 0 is success
864 else
865 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 operation_end(ret_val, simulate);
867 return 0;
868 }
869 if (function == "refreshsizes")
870 {
871 operation_start("Refreshing Sizes");
872 if (simulate) {
873 simulate_progress_bar();
874 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400875 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400876 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000877 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400878 }
879 if (function == "nandroid")
880 {
881 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400882 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400883
884 if (simulate) {
885 DataManager::SetValue("tw_partition", "Simulation");
886 simulate_progress_bar();
887 } else {
888 if (arg == "backup") {
889 string Backup_Name;
890 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500891 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400892 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500893 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400894 else {
895 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400896 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400897 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400898 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
899 } else if (arg == "restore") {
900 string Restore_Name;
901 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400902 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400903 } else {
904 operation_end(1, simulate);
905 return -1;
906 }
907 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000908 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400909 if (ret == false)
910 ret = 1; // 1 for failure
911 else
912 ret = 0; // 0 for success
Dees_Troy83bd4832013-05-04 12:39:56 +0000913 operation_end(ret, simulate);
914 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400915 }
916 if (function == "fixpermissions")
917 {
918 operation_start("Fix Permissions");
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400920 if (simulate) {
921 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400922 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400923 int op_status = PartitionManager.Fix_Permissions();
924 if (op_status != 0)
925 op_status = 1; // failure
926 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400927 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400928 return 0;
929 }
930 if (function == "dd")
931 {
932 operation_start("imaging");
933
934 if (simulate) {
935 simulate_progress_bar();
936 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500937 string result;
938 string cmd = "dd " + arg;
939 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400940 }
941 operation_end(0, simulate);
942 return 0;
943 }
944 if (function == "partitionsd")
945 {
946 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400947 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400948
949 if (simulate) {
950 simulate_progress_bar();
951 } else {
952 int allow_partition;
953 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
954 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400956 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400957 if (!PartitionManager.Partition_SDCard())
958 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400959 }
960 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400961 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400962 return 0;
963 }
964 if (function == "installhtcdumlock")
965 {
966 operation_start("Install HTC Dumlock");
967 if (simulate) {
968 simulate_progress_bar();
969 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400970 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400971
972 operation_end(0, simulate);
973 return 0;
974 }
975 if (function == "htcdumlockrestoreboot")
976 {
977 operation_start("HTC Dumlock Restore Boot");
978 if (simulate) {
979 simulate_progress_bar();
980 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400981 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400982
983 operation_end(0, simulate);
984 return 0;
985 }
986 if (function == "htcdumlockreflashrecovery")
987 {
988 operation_start("HTC Dumlock Reflash Recovery");
989 if (simulate) {
990 simulate_progress_bar();
991 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400992 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400993
994 operation_end(0, simulate);
995 return 0;
996 }
997 if (function == "cmd")
998 {
999 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001000 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001001
1002 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001004 if (simulate) {
1005 simulate_progress_bar();
1006 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001007 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -04001008 if (op_status != 0)
1009 op_status = 1;
1010 }
1011
1012 operation_end(op_status, simulate);
1013 return 0;
1014 }
1015 if (function == "terminalcommand")
1016 {
1017 int op_status = 0;
1018 string cmdpath, command;
1019
1020 DataManager::GetValue("tw_terminal_location", cmdpath);
1021 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001023 if (simulate) {
1024 simulate_progress_bar();
1025 operation_end(op_status, simulate);
1026 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001027 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001028 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001029 DataManager::SetValue("tw_terminal_command_thread", command);
1030 DataManager::SetValue("tw_terminal_state", 1);
1031 DataManager::SetValue("tw_background_thread_running", 1);
1032 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1033 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001035 DataManager::SetValue("tw_terminal_state", 0);
1036 DataManager::SetValue("tw_background_thread_running", 0);
1037 operation_end(1, simulate);
1038 }
1039 }
1040 return 0;
1041 }
1042 if (function == "killterminal")
1043 {
1044 int op_status = 0;
1045
Dees_Troy2673cec2013-04-02 20:22:16 +00001046 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001047 operation_start("KillCommand");
1048 DataManager::SetValue("tw_operation_status", 0);
1049 DataManager::SetValue("tw_operation_state", 1);
1050 DataManager::SetValue("tw_terminal_state", 0);
1051 DataManager::SetValue("tw_background_thread_running", 0);
1052 DataManager::SetValue(TW_ACTION_BUSY, 0);
1053 return 0;
1054 }
1055 if (function == "reinjecttwrp")
1056 {
1057 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001058 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001059 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001060 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001061 if (simulate) {
1062 simulate_progress_bar();
1063 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001064 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy2673cec2013-04-02 20:22:16 +00001065 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001066 }
1067
1068 operation_end(op_status, simulate);
1069 return 0;
1070 }
1071 if (function == "checkbackupname")
1072 {
1073 int op_status = 0;
1074
1075 operation_start("CheckBackupName");
1076 if (simulate) {
1077 simulate_progress_bar();
1078 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001079 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001080 if (op_status != 0)
1081 op_status = 1;
1082 }
1083
1084 operation_end(op_status, simulate);
1085 return 0;
1086 }
1087 if (function == "decrypt")
1088 {
1089 int op_status = 0;
1090
1091 operation_start("Decrypt");
1092 if (simulate) {
1093 simulate_progress_bar();
1094 } else {
1095 string Password;
1096 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001097 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001098 if (op_status != 0)
1099 op_status = 1;
1100 else {
1101 int load_theme = 1;
1102
1103 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001104
1105 if (load_theme) {
1106 int has_datamedia;
1107
1108 // Check for a custom theme and load it if exists
1109 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1110 if (has_datamedia != 0) {
1111 struct stat st;
1112 int check = 0;
1113 std::string theme_path;
1114
1115 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001116 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 check = 1;
1119 }
1120
1121 theme_path += "/TWRP/theme/ui.zip";
1122 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1123 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1124 {
1125 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001126 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001127 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1128 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001130 }
1131 }
1132 }
1133 }
1134 }
1135 }
1136 }
1137
1138 operation_end(op_status, simulate);
1139 return 0;
1140 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001141 if (function == "adbsideload")
1142 {
1143 int ret = 0;
1144
1145 operation_start("Sideload");
1146 if (simulate) {
1147 simulate_progress_bar();
1148 } else {
1149 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001150 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001151 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001152
1153 if (!PartitionManager.Mount_Current_Storage(true)) {
1154 operation_end(1, simulate);
1155 return 0;
1156 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001157 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1158 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001159 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001160 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001162 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001163 ret = apply_from_adb(Sideload_File.c_str());
1164 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 -05001165 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001166 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001167 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001168 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1169 PartitionManager.Wipe_By_Path("/cache");
1170 if (wipe_dalvik)
1171 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001172 } else {
1173 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001174 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001175 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1176 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001178 if (simulate) {
1179 simulate_progress_bar();
1180 } else {
1181 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1182 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001183 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001184 else {
1185 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 -05001186 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001187 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001188 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001189 }
1190 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001191 }
1192 operation_end(ret, simulate);
1193 return 0;
1194 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001195 if (function == "adbsideloadcancel")
1196 {
1197 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001198 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001199 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001200 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001201 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001202 property_get("tw_child_pid", child_prop, "error");
1203 if (strcmp(child_prop, "error") == 0) {
1204 LOGERR("Unable to get child ID from prop\n");
1205 return 0;
1206 }
1207 child_pid = atoi(child_prop);
1208 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001209 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001210 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001211 return 0;
1212 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001213 if (function == "openrecoveryscript") {
1214 operation_start("OpenRecoveryScript");
1215 if (simulate) {
1216 simulate_progress_bar();
1217 } else {
1218 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1219 // that we converted to ORS commands during boot in recovery.cpp.
1220 // Run those first.
1221 int reboot = 0;
1222 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001223 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001224 if (OpenRecoveryScript::run_script_file() == 0) {
1225 reboot = 1;
1226 }
1227 }
1228 // Check for the ORS file in /cache and attempt to run those commands.
1229 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001231 if (OpenRecoveryScript::run_script_file() == 0) {
1232 reboot = 1;
1233 }
1234 }
1235 if (reboot) {
1236 usleep(2000000); // Sleep for 2 seconds before rebooting
1237 TWFunc::tw_reboot(rb_system);
1238 } else {
1239 DataManager::SetValue("tw_page_done", 1);
1240 }
1241 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001242 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001243 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001244 if (function == "installsu")
1245 {
1246 int op_status = 0;
1247
1248 operation_start("Install SuperSU");
1249 if (simulate) {
1250 simulate_progress_bar();
1251 } else {
1252 if (!TWFunc::Install_SuperSU())
1253 op_status = 1;
1254 }
1255
1256 operation_end(op_status, simulate);
1257 return 0;
1258 }
1259 if (function == "fixsu")
1260 {
1261 int op_status = 0;
1262
1263 operation_start("Fixing Superuser Permissions");
1264 if (simulate) {
1265 simulate_progress_bar();
1266 } else {
1267 if (!TWFunc::Fix_su_Perms())
1268 op_status = 1;
1269 }
1270
1271 operation_end(op_status, simulate);
1272 return 0;
1273 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001274 if (function == "decrypt_backup")
1275 {
1276 int op_status = 0;
1277
1278 operation_start("Try Restore Decrypt");
1279 if (simulate) {
1280 simulate_progress_bar();
1281 } else {
1282 string Restore_Path, Filename, Password;
1283 DataManager::GetValue("tw_restore", Restore_Path);
1284 Restore_Path += "/";
1285 DataManager::GetValue("tw_restore_password", Password);
1286 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1287 op_status = 0; // success
1288 else
1289 op_status = 1; // fail
1290 }
1291
1292 operation_end(op_status, simulate);
1293 return 0;
1294 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001295 }
1296 else
1297 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001298 pthread_t t;
1299 pthread_create(&t, NULL, thread_start, this);
Dees_Troy51a0e822012-09-05 15:24:24 -04001300 return 0;
1301 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001302 LOGERR("Unknown action '%s'\n", function.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001303 return -1;
1304}
1305
1306int GUIAction::getKeyByName(std::string key)
1307{
1308 if (key == "home") return KEY_HOME;
1309 else if (key == "menu") return KEY_MENU;
1310 else if (key == "back") return KEY_BACK;
1311 else if (key == "search") return KEY_SEARCH;
1312 else if (key == "voldown") return KEY_VOLUMEDOWN;
1313 else if (key == "volup") return KEY_VOLUMEUP;
1314 else if (key == "power") {
1315 int ret_val;
1316 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1317 if (!ret_val)
1318 return KEY_POWER;
1319 else
1320 return ret_val;
1321 }
1322
1323 return atol(key.c_str());
1324}
1325
1326void* GUIAction::command_thread(void *cookie)
1327{
1328 string command;
1329 FILE* fp;
1330 char line[512];
1331
1332 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001333 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001334 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001336 } 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
Dees_Troy4be841b2012-09-26 14:07:15 -04001359 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001360 bytes_read = read(fd, line, sizeof(line));
1361 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001363 else
1364 keep_going = 0; // Done executing
1365 }
1366 }
1367 fclose(fp);
1368 }
1369 DataManager::SetValue("tw_operation_status", 0);
1370 DataManager::SetValue("tw_operation_state", 1);
1371 DataManager::SetValue("tw_terminal_state", 0);
1372 DataManager::SetValue("tw_background_thread_running", 0);
1373 DataManager::SetValue(TW_ACTION_BUSY, 0);
1374 return NULL;
1375}