blob: 14ef71609f1af827248f36443d60250fe18e40ff [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"
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028extern "C" {
29#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include "../minuitwrp/minui.h"
31#include "../recovery_ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Dees_Troy43d8b002012-09-17 16:00:01 -040035#include "../minadbd/adb.h"
36
Dees_Troy32c8eb82012-09-11 15:28:06 -040037int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040038void 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 -040039int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040040int gui_start();
41};
42
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
Dees_Troy43d8b002012-09-17 16:00:01 -040046extern RecoveryUI* ui;
47
Dees_Troy51a0e822012-09-05 15:24:24 -040048void curtainClose(void);
49
50GUIAction::GUIAction(xml_node<>* node)
51 : Conditional(node)
52{
53 xml_node<>* child;
54 xml_node<>* actions;
55 xml_attribute<>* attr;
56
57 mKey = 0;
58
59 if (!node) return;
60
61 // First, get the action
62 actions = node->first_node("actions");
63 if (actions) child = actions->first_node("action");
64 else child = node->first_node("action");
65
66 if (!child) return;
67
68 while (child)
69 {
70 Action action;
71
72 attr = child->first_attribute("function");
73 if (!attr) return;
74
75 action.mFunction = attr->value();
76 action.mArg = child->value();
77 mActions.push_back(action);
78
79 child = child->next_sibling("action");
80 }
81
82 // Now, let's get either the key or region
83 child = node->first_node("touch");
84 if (child)
85 {
86 attr = child->first_attribute("key");
87 if (attr)
88 {
89 std::string key = attr->value();
90
91 mKey = getKeyByName(key);
92 }
93 else
94 {
95 attr = child->first_attribute("x");
96 if (!attr) return;
97 mActionX = atol(attr->value());
98 attr = child->first_attribute("y");
99 if (!attr) return;
100 mActionY = atol(attr->value());
101 attr = child->first_attribute("w");
102 if (!attr) return;
103 mActionW = atol(attr->value());
104 attr = child->first_attribute("h");
105 if (!attr) return;
106 mActionH = atol(attr->value());
107 }
108 }
109}
110
111int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
112{
113 if (state == TOUCH_RELEASE)
114 doActions();
115
116 return 0;
117}
118
119int GUIAction::NotifyKey(int key)
120{
121 if (!mKey || key != mKey)
122 return 1;
123
124 doActions();
125 return 0;
126}
127
128int GUIAction::NotifyVarChange(std::string varName, std::string value)
129{
130 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
131 doActions();
132
133 // This handles notifying the condition system of page start
134 if (varName.empty() && isConditionValid())
135 NotifyPageSet();
136
137 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
138 doActions();
139
140 return 0;
141}
142
143void GUIAction::simulate_progress_bar(void)
144{
145 ui_print("Simulating actions...\n");
146 for (int i = 0; i < 5; i++)
147 {
148 usleep(500000);
149 DataManager::SetValue("ui_progress", i * 20);
150 }
151}
152
Dees_Troy657c3092012-09-10 20:32:10 -0400153int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400154{
155 int ret_val = 0;
156
157 DataManager::SetValue("ui_progress", 0);
158
159 if (filename.empty())
160 {
161 LOGE("No file specified.\n");
162 return -1;
163 }
164
165 // We're going to jump to this page first, like a loading page
166 gui_changePage(pageName);
167
168 int fd = -1;
169 ZipArchive zip;
170
Dees_Troy657c3092012-09-10 20:32:10 -0400171 if (!PartitionManager.Mount_By_Path(filename, true))
172 return -1;
173
174 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 {
176 LOGE("Unable to open zip file.\n");
177 return -1;
178 }
179
180 // Check the zip to see if it has a custom installer theme
181 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
182 if (twrp != NULL)
183 {
184 unlink("/tmp/twrp.zip");
185 fd = creat("/tmp/twrp.zip", 0666);
186 }
187 if (fd >= 0 && twrp != NULL &&
188 mzExtractZipEntryToFile(&zip, twrp, fd) &&
189 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
190 {
191 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400192 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 gui_changePage("main");
194 }
195 else
196 {
197 // In this case, we just use the default page
198 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400199 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 }
201 if (fd >= 0)
202 close(fd);
203
204 if (simulate) {
205 simulate_progress_bar();
206 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400207 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400208
209 // Now, check if we need to ensure TWRP remains installed...
210 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 if (stat("/sbin/installTwrp", &st) == 0)
213 {
214 DataManager::SetValue("tw_operation", "Configuring TWRP");
215 DataManager::SetValue("tw_partition", "");
216 ui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500217 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 {
219 ui_print("Unable to configure TWRP with this kernel.\n");
220 }
221 }
222 }
223
224 // Done
225 DataManager::SetValue("ui_progress", 100);
226 DataManager::SetValue("ui_progress", 0);
227 return ret_val;
228}
229
230int GUIAction::doActions()
231{
232 if (mActions.size() < 1) return -1;
233 if (mActions.size() == 1)
234 return doAction(mActions.at(0), 0);
235
236 // For multi-action, we always use a thread
237 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000238 pthread_attr_t tattr;
239
240 if (pthread_attr_init(&tattr)) {
241 LOGE("Unable to pthread_attr_init\n");
242 return -1;
243 }
244 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
245 LOGE("Error setting pthread_attr_setdetachstate\n");
246 return -1;
247 }
248 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
249 LOGE("Error setting pthread_attr_setscope\n");
250 return -1;
251 }
252 if (pthread_attr_setstacksize(&tattr, 524288)) {
253 LOGE("Error setting pthread_attr_setstacksize\n");
254 return -1;
255 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000256 LOGI("Creating thread\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000257 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000258 if (ret) {
259 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
260 thread_start(this);
261 } else {
262 if (pthread_join(t, NULL)) {
263 LOGE("Error joining threads\n");
264 } else {
265 LOGI("Thread joined\n");
266 }
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);
328}
329
330int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
331{
332 static string zip_queue[10];
333 static int zip_queue_index;
334 static pthread_t terminal_command;
335 int simulate;
336
337 std::string arg = gui_parse_text(action.mArg);
338
339 std::string function = gui_parse_text(action.mFunction);
340
341 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
342
343 if (function == "reboot")
344 {
345 //curtainClose(); this sometimes causes a crash
346
347 sync();
348
Dees_Troya58bead2012-09-27 09:49:29 -0400349 if (arg == "recovery")
350 TWFunc::tw_reboot(rb_recovery);
351 else if (arg == "poweroff")
352 TWFunc::tw_reboot(rb_poweroff);
353 else if (arg == "bootloader")
354 TWFunc::tw_reboot(rb_bootloader);
355 else if (arg == "download")
356 TWFunc::tw_reboot(rb_download);
357 else
358 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
360 // This should never occur
361 return -1;
362 }
363 if (function == "home")
364 {
365 PageManager::SelectPackage("TWRP");
366 gui_changePage("main");
367 return 0;
368 }
369
370 if (function == "key")
371 {
372 PageManager::NotifyKey(getKeyByName(arg));
373 return 0;
374 }
375
376 if (function == "page") {
377 std::string page_name = gui_parse_text(arg);
378 return gui_changePage(page_name);
379 }
380
381 if (function == "reload") {
382 int check = 0, ret_val = 0;
383 std::string theme_path;
384
385 operation_start("Reload Theme");
386 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400388 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
389 check = 1;
390 }
391
392 theme_path += "/TWRP/theme/ui.zip";
393 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
394 {
395 // Loading the custom theme failed - try loading the stock theme
396 LOGI("Attempting to reload stock theme...\n");
397 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
398 {
399 LOGE("Failed to load base packages.\n");
400 ret_val = 1;
401 }
402 }
403 operation_end(ret_val, simulate);
404 }
405
406 if (function == "readBackup")
407 {
408 string Restore_Name;
409 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 return 0;
412 }
413
414 if (function == "set")
415 {
416 if (arg.find('=') != string::npos)
417 {
418 string varName = arg.substr(0, arg.find('='));
419 string value = arg.substr(arg.find('=') + 1, string::npos);
420
421 DataManager::GetValue(value, value);
422 DataManager::SetValue(varName, value);
423 }
424 else
425 DataManager::SetValue(arg, "1");
426 return 0;
427 }
428 if (function == "clear")
429 {
430 DataManager::SetValue(arg, "0");
431 return 0;
432 }
433
434 if (function == "mount")
435 {
436 if (arg == "usb")
437 {
438 DataManager::SetValue(TW_ACTION_BUSY, 1);
439 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400440 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 else
442 ui_print("Simulating actions...\n");
443 }
444 else if (!simulate)
445 {
446 string cmd;
447 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400448 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400450 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 else
Dees_Troy51127312012-09-08 13:08:49 -0400452 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 } else
454 ui_print("Simulating actions...\n");
455 return 0;
456 }
457
458 if (function == "umount" || function == "unmount")
459 {
460 if (arg == "usb")
461 {
462 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400463 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 else
465 ui_print("Simulating actions...\n");
466 DataManager::SetValue(TW_ACTION_BUSY, 0);
467 }
468 else if (!simulate)
469 {
470 string cmd;
471 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400472 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400474 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 else
Dees_Troy51127312012-09-08 13:08:49 -0400476 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 } else
478 ui_print("Simulating actions...\n");
479 return 0;
480 }
481
482 if (function == "restoredefaultsettings")
483 {
484 operation_start("Restore Defaults");
485 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
486 ui_print("Simulating actions...\n");
487 else {
488 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400489 PartitionManager.Update_System_Details();
490 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 }
492 operation_end(0, simulate);
493 }
494
495 if (function == "copylog")
496 {
497 operation_start("Copy Log");
498 if (!simulate)
499 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400501 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500502 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
503 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 sync();
505 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
506 } else
507 simulate_progress_bar();
508 operation_end(0, simulate);
509 return 0;
510 }
511
512 if (function == "compute" || function == "addsubtract")
513 {
514 if (arg.find("+") != string::npos)
515 {
516 string varName = arg.substr(0, arg.find('+'));
517 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
518 int amount_to_add = atoi(string_to_add.c_str());
519 int value;
520
521 DataManager::GetValue(varName, value);
522 DataManager::SetValue(varName, value + amount_to_add);
523 return 0;
524 }
525 if (arg.find("-") != string::npos)
526 {
527 string varName = arg.substr(0, arg.find('-'));
528 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
529 int amount_to_subtract = atoi(string_to_subtract.c_str());
530 int value;
531
532 DataManager::GetValue(varName, value);
533 value -= amount_to_subtract;
534 if (value <= 0)
535 value = 0;
536 DataManager::SetValue(varName, value);
537 return 0;
538 }
539 }
540
541 if (function == "setguitimezone")
542 {
543 string SelectedZone;
544 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
545 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
546 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
547
548 int dst;
549 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
550
551 string offset;
552 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
553
554 string NewTimeZone = Zone;
555 if (offset != "0")
556 NewTimeZone += ":" + offset;
557
558 if (dst != 0)
559 NewTimeZone += DSTZone;
560
561 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400562 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 return 0;
564 }
565
566 if (function == "togglestorage") {
567 if (arg == "internal") {
568 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
569 } else if (arg == "external") {
570 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
571 }
Dees_Troy51127312012-09-08 13:08:49 -0400572 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400573 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400574 string zip_path, zip_root;
575 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
576 zip_root = TWFunc::Get_Root_Path(zip_path);
577#ifdef RECOVERY_SDCARD_ON_DATA
578 #ifndef TW_EXTERNAL_STORAGE_PATH
579 if (zip_root != "/sdcard")
580 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
581 #else
582 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
583 if (zip_root != "/emmc")
584 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
585 } else {
586 if (zip_root != "/sdcard")
587 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
588 }
589 #endif
590#else
591 if (zip_root != DataManager::GetCurrentStoragePath())
592 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
593#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 // Save the current zip location to the external variable
595 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
596 // Change the current zip location to the internal variable
597 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
598 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400599 string zip_path, zip_root;
600 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
601 zip_root = TWFunc::Get_Root_Path(zip_path);
602 if (zip_root != DataManager::GetCurrentStoragePath()) {
603 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
604 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400605 // Save the current zip location to the internal variable
606 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
607 // Change the current zip location to the external variable
608 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
609 }
610 } else {
611 // We weren't able to toggle for some reason, restore original setting
612 if (arg == "internal") {
613 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
614 } else if (arg == "external") {
615 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
616 }
617 }
618 return 0;
619 }
620
621 if (function == "overlay")
622 return gui_changeOverlay(arg);
623
624 if (function == "queuezip")
625 {
626 if (zip_queue_index >= 10) {
627 ui_print("Maximum zip queue reached!\n");
628 return 0;
629 }
630 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
631 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
632 zip_queue_index++;
633 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
634 }
635 return 0;
636 }
637
638 if (function == "cancelzip")
639 {
640 if (zip_queue_index <= 0) {
641 ui_print("Minimum zip queue reached!\n");
642 return 0;
643 } else {
644 zip_queue_index--;
645 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
646 }
647 return 0;
648 }
649
650 if (function == "queueclear")
651 {
652 zip_queue_index = 0;
653 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
654 return 0;
655 }
656
657 if (function == "sleep")
658 {
659 operation_start("Sleep");
660 usleep(atoi(arg.c_str()));
661 operation_end(0, simulate);
662 return 0;
663 }
664
665 if (isThreaded)
666 {
667 if (function == "fileexists")
668 {
669 struct stat st;
670 string newpath = arg + "/.";
671
672 operation_start("FileExists");
673 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
674 operation_end(0, simulate);
675 else
676 operation_end(1, simulate);
677 }
678
679 if (function == "flash")
680 {
Dees_Troy657c3092012-09-10 20:32:10 -0400681 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400682
683 for (i=0; i<zip_queue_index; i++) {
684 operation_start("Flashing");
685 DataManager::SetValue("tw_filename", zip_queue[i]);
686 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
687
Dees_Troy657c3092012-09-10 20:32:10 -0400688 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400689 if (ret_val != 0) {
690 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
691 i = 10; // Error flashing zip - exit queue
692 ret_val = 1;
693 }
694 }
695 zip_queue_index = 0;
696 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
697
Dees_Troy657c3092012-09-10 20:32:10 -0400698 if (wipe_cache)
699 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500700 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
702 operation_start("ReinjectTWRP");
703 ui_print("Injecting TWRP into boot image...\n");
704 if (simulate) {
705 simulate_progress_bar();
706 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400707 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
708 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500709 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400710 else {
711 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 -0500712 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400713 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 ui_print("TWRP injection complete.\n");
715 }
716 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400717 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 operation_end(ret_val, simulate);
719 return 0;
720 }
721 if (function == "wipe")
722 {
723 operation_start("Format");
724 DataManager::SetValue("tw_partition", arg);
725
Dees_Troy38bd7602012-09-14 13:33:53 -0400726 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400727
728 if (simulate) {
729 simulate_progress_bar();
730 } else {
731 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400732 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400734 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400740 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 } else if (arg == "INTERNAL") {
742 int has_datamedia, dual_storage;
743
744 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
745 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400746 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400747 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400748 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 }
750 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400751 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400752
Dees_Troy38bd7602012-09-14 13:33:53 -0400753 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
754 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400755 } else if (arg == "ANDROIDSECURE") {
756 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400757 } else
758 ret_val = PartitionManager.Wipe_By_Path(arg);
759
760 if (arg == DataManager::GetSettingsStoragePath()) {
761 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
762 string Storage_Path = DataManager::GetSettingsStoragePath();
763
764 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
765 LOGI("Making TWRP folder and saving settings.\n");
766 Storage_Path += "/TWRP";
767 mkdir(Storage_Path.c_str(), 0777);
768 DataManager::Flush();
769 } else {
770 LOGE("Unable to recreate TWRP folder and save settings.\n");
771 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 }
773 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 if (ret_val)
776 ret_val = 0; // 0 is success
777 else
778 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400779 operation_end(ret_val, simulate);
780 return 0;
781 }
782 if (function == "refreshsizes")
783 {
784 operation_start("Refreshing Sizes");
785 if (simulate) {
786 simulate_progress_bar();
787 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400788 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400789 operation_end(0, simulate);
790 }
791 if (function == "nandroid")
792 {
793 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400794 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400795
796 if (simulate) {
797 DataManager::SetValue("tw_partition", "Simulation");
798 simulate_progress_bar();
799 } else {
800 if (arg == "backup") {
801 string Backup_Name;
802 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500803 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400804 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500805 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400806 else {
807 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400809 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
811 } else if (arg == "restore") {
812 string Restore_Name;
813 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400814 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400815 } else {
816 operation_end(1, simulate);
817 return -1;
818 }
819 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400820 if (ret == false)
821 ret = 1; // 1 for failure
822 else
823 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500824 operation_end(ret, simulate);
825 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826 }
827 if (function == "fixpermissions")
828 {
829 operation_start("Fix Permissions");
830 LOGI("fix permissions started!\n");
831 if (simulate) {
832 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400833 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400834 int op_status = PartitionManager.Fix_Permissions();
835 if (op_status != 0)
836 op_status = 1; // failure
837 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400838 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400839 return 0;
840 }
841 if (function == "dd")
842 {
843 operation_start("imaging");
844
845 if (simulate) {
846 simulate_progress_bar();
847 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500848 string result;
849 string cmd = "dd " + arg;
850 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400851 }
852 operation_end(0, simulate);
853 return 0;
854 }
855 if (function == "partitionsd")
856 {
857 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400858 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400859
860 if (simulate) {
861 simulate_progress_bar();
862 } else {
863 int allow_partition;
864 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
865 if (allow_partition == 0) {
866 ui_print("This device does not have a real SD Card!\nAborting!\n");
867 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400868 if (!PartitionManager.Partition_SDCard())
869 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400870 }
871 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400872 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400873 return 0;
874 }
875 if (function == "installhtcdumlock")
876 {
877 operation_start("Install HTC Dumlock");
878 if (simulate) {
879 simulate_progress_bar();
880 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400881 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400882
883 operation_end(0, simulate);
884 return 0;
885 }
886 if (function == "htcdumlockrestoreboot")
887 {
888 operation_start("HTC Dumlock Restore Boot");
889 if (simulate) {
890 simulate_progress_bar();
891 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400892 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400893
894 operation_end(0, simulate);
895 return 0;
896 }
897 if (function == "htcdumlockreflashrecovery")
898 {
899 operation_start("HTC Dumlock Reflash Recovery");
900 if (simulate) {
901 simulate_progress_bar();
902 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400903 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400904
905 operation_end(0, simulate);
906 return 0;
907 }
908 if (function == "cmd")
909 {
910 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500911 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400912
913 operation_start("Command");
914 LOGI("Running command: '%s'\n", arg.c_str());
915 if (simulate) {
916 simulate_progress_bar();
917 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500918 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400919 if (op_status != 0)
920 op_status = 1;
921 }
922
923 operation_end(op_status, simulate);
924 return 0;
925 }
926 if (function == "terminalcommand")
927 {
928 int op_status = 0;
929 string cmdpath, command;
930
931 DataManager::GetValue("tw_terminal_location", cmdpath);
932 operation_start("CommandOutput");
933 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
934 if (simulate) {
935 simulate_progress_bar();
936 operation_end(op_status, simulate);
937 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400938 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400939 LOGI("Actual command is: '%s'\n", command.c_str());
940 DataManager::SetValue("tw_terminal_command_thread", command);
941 DataManager::SetValue("tw_terminal_state", 1);
942 DataManager::SetValue("tw_background_thread_running", 1);
943 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
944 if (op_status != 0) {
945 LOGE("Error starting terminal command thread, %i.\n", op_status);
946 DataManager::SetValue("tw_terminal_state", 0);
947 DataManager::SetValue("tw_background_thread_running", 0);
948 operation_end(1, simulate);
949 }
950 }
951 return 0;
952 }
953 if (function == "killterminal")
954 {
955 int op_status = 0;
956
957 LOGI("Sending kill command...\n");
958 operation_start("KillCommand");
959 DataManager::SetValue("tw_operation_status", 0);
960 DataManager::SetValue("tw_operation_state", 1);
961 DataManager::SetValue("tw_terminal_state", 0);
962 DataManager::SetValue("tw_background_thread_running", 0);
963 DataManager::SetValue(TW_ACTION_BUSY, 0);
964 return 0;
965 }
966 if (function == "reinjecttwrp")
967 {
968 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500969 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400970 operation_start("ReinjectTWRP");
971 ui_print("Injecting TWRP into boot image...\n");
972 if (simulate) {
973 simulate_progress_bar();
974 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500975 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400976 ui_print("TWRP injection complete.\n");
977 }
978
979 operation_end(op_status, simulate);
980 return 0;
981 }
982 if (function == "checkbackupname")
983 {
984 int op_status = 0;
985
986 operation_start("CheckBackupName");
987 if (simulate) {
988 simulate_progress_bar();
989 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400990 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400991 if (op_status != 0)
992 op_status = 1;
993 }
994
995 operation_end(op_status, simulate);
996 return 0;
997 }
998 if (function == "decrypt")
999 {
1000 int op_status = 0;
1001
1002 operation_start("Decrypt");
1003 if (simulate) {
1004 simulate_progress_bar();
1005 } else {
1006 string Password;
1007 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001008 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001009 if (op_status != 0)
1010 op_status = 1;
1011 else {
1012 int load_theme = 1;
1013
1014 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1015 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -04001016 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001017 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -04001018 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 usleep(2000000); // Sleep for 2 seconds before rebooting
Dees_Troya58bead2012-09-27 09:49:29 -04001020 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -04001021 load_theme = 0;
1022 }
1023 }
1024
1025 if (load_theme) {
1026 int has_datamedia;
1027
1028 // Check for a custom theme and load it if exists
1029 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1030 if (has_datamedia != 0) {
1031 struct stat st;
1032 int check = 0;
1033 std::string theme_path;
1034
1035 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001036 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001037 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1038 check = 1;
1039 }
1040
1041 theme_path += "/TWRP/theme/ui.zip";
1042 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1043 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1044 {
1045 // Loading the custom theme failed - try loading the stock theme
1046 LOGI("Attempting to reload stock theme...\n");
1047 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1048 {
1049 LOGE("Failed to load base packages.\n");
1050 }
1051 }
1052 }
1053 }
1054 }
1055 }
1056 }
1057
1058 operation_end(op_status, simulate);
1059 return 0;
1060 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001061 if (function == "adbsideload")
1062 {
1063 int ret = 0;
1064
1065 operation_start("Sideload");
1066 if (simulate) {
1067 simulate_progress_bar();
1068 } else {
1069 int wipe_cache = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001070 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001071
1072 if (!PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001073 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001074 operation_end(1, simulate);
1075 return 0;
1076 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001077 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1078 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001079 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001080 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001081 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001082 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001083 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001084 ret = 1; // failure
1085 else if (wipe_cache)
1086 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001087 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1088 operation_start("ReinjectTWRP");
1089 ui_print("Injecting TWRP into boot image...\n");
1090 if (simulate) {
1091 simulate_progress_bar();
1092 } else {
1093 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1094 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001095 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001096 else {
1097 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 -05001098 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001099 }
1100 ui_print("TWRP injection complete.\n");
1101 }
1102 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001103 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001104 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troy43d8b002012-09-17 16:00:01 -04001105 operation_end(ret, simulate);
1106 return 0;
1107 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001108 if (function == "adbsideloadcancel")
1109 {
1110 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001111 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001112 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001113 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001114 DataManager::GetValue("tw_child_pid", child_pid);
1115 ui_print("Cancelling ADB sideload...\n");
1116 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001117 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001118 return 0;
1119 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001120 }
1121 else
1122 {
1123 pthread_t t;
1124 pthread_create(&t, NULL, thread_start, this);
1125 return 0;
1126 }
1127 return -1;
1128}
1129
1130int GUIAction::getKeyByName(std::string key)
1131{
1132 if (key == "home") return KEY_HOME;
1133 else if (key == "menu") return KEY_MENU;
1134 else if (key == "back") return KEY_BACK;
1135 else if (key == "search") return KEY_SEARCH;
1136 else if (key == "voldown") return KEY_VOLUMEDOWN;
1137 else if (key == "volup") return KEY_VOLUMEUP;
1138 else if (key == "power") {
1139 int ret_val;
1140 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1141 if (!ret_val)
1142 return KEY_POWER;
1143 else
1144 return ret_val;
1145 }
1146
1147 return atol(key.c_str());
1148}
1149
1150void* GUIAction::command_thread(void *cookie)
1151{
1152 string command;
1153 FILE* fp;
1154 char line[512];
1155
1156 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001157 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001158 if (fp == NULL) {
1159 LOGE("Error opening command to run.\n");
1160 } else {
1161 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1162 struct timeval timeout;
1163 fd_set fdset;
1164
1165 while(keep_going)
1166 {
1167 FD_ZERO(&fdset);
1168 FD_SET(fd, &fdset);
1169 timeout.tv_sec = 0;
1170 timeout.tv_usec = 400000;
1171 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1172 if (has_data == 0) {
1173 // Timeout reached
1174 DataManager::GetValue("tw_terminal_state", check);
1175 if (check == 0) {
1176 keep_going = 0;
1177 }
1178 } else if (has_data < 0) {
1179 // End of execution
1180 keep_going = 0;
1181 } else {
1182 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001183 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001184 bytes_read = read(fd, line, sizeof(line));
1185 if (bytes_read > 0)
1186 ui_print("%s", line); // Display output
1187 else
1188 keep_going = 0; // Done executing
1189 }
1190 }
1191 fclose(fp);
1192 }
1193 DataManager::SetValue("tw_operation_status", 0);
1194 DataManager::SetValue("tw_operation_state", 1);
1195 DataManager::SetValue("tw_terminal_state", 0);
1196 DataManager::SetValue("tw_background_thread_running", 0);
1197 DataManager::SetValue(TW_ACTION_BUSY, 0);
1198 return NULL;
1199}