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