blob: 22e362177ae0968b73d8dd2c597138b2b46304e5 [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 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500252 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troyab4963c2013-01-16 20:35:51 +0000253 LOGE("Error setting pthread_attr_setstacksize\n");
254 return -1;
255 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500256 */
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000257 LOGI("Creating thread\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000258 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000259 if (ret) {
260 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
261 thread_start(this);
262 } else {
263 if (pthread_join(t, NULL)) {
264 LOGE("Error joining threads\n");
265 } else {
266 LOGI("Thread joined\n");
267 }
268 }
269 if (pthread_attr_destroy(&tattr)) {
270 LOGE("Failed to pthread_attr_destroy\n");
271 return -1;
272 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400273
274 return 0;
275}
276
277void* GUIAction::thread_start(void *cookie)
278{
279 GUIAction* ourThis = (GUIAction*) cookie;
280
281 DataManager::SetValue(TW_ACTION_BUSY, 1);
282
283 if (ourThis->mActions.size() > 1)
284 {
285 std::vector<Action>::iterator iter;
286 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
287 ourThis->doAction(*iter, 1);
288 }
289 else
290 {
291 ourThis->doAction(ourThis->mActions.at(0), 1);
292 }
293 int check = 0;
294 DataManager::GetValue("tw_background_thread_running", check);
295 if (check == 0)
296 DataManager::SetValue(TW_ACTION_BUSY, 0);
297 return NULL;
298}
299
300void GUIAction::operation_start(const string operation_name)
301{
302 DataManager::SetValue(TW_ACTION_BUSY, 1);
303 DataManager::SetValue("ui_progress", 0);
304 DataManager::SetValue("tw_operation", operation_name);
305 DataManager::SetValue("tw_operation_status", 0);
306 DataManager::SetValue("tw_operation_state", 0);
307}
308
309void GUIAction::operation_end(const int operation_status, const int simulate)
310{
311 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400312 DataManager::SetValue("ui_progress", 100);
313 if (simulate) {
314 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
315 if (simulate_fail != 0)
316 DataManager::SetValue("tw_operation_status", 1);
317 else
318 DataManager::SetValue("tw_operation_status", 0);
319 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500320 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500322 }
323 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 }
327 DataManager::SetValue("tw_operation_state", 1);
328 DataManager::SetValue(TW_ACTION_BUSY, 0);
329}
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
344 if (function == "reboot")
345 {
346 //curtainClose(); this sometimes causes a crash
347
348 sync();
349
Dees_Troya58bead2012-09-27 09:49:29 -0400350 if (arg == "recovery")
351 TWFunc::tw_reboot(rb_recovery);
352 else if (arg == "poweroff")
353 TWFunc::tw_reboot(rb_poweroff);
354 else if (arg == "bootloader")
355 TWFunc::tw_reboot(rb_bootloader);
356 else if (arg == "download")
357 TWFunc::tw_reboot(rb_download);
358 else
359 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400360
361 // This should never occur
362 return -1;
363 }
364 if (function == "home")
365 {
366 PageManager::SelectPackage("TWRP");
367 gui_changePage("main");
368 return 0;
369 }
370
371 if (function == "key")
372 {
373 PageManager::NotifyKey(getKeyByName(arg));
374 return 0;
375 }
376
377 if (function == "page") {
378 std::string page_name = gui_parse_text(arg);
379 return gui_changePage(page_name);
380 }
381
382 if (function == "reload") {
383 int check = 0, ret_val = 0;
384 std::string theme_path;
385
386 operation_start("Reload Theme");
387 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400389 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
390 check = 1;
391 }
392
393 theme_path += "/TWRP/theme/ui.zip";
394 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
395 {
396 // Loading the custom theme failed - try loading the stock theme
397 LOGI("Attempting to reload stock theme...\n");
398 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
399 {
400 LOGE("Failed to load base packages.\n");
401 ret_val = 1;
402 }
403 }
404 operation_end(ret_val, simulate);
405 }
406
407 if (function == "readBackup")
408 {
409 string Restore_Name;
410 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400411 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 return 0;
413 }
414
415 if (function == "set")
416 {
417 if (arg.find('=') != string::npos)
418 {
419 string varName = arg.substr(0, arg.find('='));
420 string value = arg.substr(arg.find('=') + 1, string::npos);
421
422 DataManager::GetValue(value, value);
423 DataManager::SetValue(varName, value);
424 }
425 else
426 DataManager::SetValue(arg, "1");
427 return 0;
428 }
429 if (function == "clear")
430 {
431 DataManager::SetValue(arg, "0");
432 return 0;
433 }
434
435 if (function == "mount")
436 {
437 if (arg == "usb")
438 {
439 DataManager::SetValue(TW_ACTION_BUSY, 1);
440 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400441 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 else
443 ui_print("Simulating actions...\n");
444 }
445 else if (!simulate)
446 {
447 string cmd;
448 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400449 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400451 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 else
Dees_Troy51127312012-09-08 13:08:49 -0400453 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 } else
455 ui_print("Simulating actions...\n");
456 return 0;
457 }
458
459 if (function == "umount" || function == "unmount")
460 {
461 if (arg == "usb")
462 {
463 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400464 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400465 else
466 ui_print("Simulating actions...\n");
467 DataManager::SetValue(TW_ACTION_BUSY, 0);
468 }
469 else if (!simulate)
470 {
471 string cmd;
472 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400473 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400475 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 else
Dees_Troy51127312012-09-08 13:08:49 -0400477 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 } else
479 ui_print("Simulating actions...\n");
480 return 0;
481 }
482
483 if (function == "restoredefaultsettings")
484 {
485 operation_start("Restore Defaults");
486 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
487 ui_print("Simulating actions...\n");
488 else {
489 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 PartitionManager.Update_System_Details();
491 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 }
493 operation_end(0, simulate);
494 }
495
496 if (function == "copylog")
497 {
498 operation_start("Copy Log");
499 if (!simulate)
500 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500501 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400502 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500503 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
504 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 sync();
506 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
507 } else
508 simulate_progress_bar();
509 operation_end(0, simulate);
510 return 0;
511 }
512
513 if (function == "compute" || function == "addsubtract")
514 {
515 if (arg.find("+") != string::npos)
516 {
517 string varName = arg.substr(0, arg.find('+'));
518 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
519 int amount_to_add = atoi(string_to_add.c_str());
520 int value;
521
522 DataManager::GetValue(varName, value);
523 DataManager::SetValue(varName, value + amount_to_add);
524 return 0;
525 }
526 if (arg.find("-") != string::npos)
527 {
528 string varName = arg.substr(0, arg.find('-'));
529 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
530 int amount_to_subtract = atoi(string_to_subtract.c_str());
531 int value;
532
533 DataManager::GetValue(varName, value);
534 value -= amount_to_subtract;
535 if (value <= 0)
536 value = 0;
537 DataManager::SetValue(varName, value);
538 return 0;
539 }
540 }
541
542 if (function == "setguitimezone")
543 {
544 string SelectedZone;
545 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
546 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
547 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
548
549 int dst;
550 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
551
552 string offset;
553 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
554
555 string NewTimeZone = Zone;
556 if (offset != "0")
557 NewTimeZone += ":" + offset;
558
559 if (dst != 0)
560 NewTimeZone += DSTZone;
561
562 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400563 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 return 0;
565 }
566
567 if (function == "togglestorage") {
568 if (arg == "internal") {
569 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
570 } else if (arg == "external") {
571 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
572 }
Dees_Troy51127312012-09-08 13:08:49 -0400573 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400574 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400575 string zip_path, zip_root;
576 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
577 zip_root = TWFunc::Get_Root_Path(zip_path);
578#ifdef RECOVERY_SDCARD_ON_DATA
579 #ifndef TW_EXTERNAL_STORAGE_PATH
580 if (zip_root != "/sdcard")
581 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
582 #else
583 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
584 if (zip_root != "/emmc")
585 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
586 } else {
587 if (zip_root != "/sdcard")
588 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
589 }
590 #endif
591#else
592 if (zip_root != DataManager::GetCurrentStoragePath())
593 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
594#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 // Save the current zip location to the external variable
596 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
597 // Change the current zip location to the internal variable
598 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
599 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400600 string zip_path, zip_root;
601 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
602 zip_root = TWFunc::Get_Root_Path(zip_path);
603 if (zip_root != DataManager::GetCurrentStoragePath()) {
604 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
605 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 // Save the current zip location to the internal variable
607 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
608 // Change the current zip location to the external variable
609 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
610 }
611 } else {
612 // We weren't able to toggle for some reason, restore original setting
613 if (arg == "internal") {
614 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
615 } else if (arg == "external") {
616 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
617 }
618 }
619 return 0;
620 }
621
622 if (function == "overlay")
623 return gui_changeOverlay(arg);
624
625 if (function == "queuezip")
626 {
627 if (zip_queue_index >= 10) {
628 ui_print("Maximum zip queue reached!\n");
629 return 0;
630 }
631 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
632 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
633 zip_queue_index++;
634 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
635 }
636 return 0;
637 }
638
639 if (function == "cancelzip")
640 {
641 if (zip_queue_index <= 0) {
642 ui_print("Minimum zip queue reached!\n");
643 return 0;
644 } else {
645 zip_queue_index--;
646 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
647 }
648 return 0;
649 }
650
651 if (function == "queueclear")
652 {
653 zip_queue_index = 0;
654 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
655 return 0;
656 }
657
658 if (function == "sleep")
659 {
660 operation_start("Sleep");
661 usleep(atoi(arg.c_str()));
662 operation_end(0, simulate);
663 return 0;
664 }
665
666 if (isThreaded)
667 {
668 if (function == "fileexists")
669 {
670 struct stat st;
671 string newpath = arg + "/.";
672
673 operation_start("FileExists");
674 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
675 operation_end(0, simulate);
676 else
677 operation_end(1, simulate);
678 }
679
680 if (function == "flash")
681 {
Dees_Troy657c3092012-09-10 20:32:10 -0400682 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400683
684 for (i=0; i<zip_queue_index; i++) {
685 operation_start("Flashing");
686 DataManager::SetValue("tw_filename", zip_queue[i]);
687 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
688
Dees_Troy657c3092012-09-10 20:32:10 -0400689 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 if (ret_val != 0) {
691 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
692 i = 10; // Error flashing zip - exit queue
693 ret_val = 1;
694 }
695 }
696 zip_queue_index = 0;
697 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
698
Dees_Troy657c3092012-09-10 20:32:10 -0400699 if (wipe_cache)
700 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500701 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
703 operation_start("ReinjectTWRP");
704 ui_print("Injecting TWRP into boot image...\n");
705 if (simulate) {
706 simulate_progress_bar();
707 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400708 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
709 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500710 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400711 else {
712 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 -0500713 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400714 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 ui_print("TWRP injection complete.\n");
716 }
717 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400718 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 operation_end(ret_val, simulate);
720 return 0;
721 }
722 if (function == "wipe")
723 {
724 operation_start("Format");
725 DataManager::SetValue("tw_partition", arg);
726
Dees_Troy38bd7602012-09-14 13:33:53 -0400727 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400728
729 if (simulate) {
730 simulate_progress_bar();
731 } else {
732 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400733 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400735 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400741 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 } else if (arg == "INTERNAL") {
743 int has_datamedia, dual_storage;
744
745 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
746 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400747 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400748 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400749 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 }
751 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400752 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400753
Dees_Troy38bd7602012-09-14 13:33:53 -0400754 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
755 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400756 } else if (arg == "ANDROIDSECURE") {
757 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 } else
759 ret_val = PartitionManager.Wipe_By_Path(arg);
760
761 if (arg == DataManager::GetSettingsStoragePath()) {
762 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
763 string Storage_Path = DataManager::GetSettingsStoragePath();
764
765 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
766 LOGI("Making TWRP folder and saving settings.\n");
767 Storage_Path += "/TWRP";
768 mkdir(Storage_Path.c_str(), 0777);
769 DataManager::Flush();
770 } else {
771 LOGE("Unable to recreate TWRP folder and save settings.\n");
772 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 }
774 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400776 if (ret_val)
777 ret_val = 0; // 0 is success
778 else
779 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 operation_end(ret_val, simulate);
781 return 0;
782 }
783 if (function == "refreshsizes")
784 {
785 operation_start("Refreshing Sizes");
786 if (simulate) {
787 simulate_progress_bar();
788 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400789 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 operation_end(0, simulate);
791 }
792 if (function == "nandroid")
793 {
794 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400795 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400796
797 if (simulate) {
798 DataManager::SetValue("tw_partition", "Simulation");
799 simulate_progress_bar();
800 } else {
801 if (arg == "backup") {
802 string Backup_Name;
803 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500804 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400805 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500806 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400807 else {
808 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400810 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
812 } else if (arg == "restore") {
813 string Restore_Name;
814 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400815 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400816 } else {
817 operation_end(1, simulate);
818 return -1;
819 }
820 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400821 if (ret == false)
822 ret = 1; // 1 for failure
823 else
824 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500825 operation_end(ret, simulate);
826 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400827 }
828 if (function == "fixpermissions")
829 {
830 operation_start("Fix Permissions");
831 LOGI("fix permissions started!\n");
832 if (simulate) {
833 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400834 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400835 int op_status = PartitionManager.Fix_Permissions();
836 if (op_status != 0)
837 op_status = 1; // failure
838 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400839 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400840 return 0;
841 }
842 if (function == "dd")
843 {
844 operation_start("imaging");
845
846 if (simulate) {
847 simulate_progress_bar();
848 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500849 string result;
850 string cmd = "dd " + arg;
851 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400852 }
853 operation_end(0, simulate);
854 return 0;
855 }
856 if (function == "partitionsd")
857 {
858 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400859 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400860
861 if (simulate) {
862 simulate_progress_bar();
863 } else {
864 int allow_partition;
865 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
866 if (allow_partition == 0) {
867 ui_print("This device does not have a real SD Card!\nAborting!\n");
868 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400869 if (!PartitionManager.Partition_SDCard())
870 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400871 }
872 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400873 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400874 return 0;
875 }
876 if (function == "installhtcdumlock")
877 {
878 operation_start("Install HTC Dumlock");
879 if (simulate) {
880 simulate_progress_bar();
881 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400882 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400883
884 operation_end(0, simulate);
885 return 0;
886 }
887 if (function == "htcdumlockrestoreboot")
888 {
889 operation_start("HTC Dumlock Restore Boot");
890 if (simulate) {
891 simulate_progress_bar();
892 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400893 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400894
895 operation_end(0, simulate);
896 return 0;
897 }
898 if (function == "htcdumlockreflashrecovery")
899 {
900 operation_start("HTC Dumlock Reflash Recovery");
901 if (simulate) {
902 simulate_progress_bar();
903 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400904 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400905
906 operation_end(0, simulate);
907 return 0;
908 }
909 if (function == "cmd")
910 {
911 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500912 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400913
914 operation_start("Command");
915 LOGI("Running command: '%s'\n", arg.c_str());
916 if (simulate) {
917 simulate_progress_bar();
918 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500919 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400920 if (op_status != 0)
921 op_status = 1;
922 }
923
924 operation_end(op_status, simulate);
925 return 0;
926 }
927 if (function == "terminalcommand")
928 {
929 int op_status = 0;
930 string cmdpath, command;
931
932 DataManager::GetValue("tw_terminal_location", cmdpath);
933 operation_start("CommandOutput");
934 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
935 if (simulate) {
936 simulate_progress_bar();
937 operation_end(op_status, simulate);
938 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400939 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400940 LOGI("Actual command is: '%s'\n", command.c_str());
941 DataManager::SetValue("tw_terminal_command_thread", command);
942 DataManager::SetValue("tw_terminal_state", 1);
943 DataManager::SetValue("tw_background_thread_running", 1);
944 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
945 if (op_status != 0) {
946 LOGE("Error starting terminal command thread, %i.\n", op_status);
947 DataManager::SetValue("tw_terminal_state", 0);
948 DataManager::SetValue("tw_background_thread_running", 0);
949 operation_end(1, simulate);
950 }
951 }
952 return 0;
953 }
954 if (function == "killterminal")
955 {
956 int op_status = 0;
957
958 LOGI("Sending kill command...\n");
959 operation_start("KillCommand");
960 DataManager::SetValue("tw_operation_status", 0);
961 DataManager::SetValue("tw_operation_state", 1);
962 DataManager::SetValue("tw_terminal_state", 0);
963 DataManager::SetValue("tw_background_thread_running", 0);
964 DataManager::SetValue(TW_ACTION_BUSY, 0);
965 return 0;
966 }
967 if (function == "reinjecttwrp")
968 {
969 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500970 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400971 operation_start("ReinjectTWRP");
972 ui_print("Injecting TWRP into boot image...\n");
973 if (simulate) {
974 simulate_progress_bar();
975 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500976 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400977 ui_print("TWRP injection complete.\n");
978 }
979
980 operation_end(op_status, simulate);
981 return 0;
982 }
983 if (function == "checkbackupname")
984 {
985 int op_status = 0;
986
987 operation_start("CheckBackupName");
988 if (simulate) {
989 simulate_progress_bar();
990 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400991 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 if (op_status != 0)
993 op_status = 1;
994 }
995
996 operation_end(op_status, simulate);
997 return 0;
998 }
999 if (function == "decrypt")
1000 {
1001 int op_status = 0;
1002
1003 operation_start("Decrypt");
1004 if (simulate) {
1005 simulate_progress_bar();
1006 } else {
1007 string Password;
1008 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001009 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 if (op_status != 0)
1011 op_status = 1;
1012 else {
1013 int load_theme = 1;
1014
1015 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001016
1017 if (load_theme) {
1018 int has_datamedia;
1019
1020 // Check for a custom theme and load it if exists
1021 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1022 if (has_datamedia != 0) {
1023 struct stat st;
1024 int check = 0;
1025 std::string theme_path;
1026
1027 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001028 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001029 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1030 check = 1;
1031 }
1032
1033 theme_path += "/TWRP/theme/ui.zip";
1034 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1035 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1036 {
1037 // Loading the custom theme failed - try loading the stock theme
1038 LOGI("Attempting to reload stock theme...\n");
1039 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1040 {
1041 LOGE("Failed to load base packages.\n");
1042 }
1043 }
1044 }
1045 }
1046 }
1047 }
1048 }
1049
1050 operation_end(op_status, simulate);
1051 return 0;
1052 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001053 if (function == "adbsideload")
1054 {
1055 int ret = 0;
1056
1057 operation_start("Sideload");
1058 if (simulate) {
1059 simulate_progress_bar();
1060 } else {
1061 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001062 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001063 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001064
1065 if (!PartitionManager.Mount_Current_Storage(true)) {
1066 operation_end(1, simulate);
1067 return 0;
1068 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001069 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1070 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001071 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001072 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001073 ui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001074 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy9a4b5692012-09-19 15:09:45 -04001075 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001076 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001077 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001078 } else {
1079 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1080 PartitionManager.Wipe_By_Path("/cache");
1081 if (wipe_dalvik)
1082 PartitionManager.Wipe_Dalvik_Cache();
1083 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001084 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1085 operation_start("ReinjectTWRP");
1086 ui_print("Injecting TWRP into boot image...\n");
1087 if (simulate) {
1088 simulate_progress_bar();
1089 } else {
1090 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1091 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001092 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001093 else {
1094 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 -05001095 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001096 }
1097 ui_print("TWRP injection complete.\n");
1098 }
1099 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001100 }
1101 operation_end(ret, simulate);
1102 return 0;
1103 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001104 if (function == "adbsideloadcancel")
1105 {
1106 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001107 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001108 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001109 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001110 DataManager::GetValue("tw_child_pid", child_pid);
1111 ui_print("Cancelling ADB sideload...\n");
1112 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001113 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001114 return 0;
1115 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001116 if (function == "openrecoveryscript") {
1117 operation_start("OpenRecoveryScript");
1118 if (simulate) {
1119 simulate_progress_bar();
1120 } else {
1121 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1122 // that we converted to ORS commands during boot in recovery.cpp.
1123 // Run those first.
1124 int reboot = 0;
1125 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1126 ui_print("Processing AOSP recovery commands...\n");
1127 if (OpenRecoveryScript::run_script_file() == 0) {
1128 reboot = 1;
1129 }
1130 }
1131 // Check for the ORS file in /cache and attempt to run those commands.
1132 if (OpenRecoveryScript::check_for_script_file()) {
1133 ui_print("Processing OpenRecoveryScript file...\n");
1134 if (OpenRecoveryScript::run_script_file() == 0) {
1135 reboot = 1;
1136 }
1137 }
1138 if (reboot) {
1139 usleep(2000000); // Sleep for 2 seconds before rebooting
1140 TWFunc::tw_reboot(rb_system);
1141 } else {
1142 DataManager::SetValue("tw_page_done", 1);
1143 }
1144 }
1145 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001146 }
1147 else
1148 {
1149 pthread_t t;
1150 pthread_create(&t, NULL, thread_start, this);
1151 return 0;
1152 }
1153 return -1;
1154}
1155
1156int GUIAction::getKeyByName(std::string key)
1157{
1158 if (key == "home") return KEY_HOME;
1159 else if (key == "menu") return KEY_MENU;
1160 else if (key == "back") return KEY_BACK;
1161 else if (key == "search") return KEY_SEARCH;
1162 else if (key == "voldown") return KEY_VOLUMEDOWN;
1163 else if (key == "volup") return KEY_VOLUMEUP;
1164 else if (key == "power") {
1165 int ret_val;
1166 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1167 if (!ret_val)
1168 return KEY_POWER;
1169 else
1170 return ret_val;
1171 }
1172
1173 return atol(key.c_str());
1174}
1175
1176void* GUIAction::command_thread(void *cookie)
1177{
1178 string command;
1179 FILE* fp;
1180 char line[512];
1181
1182 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001183 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001184 if (fp == NULL) {
1185 LOGE("Error opening command to run.\n");
1186 } else {
1187 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1188 struct timeval timeout;
1189 fd_set fdset;
1190
1191 while(keep_going)
1192 {
1193 FD_ZERO(&fdset);
1194 FD_SET(fd, &fdset);
1195 timeout.tv_sec = 0;
1196 timeout.tv_usec = 400000;
1197 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1198 if (has_data == 0) {
1199 // Timeout reached
1200 DataManager::GetValue("tw_terminal_state", check);
1201 if (check == 0) {
1202 keep_going = 0;
1203 }
1204 } else if (has_data < 0) {
1205 // End of execution
1206 keep_going = 0;
1207 } else {
1208 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001209 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001210 bytes_read = read(fd, line, sizeof(line));
1211 if (bytes_read > 0)
1212 ui_print("%s", line); // Display output
1213 else
1214 keep_going = 0; // Done executing
1215 }
1216 }
1217 fclose(fp);
1218 }
1219 DataManager::SetValue("tw_operation_status", 0);
1220 DataManager::SetValue("tw_operation_state", 1);
1221 DataManager::SetValue("tw_terminal_state", 0);
1222 DataManager::SetValue("tw_background_thread_running", 0);
1223 DataManager::SetValue(TW_ACTION_BUSY, 0);
1224 return NULL;
1225}