blob: 4c3d88ed35a97a7512fe6acdbd36719d35168907 [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_Troy5bf43922012-09-07 16:07:55 -040032#include "../extra-functions.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;
48
Dees_Troy51a0e822012-09-05 15:24:24 -040049void curtainClose(void);
50
51GUIAction::GUIAction(xml_node<>* node)
52 : Conditional(node)
53{
54 xml_node<>* child;
55 xml_node<>* actions;
56 xml_attribute<>* attr;
57
58 mKey = 0;
59
60 if (!node) return;
61
62 // First, get the action
63 actions = node->first_node("actions");
64 if (actions) child = actions->first_node("action");
65 else child = node->first_node("action");
66
67 if (!child) return;
68
69 while (child)
70 {
71 Action action;
72
73 attr = child->first_attribute("function");
74 if (!attr) return;
75
76 action.mFunction = attr->value();
77 action.mArg = child->value();
78 mActions.push_back(action);
79
80 child = child->next_sibling("action");
81 }
82
83 // Now, let's get either the key or region
84 child = node->first_node("touch");
85 if (child)
86 {
87 attr = child->first_attribute("key");
88 if (attr)
89 {
90 std::string key = attr->value();
91
92 mKey = getKeyByName(key);
93 }
94 else
95 {
96 attr = child->first_attribute("x");
97 if (!attr) return;
98 mActionX = atol(attr->value());
99 attr = child->first_attribute("y");
100 if (!attr) return;
101 mActionY = atol(attr->value());
102 attr = child->first_attribute("w");
103 if (!attr) return;
104 mActionW = atol(attr->value());
105 attr = child->first_attribute("h");
106 if (!attr) return;
107 mActionH = atol(attr->value());
108 }
109 }
110}
111
112int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
113{
114 if (state == TOUCH_RELEASE)
115 doActions();
116
117 return 0;
118}
119
120int GUIAction::NotifyKey(int key)
121{
122 if (!mKey || key != mKey)
123 return 1;
124
125 doActions();
126 return 0;
127}
128
129int GUIAction::NotifyVarChange(std::string varName, std::string value)
130{
131 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
132 doActions();
133
134 // This handles notifying the condition system of page start
135 if (varName.empty() && isConditionValid())
136 NotifyPageSet();
137
138 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
139 doActions();
140
141 return 0;
142}
143
144void GUIAction::simulate_progress_bar(void)
145{
146 ui_print("Simulating actions...\n");
147 for (int i = 0; i < 5; i++)
148 {
149 usleep(500000);
150 DataManager::SetValue("ui_progress", i * 20);
151 }
152}
153
Dees_Troy657c3092012-09-10 20:32:10 -0400154int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400155{
156 int ret_val = 0;
157
158 DataManager::SetValue("ui_progress", 0);
159
160 if (filename.empty())
161 {
162 LOGE("No file specified.\n");
163 return -1;
164 }
165
166 // We're going to jump to this page first, like a loading page
167 gui_changePage(pageName);
168
169 int fd = -1;
170 ZipArchive zip;
171
Dees_Troy657c3092012-09-10 20:32:10 -0400172 if (!PartitionManager.Mount_By_Path(filename, true))
173 return -1;
174
175 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400176 {
177 LOGE("Unable to open zip file.\n");
178 return -1;
179 }
180
181 // Check the zip to see if it has a custom installer theme
182 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
183 if (twrp != NULL)
184 {
185 unlink("/tmp/twrp.zip");
186 fd = creat("/tmp/twrp.zip", 0666);
187 }
188 if (fd >= 0 && twrp != NULL &&
189 mzExtractZipEntryToFile(&zip, twrp, fd) &&
190 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
191 {
192 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400193 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 gui_changePage("main");
195 }
196 else
197 {
198 // In this case, we just use the default page
199 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400200 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400201 }
202 if (fd >= 0)
203 close(fd);
204
205 if (simulate) {
206 simulate_progress_bar();
207 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400208 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
210 // Now, check if we need to ensure TWRP remains installed...
211 struct stat st;
212 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");
Dees_Troy8170a922012-09-18 15:40:25 -0400217 if (system("/sbin/installTwrp reinstall") < 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;
238 pthread_create(&t, NULL, thread_start, this);
239
240 return 0;
241}
242
243void* GUIAction::thread_start(void *cookie)
244{
245 GUIAction* ourThis = (GUIAction*) cookie;
246
247 DataManager::SetValue(TW_ACTION_BUSY, 1);
248
249 if (ourThis->mActions.size() > 1)
250 {
251 std::vector<Action>::iterator iter;
252 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
253 ourThis->doAction(*iter, 1);
254 }
255 else
256 {
257 ourThis->doAction(ourThis->mActions.at(0), 1);
258 }
259 int check = 0;
260 DataManager::GetValue("tw_background_thread_running", check);
261 if (check == 0)
262 DataManager::SetValue(TW_ACTION_BUSY, 0);
263 return NULL;
264}
265
266void GUIAction::operation_start(const string operation_name)
267{
268 DataManager::SetValue(TW_ACTION_BUSY, 1);
269 DataManager::SetValue("ui_progress", 0);
270 DataManager::SetValue("tw_operation", operation_name);
271 DataManager::SetValue("tw_operation_status", 0);
272 DataManager::SetValue("tw_operation_state", 0);
273}
274
275void GUIAction::operation_end(const int operation_status, const int simulate)
276{
277 int simulate_fail;
278
279 DataManager::SetValue("ui_progress", 100);
280 if (simulate) {
281 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
282 if (simulate_fail != 0)
283 DataManager::SetValue("tw_operation_status", 1);
284 else
285 DataManager::SetValue("tw_operation_status", 0);
286 } else {
287 if (operation_status != 0)
288 DataManager::SetValue("tw_operation_status", 1);
289 else
290 DataManager::SetValue("tw_operation_status", 0);
291 }
292 DataManager::SetValue("tw_operation_state", 1);
293 DataManager::SetValue(TW_ACTION_BUSY, 0);
294}
295
296int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
297{
298 static string zip_queue[10];
299 static int zip_queue_index;
300 static pthread_t terminal_command;
301 int simulate;
302
303 std::string arg = gui_parse_text(action.mArg);
304
305 std::string function = gui_parse_text(action.mFunction);
306
307 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
308
309 if (function == "reboot")
310 {
311 //curtainClose(); this sometimes causes a crash
312
313 sync();
314
Dees_Troya58bead2012-09-27 09:49:29 -0400315 if (arg == "recovery")
316 TWFunc::tw_reboot(rb_recovery);
317 else if (arg == "poweroff")
318 TWFunc::tw_reboot(rb_poweroff);
319 else if (arg == "bootloader")
320 TWFunc::tw_reboot(rb_bootloader);
321 else if (arg == "download")
322 TWFunc::tw_reboot(rb_download);
323 else
324 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400325
326 // This should never occur
327 return -1;
328 }
329 if (function == "home")
330 {
331 PageManager::SelectPackage("TWRP");
332 gui_changePage("main");
333 return 0;
334 }
335
336 if (function == "key")
337 {
338 PageManager::NotifyKey(getKeyByName(arg));
339 return 0;
340 }
341
342 if (function == "page") {
343 std::string page_name = gui_parse_text(arg);
344 return gui_changePage(page_name);
345 }
346
347 if (function == "reload") {
348 int check = 0, ret_val = 0;
349 std::string theme_path;
350
351 operation_start("Reload Theme");
352 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
355 check = 1;
356 }
357
358 theme_path += "/TWRP/theme/ui.zip";
359 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
360 {
361 // Loading the custom theme failed - try loading the stock theme
362 LOGI("Attempting to reload stock theme...\n");
363 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
364 {
365 LOGE("Failed to load base packages.\n");
366 ret_val = 1;
367 }
368 }
369 operation_end(ret_val, simulate);
370 }
371
372 if (function == "readBackup")
373 {
374 string Restore_Name;
375 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400376 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400377 return 0;
378 }
379
380 if (function == "set")
381 {
382 if (arg.find('=') != string::npos)
383 {
384 string varName = arg.substr(0, arg.find('='));
385 string value = arg.substr(arg.find('=') + 1, string::npos);
386
387 DataManager::GetValue(value, value);
388 DataManager::SetValue(varName, value);
389 }
390 else
391 DataManager::SetValue(arg, "1");
392 return 0;
393 }
394 if (function == "clear")
395 {
396 DataManager::SetValue(arg, "0");
397 return 0;
398 }
399
400 if (function == "mount")
401 {
402 if (arg == "usb")
403 {
404 DataManager::SetValue(TW_ACTION_BUSY, 1);
405 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400406 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 else
408 ui_print("Simulating actions...\n");
409 }
410 else if (!simulate)
411 {
412 string cmd;
413 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400414 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400416 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 else
Dees_Troy51127312012-09-08 13:08:49 -0400418 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 } else
420 ui_print("Simulating actions...\n");
421 return 0;
422 }
423
424 if (function == "umount" || function == "unmount")
425 {
426 if (arg == "usb")
427 {
428 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400429 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400430 else
431 ui_print("Simulating actions...\n");
432 DataManager::SetValue(TW_ACTION_BUSY, 0);
433 }
434 else if (!simulate)
435 {
436 string cmd;
437 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400438 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400440 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 else
Dees_Troy51127312012-09-08 13:08:49 -0400442 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 } else
444 ui_print("Simulating actions...\n");
445 return 0;
446 }
447
448 if (function == "restoredefaultsettings")
449 {
450 operation_start("Restore Defaults");
451 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
452 ui_print("Simulating actions...\n");
453 else {
454 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 PartitionManager.Update_System_Details();
456 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 }
458 operation_end(0, simulate);
459 }
460
461 if (function == "copylog")
462 {
463 operation_start("Copy Log");
464 if (!simulate)
465 {
466 char command[255];
467
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400469 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400470 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 sync();
472 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
473 } else
474 simulate_progress_bar();
475 operation_end(0, simulate);
476 return 0;
477 }
478
479 if (function == "compute" || function == "addsubtract")
480 {
481 if (arg.find("+") != string::npos)
482 {
483 string varName = arg.substr(0, arg.find('+'));
484 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
485 int amount_to_add = atoi(string_to_add.c_str());
486 int value;
487
488 DataManager::GetValue(varName, value);
489 DataManager::SetValue(varName, value + amount_to_add);
490 return 0;
491 }
492 if (arg.find("-") != string::npos)
493 {
494 string varName = arg.substr(0, arg.find('-'));
495 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
496 int amount_to_subtract = atoi(string_to_subtract.c_str());
497 int value;
498
499 DataManager::GetValue(varName, value);
500 value -= amount_to_subtract;
501 if (value <= 0)
502 value = 0;
503 DataManager::SetValue(varName, value);
504 return 0;
505 }
506 }
507
508 if (function == "setguitimezone")
509 {
510 string SelectedZone;
511 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
512 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
513 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
514
515 int dst;
516 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
517
518 string offset;
519 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
520
521 string NewTimeZone = Zone;
522 if (offset != "0")
523 NewTimeZone += ":" + offset;
524
525 if (dst != 0)
526 NewTimeZone += DSTZone;
527
528 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400529 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 return 0;
531 }
532
533 if (function == "togglestorage") {
534 if (arg == "internal") {
535 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
536 } else if (arg == "external") {
537 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
538 }
Dees_Troy51127312012-09-08 13:08:49 -0400539 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400541 string zip_path, zip_root;
542 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
543 zip_root = TWFunc::Get_Root_Path(zip_path);
544#ifdef RECOVERY_SDCARD_ON_DATA
545 #ifndef TW_EXTERNAL_STORAGE_PATH
546 if (zip_root != "/sdcard")
547 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
548 #else
549 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
550 if (zip_root != "/emmc")
551 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
552 } else {
553 if (zip_root != "/sdcard")
554 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
555 }
556 #endif
557#else
558 if (zip_root != DataManager::GetCurrentStoragePath())
559 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
560#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 // Save the current zip location to the external variable
562 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
563 // Change the current zip location to the internal variable
564 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
565 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400566 string zip_path, zip_root;
567 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
568 zip_root = TWFunc::Get_Root_Path(zip_path);
569 if (zip_root != DataManager::GetCurrentStoragePath()) {
570 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
571 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400572 // Save the current zip location to the internal variable
573 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
574 // Change the current zip location to the external variable
575 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
576 }
577 } else {
578 // We weren't able to toggle for some reason, restore original setting
579 if (arg == "internal") {
580 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
581 } else if (arg == "external") {
582 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
583 }
584 }
585 return 0;
586 }
587
588 if (function == "overlay")
589 return gui_changeOverlay(arg);
590
591 if (function == "queuezip")
592 {
593 if (zip_queue_index >= 10) {
594 ui_print("Maximum zip queue reached!\n");
595 return 0;
596 }
597 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
598 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
599 zip_queue_index++;
600 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
601 }
602 return 0;
603 }
604
605 if (function == "cancelzip")
606 {
607 if (zip_queue_index <= 0) {
608 ui_print("Minimum zip queue reached!\n");
609 return 0;
610 } else {
611 zip_queue_index--;
612 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
613 }
614 return 0;
615 }
616
617 if (function == "queueclear")
618 {
619 zip_queue_index = 0;
620 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
621 return 0;
622 }
623
624 if (function == "sleep")
625 {
626 operation_start("Sleep");
627 usleep(atoi(arg.c_str()));
628 operation_end(0, simulate);
629 return 0;
630 }
631
632 if (isThreaded)
633 {
634 if (function == "fileexists")
635 {
636 struct stat st;
637 string newpath = arg + "/.";
638
639 operation_start("FileExists");
640 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
641 operation_end(0, simulate);
642 else
643 operation_end(1, simulate);
644 }
645
646 if (function == "flash")
647 {
Dees_Troy657c3092012-09-10 20:32:10 -0400648 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400649
650 for (i=0; i<zip_queue_index; i++) {
651 operation_start("Flashing");
652 DataManager::SetValue("tw_filename", zip_queue[i]);
653 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
654
Dees_Troy657c3092012-09-10 20:32:10 -0400655 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400656 if (ret_val != 0) {
657 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
658 i = 10; // Error flashing zip - exit queue
659 ret_val = 1;
660 }
661 }
662 zip_queue_index = 0;
663 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
664
Dees_Troy657c3092012-09-10 20:32:10 -0400665 if (wipe_cache)
666 PartitionManager.Wipe_By_Path("/cache");
667
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
669 operation_start("ReinjectTWRP");
670 ui_print("Injecting TWRP into boot image...\n");
671 if (simulate) {
672 simulate_progress_bar();
673 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400674 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 ui_print("TWRP injection complete.\n");
676 }
677 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400678 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 operation_end(ret_val, simulate);
680 return 0;
681 }
682 if (function == "wipe")
683 {
684 operation_start("Format");
685 DataManager::SetValue("tw_partition", arg);
686
Dees_Troy38bd7602012-09-14 13:33:53 -0400687 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400688
689 if (simulate) {
690 simulate_progress_bar();
691 } else {
692 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400693 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400695 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400697 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400699 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400701 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 } else if (arg == "INTERNAL") {
703 int has_datamedia, dual_storage;
704
705 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
706 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400707 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400709 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 }
711 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400712 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
Dees_Troy38bd7602012-09-14 13:33:53 -0400714 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
715 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400716 } else if (arg == "ANDROIDSECURE") {
717 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400718 } else
719 ret_val = PartitionManager.Wipe_By_Path(arg);
720
721 if (arg == DataManager::GetSettingsStoragePath()) {
722 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
723 string Storage_Path = DataManager::GetSettingsStoragePath();
724
725 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
726 LOGI("Making TWRP folder and saving settings.\n");
727 Storage_Path += "/TWRP";
728 mkdir(Storage_Path.c_str(), 0777);
729 DataManager::Flush();
730 } else {
731 LOGE("Unable to recreate TWRP folder and save settings.\n");
732 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 }
734 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400735 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 if (ret_val)
737 ret_val = 0; // 0 is success
738 else
739 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 operation_end(ret_val, simulate);
741 return 0;
742 }
743 if (function == "refreshsizes")
744 {
745 operation_start("Refreshing Sizes");
746 if (simulate) {
747 simulate_progress_bar();
748 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400749 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 operation_end(0, simulate);
751 }
752 if (function == "nandroid")
753 {
754 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400756
757 if (simulate) {
758 DataManager::SetValue("tw_partition", "Simulation");
759 simulate_progress_bar();
760 } else {
761 if (arg == "backup") {
762 string Backup_Name;
763 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400764 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400765 ret = PartitionManager.Run_Backup();
766 else {
767 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400769 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
771 } else if (arg == "restore") {
772 string Restore_Name;
773 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400774 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400775 } else {
776 operation_end(1, simulate);
777 return -1;
778 }
779 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400780 if (ret == false)
781 ret = 1; // 1 for failure
782 else
783 ret = 0; // 0 for success
784 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 return 0;
786 }
787 if (function == "fixpermissions")
788 {
789 operation_start("Fix Permissions");
790 LOGI("fix permissions started!\n");
791 if (simulate) {
792 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400793 } else {
794 int op_status;
795 if (!PartitionManager.Mount_By_Path("/data", true) || !PartitionManager.Mount_By_Path("/system", true))
796 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400797
Dees_Troy4be841b2012-09-26 14:07:15 -0400798 DataManager::SetValue("tw_terminal_command_thread", "./sbin/fix_permissions.sh");
799 DataManager::SetValue("tw_terminal_state", 1);
800 DataManager::SetValue("tw_background_thread_running", 1);
801 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
802 if (op_status != 0) {
803 LOGE("Error starting terminal command thread, %i.\n", op_status);
804 DataManager::SetValue("tw_terminal_state", 0);
805 DataManager::SetValue("tw_background_thread_running", 0);
806 operation_end(1, simulate);
807 }
808 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 return 0;
810 }
811 if (function == "dd")
812 {
813 operation_start("imaging");
814
815 if (simulate) {
816 simulate_progress_bar();
817 } else {
818 char cmd[512];
819 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400820 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 }
822 operation_end(0, simulate);
823 return 0;
824 }
825 if (function == "partitionsd")
826 {
827 operation_start("Partition SD Card");
828
829 if (simulate) {
830 simulate_progress_bar();
831 } else {
832 int allow_partition;
833 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
834 if (allow_partition == 0) {
835 ui_print("This device does not have a real SD Card!\nAborting!\n");
836 } else {
837 // Below seen in Koush's recovery
838 char sddevice[256];
839 char mkdir_path[255];
Dees_Troya2ac96a2012-09-26 13:07:22 -0400840#ifdef TW_EXTERNAL_STORAGE_PATH
841 TWPartition* SDCard = PartitionManager.Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
842#else
843 TWPartition* SDCard = PartitionManager.Find_Partition_By_Path("/sdcard");
844#endif
845 if (SDCard == NULL) {
846 LOGE("Unable to locate device to partition.\n");
847 operation_end(1, simulate);
848 return 0;
849 }
850 strcpy(sddevice, SDCard->Actual_Block_Device.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400851 // Just need block not whole partition
Dees_Troy8170a922012-09-18 15:40:25 -0400852 sddevice[strlen("/dev/block/mmcblkX")] = '\0';
Dees_Troy51a0e822012-09-05 15:24:24 -0400853
854 char es[64];
855 std::string ext_format, sd_path;
856 int ext, swap;
857 DataManager::GetValue("tw_sdext_size", ext);
858 DataManager::GetValue("tw_swap_size", swap);
859 DataManager::GetValue("tw_sdpart_file_system", ext_format);
860 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
861 LOGI("\nrunning script: %s\n", es);
862 run_script("\nContinue partitioning?",
863 "\nPartitioning sdcard : ",
864 es,
865 "\nunable to execute parted!\n(%s)\n",
866 "\nOops... something went wrong!\nPlease check the recovery log!\n",
867 "\nPartitioning complete!\n\n",
868 "\nPartitioning aborted!\n\n", 0);
869
870 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
871#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400872 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400873 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
874 memset(mkdir_path, 0, sizeof(mkdir_path));
875 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
876#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400877 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400878 strcpy(mkdir_path, "/sdcard/TWRP");
879#endif
880 mkdir(mkdir_path, 0777);
881 DataManager::Flush();
882#ifdef TW_EXTERNAL_STORAGE_PATH
883 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
884 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
885 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
886#else
887 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
888 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
889 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
890#endif
891 // This is sometimes needed to make a healthy ext4 partition
892 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
893 char command[256];
894 LOGE("Fix this format command!\n");
895 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
896 ui_print("Formatting sd-ext as ext4...\n");
897 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
Dees_Troy8170a922012-09-18 15:40:25 -0400898 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400899 ui_print("DONE\n");
900 }
901
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400903 }
904 }
905 operation_end(0, simulate);
906 return 0;
907 }
908 if (function == "installhtcdumlock")
909 {
910 operation_start("Install HTC Dumlock");
911 if (simulate) {
912 simulate_progress_bar();
913 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400914 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400915
916 operation_end(0, simulate);
917 return 0;
918 }
919 if (function == "htcdumlockrestoreboot")
920 {
921 operation_start("HTC Dumlock Restore Boot");
922 if (simulate) {
923 simulate_progress_bar();
924 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400925 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400926
927 operation_end(0, simulate);
928 return 0;
929 }
930 if (function == "htcdumlockreflashrecovery")
931 {
932 operation_start("HTC Dumlock Reflash Recovery");
933 if (simulate) {
934 simulate_progress_bar();
935 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400936 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400937
938 operation_end(0, simulate);
939 return 0;
940 }
941 if (function == "cmd")
942 {
943 int op_status = 0;
944
945 operation_start("Command");
946 LOGI("Running command: '%s'\n", arg.c_str());
947 if (simulate) {
948 simulate_progress_bar();
949 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400950 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400951 if (op_status != 0)
952 op_status = 1;
953 }
954
955 operation_end(op_status, simulate);
956 return 0;
957 }
958 if (function == "terminalcommand")
959 {
960 int op_status = 0;
961 string cmdpath, command;
962
963 DataManager::GetValue("tw_terminal_location", cmdpath);
964 operation_start("CommandOutput");
965 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
966 if (simulate) {
967 simulate_progress_bar();
968 operation_end(op_status, simulate);
969 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400970 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400971 LOGI("Actual command is: '%s'\n", command.c_str());
972 DataManager::SetValue("tw_terminal_command_thread", command);
973 DataManager::SetValue("tw_terminal_state", 1);
974 DataManager::SetValue("tw_background_thread_running", 1);
975 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
976 if (op_status != 0) {
977 LOGE("Error starting terminal command thread, %i.\n", op_status);
978 DataManager::SetValue("tw_terminal_state", 0);
979 DataManager::SetValue("tw_background_thread_running", 0);
980 operation_end(1, simulate);
981 }
982 }
983 return 0;
984 }
985 if (function == "killterminal")
986 {
987 int op_status = 0;
988
989 LOGI("Sending kill command...\n");
990 operation_start("KillCommand");
991 DataManager::SetValue("tw_operation_status", 0);
992 DataManager::SetValue("tw_operation_state", 1);
993 DataManager::SetValue("tw_terminal_state", 0);
994 DataManager::SetValue("tw_background_thread_running", 0);
995 DataManager::SetValue(TW_ACTION_BUSY, 0);
996 return 0;
997 }
998 if (function == "reinjecttwrp")
999 {
1000 int op_status = 0;
1001
1002 operation_start("ReinjectTWRP");
1003 ui_print("Injecting TWRP into boot image...\n");
1004 if (simulate) {
1005 simulate_progress_bar();
1006 } else {
Dees_Troy8170a922012-09-18 15:40:25 -04001007 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -04001008 ui_print("TWRP injection complete.\n");
1009 }
1010
1011 operation_end(op_status, simulate);
1012 return 0;
1013 }
1014 if (function == "checkbackupname")
1015 {
1016 int op_status = 0;
1017
1018 operation_start("CheckBackupName");
1019 if (simulate) {
1020 simulate_progress_bar();
1021 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001022 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001023 if (op_status != 0)
1024 op_status = 1;
1025 }
1026
1027 operation_end(op_status, simulate);
1028 return 0;
1029 }
1030 if (function == "decrypt")
1031 {
1032 int op_status = 0;
1033
1034 operation_start("Decrypt");
1035 if (simulate) {
1036 simulate_progress_bar();
1037 } else {
1038 string Password;
1039 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001040 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001041 if (op_status != 0)
1042 op_status = 1;
1043 else {
1044 int load_theme = 1;
1045
1046 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1047 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -04001048 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001049 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -04001050 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001051 usleep(2000000); // Sleep for 2 seconds before rebooting
Dees_Troya58bead2012-09-27 09:49:29 -04001052 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 load_theme = 0;
1054 }
1055 }
1056
1057 if (load_theme) {
1058 int has_datamedia;
1059
1060 // Check for a custom theme and load it if exists
1061 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1062 if (has_datamedia != 0) {
1063 struct stat st;
1064 int check = 0;
1065 std::string theme_path;
1066
1067 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001069 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1070 check = 1;
1071 }
1072
1073 theme_path += "/TWRP/theme/ui.zip";
1074 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1075 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1076 {
1077 // Loading the custom theme failed - try loading the stock theme
1078 LOGI("Attempting to reload stock theme...\n");
1079 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1080 {
1081 LOGE("Failed to load base packages.\n");
1082 }
1083 }
1084 }
1085 }
1086 }
1087 }
1088 }
1089
1090 operation_end(op_status, simulate);
1091 return 0;
1092 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001093 if (function == "adbsideload")
1094 {
1095 int ret = 0;
1096
1097 operation_start("Sideload");
1098 if (simulate) {
1099 simulate_progress_bar();
1100 } else {
1101 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001102 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001103
1104 if (!PartitionManager.Mount_Current_Storage(true)) {
1105 operation_end(1, simulate);
1106 return 0;
1107 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001108 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1109 if (TWFunc::Path_Exists(Sideload_File)) {
1110 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001111 system(Command.c_str());
1112 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001114 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001115 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001116 ret = 1; // failure
1117 else if (wipe_cache)
1118 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 }
1120 operation_end(ret, simulate);
1121 return 0;
1122 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001123 if (function == "adbsideloadcancel")
1124 {
1125 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001126 string Command, Sideload_File;
1127 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1128 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001129 system(Command.c_str());
1130 DataManager::GetValue("tw_child_pid", child_pid);
1131 ui_print("Cancelling ADB sideload...\n");
1132 kill(child_pid, SIGTERM);
1133 return 0;
1134 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001135 }
1136 else
1137 {
1138 pthread_t t;
1139 pthread_create(&t, NULL, thread_start, this);
1140 return 0;
1141 }
1142 return -1;
1143}
1144
1145int GUIAction::getKeyByName(std::string key)
1146{
1147 if (key == "home") return KEY_HOME;
1148 else if (key == "menu") return KEY_MENU;
1149 else if (key == "back") return KEY_BACK;
1150 else if (key == "search") return KEY_SEARCH;
1151 else if (key == "voldown") return KEY_VOLUMEDOWN;
1152 else if (key == "volup") return KEY_VOLUMEUP;
1153 else if (key == "power") {
1154 int ret_val;
1155 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1156 if (!ret_val)
1157 return KEY_POWER;
1158 else
1159 return ret_val;
1160 }
1161
1162 return atol(key.c_str());
1163}
1164
1165void* GUIAction::command_thread(void *cookie)
1166{
1167 string command;
1168 FILE* fp;
1169 char line[512];
1170
1171 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001172 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001173 if (fp == NULL) {
1174 LOGE("Error opening command to run.\n");
1175 } else {
1176 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1177 struct timeval timeout;
1178 fd_set fdset;
1179
1180 while(keep_going)
1181 {
1182 FD_ZERO(&fdset);
1183 FD_SET(fd, &fdset);
1184 timeout.tv_sec = 0;
1185 timeout.tv_usec = 400000;
1186 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1187 if (has_data == 0) {
1188 // Timeout reached
1189 DataManager::GetValue("tw_terminal_state", check);
1190 if (check == 0) {
1191 keep_going = 0;
1192 }
1193 } else if (has_data < 0) {
1194 // End of execution
1195 keep_going = 0;
1196 } else {
1197 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001198 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001199 bytes_read = read(fd, line, sizeof(line));
1200 if (bytes_read > 0)
1201 ui_print("%s", line); // Display output
1202 else
1203 keep_going = 0; // Done executing
1204 }
1205 }
1206 fclose(fp);
1207 }
1208 DataManager::SetValue("tw_operation_status", 0);
1209 DataManager::SetValue("tw_operation_state", 1);
1210 DataManager::SetValue("tw_terminal_state", 0);
1211 DataManager::SetValue("tw_background_thread_running", 0);
1212 DataManager::SetValue(TW_ACTION_BUSY, 0);
1213 return NULL;
1214}