blob: ca968d088695acee6ceb246fb34f17bdb0f37664 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// image.cpp - GUIImage object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/ioctl.h>
13#include <linux/input.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040017#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <string>
20#include <sstream>
21#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040023#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
Dees_Troy43d8b002012-09-17 16:00:01 -040025#include "../ui.h"
26#include "../adb_install.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050027#include "blanktimer.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040028
Dees_Troy51a0e822012-09-05 15:24:24 -040029extern "C" {
30#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040031#include "../minuitwrp/minui.h"
32#include "../recovery_ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040034#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035
Dees_Troy43d8b002012-09-17 16:00:01 -040036#include "../minadbd/adb.h"
37
Dees_Troy32c8eb82012-09-11 15:28:06 -040038int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040039void 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 -040040int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040041int gui_start();
42};
43
44#include "rapidxml.hpp"
45#include "objects.hpp"
46
Dees_Troy43d8b002012-09-17 16:00:01 -040047extern RecoveryUI* ui;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050048extern blanktimer blankTimer;
Dees_Troy43d8b002012-09-17 16:00:01 -040049
Dees_Troy51a0e822012-09-05 15:24:24 -040050void curtainClose(void);
51
52GUIAction::GUIAction(xml_node<>* node)
53 : Conditional(node)
54{
55 xml_node<>* child;
56 xml_node<>* actions;
57 xml_attribute<>* attr;
58
59 mKey = 0;
60
61 if (!node) return;
62
63 // First, get the action
64 actions = node->first_node("actions");
65 if (actions) child = actions->first_node("action");
66 else child = node->first_node("action");
67
68 if (!child) return;
69
70 while (child)
71 {
72 Action action;
73
74 attr = child->first_attribute("function");
75 if (!attr) return;
76
77 action.mFunction = attr->value();
78 action.mArg = child->value();
79 mActions.push_back(action);
80
81 child = child->next_sibling("action");
82 }
83
84 // Now, let's get either the key or region
85 child = node->first_node("touch");
86 if (child)
87 {
88 attr = child->first_attribute("key");
89 if (attr)
90 {
91 std::string key = attr->value();
92
93 mKey = getKeyByName(key);
94 }
95 else
96 {
97 attr = child->first_attribute("x");
98 if (!attr) return;
99 mActionX = atol(attr->value());
100 attr = child->first_attribute("y");
101 if (!attr) return;
102 mActionY = atol(attr->value());
103 attr = child->first_attribute("w");
104 if (!attr) return;
105 mActionW = atol(attr->value());
106 attr = child->first_attribute("h");
107 if (!attr) return;
108 mActionH = atol(attr->value());
109 }
110 }
111}
112
113int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
114{
115 if (state == TOUCH_RELEASE)
116 doActions();
117
118 return 0;
119}
120
121int GUIAction::NotifyKey(int key)
122{
123 if (!mKey || key != mKey)
124 return 1;
125
126 doActions();
127 return 0;
128}
129
130int GUIAction::NotifyVarChange(std::string varName, std::string value)
131{
132 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
133 doActions();
134
135 // This handles notifying the condition system of page start
136 if (varName.empty() && isConditionValid())
137 NotifyPageSet();
138
139 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
140 doActions();
141
142 return 0;
143}
144
145void GUIAction::simulate_progress_bar(void)
146{
147 ui_print("Simulating actions...\n");
148 for (int i = 0; i < 5; i++)
149 {
150 usleep(500000);
151 DataManager::SetValue("ui_progress", i * 20);
152 }
153}
154
Dees_Troy657c3092012-09-10 20:32:10 -0400155int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400156{
157 int ret_val = 0;
158
159 DataManager::SetValue("ui_progress", 0);
160
161 if (filename.empty())
162 {
163 LOGE("No file specified.\n");
164 return -1;
165 }
166
167 // We're going to jump to this page first, like a loading page
168 gui_changePage(pageName);
169
170 int fd = -1;
171 ZipArchive zip;
172
Dees_Troy657c3092012-09-10 20:32:10 -0400173 if (!PartitionManager.Mount_By_Path(filename, true))
174 return -1;
175
176 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 {
178 LOGE("Unable to open zip file.\n");
179 return -1;
180 }
181
182 // Check the zip to see if it has a custom installer theme
183 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
184 if (twrp != NULL)
185 {
186 unlink("/tmp/twrp.zip");
187 fd = creat("/tmp/twrp.zip", 0666);
188 }
189 if (fd >= 0 && twrp != NULL &&
190 mzExtractZipEntryToFile(&zip, twrp, fd) &&
191 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
192 {
193 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400194 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 gui_changePage("main");
196 }
197 else
198 {
199 // In this case, we just use the default page
200 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400201 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400202 }
203 if (fd >= 0)
204 close(fd);
205
206 if (simulate) {
207 simulate_progress_bar();
208 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400209 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
211 // Now, check if we need to ensure TWRP remains installed...
212 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500213 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 if (stat("/sbin/installTwrp", &st) == 0)
215 {
216 DataManager::SetValue("tw_operation", "Configuring TWRP");
217 DataManager::SetValue("tw_partition", "");
218 ui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500219 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 {
221 ui_print("Unable to configure TWRP with this kernel.\n");
222 }
223 }
224 }
225
226 // Done
227 DataManager::SetValue("ui_progress", 100);
228 DataManager::SetValue("ui_progress", 0);
229 return ret_val;
230}
231
232int GUIAction::doActions()
233{
234 if (mActions.size() < 1) return -1;
235 if (mActions.size() == 1)
236 return doAction(mActions.at(0), 0);
237
238 // For multi-action, we always use a thread
239 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000240 pthread_attr_t tattr;
241
242 if (pthread_attr_init(&tattr)) {
243 LOGE("Unable to pthread_attr_init\n");
244 return -1;
245 }
246 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
247 LOGE("Error setting pthread_attr_setdetachstate\n");
248 return -1;
249 }
250 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
251 LOGE("Error setting pthread_attr_setscope\n");
252 return -1;
253 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500254 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troyab4963c2013-01-16 20:35:51 +0000255 LOGE("Error setting pthread_attr_setstacksize\n");
256 return -1;
257 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500258 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000259 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000260 if (ret) {
261 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
262 thread_start(this);
263 } else {
264 if (pthread_join(t, NULL)) {
265 LOGE("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000266 }
267 }
268 if (pthread_attr_destroy(&tattr)) {
269 LOGE("Failed to pthread_attr_destroy\n");
270 return -1;
271 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
273 return 0;
274}
275
276void* GUIAction::thread_start(void *cookie)
277{
278 GUIAction* ourThis = (GUIAction*) cookie;
279
280 DataManager::SetValue(TW_ACTION_BUSY, 1);
281
282 if (ourThis->mActions.size() > 1)
283 {
284 std::vector<Action>::iterator iter;
285 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
286 ourThis->doAction(*iter, 1);
287 }
288 else
289 {
290 ourThis->doAction(ourThis->mActions.at(0), 1);
291 }
292 int check = 0;
293 DataManager::GetValue("tw_background_thread_running", check);
294 if (check == 0)
295 DataManager::SetValue(TW_ACTION_BUSY, 0);
296 return NULL;
297}
298
299void GUIAction::operation_start(const string operation_name)
300{
301 DataManager::SetValue(TW_ACTION_BUSY, 1);
302 DataManager::SetValue("ui_progress", 0);
303 DataManager::SetValue("tw_operation", operation_name);
304 DataManager::SetValue("tw_operation_status", 0);
305 DataManager::SetValue("tw_operation_state", 0);
306}
307
308void GUIAction::operation_end(const int operation_status, const int simulate)
309{
310 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311 DataManager::SetValue("ui_progress", 100);
312 if (simulate) {
313 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
314 if (simulate_fail != 0)
315 DataManager::SetValue("tw_operation_status", 1);
316 else
317 DataManager::SetValue("tw_operation_status", 0);
318 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500319 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400320 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321 }
322 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400323 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400325 }
326 DataManager::SetValue("tw_operation_state", 1);
327 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500328 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400329}
330
331int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
332{
333 static string zip_queue[10];
334 static int zip_queue_index;
335 static pthread_t terminal_command;
336 int simulate;
337
338 std::string arg = gui_parse_text(action.mArg);
339
340 std::string function = gui_parse_text(action.mFunction);
341
342 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
343
Dees_Troy6ef66352013-02-21 08:26:57 -0600344 if (function == "reboot")
345 {
346 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
Dees_Troy6ef66352013-02-21 08:26:57 -0600348 sync();
349 DataManager::SetValue("tw_gui_done", 1);
350 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Dees_Troy6ef66352013-02-21 08:26:57 -0600352 return 0;
353 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 if (function == "home")
355 {
356 PageManager::SelectPackage("TWRP");
357 gui_changePage("main");
358 return 0;
359 }
360
361 if (function == "key")
362 {
363 PageManager::NotifyKey(getKeyByName(arg));
364 return 0;
365 }
366
367 if (function == "page") {
368 std::string page_name = gui_parse_text(arg);
369 return gui_changePage(page_name);
370 }
371
372 if (function == "reload") {
373 int check = 0, ret_val = 0;
374 std::string theme_path;
375
376 operation_start("Reload Theme");
377 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
380 check = 1;
381 }
382
383 theme_path += "/TWRP/theme/ui.zip";
384 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
385 {
386 // Loading the custom theme failed - try loading the stock theme
387 LOGI("Attempting to reload stock theme...\n");
388 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
389 {
390 LOGE("Failed to load base packages.\n");
391 ret_val = 1;
392 }
393 }
394 operation_end(ret_val, simulate);
395 }
396
397 if (function == "readBackup")
398 {
399 string Restore_Name;
400 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 return 0;
403 }
404
405 if (function == "set")
406 {
407 if (arg.find('=') != string::npos)
408 {
409 string varName = arg.substr(0, arg.find('='));
410 string value = arg.substr(arg.find('=') + 1, string::npos);
411
412 DataManager::GetValue(value, value);
413 DataManager::SetValue(varName, value);
414 }
415 else
416 DataManager::SetValue(arg, "1");
417 return 0;
418 }
419 if (function == "clear")
420 {
421 DataManager::SetValue(arg, "0");
422 return 0;
423 }
424
425 if (function == "mount")
426 {
427 if (arg == "usb")
428 {
429 DataManager::SetValue(TW_ACTION_BUSY, 1);
430 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400431 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 else
433 ui_print("Simulating actions...\n");
434 }
435 else if (!simulate)
436 {
437 string cmd;
438 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400439 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400441 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 else
Dees_Troy51127312012-09-08 13:08:49 -0400443 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 } else
445 ui_print("Simulating actions...\n");
446 return 0;
447 }
448
449 if (function == "umount" || function == "unmount")
450 {
451 if (arg == "usb")
452 {
453 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400454 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400455 else
456 ui_print("Simulating actions...\n");
457 DataManager::SetValue(TW_ACTION_BUSY, 0);
458 }
459 else if (!simulate)
460 {
461 string cmd;
462 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400463 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400465 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 else
Dees_Troy51127312012-09-08 13:08:49 -0400467 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468 } else
469 ui_print("Simulating actions...\n");
470 return 0;
471 }
472
473 if (function == "restoredefaultsettings")
474 {
475 operation_start("Restore Defaults");
476 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
477 ui_print("Simulating actions...\n");
478 else {
479 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400480 PartitionManager.Update_System_Details();
481 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 }
483 operation_end(0, simulate);
484 }
485
486 if (function == "copylog")
487 {
488 operation_start("Copy Log");
489 if (!simulate)
490 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500491 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400492 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500493 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
494 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 sync();
496 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
497 } else
498 simulate_progress_bar();
499 operation_end(0, simulate);
500 return 0;
501 }
502
503 if (function == "compute" || function == "addsubtract")
504 {
505 if (arg.find("+") != string::npos)
506 {
507 string varName = arg.substr(0, arg.find('+'));
508 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
509 int amount_to_add = atoi(string_to_add.c_str());
510 int value;
511
512 DataManager::GetValue(varName, value);
513 DataManager::SetValue(varName, value + amount_to_add);
514 return 0;
515 }
516 if (arg.find("-") != string::npos)
517 {
518 string varName = arg.substr(0, arg.find('-'));
519 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
520 int amount_to_subtract = atoi(string_to_subtract.c_str());
521 int value;
522
523 DataManager::GetValue(varName, value);
524 value -= amount_to_subtract;
525 if (value <= 0)
526 value = 0;
527 DataManager::SetValue(varName, value);
528 return 0;
529 }
530 }
531
532 if (function == "setguitimezone")
533 {
534 string SelectedZone;
535 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
536 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
537 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
538
539 int dst;
540 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
541
542 string offset;
543 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
544
545 string NewTimeZone = Zone;
546 if (offset != "0")
547 NewTimeZone += ":" + offset;
548
549 if (dst != 0)
550 NewTimeZone += DSTZone;
551
552 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400553 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400554 return 0;
555 }
556
557 if (function == "togglestorage") {
558 if (arg == "internal") {
559 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
560 } else if (arg == "external") {
561 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
562 }
Dees_Troy51127312012-09-08 13:08:49 -0400563 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400565 string zip_path, zip_root;
566 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
567 zip_root = TWFunc::Get_Root_Path(zip_path);
568#ifdef RECOVERY_SDCARD_ON_DATA
569 #ifndef TW_EXTERNAL_STORAGE_PATH
570 if (zip_root != "/sdcard")
571 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
572 #else
573 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
574 if (zip_root != "/emmc")
575 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
576 } else {
577 if (zip_root != "/sdcard")
578 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
579 }
580 #endif
581#else
582 if (zip_root != DataManager::GetCurrentStoragePath())
583 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
584#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 // Save the current zip location to the external variable
586 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
587 // Change the current zip location to the internal variable
588 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
589 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400590 string zip_path, zip_root;
591 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
592 zip_root = TWFunc::Get_Root_Path(zip_path);
593 if (zip_root != DataManager::GetCurrentStoragePath()) {
594 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
595 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400596 // Save the current zip location to the internal variable
597 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
598 // Change the current zip location to the external variable
599 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
600 }
601 } else {
602 // We weren't able to toggle for some reason, restore original setting
603 if (arg == "internal") {
604 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
605 } else if (arg == "external") {
606 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
607 }
608 }
609 return 0;
610 }
611
612 if (function == "overlay")
613 return gui_changeOverlay(arg);
614
615 if (function == "queuezip")
616 {
617 if (zip_queue_index >= 10) {
618 ui_print("Maximum zip queue reached!\n");
619 return 0;
620 }
621 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
622 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
623 zip_queue_index++;
624 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
625 }
626 return 0;
627 }
628
629 if (function == "cancelzip")
630 {
631 if (zip_queue_index <= 0) {
632 ui_print("Minimum zip queue reached!\n");
633 return 0;
634 } else {
635 zip_queue_index--;
636 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
637 }
638 return 0;
639 }
640
641 if (function == "queueclear")
642 {
643 zip_queue_index = 0;
644 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
645 return 0;
646 }
647
648 if (function == "sleep")
649 {
650 operation_start("Sleep");
651 usleep(atoi(arg.c_str()));
652 operation_end(0, simulate);
653 return 0;
654 }
655
656 if (isThreaded)
657 {
658 if (function == "fileexists")
659 {
660 struct stat st;
661 string newpath = arg + "/.";
662
663 operation_start("FileExists");
664 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
665 operation_end(0, simulate);
666 else
667 operation_end(1, simulate);
668 }
669
670 if (function == "flash")
671 {
Dees_Troy657c3092012-09-10 20:32:10 -0400672 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400673
674 for (i=0; i<zip_queue_index; i++) {
675 operation_start("Flashing");
676 DataManager::SetValue("tw_filename", zip_queue[i]);
677 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
678
Dees_Troy657c3092012-09-10 20:32:10 -0400679 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400680 if (ret_val != 0) {
681 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
682 i = 10; // Error flashing zip - exit queue
683 ret_val = 1;
684 }
685 }
686 zip_queue_index = 0;
687 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
688
Dees_Troy657c3092012-09-10 20:32:10 -0400689 if (wipe_cache)
690 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500691 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
693 operation_start("ReinjectTWRP");
694 ui_print("Injecting TWRP into boot image...\n");
695 if (simulate) {
696 simulate_progress_bar();
697 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400698 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
699 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500700 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400701 else {
702 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 -0500703 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400704 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 ui_print("TWRP injection complete.\n");
706 }
707 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400708 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 operation_end(ret_val, simulate);
710 return 0;
711 }
712 if (function == "wipe")
713 {
714 operation_start("Format");
715 DataManager::SetValue("tw_partition", arg);
716
Dees_Troy38bd7602012-09-14 13:33:53 -0400717 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400718
719 if (simulate) {
720 simulate_progress_bar();
721 } else {
722 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400723 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400725 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400727 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400729 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400731 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 } else if (arg == "INTERNAL") {
733 int has_datamedia, dual_storage;
734
735 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
736 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 }
741 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400742 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743
Dees_Troy38bd7602012-09-14 13:33:53 -0400744 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
745 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400746 } else if (arg == "ANDROIDSECURE") {
747 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400748 } else
749 ret_val = PartitionManager.Wipe_By_Path(arg);
750
751 if (arg == DataManager::GetSettingsStoragePath()) {
752 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
753 string Storage_Path = DataManager::GetSettingsStoragePath();
754
755 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
756 LOGI("Making TWRP folder and saving settings.\n");
757 Storage_Path += "/TWRP";
758 mkdir(Storage_Path.c_str(), 0777);
759 DataManager::Flush();
760 } else {
761 LOGE("Unable to recreate TWRP folder and save settings.\n");
762 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 }
764 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400766 if (ret_val)
767 ret_val = 0; // 0 is success
768 else
769 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 operation_end(ret_val, simulate);
771 return 0;
772 }
773 if (function == "refreshsizes")
774 {
775 operation_start("Refreshing Sizes");
776 if (simulate) {
777 simulate_progress_bar();
778 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400779 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 operation_end(0, simulate);
781 }
782 if (function == "nandroid")
783 {
784 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400785 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400786
787 if (simulate) {
788 DataManager::SetValue("tw_partition", "Simulation");
789 simulate_progress_bar();
790 } else {
791 if (arg == "backup") {
792 string Backup_Name;
793 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500794 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400795 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500796 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400797 else {
798 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400799 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400800 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
802 } else if (arg == "restore") {
803 string Restore_Name;
804 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400805 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 } else {
807 operation_end(1, simulate);
808 return -1;
809 }
810 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400811 if (ret == false)
812 ret = 1; // 1 for failure
813 else
814 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500815 operation_end(ret, simulate);
816 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400817 }
818 if (function == "fixpermissions")
819 {
820 operation_start("Fix Permissions");
821 LOGI("fix permissions started!\n");
822 if (simulate) {
823 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400824 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400825 int op_status = PartitionManager.Fix_Permissions();
826 if (op_status != 0)
827 op_status = 1; // failure
828 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400829 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400830 return 0;
831 }
832 if (function == "dd")
833 {
834 operation_start("imaging");
835
836 if (simulate) {
837 simulate_progress_bar();
838 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500839 string result;
840 string cmd = "dd " + arg;
841 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400842 }
843 operation_end(0, simulate);
844 return 0;
845 }
846 if (function == "partitionsd")
847 {
848 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400849 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400850
851 if (simulate) {
852 simulate_progress_bar();
853 } else {
854 int allow_partition;
855 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
856 if (allow_partition == 0) {
857 ui_print("This device does not have a real SD Card!\nAborting!\n");
858 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400859 if (!PartitionManager.Partition_SDCard())
860 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400861 }
862 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400863 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400864 return 0;
865 }
866 if (function == "installhtcdumlock")
867 {
868 operation_start("Install HTC Dumlock");
869 if (simulate) {
870 simulate_progress_bar();
871 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400873
874 operation_end(0, simulate);
875 return 0;
876 }
877 if (function == "htcdumlockrestoreboot")
878 {
879 operation_start("HTC Dumlock Restore Boot");
880 if (simulate) {
881 simulate_progress_bar();
882 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400883 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400884
885 operation_end(0, simulate);
886 return 0;
887 }
888 if (function == "htcdumlockreflashrecovery")
889 {
890 operation_start("HTC Dumlock Reflash Recovery");
891 if (simulate) {
892 simulate_progress_bar();
893 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400895
896 operation_end(0, simulate);
897 return 0;
898 }
899 if (function == "cmd")
900 {
901 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500902 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400903
904 operation_start("Command");
905 LOGI("Running command: '%s'\n", arg.c_str());
906 if (simulate) {
907 simulate_progress_bar();
908 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500909 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400910 if (op_status != 0)
911 op_status = 1;
912 }
913
914 operation_end(op_status, simulate);
915 return 0;
916 }
917 if (function == "terminalcommand")
918 {
919 int op_status = 0;
920 string cmdpath, command;
921
922 DataManager::GetValue("tw_terminal_location", cmdpath);
923 operation_start("CommandOutput");
924 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
925 if (simulate) {
926 simulate_progress_bar();
927 operation_end(op_status, simulate);
928 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400929 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400930 LOGI("Actual command is: '%s'\n", command.c_str());
931 DataManager::SetValue("tw_terminal_command_thread", command);
932 DataManager::SetValue("tw_terminal_state", 1);
933 DataManager::SetValue("tw_background_thread_running", 1);
934 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
935 if (op_status != 0) {
936 LOGE("Error starting terminal command thread, %i.\n", op_status);
937 DataManager::SetValue("tw_terminal_state", 0);
938 DataManager::SetValue("tw_background_thread_running", 0);
939 operation_end(1, simulate);
940 }
941 }
942 return 0;
943 }
944 if (function == "killterminal")
945 {
946 int op_status = 0;
947
948 LOGI("Sending kill command...\n");
949 operation_start("KillCommand");
950 DataManager::SetValue("tw_operation_status", 0);
951 DataManager::SetValue("tw_operation_state", 1);
952 DataManager::SetValue("tw_terminal_state", 0);
953 DataManager::SetValue("tw_background_thread_running", 0);
954 DataManager::SetValue(TW_ACTION_BUSY, 0);
955 return 0;
956 }
957 if (function == "reinjecttwrp")
958 {
959 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500960 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400961 operation_start("ReinjectTWRP");
962 ui_print("Injecting TWRP into boot image...\n");
963 if (simulate) {
964 simulate_progress_bar();
965 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500966 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400967 ui_print("TWRP injection complete.\n");
968 }
969
970 operation_end(op_status, simulate);
971 return 0;
972 }
973 if (function == "checkbackupname")
974 {
975 int op_status = 0;
976
977 operation_start("CheckBackupName");
978 if (simulate) {
979 simulate_progress_bar();
980 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400981 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400982 if (op_status != 0)
983 op_status = 1;
984 }
985
986 operation_end(op_status, simulate);
987 return 0;
988 }
989 if (function == "decrypt")
990 {
991 int op_status = 0;
992
993 operation_start("Decrypt");
994 if (simulate) {
995 simulate_progress_bar();
996 } else {
997 string Password;
998 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -0400999 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001000 if (op_status != 0)
1001 op_status = 1;
1002 else {
1003 int load_theme = 1;
1004
1005 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001006
1007 if (load_theme) {
1008 int has_datamedia;
1009
1010 // Check for a custom theme and load it if exists
1011 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1012 if (has_datamedia != 0) {
1013 struct stat st;
1014 int check = 0;
1015 std::string theme_path;
1016
1017 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001018 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1020 check = 1;
1021 }
1022
1023 theme_path += "/TWRP/theme/ui.zip";
1024 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1025 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1026 {
1027 // Loading the custom theme failed - try loading the stock theme
1028 LOGI("Attempting to reload stock theme...\n");
1029 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1030 {
1031 LOGE("Failed to load base packages.\n");
1032 }
1033 }
1034 }
1035 }
1036 }
1037 }
1038 }
1039
1040 operation_end(op_status, simulate);
1041 return 0;
1042 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001043 if (function == "adbsideload")
1044 {
1045 int ret = 0;
1046
1047 operation_start("Sideload");
1048 if (simulate) {
1049 simulate_progress_bar();
1050 } else {
1051 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001052 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001053 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001054
1055 if (!PartitionManager.Mount_Current_Storage(true)) {
1056 operation_end(1, simulate);
1057 return 0;
1058 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001059 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1060 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001061 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001062 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001063 ui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001064 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy9a4b5692012-09-19 15:09:45 -04001065 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001066 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001067 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001068 } else {
1069 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1070 PartitionManager.Wipe_By_Path("/cache");
1071 if (wipe_dalvik)
1072 PartitionManager.Wipe_Dalvik_Cache();
1073 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001074 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1075 operation_start("ReinjectTWRP");
1076 ui_print("Injecting TWRP into boot image...\n");
1077 if (simulate) {
1078 simulate_progress_bar();
1079 } else {
1080 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1081 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001082 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001083 else {
1084 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 -05001085 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001086 }
1087 ui_print("TWRP injection complete.\n");
1088 }
1089 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 }
1091 operation_end(ret, simulate);
1092 return 0;
1093 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001094 if (function == "adbsideloadcancel")
1095 {
1096 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001097 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001098 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001099 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001100 DataManager::GetValue("tw_child_pid", child_pid);
1101 ui_print("Cancelling ADB sideload...\n");
1102 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001103 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001104 return 0;
1105 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001106 if (function == "openrecoveryscript") {
1107 operation_start("OpenRecoveryScript");
1108 if (simulate) {
1109 simulate_progress_bar();
1110 } else {
1111 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1112 // that we converted to ORS commands during boot in recovery.cpp.
1113 // Run those first.
1114 int reboot = 0;
1115 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1116 ui_print("Processing AOSP recovery commands...\n");
1117 if (OpenRecoveryScript::run_script_file() == 0) {
1118 reboot = 1;
1119 }
1120 }
1121 // Check for the ORS file in /cache and attempt to run those commands.
1122 if (OpenRecoveryScript::check_for_script_file()) {
1123 ui_print("Processing OpenRecoveryScript file...\n");
1124 if (OpenRecoveryScript::run_script_file() == 0) {
1125 reboot = 1;
1126 }
1127 }
1128 if (reboot) {
1129 usleep(2000000); // Sleep for 2 seconds before rebooting
1130 TWFunc::tw_reboot(rb_system);
1131 } else {
1132 DataManager::SetValue("tw_page_done", 1);
1133 }
1134 }
1135 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001136 if (function == "installsu")
1137 {
1138 int op_status = 0;
1139
1140 operation_start("Install SuperSU");
1141 if (simulate) {
1142 simulate_progress_bar();
1143 } else {
1144 if (!TWFunc::Install_SuperSU())
1145 op_status = 1;
1146 }
1147
1148 operation_end(op_status, simulate);
1149 return 0;
1150 }
1151 if (function == "fixsu")
1152 {
1153 int op_status = 0;
1154
1155 operation_start("Fixing Superuser Permissions");
1156 if (simulate) {
1157 simulate_progress_bar();
1158 } else {
1159 if (!TWFunc::Fix_su_Perms())
1160 op_status = 1;
1161 }
1162
1163 operation_end(op_status, simulate);
1164 return 0;
1165 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001166 }
1167 else
1168 {
1169 pthread_t t;
1170 pthread_create(&t, NULL, thread_start, this);
1171 return 0;
1172 }
1173 return -1;
1174}
1175
1176int GUIAction::getKeyByName(std::string key)
1177{
1178 if (key == "home") return KEY_HOME;
1179 else if (key == "menu") return KEY_MENU;
1180 else if (key == "back") return KEY_BACK;
1181 else if (key == "search") return KEY_SEARCH;
1182 else if (key == "voldown") return KEY_VOLUMEDOWN;
1183 else if (key == "volup") return KEY_VOLUMEUP;
1184 else if (key == "power") {
1185 int ret_val;
1186 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1187 if (!ret_val)
1188 return KEY_POWER;
1189 else
1190 return ret_val;
1191 }
1192
1193 return atol(key.c_str());
1194}
1195
1196void* GUIAction::command_thread(void *cookie)
1197{
1198 string command;
1199 FILE* fp;
1200 char line[512];
1201
1202 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001203 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001204 if (fp == NULL) {
1205 LOGE("Error opening command to run.\n");
1206 } else {
1207 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1208 struct timeval timeout;
1209 fd_set fdset;
1210
1211 while(keep_going)
1212 {
1213 FD_ZERO(&fdset);
1214 FD_SET(fd, &fdset);
1215 timeout.tv_sec = 0;
1216 timeout.tv_usec = 400000;
1217 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1218 if (has_data == 0) {
1219 // Timeout reached
1220 DataManager::GetValue("tw_terminal_state", check);
1221 if (check == 0) {
1222 keep_going = 0;
1223 }
1224 } else if (has_data < 0) {
1225 // End of execution
1226 keep_going = 0;
1227 } else {
1228 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001229 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001230 bytes_read = read(fd, line, sizeof(line));
1231 if (bytes_read > 0)
1232 ui_print("%s", line); // Display output
1233 else
1234 keep_going = 0; // Done executing
1235 }
1236 }
1237 fclose(fp);
1238 }
1239 DataManager::SetValue("tw_operation_status", 0);
1240 DataManager::SetValue("tw_operation_state", 1);
1241 DataManager::SetValue("tw_terminal_state", 0);
1242 DataManager::SetValue("tw_background_thread_running", 0);
1243 DataManager::SetValue(TW_ACTION_BUSY, 0);
1244 return NULL;
1245}