blob: afa52d1f830b22c9158e80ff1730bb2a0434b5d1 [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 {
793 int op_status;
794 if (!PartitionManager.Mount_By_Path("/data", true) || !PartitionManager.Mount_By_Path("/system", true))
795 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400796
Dees_Troy4be841b2012-09-26 14:07:15 -0400797 DataManager::SetValue("tw_terminal_command_thread", "./sbin/fix_permissions.sh");
798 DataManager::SetValue("tw_terminal_state", 1);
799 DataManager::SetValue("tw_background_thread_running", 1);
800 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
801 if (op_status != 0) {
802 LOGE("Error starting terminal command thread, %i.\n", op_status);
803 DataManager::SetValue("tw_terminal_state", 0);
804 DataManager::SetValue("tw_background_thread_running", 0);
805 operation_end(1, simulate);
806 }
807 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 return 0;
809 }
810 if (function == "dd")
811 {
812 operation_start("imaging");
813
814 if (simulate) {
815 simulate_progress_bar();
816 } else {
817 char cmd[512];
818 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400819 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 }
821 operation_end(0, simulate);
822 return 0;
823 }
824 if (function == "partitionsd")
825 {
826 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400827 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400828
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 {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400837 if (!PartitionManager.Partition_SDCard())
838 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400839 }
840 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400841 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400842 return 0;
843 }
844 if (function == "installhtcdumlock")
845 {
846 operation_start("Install HTC Dumlock");
847 if (simulate) {
848 simulate_progress_bar();
849 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400850 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400851
852 operation_end(0, simulate);
853 return 0;
854 }
855 if (function == "htcdumlockrestoreboot")
856 {
857 operation_start("HTC Dumlock Restore Boot");
858 if (simulate) {
859 simulate_progress_bar();
860 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400861 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400862
863 operation_end(0, simulate);
864 return 0;
865 }
866 if (function == "htcdumlockreflashrecovery")
867 {
868 operation_start("HTC Dumlock Reflash Recovery");
869 if (simulate) {
870 simulate_progress_bar();
871 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400873
874 operation_end(0, simulate);
875 return 0;
876 }
877 if (function == "cmd")
878 {
879 int op_status = 0;
880
881 operation_start("Command");
882 LOGI("Running command: '%s'\n", arg.c_str());
883 if (simulate) {
884 simulate_progress_bar();
885 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400886 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400887 if (op_status != 0)
888 op_status = 1;
889 }
890
891 operation_end(op_status, simulate);
892 return 0;
893 }
894 if (function == "terminalcommand")
895 {
896 int op_status = 0;
897 string cmdpath, command;
898
899 DataManager::GetValue("tw_terminal_location", cmdpath);
900 operation_start("CommandOutput");
901 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
902 if (simulate) {
903 simulate_progress_bar();
904 operation_end(op_status, simulate);
905 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400906 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 LOGI("Actual command is: '%s'\n", command.c_str());
908 DataManager::SetValue("tw_terminal_command_thread", command);
909 DataManager::SetValue("tw_terminal_state", 1);
910 DataManager::SetValue("tw_background_thread_running", 1);
911 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
912 if (op_status != 0) {
913 LOGE("Error starting terminal command thread, %i.\n", op_status);
914 DataManager::SetValue("tw_terminal_state", 0);
915 DataManager::SetValue("tw_background_thread_running", 0);
916 operation_end(1, simulate);
917 }
918 }
919 return 0;
920 }
921 if (function == "killterminal")
922 {
923 int op_status = 0;
924
925 LOGI("Sending kill command...\n");
926 operation_start("KillCommand");
927 DataManager::SetValue("tw_operation_status", 0);
928 DataManager::SetValue("tw_operation_state", 1);
929 DataManager::SetValue("tw_terminal_state", 0);
930 DataManager::SetValue("tw_background_thread_running", 0);
931 DataManager::SetValue(TW_ACTION_BUSY, 0);
932 return 0;
933 }
934 if (function == "reinjecttwrp")
935 {
936 int op_status = 0;
937
938 operation_start("ReinjectTWRP");
939 ui_print("Injecting TWRP into boot image...\n");
940 if (simulate) {
941 simulate_progress_bar();
942 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400943 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400944 ui_print("TWRP injection complete.\n");
945 }
946
947 operation_end(op_status, simulate);
948 return 0;
949 }
950 if (function == "checkbackupname")
951 {
952 int op_status = 0;
953
954 operation_start("CheckBackupName");
955 if (simulate) {
956 simulate_progress_bar();
957 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400958 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400959 if (op_status != 0)
960 op_status = 1;
961 }
962
963 operation_end(op_status, simulate);
964 return 0;
965 }
966 if (function == "decrypt")
967 {
968 int op_status = 0;
969
970 operation_start("Decrypt");
971 if (simulate) {
972 simulate_progress_bar();
973 } else {
974 string Password;
975 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -0400976 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -0400977 if (op_status != 0)
978 op_status = 1;
979 else {
980 int load_theme = 1;
981
982 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
983 DataManager::ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -0400984 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400985 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -0400986 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400987 usleep(2000000); // Sleep for 2 seconds before rebooting
Dees_Troya58bead2012-09-27 09:49:29 -0400988 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400989 load_theme = 0;
990 }
991 }
992
993 if (load_theme) {
994 int has_datamedia;
995
996 // Check for a custom theme and load it if exists
997 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
998 if (has_datamedia != 0) {
999 struct stat st;
1000 int check = 0;
1001 std::string theme_path;
1002
1003 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001004 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001005 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1006 check = 1;
1007 }
1008
1009 theme_path += "/TWRP/theme/ui.zip";
1010 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1011 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1012 {
1013 // Loading the custom theme failed - try loading the stock theme
1014 LOGI("Attempting to reload stock theme...\n");
1015 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1016 {
1017 LOGE("Failed to load base packages.\n");
1018 }
1019 }
1020 }
1021 }
1022 }
1023 }
1024 }
1025
1026 operation_end(op_status, simulate);
1027 return 0;
1028 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001029 if (function == "adbsideload")
1030 {
1031 int ret = 0;
1032
1033 operation_start("Sideload");
1034 if (simulate) {
1035 simulate_progress_bar();
1036 } else {
1037 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001038 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001039
1040 if (!PartitionManager.Mount_Current_Storage(true)) {
1041 operation_end(1, simulate);
1042 return 0;
1043 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001044 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1045 if (TWFunc::Path_Exists(Sideload_File)) {
1046 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001047 system(Command.c_str());
1048 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001049 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001050 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001052 ret = 1; // failure
1053 else if (wipe_cache)
1054 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001055 }
1056 operation_end(ret, simulate);
1057 return 0;
1058 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001059 if (function == "adbsideloadcancel")
1060 {
1061 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001062 string Command, Sideload_File;
1063 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1064 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001065 system(Command.c_str());
1066 DataManager::GetValue("tw_child_pid", child_pid);
1067 ui_print("Cancelling ADB sideload...\n");
1068 kill(child_pid, SIGTERM);
1069 return 0;
1070 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 }
1072 else
1073 {
1074 pthread_t t;
1075 pthread_create(&t, NULL, thread_start, this);
1076 return 0;
1077 }
1078 return -1;
1079}
1080
1081int GUIAction::getKeyByName(std::string key)
1082{
1083 if (key == "home") return KEY_HOME;
1084 else if (key == "menu") return KEY_MENU;
1085 else if (key == "back") return KEY_BACK;
1086 else if (key == "search") return KEY_SEARCH;
1087 else if (key == "voldown") return KEY_VOLUMEDOWN;
1088 else if (key == "volup") return KEY_VOLUMEUP;
1089 else if (key == "power") {
1090 int ret_val;
1091 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1092 if (!ret_val)
1093 return KEY_POWER;
1094 else
1095 return ret_val;
1096 }
1097
1098 return atol(key.c_str());
1099}
1100
1101void* GUIAction::command_thread(void *cookie)
1102{
1103 string command;
1104 FILE* fp;
1105 char line[512];
1106
1107 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001108 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001109 if (fp == NULL) {
1110 LOGE("Error opening command to run.\n");
1111 } else {
1112 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1113 struct timeval timeout;
1114 fd_set fdset;
1115
1116 while(keep_going)
1117 {
1118 FD_ZERO(&fdset);
1119 FD_SET(fd, &fdset);
1120 timeout.tv_sec = 0;
1121 timeout.tv_usec = 400000;
1122 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1123 if (has_data == 0) {
1124 // Timeout reached
1125 DataManager::GetValue("tw_terminal_state", check);
1126 if (check == 0) {
1127 keep_going = 0;
1128 }
1129 } else if (has_data < 0) {
1130 // End of execution
1131 keep_going = 0;
1132 } else {
1133 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001134 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001135 bytes_read = read(fd, line, sizeof(line));
1136 if (bytes_read > 0)
1137 ui_print("%s", line); // Display output
1138 else
1139 keep_going = 0; // Done executing
1140 }
1141 }
1142 fclose(fp);
1143 }
1144 DataManager::SetValue("tw_operation_status", 0);
1145 DataManager::SetValue("tw_operation_state", 1);
1146 DataManager::SetValue("tw_terminal_state", 0);
1147 DataManager::SetValue("tw_background_thread_running", 0);
1148 DataManager::SetValue(TW_ACTION_BUSY, 0);
1149 return NULL;
1150}