blob: 64049b2c1272b4addbf289dacf40ca034aa08ed7 [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_Troy51a0e822012-09-05 15:24:24 -040034
35#include <string>
36#include <sstream>
37#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040038#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040039#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Dees_Troy43d8b002012-09-17 16:00:01 -040041#include "../adb_install.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050042#include "blanktimer.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043
Dees_Troy51a0e822012-09-05 15:24:24 -040044extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000045#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040046#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040047#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040048#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000049#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040050#include "../minadbd/adb.h"
51
Dees_Troy32c8eb82012-09-11 15:28:06 -040052int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040053void 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 -040054int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040055int gui_start();
56};
57
58#include "rapidxml.hpp"
59#include "objects.hpp"
60
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050061extern blanktimer blankTimer;
Dees_Troy43d8b002012-09-17 16:00:01 -040062
Dees_Troy51a0e822012-09-05 15:24:24 -040063void curtainClose(void);
64
65GUIAction::GUIAction(xml_node<>* node)
66 : Conditional(node)
67{
68 xml_node<>* child;
69 xml_node<>* actions;
70 xml_attribute<>* attr;
71
72 mKey = 0;
73
74 if (!node) return;
75
76 // First, get the action
77 actions = node->first_node("actions");
78 if (actions) child = actions->first_node("action");
79 else child = node->first_node("action");
80
81 if (!child) return;
82
83 while (child)
84 {
85 Action action;
86
87 attr = child->first_attribute("function");
88 if (!attr) return;
89
90 action.mFunction = attr->value();
91 action.mArg = child->value();
92 mActions.push_back(action);
93
94 child = child->next_sibling("action");
95 }
96
97 // Now, let's get either the key or region
98 child = node->first_node("touch");
99 if (child)
100 {
101 attr = child->first_attribute("key");
102 if (attr)
103 {
104 std::string key = attr->value();
105
106 mKey = getKeyByName(key);
107 }
108 else
109 {
110 attr = child->first_attribute("x");
111 if (!attr) return;
112 mActionX = atol(attr->value());
113 attr = child->first_attribute("y");
114 if (!attr) return;
115 mActionY = atol(attr->value());
116 attr = child->first_attribute("w");
117 if (!attr) return;
118 mActionW = atol(attr->value());
119 attr = child->first_attribute("h");
120 if (!attr) return;
121 mActionH = atol(attr->value());
122 }
123 }
124}
125
126int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
127{
128 if (state == TOUCH_RELEASE)
129 doActions();
130
131 return 0;
132}
133
134int GUIAction::NotifyKey(int key)
135{
136 if (!mKey || key != mKey)
137 return 1;
138
139 doActions();
140 return 0;
141}
142
143int GUIAction::NotifyVarChange(std::string varName, std::string value)
144{
145 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
146 doActions();
147
148 // This handles notifying the condition system of page start
149 if (varName.empty() && isConditionValid())
150 NotifyPageSet();
151
152 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
153 doActions();
154
155 return 0;
156}
157
158void GUIAction::simulate_progress_bar(void)
159{
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400161 for (int i = 0; i < 5; i++)
162 {
163 usleep(500000);
164 DataManager::SetValue("ui_progress", i * 20);
165 }
166}
167
Dees_Troy657c3092012-09-10 20:32:10 -0400168int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400169{
170 int ret_val = 0;
171
172 DataManager::SetValue("ui_progress", 0);
173
174 if (filename.empty())
175 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000176 LOGERR("No file specified.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 return -1;
178 }
179
180 // We're going to jump to this page first, like a loading page
181 gui_changePage(pageName);
182
183 int fd = -1;
184 ZipArchive zip;
185
Dees_Troy657c3092012-09-10 20:32:10 -0400186 if (!PartitionManager.Mount_By_Path(filename, true))
187 return -1;
188
189 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 LOGERR("Unable to open zip file.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 return -1;
193 }
194
195 // Check the zip to see if it has a custom installer theme
196 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
197 if (twrp != NULL)
198 {
199 unlink("/tmp/twrp.zip");
200 fd = creat("/tmp/twrp.zip", 0666);
201 }
202 if (fd >= 0 && twrp != NULL &&
203 mzExtractZipEntryToFile(&zip, twrp, fd) &&
204 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
205 {
206 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400207 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 gui_changePage("main");
209 }
210 else
211 {
212 // In this case, we just use the default page
213 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400214 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 }
216 if (fd >= 0)
217 close(fd);
218
219 if (simulate) {
220 simulate_progress_bar();
221 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400222 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400223
224 // Now, check if we need to ensure TWRP remains installed...
225 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500226 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 if (stat("/sbin/installTwrp", &st) == 0)
228 {
229 DataManager::SetValue("tw_operation", "Configuring TWRP");
230 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 gui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500232 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400233 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 }
236 }
237 }
238
239 // Done
240 DataManager::SetValue("ui_progress", 100);
241 DataManager::SetValue("ui_progress", 0);
242 return ret_val;
243}
244
245int GUIAction::doActions()
246{
247 if (mActions.size() < 1) return -1;
248 if (mActions.size() == 1)
249 return doAction(mActions.at(0), 0);
250
251 // For multi-action, we always use a thread
252 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000253 pthread_attr_t tattr;
254
255 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000257 return -1;
258 }
259 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000261 return -1;
262 }
263 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000265 return -1;
266 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500267 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000268 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000269 return -1;
270 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500271 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000272 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000273 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000275 thread_start(this);
276 } else {
277 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000278 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000279 }
280 }
281 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 return -1;
284 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
286 return 0;
287}
288
289void* GUIAction::thread_start(void *cookie)
290{
291 GUIAction* ourThis = (GUIAction*) cookie;
292
293 DataManager::SetValue(TW_ACTION_BUSY, 1);
294
295 if (ourThis->mActions.size() > 1)
296 {
297 std::vector<Action>::iterator iter;
298 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
299 ourThis->doAction(*iter, 1);
300 }
301 else
302 {
303 ourThis->doAction(ourThis->mActions.at(0), 1);
304 }
305 int check = 0;
306 DataManager::GetValue("tw_background_thread_running", check);
307 if (check == 0)
308 DataManager::SetValue(TW_ACTION_BUSY, 0);
309 return NULL;
310}
311
312void GUIAction::operation_start(const string operation_name)
313{
314 DataManager::SetValue(TW_ACTION_BUSY, 1);
315 DataManager::SetValue("ui_progress", 0);
316 DataManager::SetValue("tw_operation", operation_name);
317 DataManager::SetValue("tw_operation_status", 0);
318 DataManager::SetValue("tw_operation_state", 0);
319}
320
321void GUIAction::operation_end(const int operation_status, const int simulate)
322{
323 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 DataManager::SetValue("ui_progress", 100);
325 if (simulate) {
326 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
327 if (simulate_fail != 0)
328 DataManager::SetValue("tw_operation_status", 1);
329 else
330 DataManager::SetValue("tw_operation_status", 0);
331 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500332 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400333 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 }
335 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400336 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500337 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400338 }
339 DataManager::SetValue("tw_operation_state", 1);
340 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500341 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
344int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
345{
346 static string zip_queue[10];
347 static int zip_queue_index;
348 static pthread_t terminal_command;
349 int simulate;
350
351 std::string arg = gui_parse_text(action.mArg);
352
353 std::string function = gui_parse_text(action.mFunction);
354
355 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
356
Dees_Troy6ef66352013-02-21 08:26:57 -0600357 if (function == "reboot")
358 {
359 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400360
Dees_Troy6ef66352013-02-21 08:26:57 -0600361 sync();
362 DataManager::SetValue("tw_gui_done", 1);
363 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
Dees_Troy6ef66352013-02-21 08:26:57 -0600365 return 0;
366 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400367 if (function == "home")
368 {
369 PageManager::SelectPackage("TWRP");
370 gui_changePage("main");
371 return 0;
372 }
373
374 if (function == "key")
375 {
376 PageManager::NotifyKey(getKeyByName(arg));
377 return 0;
378 }
379
380 if (function == "page") {
381 std::string page_name = gui_parse_text(arg);
382 return gui_changePage(page_name);
383 }
384
385 if (function == "reload") {
386 int check = 0, ret_val = 0;
387 std::string theme_path;
388
389 operation_start("Reload Theme");
390 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400391 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000392 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 check = 1;
394 }
395
396 theme_path += "/TWRP/theme/ui.zip";
397 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
398 {
399 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000400 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
402 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000403 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 ret_val = 1;
405 }
406 }
407 operation_end(ret_val, simulate);
408 }
409
410 if (function == "readBackup")
411 {
412 string Restore_Name;
413 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 return 0;
416 }
417
418 if (function == "set")
419 {
420 if (arg.find('=') != string::npos)
421 {
422 string varName = arg.substr(0, arg.find('='));
423 string value = arg.substr(arg.find('=') + 1, string::npos);
424
425 DataManager::GetValue(value, value);
426 DataManager::SetValue(varName, value);
427 }
428 else
429 DataManager::SetValue(arg, "1");
430 return 0;
431 }
432 if (function == "clear")
433 {
434 DataManager::SetValue(arg, "0");
435 return 0;
436 }
437
438 if (function == "mount")
439 {
440 if (arg == "usb")
441 {
442 DataManager::SetValue(TW_ACTION_BUSY, 1);
443 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400444 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000446 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 }
448 else if (!simulate)
449 {
450 string cmd;
451 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400452 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400454 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400455 else
Dees_Troy51127312012-09-08 13:08:49 -0400456 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 return 0;
460 }
461
462 if (function == "umount" || function == "unmount")
463 {
464 if (arg == "usb")
465 {
466 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400467 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400468 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000469 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400470 DataManager::SetValue(TW_ACTION_BUSY, 0);
471 }
472 else if (!simulate)
473 {
474 string cmd;
475 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400476 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400478 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 else
Dees_Troy51127312012-09-08 13:08:49 -0400480 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 return 0;
484 }
485
486 if (function == "restoredefaultsettings")
487 {
488 operation_start("Restore Defaults");
489 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000490 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 else {
492 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400493 PartitionManager.Update_System_Details();
494 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 }
496 operation_end(0, simulate);
497 }
498
499 if (function == "copylog")
500 {
501 operation_start("Copy Log");
502 if (!simulate)
503 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500504 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400505 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500506 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
507 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 } else
511 simulate_progress_bar();
512 operation_end(0, simulate);
513 return 0;
514 }
515
516 if (function == "compute" || function == "addsubtract")
517 {
518 if (arg.find("+") != string::npos)
519 {
520 string varName = arg.substr(0, arg.find('+'));
521 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
522 int amount_to_add = atoi(string_to_add.c_str());
523 int value;
524
525 DataManager::GetValue(varName, value);
526 DataManager::SetValue(varName, value + amount_to_add);
527 return 0;
528 }
529 if (arg.find("-") != string::npos)
530 {
531 string varName = arg.substr(0, arg.find('-'));
532 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
533 int amount_to_subtract = atoi(string_to_subtract.c_str());
534 int value;
535
536 DataManager::GetValue(varName, value);
537 value -= amount_to_subtract;
538 if (value <= 0)
539 value = 0;
540 DataManager::SetValue(varName, value);
541 return 0;
542 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200543 if (arg.find("*") != string::npos)
544 {
545 string varName = arg.substr(0, arg.find('*'));
546 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
547 int multiply_by = atoi(multiply_by_str.c_str());
548 int value;
549
550 DataManager::GetValue(varName, value);
551 DataManager::SetValue(varName, value*multiply_by);
552 return 0;
553 }
554 if (arg.find("/") != string::npos)
555 {
556 string varName = arg.substr(0, arg.find('/'));
557 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
558 int divide_by = atoi(divide_by_str.c_str());
559 int value;
560
561 if(divide_by != 0)
562 {
563 DataManager::GetValue(varName, value);
564 DataManager::SetValue(varName, value/divide_by);
565 }
566 return 0;
567 }
568 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
569 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 }
571
572 if (function == "setguitimezone")
573 {
574 string SelectedZone;
575 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
576 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
577 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
578
579 int dst;
580 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
581
582 string offset;
583 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
584
585 string NewTimeZone = Zone;
586 if (offset != "0")
587 NewTimeZone += ":" + offset;
588
589 if (dst != 0)
590 NewTimeZone += DSTZone;
591
592 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400593 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 return 0;
595 }
596
597 if (function == "togglestorage") {
598 if (arg == "internal") {
599 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
600 } else if (arg == "external") {
601 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
602 }
Dees_Troy51127312012-09-08 13:08:49 -0400603 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400605 string zip_path, zip_root;
606 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
607 zip_root = TWFunc::Get_Root_Path(zip_path);
608#ifdef RECOVERY_SDCARD_ON_DATA
609 #ifndef TW_EXTERNAL_STORAGE_PATH
610 if (zip_root != "/sdcard")
611 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
612 #else
613 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
614 if (zip_root != "/emmc")
615 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
616 } else {
617 if (zip_root != "/sdcard")
618 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
619 }
620 #endif
621#else
622 if (zip_root != DataManager::GetCurrentStoragePath())
623 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
624#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400625 // Save the current zip location to the external variable
626 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
627 // Change the current zip location to the internal variable
628 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
629 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400630 string zip_path, zip_root;
631 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
632 zip_root = TWFunc::Get_Root_Path(zip_path);
633 if (zip_root != DataManager::GetCurrentStoragePath()) {
634 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
635 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400636 // Save the current zip location to the internal variable
637 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
638 // Change the current zip location to the external variable
639 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
640 }
641 } else {
642 // We weren't able to toggle for some reason, restore original setting
643 if (arg == "internal") {
644 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
645 } else if (arg == "external") {
646 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
647 }
648 }
649 return 0;
650 }
651
652 if (function == "overlay")
653 return gui_changeOverlay(arg);
654
655 if (function == "queuezip")
656 {
657 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 return 0;
660 }
661 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
662 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
663 zip_queue_index++;
664 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
665 }
666 return 0;
667 }
668
669 if (function == "cancelzip")
670 {
671 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 return 0;
674 } else {
675 zip_queue_index--;
676 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
677 }
678 return 0;
679 }
680
681 if (function == "queueclear")
682 {
683 zip_queue_index = 0;
684 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
685 return 0;
686 }
687
688 if (function == "sleep")
689 {
690 operation_start("Sleep");
691 usleep(atoi(arg.c_str()));
692 operation_end(0, simulate);
693 return 0;
694 }
695
696 if (isThreaded)
697 {
698 if (function == "fileexists")
699 {
700 struct stat st;
701 string newpath = arg + "/.";
702
703 operation_start("FileExists");
704 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
705 operation_end(0, simulate);
706 else
707 operation_end(1, simulate);
708 }
709
710 if (function == "flash")
711 {
Dees_Troy657c3092012-09-10 20:32:10 -0400712 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
714 for (i=0; i<zip_queue_index; i++) {
715 operation_start("Flashing");
716 DataManager::SetValue("tw_filename", zip_queue[i]);
717 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
718
Dees_Troy657c3092012-09-10 20:32:10 -0400719 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400720 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000721 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 i = 10; // Error flashing zip - exit queue
723 ret_val = 1;
724 }
725 }
726 zip_queue_index = 0;
727 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
728
Dees_Troy657c3092012-09-10 20:32:10 -0400729 if (wipe_cache)
730 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500731 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
733 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 if (simulate) {
736 simulate_progress_bar();
737 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400738 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
739 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500740 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400741 else {
742 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 -0500743 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400744 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 }
747 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400748 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 operation_end(ret_val, simulate);
750 return 0;
751 }
752 if (function == "wipe")
753 {
754 operation_start("Format");
755 DataManager::SetValue("tw_partition", arg);
756
Dees_Troy38bd7602012-09-14 13:33:53 -0400757 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400758
759 if (simulate) {
760 simulate_progress_bar();
761 } else {
762 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400763 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400765 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400767 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400769 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400771 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 } else if (arg == "INTERNAL") {
773 int has_datamedia, dual_storage;
774
775 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
776 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400777 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400779 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 }
781 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400782 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400783
Dees_Troy38bd7602012-09-14 13:33:53 -0400784 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
785 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400786 } else if (arg == "ANDROIDSECURE") {
787 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500788 } else if (arg == "LIST") {
789 string Wipe_List, wipe_path;
790 bool skip = false;
791 ret_val = true;
792 TWPartition* wipe_part = NULL;
793
794 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500796 if (!Wipe_List.empty()) {
797 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
798 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
799 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000800 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500801 if (wipe_path == "/and-sec") {
802 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000803 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500804 ret_val = false;
805 break;
806 } else {
807 skip = true;
808 }
809 } else if (wipe_path == "DALVIK") {
810 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000811 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500812 ret_val = false;
813 break;
814 } else {
815 skip = true;
816 }
817 }
818 if (!skip) {
819 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500821 ret_val = false;
822 break;
823 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
824 arg = wipe_path;
825 }
826 } else {
827 skip = false;
828 }
829 start_pos = end_pos + 1;
830 end_pos = Wipe_List.find(";", start_pos);
831 }
832 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400833 } else
834 ret_val = PartitionManager.Wipe_By_Path(arg);
835
836 if (arg == DataManager::GetSettingsStoragePath()) {
837 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
838 string Storage_Path = DataManager::GetSettingsStoragePath();
839
840 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400842 Storage_Path += "/TWRP";
843 mkdir(Storage_Path.c_str(), 0777);
844 DataManager::Flush();
845 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400847 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400848 }
849 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400850 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400851 if (ret_val)
852 ret_val = 0; // 0 is success
853 else
854 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400855 operation_end(ret_val, simulate);
856 return 0;
857 }
858 if (function == "refreshsizes")
859 {
860 operation_start("Refreshing Sizes");
861 if (simulate) {
862 simulate_progress_bar();
863 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400864 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 operation_end(0, simulate);
866 }
867 if (function == "nandroid")
868 {
869 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400870 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400871
872 if (simulate) {
873 DataManager::SetValue("tw_partition", "Simulation");
874 simulate_progress_bar();
875 } else {
876 if (arg == "backup") {
877 string Backup_Name;
878 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500879 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400880 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500881 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400882 else {
883 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400884 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400885 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400886 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
887 } else if (arg == "restore") {
888 string Restore_Name;
889 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400890 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400891 } else {
892 operation_end(1, simulate);
893 return -1;
894 }
895 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400896 if (ret == false)
897 ret = 1; // 1 for failure
898 else
899 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500900 operation_end(ret, simulate);
901 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400902 }
903 if (function == "fixpermissions")
904 {
905 operation_start("Fix Permissions");
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 if (simulate) {
908 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400909 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400910 int op_status = PartitionManager.Fix_Permissions();
911 if (op_status != 0)
912 op_status = 1; // failure
913 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400914 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400915 return 0;
916 }
917 if (function == "dd")
918 {
919 operation_start("imaging");
920
921 if (simulate) {
922 simulate_progress_bar();
923 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500924 string result;
925 string cmd = "dd " + arg;
926 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400927 }
928 operation_end(0, simulate);
929 return 0;
930 }
931 if (function == "partitionsd")
932 {
933 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400934 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400935
936 if (simulate) {
937 simulate_progress_bar();
938 } else {
939 int allow_partition;
940 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
941 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400943 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400944 if (!PartitionManager.Partition_SDCard())
945 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400946 }
947 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400948 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400949 return 0;
950 }
951 if (function == "installhtcdumlock")
952 {
953 operation_start("Install HTC Dumlock");
954 if (simulate) {
955 simulate_progress_bar();
956 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400957 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400958
959 operation_end(0, simulate);
960 return 0;
961 }
962 if (function == "htcdumlockrestoreboot")
963 {
964 operation_start("HTC Dumlock Restore Boot");
965 if (simulate) {
966 simulate_progress_bar();
967 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400968 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400969
970 operation_end(0, simulate);
971 return 0;
972 }
973 if (function == "htcdumlockreflashrecovery")
974 {
975 operation_start("HTC Dumlock Reflash Recovery");
976 if (simulate) {
977 simulate_progress_bar();
978 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400980
981 operation_end(0, simulate);
982 return 0;
983 }
984 if (function == "cmd")
985 {
986 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500987 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400988
989 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +0000990 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400991 if (simulate) {
992 simulate_progress_bar();
993 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500994 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400995 if (op_status != 0)
996 op_status = 1;
997 }
998
999 operation_end(op_status, simulate);
1000 return 0;
1001 }
1002 if (function == "terminalcommand")
1003 {
1004 int op_status = 0;
1005 string cmdpath, command;
1006
1007 DataManager::GetValue("tw_terminal_location", cmdpath);
1008 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001009 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 if (simulate) {
1011 simulate_progress_bar();
1012 operation_end(op_status, simulate);
1013 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001014 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001016 DataManager::SetValue("tw_terminal_command_thread", command);
1017 DataManager::SetValue("tw_terminal_state", 1);
1018 DataManager::SetValue("tw_background_thread_running", 1);
1019 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1020 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001022 DataManager::SetValue("tw_terminal_state", 0);
1023 DataManager::SetValue("tw_background_thread_running", 0);
1024 operation_end(1, simulate);
1025 }
1026 }
1027 return 0;
1028 }
1029 if (function == "killterminal")
1030 {
1031 int op_status = 0;
1032
Dees_Troy2673cec2013-04-02 20:22:16 +00001033 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001034 operation_start("KillCommand");
1035 DataManager::SetValue("tw_operation_status", 0);
1036 DataManager::SetValue("tw_operation_state", 1);
1037 DataManager::SetValue("tw_terminal_state", 0);
1038 DataManager::SetValue("tw_background_thread_running", 0);
1039 DataManager::SetValue(TW_ACTION_BUSY, 0);
1040 return 0;
1041 }
1042 if (function == "reinjecttwrp")
1043 {
1044 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001045 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001046 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001048 if (simulate) {
1049 simulate_progress_bar();
1050 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001051 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 }
1054
1055 operation_end(op_status, simulate);
1056 return 0;
1057 }
1058 if (function == "checkbackupname")
1059 {
1060 int op_status = 0;
1061
1062 operation_start("CheckBackupName");
1063 if (simulate) {
1064 simulate_progress_bar();
1065 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001066 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001067 if (op_status != 0)
1068 op_status = 1;
1069 }
1070
1071 operation_end(op_status, simulate);
1072 return 0;
1073 }
1074 if (function == "decrypt")
1075 {
1076 int op_status = 0;
1077
1078 operation_start("Decrypt");
1079 if (simulate) {
1080 simulate_progress_bar();
1081 } else {
1082 string Password;
1083 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001085 if (op_status != 0)
1086 op_status = 1;
1087 else {
1088 int load_theme = 1;
1089
1090 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001091
1092 if (load_theme) {
1093 int has_datamedia;
1094
1095 // Check for a custom theme and load it if exists
1096 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1097 if (has_datamedia != 0) {
1098 struct stat st;
1099 int check = 0;
1100 std::string theme_path;
1101
1102 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001104 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001105 check = 1;
1106 }
1107
1108 theme_path += "/TWRP/theme/ui.zip";
1109 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1110 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1111 {
1112 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001114 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1115 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001117 }
1118 }
1119 }
1120 }
1121 }
1122 }
1123 }
1124
1125 operation_end(op_status, simulate);
1126 return 0;
1127 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001128 if (function == "adbsideload")
1129 {
1130 int ret = 0;
1131
1132 operation_start("Sideload");
1133 if (simulate) {
1134 simulate_progress_bar();
1135 } else {
1136 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001137 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001138 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001139
1140 if (!PartitionManager.Mount_Current_Storage(true)) {
1141 operation_end(1, simulate);
1142 return 0;
1143 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001144 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1145 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001146 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001147 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001149 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 ret = apply_from_adb(Sideload_File.c_str());
1151 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 -05001152 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001153 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001154 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001155 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1156 PartitionManager.Wipe_By_Path("/cache");
1157 if (wipe_dalvik)
1158 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 } else {
1160 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001161 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001162 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1163 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001164 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001165 if (simulate) {
1166 simulate_progress_bar();
1167 } else {
1168 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1169 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001170 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001171 else {
1172 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 -05001173 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001174 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001176 }
1177 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001178 }
1179 operation_end(ret, simulate);
1180 return 0;
1181 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001182 if (function == "adbsideloadcancel")
1183 {
1184 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001185 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001186 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001187 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001188 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001189 property_get("tw_child_pid", child_prop, "error");
1190 if (strcmp(child_prop, "error") == 0) {
1191 LOGERR("Unable to get child ID from prop\n");
1192 return 0;
1193 }
1194 child_pid = atoi(child_prop);
1195 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001196 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001197 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001198 return 0;
1199 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001200 if (function == "openrecoveryscript") {
1201 operation_start("OpenRecoveryScript");
1202 if (simulate) {
1203 simulate_progress_bar();
1204 } else {
1205 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1206 // that we converted to ORS commands during boot in recovery.cpp.
1207 // Run those first.
1208 int reboot = 0;
1209 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001210 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001211 if (OpenRecoveryScript::run_script_file() == 0) {
1212 reboot = 1;
1213 }
1214 }
1215 // Check for the ORS file in /cache and attempt to run those commands.
1216 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001217 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001218 if (OpenRecoveryScript::run_script_file() == 0) {
1219 reboot = 1;
1220 }
1221 }
1222 if (reboot) {
1223 usleep(2000000); // Sleep for 2 seconds before rebooting
1224 TWFunc::tw_reboot(rb_system);
1225 } else {
1226 DataManager::SetValue("tw_page_done", 1);
1227 }
1228 }
1229 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001230 if (function == "installsu")
1231 {
1232 int op_status = 0;
1233
1234 operation_start("Install SuperSU");
1235 if (simulate) {
1236 simulate_progress_bar();
1237 } else {
1238 if (!TWFunc::Install_SuperSU())
1239 op_status = 1;
1240 }
1241
1242 operation_end(op_status, simulate);
1243 return 0;
1244 }
1245 if (function == "fixsu")
1246 {
1247 int op_status = 0;
1248
1249 operation_start("Fixing Superuser Permissions");
1250 if (simulate) {
1251 simulate_progress_bar();
1252 } else {
1253 if (!TWFunc::Fix_su_Perms())
1254 op_status = 1;
1255 }
1256
1257 operation_end(op_status, simulate);
1258 return 0;
1259 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001260 }
1261 else
1262 {
1263 pthread_t t;
1264 pthread_create(&t, NULL, thread_start, this);
1265 return 0;
1266 }
1267 return -1;
1268}
1269
1270int GUIAction::getKeyByName(std::string key)
1271{
1272 if (key == "home") return KEY_HOME;
1273 else if (key == "menu") return KEY_MENU;
1274 else if (key == "back") return KEY_BACK;
1275 else if (key == "search") return KEY_SEARCH;
1276 else if (key == "voldown") return KEY_VOLUMEDOWN;
1277 else if (key == "volup") return KEY_VOLUMEUP;
1278 else if (key == "power") {
1279 int ret_val;
1280 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1281 if (!ret_val)
1282 return KEY_POWER;
1283 else
1284 return ret_val;
1285 }
1286
1287 return atol(key.c_str());
1288}
1289
1290void* GUIAction::command_thread(void *cookie)
1291{
1292 string command;
1293 FILE* fp;
1294 char line[512];
1295
1296 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001297 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001298 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001300 } else {
1301 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1302 struct timeval timeout;
1303 fd_set fdset;
1304
1305 while(keep_going)
1306 {
1307 FD_ZERO(&fdset);
1308 FD_SET(fd, &fdset);
1309 timeout.tv_sec = 0;
1310 timeout.tv_usec = 400000;
1311 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1312 if (has_data == 0) {
1313 // Timeout reached
1314 DataManager::GetValue("tw_terminal_state", check);
1315 if (check == 0) {
1316 keep_going = 0;
1317 }
1318 } else if (has_data < 0) {
1319 // End of execution
1320 keep_going = 0;
1321 } else {
1322 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001323 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001324 bytes_read = read(fd, line, sizeof(line));
1325 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001327 else
1328 keep_going = 0; // Done executing
1329 }
1330 }
1331 fclose(fp);
1332 }
1333 DataManager::SetValue("tw_operation_status", 0);
1334 DataManager::SetValue("tw_operation_state", 1);
1335 DataManager::SetValue("tw_terminal_state", 0);
1336 DataManager::SetValue("tw_background_thread_running", 0);
1337 DataManager::SetValue(TW_ACTION_BUSY, 0);
1338 return NULL;
1339}