blob: b35074cf07f9a04906be21857f307a0c1de58610 [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 }
543 }
544
545 if (function == "setguitimezone")
546 {
547 string SelectedZone;
548 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
549 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
550 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
551
552 int dst;
553 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
554
555 string offset;
556 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
557
558 string NewTimeZone = Zone;
559 if (offset != "0")
560 NewTimeZone += ":" + offset;
561
562 if (dst != 0)
563 NewTimeZone += DSTZone;
564
565 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400566 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400567 return 0;
568 }
569
570 if (function == "togglestorage") {
571 if (arg == "internal") {
572 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
573 } else if (arg == "external") {
574 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
575 }
Dees_Troy51127312012-09-08 13:08:49 -0400576 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400578 string zip_path, zip_root;
579 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
580 zip_root = TWFunc::Get_Root_Path(zip_path);
581#ifdef RECOVERY_SDCARD_ON_DATA
582 #ifndef TW_EXTERNAL_STORAGE_PATH
583 if (zip_root != "/sdcard")
584 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
585 #else
586 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
587 if (zip_root != "/emmc")
588 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
589 } else {
590 if (zip_root != "/sdcard")
591 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
592 }
593 #endif
594#else
595 if (zip_root != DataManager::GetCurrentStoragePath())
596 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
597#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 // Save the current zip location to the external variable
599 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
600 // Change the current zip location to the internal variable
601 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
602 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400603 string zip_path, zip_root;
604 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
605 zip_root = TWFunc::Get_Root_Path(zip_path);
606 if (zip_root != DataManager::GetCurrentStoragePath()) {
607 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
608 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 // Save the current zip location to the internal variable
610 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
611 // Change the current zip location to the external variable
612 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
613 }
614 } else {
615 // We weren't able to toggle for some reason, restore original setting
616 if (arg == "internal") {
617 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
618 } else if (arg == "external") {
619 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
620 }
621 }
622 return 0;
623 }
624
625 if (function == "overlay")
626 return gui_changeOverlay(arg);
627
628 if (function == "queuezip")
629 {
630 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000631 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 return 0;
633 }
634 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
635 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
636 zip_queue_index++;
637 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
638 }
639 return 0;
640 }
641
642 if (function == "cancelzip")
643 {
644 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000645 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 return 0;
647 } else {
648 zip_queue_index--;
649 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
650 }
651 return 0;
652 }
653
654 if (function == "queueclear")
655 {
656 zip_queue_index = 0;
657 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
658 return 0;
659 }
660
661 if (function == "sleep")
662 {
663 operation_start("Sleep");
664 usleep(atoi(arg.c_str()));
665 operation_end(0, simulate);
666 return 0;
667 }
668
669 if (isThreaded)
670 {
671 if (function == "fileexists")
672 {
673 struct stat st;
674 string newpath = arg + "/.";
675
676 operation_start("FileExists");
677 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
678 operation_end(0, simulate);
679 else
680 operation_end(1, simulate);
681 }
682
683 if (function == "flash")
684 {
Dees_Troy657c3092012-09-10 20:32:10 -0400685 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400686
687 for (i=0; i<zip_queue_index; i++) {
688 operation_start("Flashing");
689 DataManager::SetValue("tw_filename", zip_queue[i]);
690 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
691
Dees_Troy657c3092012-09-10 20:32:10 -0400692 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000694 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400695 i = 10; // Error flashing zip - exit queue
696 ret_val = 1;
697 }
698 }
699 zip_queue_index = 0;
700 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
701
Dees_Troy657c3092012-09-10 20:32:10 -0400702 if (wipe_cache)
703 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500704 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
706 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 if (simulate) {
709 simulate_progress_bar();
710 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400711 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
712 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500713 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400714 else {
715 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 -0500716 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400717 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 }
720 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400721 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 operation_end(ret_val, simulate);
723 return 0;
724 }
725 if (function == "wipe")
726 {
727 operation_start("Format");
728 DataManager::SetValue("tw_partition", arg);
729
Dees_Troy38bd7602012-09-14 13:33:53 -0400730 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400731
732 if (simulate) {
733 simulate_progress_bar();
734 } else {
735 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400740 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400742 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400744 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 } else if (arg == "INTERNAL") {
746 int has_datamedia, dual_storage;
747
748 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
749 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400750 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400752 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400753 }
754 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400755 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400756
Dees_Troy38bd7602012-09-14 13:33:53 -0400757 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
758 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400759 } else if (arg == "ANDROIDSECURE") {
760 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500761 } else if (arg == "LIST") {
762 string Wipe_List, wipe_path;
763 bool skip = false;
764 ret_val = true;
765 TWPartition* wipe_part = NULL;
766
767 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000768 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500769 if (!Wipe_List.empty()) {
770 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
771 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
772 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000773 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500774 if (wipe_path == "/and-sec") {
775 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000776 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500777 ret_val = false;
778 break;
779 } else {
780 skip = true;
781 }
782 } else if (wipe_path == "DALVIK") {
783 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500785 ret_val = false;
786 break;
787 } else {
788 skip = true;
789 }
790 }
791 if (!skip) {
792 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500794 ret_val = false;
795 break;
796 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
797 arg = wipe_path;
798 }
799 } else {
800 skip = false;
801 }
802 start_pos = end_pos + 1;
803 end_pos = Wipe_List.find(";", start_pos);
804 }
805 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400806 } else
807 ret_val = PartitionManager.Wipe_By_Path(arg);
808
809 if (arg == DataManager::GetSettingsStoragePath()) {
810 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
811 string Storage_Path = DataManager::GetSettingsStoragePath();
812
813 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000814 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 Storage_Path += "/TWRP";
816 mkdir(Storage_Path.c_str(), 0777);
817 DataManager::Flush();
818 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400820 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 }
822 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400823 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400824 if (ret_val)
825 ret_val = 0; // 0 is success
826 else
827 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400828 operation_end(ret_val, simulate);
829 return 0;
830 }
831 if (function == "refreshsizes")
832 {
833 operation_start("Refreshing Sizes");
834 if (simulate) {
835 simulate_progress_bar();
836 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400837 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400838 operation_end(0, simulate);
839 }
840 if (function == "nandroid")
841 {
842 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400843 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400844
845 if (simulate) {
846 DataManager::SetValue("tw_partition", "Simulation");
847 simulate_progress_bar();
848 } else {
849 if (arg == "backup") {
850 string Backup_Name;
851 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500852 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400853 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500854 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400855 else {
856 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400858 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
860 } else if (arg == "restore") {
861 string Restore_Name;
862 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400863 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400864 } else {
865 operation_end(1, simulate);
866 return -1;
867 }
868 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400869 if (ret == false)
870 ret = 1; // 1 for failure
871 else
872 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500873 operation_end(ret, simulate);
874 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400875 }
876 if (function == "fixpermissions")
877 {
878 operation_start("Fix Permissions");
Dees_Troy2673cec2013-04-02 20:22:16 +0000879 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400880 if (simulate) {
881 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400882 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400883 int op_status = PartitionManager.Fix_Permissions();
884 if (op_status != 0)
885 op_status = 1; // failure
886 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400887 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400888 return 0;
889 }
890 if (function == "dd")
891 {
892 operation_start("imaging");
893
894 if (simulate) {
895 simulate_progress_bar();
896 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500897 string result;
898 string cmd = "dd " + arg;
899 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400900 }
901 operation_end(0, simulate);
902 return 0;
903 }
904 if (function == "partitionsd")
905 {
906 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400907 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400908
909 if (simulate) {
910 simulate_progress_bar();
911 } else {
912 int allow_partition;
913 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
914 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000915 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400916 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400917 if (!PartitionManager.Partition_SDCard())
918 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400919 }
920 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400921 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400922 return 0;
923 }
924 if (function == "installhtcdumlock")
925 {
926 operation_start("Install HTC Dumlock");
927 if (simulate) {
928 simulate_progress_bar();
929 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400931
932 operation_end(0, simulate);
933 return 0;
934 }
935 if (function == "htcdumlockrestoreboot")
936 {
937 operation_start("HTC Dumlock Restore Boot");
938 if (simulate) {
939 simulate_progress_bar();
940 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400942
943 operation_end(0, simulate);
944 return 0;
945 }
946 if (function == "htcdumlockreflashrecovery")
947 {
948 operation_start("HTC Dumlock Reflash Recovery");
949 if (simulate) {
950 simulate_progress_bar();
951 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400952 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400953
954 operation_end(0, simulate);
955 return 0;
956 }
957 if (function == "cmd")
958 {
959 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500960 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400961
962 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +0000963 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400964 if (simulate) {
965 simulate_progress_bar();
966 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500967 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400968 if (op_status != 0)
969 op_status = 1;
970 }
971
972 operation_end(op_status, simulate);
973 return 0;
974 }
975 if (function == "terminalcommand")
976 {
977 int op_status = 0;
978 string cmdpath, command;
979
980 DataManager::GetValue("tw_terminal_location", cmdpath);
981 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +0000982 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400983 if (simulate) {
984 simulate_progress_bar();
985 operation_end(op_status, simulate);
986 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400987 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +0000988 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400989 DataManager::SetValue("tw_terminal_command_thread", command);
990 DataManager::SetValue("tw_terminal_state", 1);
991 DataManager::SetValue("tw_background_thread_running", 1);
992 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
993 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000994 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400995 DataManager::SetValue("tw_terminal_state", 0);
996 DataManager::SetValue("tw_background_thread_running", 0);
997 operation_end(1, simulate);
998 }
999 }
1000 return 0;
1001 }
1002 if (function == "killterminal")
1003 {
1004 int op_status = 0;
1005
Dees_Troy2673cec2013-04-02 20:22:16 +00001006 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001007 operation_start("KillCommand");
1008 DataManager::SetValue("tw_operation_status", 0);
1009 DataManager::SetValue("tw_operation_state", 1);
1010 DataManager::SetValue("tw_terminal_state", 0);
1011 DataManager::SetValue("tw_background_thread_running", 0);
1012 DataManager::SetValue(TW_ACTION_BUSY, 0);
1013 return 0;
1014 }
1015 if (function == "reinjecttwrp")
1016 {
1017 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001018 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001021 if (simulate) {
1022 simulate_progress_bar();
1023 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001024 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001026 }
1027
1028 operation_end(op_status, simulate);
1029 return 0;
1030 }
1031 if (function == "checkbackupname")
1032 {
1033 int op_status = 0;
1034
1035 operation_start("CheckBackupName");
1036 if (simulate) {
1037 simulate_progress_bar();
1038 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001039 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001040 if (op_status != 0)
1041 op_status = 1;
1042 }
1043
1044 operation_end(op_status, simulate);
1045 return 0;
1046 }
1047 if (function == "decrypt")
1048 {
1049 int op_status = 0;
1050
1051 operation_start("Decrypt");
1052 if (simulate) {
1053 simulate_progress_bar();
1054 } else {
1055 string Password;
1056 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001058 if (op_status != 0)
1059 op_status = 1;
1060 else {
1061 int load_theme = 1;
1062
1063 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001064
1065 if (load_theme) {
1066 int has_datamedia;
1067
1068 // Check for a custom theme and load it if exists
1069 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1070 if (has_datamedia != 0) {
1071 struct stat st;
1072 int check = 0;
1073 std::string theme_path;
1074
1075 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001076 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001077 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001078 check = 1;
1079 }
1080
1081 theme_path += "/TWRP/theme/ui.zip";
1082 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1083 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1084 {
1085 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001086 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001087 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1088 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001090 }
1091 }
1092 }
1093 }
1094 }
1095 }
1096 }
1097
1098 operation_end(op_status, simulate);
1099 return 0;
1100 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001101 if (function == "adbsideload")
1102 {
1103 int ret = 0;
1104
1105 operation_start("Sideload");
1106 if (simulate) {
1107 simulate_progress_bar();
1108 } else {
1109 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001110 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001111 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001112
1113 if (!PartitionManager.Mount_Current_Storage(true)) {
1114 operation_end(1, simulate);
1115 return 0;
1116 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001117 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1118 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001119 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001120 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001121 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001122 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 ret = apply_from_adb(Sideload_File.c_str());
1124 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 -05001125 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001126 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001128 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1129 PartitionManager.Wipe_By_Path("/cache");
1130 if (wipe_dalvik)
1131 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 } else {
1133 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001134 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001135 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1136 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001137 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001138 if (simulate) {
1139 simulate_progress_bar();
1140 } else {
1141 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1142 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001143 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001144 else {
1145 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 -05001146 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001147 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001149 }
1150 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001151 }
1152 operation_end(ret, simulate);
1153 return 0;
1154 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001155 if (function == "adbsideloadcancel")
1156 {
1157 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001158 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001159 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001160 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001161 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 property_get("tw_child_pid", child_prop, "error");
1163 if (strcmp(child_prop, "error") == 0) {
1164 LOGERR("Unable to get child ID from prop\n");
1165 return 0;
1166 }
1167 child_pid = atoi(child_prop);
1168 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001169 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001170 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001171 return 0;
1172 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001173 if (function == "openrecoveryscript") {
1174 operation_start("OpenRecoveryScript");
1175 if (simulate) {
1176 simulate_progress_bar();
1177 } else {
1178 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1179 // that we converted to ORS commands during boot in recovery.cpp.
1180 // Run those first.
1181 int reboot = 0;
1182 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001183 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001184 if (OpenRecoveryScript::run_script_file() == 0) {
1185 reboot = 1;
1186 }
1187 }
1188 // Check for the ORS file in /cache and attempt to run those commands.
1189 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001191 if (OpenRecoveryScript::run_script_file() == 0) {
1192 reboot = 1;
1193 }
1194 }
1195 if (reboot) {
1196 usleep(2000000); // Sleep for 2 seconds before rebooting
1197 TWFunc::tw_reboot(rb_system);
1198 } else {
1199 DataManager::SetValue("tw_page_done", 1);
1200 }
1201 }
1202 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001203 if (function == "installsu")
1204 {
1205 int op_status = 0;
1206
1207 operation_start("Install SuperSU");
1208 if (simulate) {
1209 simulate_progress_bar();
1210 } else {
1211 if (!TWFunc::Install_SuperSU())
1212 op_status = 1;
1213 }
1214
1215 operation_end(op_status, simulate);
1216 return 0;
1217 }
1218 if (function == "fixsu")
1219 {
1220 int op_status = 0;
1221
1222 operation_start("Fixing Superuser Permissions");
1223 if (simulate) {
1224 simulate_progress_bar();
1225 } else {
1226 if (!TWFunc::Fix_su_Perms())
1227 op_status = 1;
1228 }
1229
1230 operation_end(op_status, simulate);
1231 return 0;
1232 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001233 }
1234 else
1235 {
1236 pthread_t t;
1237 pthread_create(&t, NULL, thread_start, this);
1238 return 0;
1239 }
1240 return -1;
1241}
1242
1243int GUIAction::getKeyByName(std::string key)
1244{
1245 if (key == "home") return KEY_HOME;
1246 else if (key == "menu") return KEY_MENU;
1247 else if (key == "back") return KEY_BACK;
1248 else if (key == "search") return KEY_SEARCH;
1249 else if (key == "voldown") return KEY_VOLUMEDOWN;
1250 else if (key == "volup") return KEY_VOLUMEUP;
1251 else if (key == "power") {
1252 int ret_val;
1253 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1254 if (!ret_val)
1255 return KEY_POWER;
1256 else
1257 return ret_val;
1258 }
1259
1260 return atol(key.c_str());
1261}
1262
1263void* GUIAction::command_thread(void *cookie)
1264{
1265 string command;
1266 FILE* fp;
1267 char line[512];
1268
1269 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001270 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001271 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001273 } else {
1274 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1275 struct timeval timeout;
1276 fd_set fdset;
1277
1278 while(keep_going)
1279 {
1280 FD_ZERO(&fdset);
1281 FD_SET(fd, &fdset);
1282 timeout.tv_sec = 0;
1283 timeout.tv_usec = 400000;
1284 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1285 if (has_data == 0) {
1286 // Timeout reached
1287 DataManager::GetValue("tw_terminal_state", check);
1288 if (check == 0) {
1289 keep_going = 0;
1290 }
1291 } else if (has_data < 0) {
1292 // End of execution
1293 keep_going = 0;
1294 } else {
1295 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001296 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001297 bytes_read = read(fd, line, sizeof(line));
1298 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001300 else
1301 keep_going = 0; // Done executing
1302 }
1303 }
1304 fclose(fp);
1305 }
1306 DataManager::SetValue("tw_operation_status", 0);
1307 DataManager::SetValue("tw_operation_state", 1);
1308 DataManager::SetValue("tw_terminal_state", 0);
1309 DataManager::SetValue("tw_background_thread_running", 0);
1310 DataManager::SetValue(TW_ACTION_BUSY, 0);
1311 return NULL;
1312}