blob: 4d9c9df59ae6866d42bb97e733425781a3d1b9cb [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;
211 if (stat("/sbin/installTwrp", &st) == 0)
212 {
213 DataManager::SetValue("tw_operation", "Configuring TWRP");
214 DataManager::SetValue("tw_partition", "");
215 ui_print("Configuring TWRP...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400216 if (system("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 {
218 ui_print("Unable to configure TWRP with this kernel.\n");
219 }
220 }
221 }
222
223 // Done
224 DataManager::SetValue("ui_progress", 100);
225 DataManager::SetValue("ui_progress", 0);
226 return ret_val;
227}
228
229int GUIAction::doActions()
230{
231 if (mActions.size() < 1) return -1;
232 if (mActions.size() == 1)
233 return doAction(mActions.at(0), 0);
234
235 // For multi-action, we always use a thread
236 pthread_t t;
237 pthread_create(&t, NULL, thread_start, this);
238
239 return 0;
240}
241
242void* GUIAction::thread_start(void *cookie)
243{
244 GUIAction* ourThis = (GUIAction*) cookie;
245
246 DataManager::SetValue(TW_ACTION_BUSY, 1);
247
248 if (ourThis->mActions.size() > 1)
249 {
250 std::vector<Action>::iterator iter;
251 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
252 ourThis->doAction(*iter, 1);
253 }
254 else
255 {
256 ourThis->doAction(ourThis->mActions.at(0), 1);
257 }
258 int check = 0;
259 DataManager::GetValue("tw_background_thread_running", check);
260 if (check == 0)
261 DataManager::SetValue(TW_ACTION_BUSY, 0);
262 return NULL;
263}
264
265void GUIAction::operation_start(const string operation_name)
266{
267 DataManager::SetValue(TW_ACTION_BUSY, 1);
268 DataManager::SetValue("ui_progress", 0);
269 DataManager::SetValue("tw_operation", operation_name);
270 DataManager::SetValue("tw_operation_status", 0);
271 DataManager::SetValue("tw_operation_state", 0);
272}
273
274void GUIAction::operation_end(const int operation_status, const int simulate)
275{
276 int simulate_fail;
277
278 DataManager::SetValue("ui_progress", 100);
279 if (simulate) {
280 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
281 if (simulate_fail != 0)
282 DataManager::SetValue("tw_operation_status", 1);
283 else
284 DataManager::SetValue("tw_operation_status", 0);
285 } else {
286 if (operation_status != 0)
287 DataManager::SetValue("tw_operation_status", 1);
288 else
289 DataManager::SetValue("tw_operation_status", 0);
290 }
291 DataManager::SetValue("tw_operation_state", 1);
292 DataManager::SetValue(TW_ACTION_BUSY, 0);
293}
294
295int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
296{
297 static string zip_queue[10];
298 static int zip_queue_index;
299 static pthread_t terminal_command;
300 int simulate;
301
302 std::string arg = gui_parse_text(action.mArg);
303
304 std::string function = gui_parse_text(action.mFunction);
305
306 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
307
308 if (function == "reboot")
309 {
310 //curtainClose(); this sometimes causes a crash
311
312 sync();
313
Dees_Troya58bead2012-09-27 09:49:29 -0400314 if (arg == "recovery")
315 TWFunc::tw_reboot(rb_recovery);
316 else if (arg == "poweroff")
317 TWFunc::tw_reboot(rb_poweroff);
318 else if (arg == "bootloader")
319 TWFunc::tw_reboot(rb_bootloader);
320 else if (arg == "download")
321 TWFunc::tw_reboot(rb_download);
322 else
323 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400324
325 // This should never occur
326 return -1;
327 }
328 if (function == "home")
329 {
330 PageManager::SelectPackage("TWRP");
331 gui_changePage("main");
332 return 0;
333 }
334
335 if (function == "key")
336 {
337 PageManager::NotifyKey(getKeyByName(arg));
338 return 0;
339 }
340
341 if (function == "page") {
342 std::string page_name = gui_parse_text(arg);
343 return gui_changePage(page_name);
344 }
345
346 if (function == "reload") {
347 int check = 0, ret_val = 0;
348 std::string theme_path;
349
350 operation_start("Reload Theme");
351 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400353 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
354 check = 1;
355 }
356
357 theme_path += "/TWRP/theme/ui.zip";
358 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
359 {
360 // Loading the custom theme failed - try loading the stock theme
361 LOGI("Attempting to reload stock theme...\n");
362 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
363 {
364 LOGE("Failed to load base packages.\n");
365 ret_val = 1;
366 }
367 }
368 operation_end(ret_val, simulate);
369 }
370
371 if (function == "readBackup")
372 {
373 string Restore_Name;
374 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400376 return 0;
377 }
378
379 if (function == "set")
380 {
381 if (arg.find('=') != string::npos)
382 {
383 string varName = arg.substr(0, arg.find('='));
384 string value = arg.substr(arg.find('=') + 1, string::npos);
385
386 DataManager::GetValue(value, value);
387 DataManager::SetValue(varName, value);
388 }
389 else
390 DataManager::SetValue(arg, "1");
391 return 0;
392 }
393 if (function == "clear")
394 {
395 DataManager::SetValue(arg, "0");
396 return 0;
397 }
398
399 if (function == "mount")
400 {
401 if (arg == "usb")
402 {
403 DataManager::SetValue(TW_ACTION_BUSY, 1);
404 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400405 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 else
407 ui_print("Simulating actions...\n");
408 }
409 else if (!simulate)
410 {
411 string cmd;
412 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400413 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400415 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 else
Dees_Troy51127312012-09-08 13:08:49 -0400417 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 } else
419 ui_print("Simulating actions...\n");
420 return 0;
421 }
422
423 if (function == "umount" || function == "unmount")
424 {
425 if (arg == "usb")
426 {
427 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400428 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400429 else
430 ui_print("Simulating actions...\n");
431 DataManager::SetValue(TW_ACTION_BUSY, 0);
432 }
433 else if (!simulate)
434 {
435 string cmd;
436 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400437 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400439 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 else
Dees_Troy51127312012-09-08 13:08:49 -0400441 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 } else
443 ui_print("Simulating actions...\n");
444 return 0;
445 }
446
447 if (function == "restoredefaultsettings")
448 {
449 operation_start("Restore Defaults");
450 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
451 ui_print("Simulating actions...\n");
452 else {
453 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 PartitionManager.Update_System_Details();
455 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400456 }
457 operation_end(0, simulate);
458 }
459
460 if (function == "copylog")
461 {
462 operation_start("Copy Log");
463 if (!simulate)
464 {
465 char command[255];
466
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400469 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400470 sync();
471 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
472 } else
473 simulate_progress_bar();
474 operation_end(0, simulate);
475 return 0;
476 }
477
478 if (function == "compute" || function == "addsubtract")
479 {
480 if (arg.find("+") != string::npos)
481 {
482 string varName = arg.substr(0, arg.find('+'));
483 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
484 int amount_to_add = atoi(string_to_add.c_str());
485 int value;
486
487 DataManager::GetValue(varName, value);
488 DataManager::SetValue(varName, value + amount_to_add);
489 return 0;
490 }
491 if (arg.find("-") != string::npos)
492 {
493 string varName = arg.substr(0, arg.find('-'));
494 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
495 int amount_to_subtract = atoi(string_to_subtract.c_str());
496 int value;
497
498 DataManager::GetValue(varName, value);
499 value -= amount_to_subtract;
500 if (value <= 0)
501 value = 0;
502 DataManager::SetValue(varName, value);
503 return 0;
504 }
505 }
506
507 if (function == "setguitimezone")
508 {
509 string SelectedZone;
510 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
511 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
512 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
513
514 int dst;
515 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
516
517 string offset;
518 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
519
520 string NewTimeZone = Zone;
521 if (offset != "0")
522 NewTimeZone += ":" + offset;
523
524 if (dst != 0)
525 NewTimeZone += DSTZone;
526
527 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400528 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 return 0;
530 }
531
532 if (function == "togglestorage") {
533 if (arg == "internal") {
534 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
535 } else if (arg == "external") {
536 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
537 }
Dees_Troy51127312012-09-08 13:08:49 -0400538 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400540 string zip_path, zip_root;
541 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
542 zip_root = TWFunc::Get_Root_Path(zip_path);
543#ifdef RECOVERY_SDCARD_ON_DATA
544 #ifndef TW_EXTERNAL_STORAGE_PATH
545 if (zip_root != "/sdcard")
546 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
547 #else
548 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
549 if (zip_root != "/emmc")
550 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
551 } else {
552 if (zip_root != "/sdcard")
553 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
554 }
555 #endif
556#else
557 if (zip_root != DataManager::GetCurrentStoragePath())
558 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
559#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 // Save the current zip location to the external variable
561 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
562 // Change the current zip location to the internal variable
563 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
564 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400565 string zip_path, zip_root;
566 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
567 zip_root = TWFunc::Get_Root_Path(zip_path);
568 if (zip_root != DataManager::GetCurrentStoragePath()) {
569 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
570 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400571 // Save the current zip location to the internal variable
572 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
573 // Change the current zip location to the external variable
574 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
575 }
576 } else {
577 // We weren't able to toggle for some reason, restore original setting
578 if (arg == "internal") {
579 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
580 } else if (arg == "external") {
581 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
582 }
583 }
584 return 0;
585 }
586
587 if (function == "overlay")
588 return gui_changeOverlay(arg);
589
590 if (function == "queuezip")
591 {
592 if (zip_queue_index >= 10) {
593 ui_print("Maximum zip queue reached!\n");
594 return 0;
595 }
596 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
597 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
598 zip_queue_index++;
599 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
600 }
601 return 0;
602 }
603
604 if (function == "cancelzip")
605 {
606 if (zip_queue_index <= 0) {
607 ui_print("Minimum zip queue reached!\n");
608 return 0;
609 } else {
610 zip_queue_index--;
611 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
612 }
613 return 0;
614 }
615
616 if (function == "queueclear")
617 {
618 zip_queue_index = 0;
619 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
620 return 0;
621 }
622
623 if (function == "sleep")
624 {
625 operation_start("Sleep");
626 usleep(atoi(arg.c_str()));
627 operation_end(0, simulate);
628 return 0;
629 }
630
631 if (isThreaded)
632 {
633 if (function == "fileexists")
634 {
635 struct stat st;
636 string newpath = arg + "/.";
637
638 operation_start("FileExists");
639 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
640 operation_end(0, simulate);
641 else
642 operation_end(1, simulate);
643 }
644
645 if (function == "flash")
646 {
Dees_Troy657c3092012-09-10 20:32:10 -0400647 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400648
649 for (i=0; i<zip_queue_index; i++) {
650 operation_start("Flashing");
651 DataManager::SetValue("tw_filename", zip_queue[i]);
652 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
653
Dees_Troy657c3092012-09-10 20:32:10 -0400654 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400655 if (ret_val != 0) {
656 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
657 i = 10; // Error flashing zip - exit queue
658 ret_val = 1;
659 }
660 }
661 zip_queue_index = 0;
662 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
663
Dees_Troy657c3092012-09-10 20:32:10 -0400664 if (wipe_cache)
665 PartitionManager.Wipe_By_Path("/cache");
666
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
668 operation_start("ReinjectTWRP");
669 ui_print("Injecting TWRP into boot image...\n");
670 if (simulate) {
671 simulate_progress_bar();
672 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400673 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 ui_print("TWRP injection complete.\n");
675 }
676 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400677 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400678 operation_end(ret_val, simulate);
679 return 0;
680 }
681 if (function == "wipe")
682 {
683 operation_start("Format");
684 DataManager::SetValue("tw_partition", arg);
685
Dees_Troy38bd7602012-09-14 13:33:53 -0400686 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400687
688 if (simulate) {
689 simulate_progress_bar();
690 } else {
691 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400692 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400694 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400695 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400696 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400698 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400700 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 } else if (arg == "INTERNAL") {
702 int has_datamedia, dual_storage;
703
704 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
705 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400706 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400708 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 }
710 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400711 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400712
Dees_Troy38bd7602012-09-14 13:33:53 -0400713 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
714 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400715 } else if (arg == "ANDROIDSECURE") {
716 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400717 } else
718 ret_val = PartitionManager.Wipe_By_Path(arg);
719
720 if (arg == DataManager::GetSettingsStoragePath()) {
721 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
722 string Storage_Path = DataManager::GetSettingsStoragePath();
723
724 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
725 LOGI("Making TWRP folder and saving settings.\n");
726 Storage_Path += "/TWRP";
727 mkdir(Storage_Path.c_str(), 0777);
728 DataManager::Flush();
729 } else {
730 LOGE("Unable to recreate TWRP folder and save settings.\n");
731 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 }
733 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400734 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400735 if (ret_val)
736 ret_val = 0; // 0 is success
737 else
738 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 operation_end(ret_val, simulate);
740 return 0;
741 }
742 if (function == "refreshsizes")
743 {
744 operation_start("Refreshing Sizes");
745 if (simulate) {
746 simulate_progress_bar();
747 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400748 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 operation_end(0, simulate);
750 }
751 if (function == "nandroid")
752 {
753 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400754 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400755
756 if (simulate) {
757 DataManager::SetValue("tw_partition", "Simulation");
758 simulate_progress_bar();
759 } else {
760 if (arg == "backup") {
761 string Backup_Name;
762 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400763 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400764 ret = PartitionManager.Run_Backup();
765 else {
766 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400768 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
770 } else if (arg == "restore") {
771 string Restore_Name;
772 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 } else {
775 operation_end(1, simulate);
776 return -1;
777 }
778 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 if (ret == false)
780 ret = 1; // 1 for failure
781 else
782 ret = 0; // 0 for success
783 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 return 0;
785 }
786 if (function == "fixpermissions")
787 {
788 operation_start("Fix Permissions");
789 LOGI("fix permissions started!\n");
790 if (simulate) {
791 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400792 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400793 int op_status = PartitionManager.Fix_Permissions();
794 if (op_status != 0)
795 op_status = 1; // failure
796 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400797 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 return 0;
799 }
800 if (function == "dd")
801 {
802 operation_start("imaging");
803
804 if (simulate) {
805 simulate_progress_bar();
806 } else {
807 char cmd[512];
808 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400809 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 }
811 operation_end(0, simulate);
812 return 0;
813 }
814 if (function == "partitionsd")
815 {
816 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400817 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400818
819 if (simulate) {
820 simulate_progress_bar();
821 } else {
822 int allow_partition;
823 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
824 if (allow_partition == 0) {
825 ui_print("This device does not have a real SD Card!\nAborting!\n");
826 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400827 if (!PartitionManager.Partition_SDCard())
828 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400829 }
830 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400831 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 return 0;
833 }
834 if (function == "installhtcdumlock")
835 {
836 operation_start("Install HTC Dumlock");
837 if (simulate) {
838 simulate_progress_bar();
839 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400840 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400841
842 operation_end(0, simulate);
843 return 0;
844 }
845 if (function == "htcdumlockrestoreboot")
846 {
847 operation_start("HTC Dumlock Restore Boot");
848 if (simulate) {
849 simulate_progress_bar();
850 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400851 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400852
853 operation_end(0, simulate);
854 return 0;
855 }
856 if (function == "htcdumlockreflashrecovery")
857 {
858 operation_start("HTC Dumlock Reflash Recovery");
859 if (simulate) {
860 simulate_progress_bar();
861 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400862 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400863
864 operation_end(0, simulate);
865 return 0;
866 }
867 if (function == "cmd")
868 {
869 int op_status = 0;
870
871 operation_start("Command");
872 LOGI("Running command: '%s'\n", arg.c_str());
873 if (simulate) {
874 simulate_progress_bar();
875 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400876 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400877 if (op_status != 0)
878 op_status = 1;
879 }
880
881 operation_end(op_status, simulate);
882 return 0;
883 }
884 if (function == "terminalcommand")
885 {
886 int op_status = 0;
887 string cmdpath, command;
888
889 DataManager::GetValue("tw_terminal_location", cmdpath);
890 operation_start("CommandOutput");
891 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
892 if (simulate) {
893 simulate_progress_bar();
894 operation_end(op_status, simulate);
895 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400896 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400897 LOGI("Actual command is: '%s'\n", command.c_str());
898 DataManager::SetValue("tw_terminal_command_thread", command);
899 DataManager::SetValue("tw_terminal_state", 1);
900 DataManager::SetValue("tw_background_thread_running", 1);
901 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
902 if (op_status != 0) {
903 LOGE("Error starting terminal command thread, %i.\n", op_status);
904 DataManager::SetValue("tw_terminal_state", 0);
905 DataManager::SetValue("tw_background_thread_running", 0);
906 operation_end(1, simulate);
907 }
908 }
909 return 0;
910 }
911 if (function == "killterminal")
912 {
913 int op_status = 0;
914
915 LOGI("Sending kill command...\n");
916 operation_start("KillCommand");
917 DataManager::SetValue("tw_operation_status", 0);
918 DataManager::SetValue("tw_operation_state", 1);
919 DataManager::SetValue("tw_terminal_state", 0);
920 DataManager::SetValue("tw_background_thread_running", 0);
921 DataManager::SetValue(TW_ACTION_BUSY, 0);
922 return 0;
923 }
924 if (function == "reinjecttwrp")
925 {
926 int op_status = 0;
927
928 operation_start("ReinjectTWRP");
929 ui_print("Injecting TWRP into boot image...\n");
930 if (simulate) {
931 simulate_progress_bar();
932 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400933 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400934 ui_print("TWRP injection complete.\n");
935 }
936
937 operation_end(op_status, simulate);
938 return 0;
939 }
940 if (function == "checkbackupname")
941 {
942 int op_status = 0;
943
944 operation_start("CheckBackupName");
945 if (simulate) {
946 simulate_progress_bar();
947 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400948 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400949 if (op_status != 0)
950 op_status = 1;
951 }
952
953 operation_end(op_status, simulate);
954 return 0;
955 }
956 if (function == "decrypt")
957 {
958 int op_status = 0;
959
960 operation_start("Decrypt");
961 if (simulate) {
962 simulate_progress_bar();
963 } else {
964 string Password;
965 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -0400966 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -0400967 if (op_status != 0)
968 op_status = 1;
969 else {
970 int load_theme = 1;
971
972 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
973 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -0400974 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400975 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400976 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400977 usleep(2000000); // Sleep for 2 seconds before rebooting
Dees_Troya58bead2012-09-27 09:49:29 -0400978 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400979 load_theme = 0;
980 }
981 }
982
983 if (load_theme) {
984 int has_datamedia;
985
986 // Check for a custom theme and load it if exists
987 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
988 if (has_datamedia != 0) {
989 struct stat st;
990 int check = 0;
991 std::string theme_path;
992
993 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400994 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400995 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
996 check = 1;
997 }
998
999 theme_path += "/TWRP/theme/ui.zip";
1000 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1001 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1002 {
1003 // Loading the custom theme failed - try loading the stock theme
1004 LOGI("Attempting to reload stock theme...\n");
1005 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1006 {
1007 LOGE("Failed to load base packages.\n");
1008 }
1009 }
1010 }
1011 }
1012 }
1013 }
1014 }
1015
1016 operation_end(op_status, simulate);
1017 return 0;
1018 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001019 if (function == "adbsideload")
1020 {
1021 int ret = 0;
1022
1023 operation_start("Sideload");
1024 if (simulate) {
1025 simulate_progress_bar();
1026 } else {
1027 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001028 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001029
1030 if (!PartitionManager.Mount_Current_Storage(true)) {
1031 operation_end(1, simulate);
1032 return 0;
1033 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001034 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1035 if (TWFunc::Path_Exists(Sideload_File)) {
1036 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001037 system(Command.c_str());
1038 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001039 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001040 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001041 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001042 ret = 1; // failure
1043 else if (wipe_cache)
1044 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001045 }
1046 operation_end(ret, simulate);
1047 return 0;
1048 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001049 if (function == "adbsideloadcancel")
1050 {
1051 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001052 string Command, Sideload_File;
1053 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1054 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001055 system(Command.c_str());
1056 DataManager::GetValue("tw_child_pid", child_pid);
1057 ui_print("Cancelling ADB sideload...\n");
1058 kill(child_pid, SIGTERM);
1059 return 0;
1060 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001061 }
1062 else
1063 {
1064 pthread_t t;
1065 pthread_create(&t, NULL, thread_start, this);
1066 return 0;
1067 }
1068 return -1;
1069}
1070
1071int GUIAction::getKeyByName(std::string key)
1072{
1073 if (key == "home") return KEY_HOME;
1074 else if (key == "menu") return KEY_MENU;
1075 else if (key == "back") return KEY_BACK;
1076 else if (key == "search") return KEY_SEARCH;
1077 else if (key == "voldown") return KEY_VOLUMEDOWN;
1078 else if (key == "volup") return KEY_VOLUMEUP;
1079 else if (key == "power") {
1080 int ret_val;
1081 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1082 if (!ret_val)
1083 return KEY_POWER;
1084 else
1085 return ret_val;
1086 }
1087
1088 return atol(key.c_str());
1089}
1090
1091void* GUIAction::command_thread(void *cookie)
1092{
1093 string command;
1094 FILE* fp;
1095 char line[512];
1096
1097 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001098 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001099 if (fp == NULL) {
1100 LOGE("Error opening command to run.\n");
1101 } else {
1102 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1103 struct timeval timeout;
1104 fd_set fdset;
1105
1106 while(keep_going)
1107 {
1108 FD_ZERO(&fdset);
1109 FD_SET(fd, &fdset);
1110 timeout.tv_sec = 0;
1111 timeout.tv_usec = 400000;
1112 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1113 if (has_data == 0) {
1114 // Timeout reached
1115 DataManager::GetValue("tw_terminal_state", check);
1116 if (check == 0) {
1117 keep_going = 0;
1118 }
1119 } else if (has_data < 0) {
1120 // End of execution
1121 keep_going = 0;
1122 } else {
1123 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001124 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 bytes_read = read(fd, line, sizeof(line));
1126 if (bytes_read > 0)
1127 ui_print("%s", line); // Display output
1128 else
1129 keep_going = 0; // Done executing
1130 }
1131 }
1132 fclose(fp);
1133 }
1134 DataManager::SetValue("tw_operation_status", 0);
1135 DataManager::SetValue("tw_operation_state", 1);
1136 DataManager::SetValue("tw_terminal_state", 0);
1137 DataManager::SetValue("tw_background_thread_running", 0);
1138 DataManager::SetValue(TW_ACTION_BUSY, 0);
1139 return NULL;
1140}