blob: e56db2b76cf60556cbd222fcfff3e01d4f6cf73f [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// image.cpp - GUIImage object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/ioctl.h>
13#include <linux/input.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040017#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <string>
20#include <sstream>
21#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040023#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
Dees_Troy43d8b002012-09-17 16:00:01 -040025#include "../ui.h"
26#include "../adb_install.h"
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028extern "C" {
29#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include "../minuitwrp/minui.h"
31#include "../recovery_ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Dees_Troy43d8b002012-09-17 16:00:01 -040035#include "../minadbd/adb.h"
36
Dees_Troy32c8eb82012-09-11 15:28:06 -040037int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040038void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040039int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040040int gui_start();
41};
42
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
Dees_Troy43d8b002012-09-17 16:00:01 -040046extern RecoveryUI* ui;
47
Dees_Troy51a0e822012-09-05 15:24:24 -040048void curtainClose(void);
49
50GUIAction::GUIAction(xml_node<>* node)
51 : Conditional(node)
52{
53 xml_node<>* child;
54 xml_node<>* actions;
55 xml_attribute<>* attr;
56
57 mKey = 0;
58
59 if (!node) return;
60
61 // First, get the action
62 actions = node->first_node("actions");
63 if (actions) child = actions->first_node("action");
64 else child = node->first_node("action");
65
66 if (!child) return;
67
68 while (child)
69 {
70 Action action;
71
72 attr = child->first_attribute("function");
73 if (!attr) return;
74
75 action.mFunction = attr->value();
76 action.mArg = child->value();
77 mActions.push_back(action);
78
79 child = child->next_sibling("action");
80 }
81
82 // Now, let's get either the key or region
83 child = node->first_node("touch");
84 if (child)
85 {
86 attr = child->first_attribute("key");
87 if (attr)
88 {
89 std::string key = attr->value();
90
91 mKey = getKeyByName(key);
92 }
93 else
94 {
95 attr = child->first_attribute("x");
96 if (!attr) return;
97 mActionX = atol(attr->value());
98 attr = child->first_attribute("y");
99 if (!attr) return;
100 mActionY = atol(attr->value());
101 attr = child->first_attribute("w");
102 if (!attr) return;
103 mActionW = atol(attr->value());
104 attr = child->first_attribute("h");
105 if (!attr) return;
106 mActionH = atol(attr->value());
107 }
108 }
109}
110
111int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
112{
113 if (state == TOUCH_RELEASE)
114 doActions();
115
116 return 0;
117}
118
119int GUIAction::NotifyKey(int key)
120{
121 if (!mKey || key != mKey)
122 return 1;
123
124 doActions();
125 return 0;
126}
127
128int GUIAction::NotifyVarChange(std::string varName, std::string value)
129{
130 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
131 doActions();
132
133 // This handles notifying the condition system of page start
134 if (varName.empty() && isConditionValid())
135 NotifyPageSet();
136
137 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
138 doActions();
139
140 return 0;
141}
142
143void GUIAction::simulate_progress_bar(void)
144{
145 ui_print("Simulating actions...\n");
146 for (int i = 0; i < 5; i++)
147 {
148 usleep(500000);
149 DataManager::SetValue("ui_progress", i * 20);
150 }
151}
152
Dees_Troy657c3092012-09-10 20:32:10 -0400153int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400154{
155 int ret_val = 0;
156
157 DataManager::SetValue("ui_progress", 0);
158
159 if (filename.empty())
160 {
161 LOGE("No file specified.\n");
162 return -1;
163 }
164
165 // We're going to jump to this page first, like a loading page
166 gui_changePage(pageName);
167
168 int fd = -1;
169 ZipArchive zip;
170
Dees_Troy657c3092012-09-10 20:32:10 -0400171 if (!PartitionManager.Mount_By_Path(filename, true))
172 return -1;
173
174 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 {
176 LOGE("Unable to open zip file.\n");
177 return -1;
178 }
179
180 // Check the zip to see if it has a custom installer theme
181 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
182 if (twrp != NULL)
183 {
184 unlink("/tmp/twrp.zip");
185 fd = creat("/tmp/twrp.zip", 0666);
186 }
187 if (fd >= 0 && twrp != NULL &&
188 mzExtractZipEntryToFile(&zip, twrp, fd) &&
189 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
190 {
191 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400192 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 gui_changePage("main");
194 }
195 else
196 {
197 // In this case, we just use the default page
198 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400199 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 }
201 if (fd >= 0)
202 close(fd);
203
204 if (simulate) {
205 simulate_progress_bar();
206 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400207 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400208
209 // Now, check if we need to ensure TWRP remains installed...
210 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 if (stat("/sbin/installTwrp", &st) == 0)
213 {
214 DataManager::SetValue("tw_operation", "Configuring TWRP");
215 DataManager::SetValue("tw_partition", "");
216 ui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500217 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 {
219 ui_print("Unable to configure TWRP with this kernel.\n");
220 }
221 }
222 }
223
224 // Done
225 DataManager::SetValue("ui_progress", 100);
226 DataManager::SetValue("ui_progress", 0);
227 return ret_val;
228}
229
230int GUIAction::doActions()
231{
232 if (mActions.size() < 1) return -1;
233 if (mActions.size() == 1)
234 return doAction(mActions.at(0), 0);
235
236 // For multi-action, we always use a thread
237 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000238 pthread_attr_t tattr;
239
240 if (pthread_attr_init(&tattr)) {
241 LOGE("Unable to pthread_attr_init\n");
242 return -1;
243 }
244 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
245 LOGE("Error setting pthread_attr_setdetachstate\n");
246 return -1;
247 }
248 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
249 LOGE("Error setting pthread_attr_setscope\n");
250 return -1;
251 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500252 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troyab4963c2013-01-16 20:35:51 +0000253 LOGE("Error setting pthread_attr_setstacksize\n");
254 return -1;
255 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500256 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000257 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000258 if (ret) {
259 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
260 thread_start(this);
261 } else {
262 if (pthread_join(t, NULL)) {
263 LOGE("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000264 }
265 }
266 if (pthread_attr_destroy(&tattr)) {
267 LOGE("Failed to pthread_attr_destroy\n");
268 return -1;
269 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400270
271 return 0;
272}
273
274void* GUIAction::thread_start(void *cookie)
275{
276 GUIAction* ourThis = (GUIAction*) cookie;
277
278 DataManager::SetValue(TW_ACTION_BUSY, 1);
279
280 if (ourThis->mActions.size() > 1)
281 {
282 std::vector<Action>::iterator iter;
283 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
284 ourThis->doAction(*iter, 1);
285 }
286 else
287 {
288 ourThis->doAction(ourThis->mActions.at(0), 1);
289 }
290 int check = 0;
291 DataManager::GetValue("tw_background_thread_running", check);
292 if (check == 0)
293 DataManager::SetValue(TW_ACTION_BUSY, 0);
294 return NULL;
295}
296
297void GUIAction::operation_start(const string operation_name)
298{
299 DataManager::SetValue(TW_ACTION_BUSY, 1);
300 DataManager::SetValue("ui_progress", 0);
301 DataManager::SetValue("tw_operation", operation_name);
302 DataManager::SetValue("tw_operation_status", 0);
303 DataManager::SetValue("tw_operation_state", 0);
304}
305
306void GUIAction::operation_end(const int operation_status, const int simulate)
307{
308 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400309 DataManager::SetValue("ui_progress", 100);
310 if (simulate) {
311 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
312 if (simulate_fail != 0)
313 DataManager::SetValue("tw_operation_status", 1);
314 else
315 DataManager::SetValue("tw_operation_status", 0);
316 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500317 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400318 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500319 }
320 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500322 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400323 }
324 DataManager::SetValue("tw_operation_state", 1);
325 DataManager::SetValue(TW_ACTION_BUSY, 0);
326}
327
328int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
329{
330 static string zip_queue[10];
331 static int zip_queue_index;
332 static pthread_t terminal_command;
333 int simulate;
334
335 std::string arg = gui_parse_text(action.mArg);
336
337 std::string function = gui_parse_text(action.mFunction);
338
339 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
340
341 if (function == "reboot")
342 {
343 //curtainClose(); this sometimes causes a crash
344
345 sync();
346
Dees_Troya58bead2012-09-27 09:49:29 -0400347 if (arg == "recovery")
348 TWFunc::tw_reboot(rb_recovery);
349 else if (arg == "poweroff")
350 TWFunc::tw_reboot(rb_poweroff);
351 else if (arg == "bootloader")
352 TWFunc::tw_reboot(rb_bootloader);
353 else if (arg == "download")
354 TWFunc::tw_reboot(rb_download);
355 else
356 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400357
358 // This should never occur
359 return -1;
360 }
361 if (function == "home")
362 {
363 PageManager::SelectPackage("TWRP");
364 gui_changePage("main");
365 return 0;
366 }
367
368 if (function == "key")
369 {
370 PageManager::NotifyKey(getKeyByName(arg));
371 return 0;
372 }
373
374 if (function == "page") {
375 std::string page_name = gui_parse_text(arg);
376 return gui_changePage(page_name);
377 }
378
379 if (function == "reload") {
380 int check = 0, ret_val = 0;
381 std::string theme_path;
382
383 operation_start("Reload Theme");
384 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400385 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
387 check = 1;
388 }
389
390 theme_path += "/TWRP/theme/ui.zip";
391 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
392 {
393 // Loading the custom theme failed - try loading the stock theme
394 LOGI("Attempting to reload stock theme...\n");
395 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
396 {
397 LOGE("Failed to load base packages.\n");
398 ret_val = 1;
399 }
400 }
401 operation_end(ret_val, simulate);
402 }
403
404 if (function == "readBackup")
405 {
406 string Restore_Name;
407 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400408 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 return 0;
410 }
411
412 if (function == "set")
413 {
414 if (arg.find('=') != string::npos)
415 {
416 string varName = arg.substr(0, arg.find('='));
417 string value = arg.substr(arg.find('=') + 1, string::npos);
418
419 DataManager::GetValue(value, value);
420 DataManager::SetValue(varName, value);
421 }
422 else
423 DataManager::SetValue(arg, "1");
424 return 0;
425 }
426 if (function == "clear")
427 {
428 DataManager::SetValue(arg, "0");
429 return 0;
430 }
431
432 if (function == "mount")
433 {
434 if (arg == "usb")
435 {
436 DataManager::SetValue(TW_ACTION_BUSY, 1);
437 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400438 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 else
440 ui_print("Simulating actions...\n");
441 }
442 else if (!simulate)
443 {
444 string cmd;
445 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400446 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400448 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 else
Dees_Troy51127312012-09-08 13:08:49 -0400450 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 } else
452 ui_print("Simulating actions...\n");
453 return 0;
454 }
455
456 if (function == "umount" || function == "unmount")
457 {
458 if (arg == "usb")
459 {
460 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400461 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400462 else
463 ui_print("Simulating actions...\n");
464 DataManager::SetValue(TW_ACTION_BUSY, 0);
465 }
466 else if (!simulate)
467 {
468 string cmd;
469 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400470 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400472 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 else
Dees_Troy51127312012-09-08 13:08:49 -0400474 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 } else
476 ui_print("Simulating actions...\n");
477 return 0;
478 }
479
480 if (function == "restoredefaultsettings")
481 {
482 operation_start("Restore Defaults");
483 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
484 ui_print("Simulating actions...\n");
485 else {
486 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400487 PartitionManager.Update_System_Details();
488 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 }
490 operation_end(0, simulate);
491 }
492
493 if (function == "copylog")
494 {
495 operation_start("Copy Log");
496 if (!simulate)
497 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500498 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400499 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500500 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
501 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 sync();
503 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
504 } else
505 simulate_progress_bar();
506 operation_end(0, simulate);
507 return 0;
508 }
509
510 if (function == "compute" || function == "addsubtract")
511 {
512 if (arg.find("+") != string::npos)
513 {
514 string varName = arg.substr(0, arg.find('+'));
515 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
516 int amount_to_add = atoi(string_to_add.c_str());
517 int value;
518
519 DataManager::GetValue(varName, value);
520 DataManager::SetValue(varName, value + amount_to_add);
521 return 0;
522 }
523 if (arg.find("-") != string::npos)
524 {
525 string varName = arg.substr(0, arg.find('-'));
526 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
527 int amount_to_subtract = atoi(string_to_subtract.c_str());
528 int value;
529
530 DataManager::GetValue(varName, value);
531 value -= amount_to_subtract;
532 if (value <= 0)
533 value = 0;
534 DataManager::SetValue(varName, value);
535 return 0;
536 }
537 }
538
539 if (function == "setguitimezone")
540 {
541 string SelectedZone;
542 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
543 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
544 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
545
546 int dst;
547 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
548
549 string offset;
550 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
551
552 string NewTimeZone = Zone;
553 if (offset != "0")
554 NewTimeZone += ":" + offset;
555
556 if (dst != 0)
557 NewTimeZone += DSTZone;
558
559 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400560 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 return 0;
562 }
563
564 if (function == "togglestorage") {
565 if (arg == "internal") {
566 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
567 } else if (arg == "external") {
568 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
569 }
Dees_Troy51127312012-09-08 13:08:49 -0400570 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400571 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400572 string zip_path, zip_root;
573 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
574 zip_root = TWFunc::Get_Root_Path(zip_path);
575#ifdef RECOVERY_SDCARD_ON_DATA
576 #ifndef TW_EXTERNAL_STORAGE_PATH
577 if (zip_root != "/sdcard")
578 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
579 #else
580 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
581 if (zip_root != "/emmc")
582 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
583 } else {
584 if (zip_root != "/sdcard")
585 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
586 }
587 #endif
588#else
589 if (zip_root != DataManager::GetCurrentStoragePath())
590 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
591#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 // Save the current zip location to the external variable
593 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
594 // Change the current zip location to the internal variable
595 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
596 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400597 string zip_path, zip_root;
598 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
599 zip_root = TWFunc::Get_Root_Path(zip_path);
600 if (zip_root != DataManager::GetCurrentStoragePath()) {
601 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
602 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400603 // Save the current zip location to the internal variable
604 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
605 // Change the current zip location to the external variable
606 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
607 }
608 } else {
609 // We weren't able to toggle for some reason, restore original setting
610 if (arg == "internal") {
611 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
612 } else if (arg == "external") {
613 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
614 }
615 }
616 return 0;
617 }
618
619 if (function == "overlay")
620 return gui_changeOverlay(arg);
621
622 if (function == "queuezip")
623 {
624 if (zip_queue_index >= 10) {
625 ui_print("Maximum zip queue reached!\n");
626 return 0;
627 }
628 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
629 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
630 zip_queue_index++;
631 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
632 }
633 return 0;
634 }
635
636 if (function == "cancelzip")
637 {
638 if (zip_queue_index <= 0) {
639 ui_print("Minimum zip queue reached!\n");
640 return 0;
641 } else {
642 zip_queue_index--;
643 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
644 }
645 return 0;
646 }
647
648 if (function == "queueclear")
649 {
650 zip_queue_index = 0;
651 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
652 return 0;
653 }
654
655 if (function == "sleep")
656 {
657 operation_start("Sleep");
658 usleep(atoi(arg.c_str()));
659 operation_end(0, simulate);
660 return 0;
661 }
662
663 if (isThreaded)
664 {
665 if (function == "fileexists")
666 {
667 struct stat st;
668 string newpath = arg + "/.";
669
670 operation_start("FileExists");
671 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
672 operation_end(0, simulate);
673 else
674 operation_end(1, simulate);
675 }
676
677 if (function == "flash")
678 {
Dees_Troy657c3092012-09-10 20:32:10 -0400679 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400680
681 for (i=0; i<zip_queue_index; i++) {
682 operation_start("Flashing");
683 DataManager::SetValue("tw_filename", zip_queue[i]);
684 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
685
Dees_Troy657c3092012-09-10 20:32:10 -0400686 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 if (ret_val != 0) {
688 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
689 i = 10; // Error flashing zip - exit queue
690 ret_val = 1;
691 }
692 }
693 zip_queue_index = 0;
694 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
695
Dees_Troy657c3092012-09-10 20:32:10 -0400696 if (wipe_cache)
697 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500698 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
700 operation_start("ReinjectTWRP");
701 ui_print("Injecting TWRP into boot image...\n");
702 if (simulate) {
703 simulate_progress_bar();
704 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400705 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
706 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500707 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400708 else {
709 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500710 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400711 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 ui_print("TWRP injection complete.\n");
713 }
714 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400715 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 operation_end(ret_val, simulate);
717 return 0;
718 }
719 if (function == "wipe")
720 {
721 operation_start("Format");
722 DataManager::SetValue("tw_partition", arg);
723
Dees_Troy38bd7602012-09-14 13:33:53 -0400724 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400725
726 if (simulate) {
727 simulate_progress_bar();
728 } else {
729 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400730 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400731 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400732 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400734 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 } else if (arg == "INTERNAL") {
740 int has_datamedia, dual_storage;
741
742 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
743 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400744 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400746 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400747 }
748 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400749 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400750
Dees_Troy38bd7602012-09-14 13:33:53 -0400751 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
752 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400753 } else if (arg == "ANDROIDSECURE") {
754 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400755 } else
756 ret_val = PartitionManager.Wipe_By_Path(arg);
757
758 if (arg == DataManager::GetSettingsStoragePath()) {
759 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
760 string Storage_Path = DataManager::GetSettingsStoragePath();
761
762 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
763 LOGI("Making TWRP folder and saving settings.\n");
764 Storage_Path += "/TWRP";
765 mkdir(Storage_Path.c_str(), 0777);
766 DataManager::Flush();
767 } else {
768 LOGE("Unable to recreate TWRP folder and save settings.\n");
769 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 }
771 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400772 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400773 if (ret_val)
774 ret_val = 0; // 0 is success
775 else
776 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 operation_end(ret_val, simulate);
778 return 0;
779 }
780 if (function == "refreshsizes")
781 {
782 operation_start("Refreshing Sizes");
783 if (simulate) {
784 simulate_progress_bar();
785 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400786 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400787 operation_end(0, simulate);
788 }
789 if (function == "nandroid")
790 {
791 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400792 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400793
794 if (simulate) {
795 DataManager::SetValue("tw_partition", "Simulation");
796 simulate_progress_bar();
797 } else {
798 if (arg == "backup") {
799 string Backup_Name;
800 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500801 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400802 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500803 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400804 else {
805 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400807 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
809 } else if (arg == "restore") {
810 string Restore_Name;
811 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400812 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400813 } else {
814 operation_end(1, simulate);
815 return -1;
816 }
817 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400818 if (ret == false)
819 ret = 1; // 1 for failure
820 else
821 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500822 operation_end(ret, simulate);
823 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 }
825 if (function == "fixpermissions")
826 {
827 operation_start("Fix Permissions");
828 LOGI("fix permissions started!\n");
829 if (simulate) {
830 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400831 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400832 int op_status = PartitionManager.Fix_Permissions();
833 if (op_status != 0)
834 op_status = 1; // failure
835 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400836 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 return 0;
838 }
839 if (function == "dd")
840 {
841 operation_start("imaging");
842
843 if (simulate) {
844 simulate_progress_bar();
845 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500846 string result;
847 string cmd = "dd " + arg;
848 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400849 }
850 operation_end(0, simulate);
851 return 0;
852 }
853 if (function == "partitionsd")
854 {
855 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400856 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400857
858 if (simulate) {
859 simulate_progress_bar();
860 } else {
861 int allow_partition;
862 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
863 if (allow_partition == 0) {
864 ui_print("This device does not have a real SD Card!\nAborting!\n");
865 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400866 if (!PartitionManager.Partition_SDCard())
867 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400868 }
869 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400870 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400871 return 0;
872 }
873 if (function == "installhtcdumlock")
874 {
875 operation_start("Install HTC Dumlock");
876 if (simulate) {
877 simulate_progress_bar();
878 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400879 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400880
881 operation_end(0, simulate);
882 return 0;
883 }
884 if (function == "htcdumlockrestoreboot")
885 {
886 operation_start("HTC Dumlock Restore Boot");
887 if (simulate) {
888 simulate_progress_bar();
889 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400890 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400891
892 operation_end(0, simulate);
893 return 0;
894 }
895 if (function == "htcdumlockreflashrecovery")
896 {
897 operation_start("HTC Dumlock Reflash Recovery");
898 if (simulate) {
899 simulate_progress_bar();
900 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400901 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400902
903 operation_end(0, simulate);
904 return 0;
905 }
906 if (function == "cmd")
907 {
908 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500909 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400910
911 operation_start("Command");
912 LOGI("Running command: '%s'\n", arg.c_str());
913 if (simulate) {
914 simulate_progress_bar();
915 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500916 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400917 if (op_status != 0)
918 op_status = 1;
919 }
920
921 operation_end(op_status, simulate);
922 return 0;
923 }
924 if (function == "terminalcommand")
925 {
926 int op_status = 0;
927 string cmdpath, command;
928
929 DataManager::GetValue("tw_terminal_location", cmdpath);
930 operation_start("CommandOutput");
931 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
932 if (simulate) {
933 simulate_progress_bar();
934 operation_end(op_status, simulate);
935 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400936 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400937 LOGI("Actual command is: '%s'\n", command.c_str());
938 DataManager::SetValue("tw_terminal_command_thread", command);
939 DataManager::SetValue("tw_terminal_state", 1);
940 DataManager::SetValue("tw_background_thread_running", 1);
941 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
942 if (op_status != 0) {
943 LOGE("Error starting terminal command thread, %i.\n", op_status);
944 DataManager::SetValue("tw_terminal_state", 0);
945 DataManager::SetValue("tw_background_thread_running", 0);
946 operation_end(1, simulate);
947 }
948 }
949 return 0;
950 }
951 if (function == "killterminal")
952 {
953 int op_status = 0;
954
955 LOGI("Sending kill command...\n");
956 operation_start("KillCommand");
957 DataManager::SetValue("tw_operation_status", 0);
958 DataManager::SetValue("tw_operation_state", 1);
959 DataManager::SetValue("tw_terminal_state", 0);
960 DataManager::SetValue("tw_background_thread_running", 0);
961 DataManager::SetValue(TW_ACTION_BUSY, 0);
962 return 0;
963 }
964 if (function == "reinjecttwrp")
965 {
966 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500967 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400968 operation_start("ReinjectTWRP");
969 ui_print("Injecting TWRP into boot image...\n");
970 if (simulate) {
971 simulate_progress_bar();
972 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500973 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400974 ui_print("TWRP injection complete.\n");
975 }
976
977 operation_end(op_status, simulate);
978 return 0;
979 }
980 if (function == "checkbackupname")
981 {
982 int op_status = 0;
983
984 operation_start("CheckBackupName");
985 if (simulate) {
986 simulate_progress_bar();
987 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400988 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400989 if (op_status != 0)
990 op_status = 1;
991 }
992
993 operation_end(op_status, simulate);
994 return 0;
995 }
996 if (function == "decrypt")
997 {
998 int op_status = 0;
999
1000 operation_start("Decrypt");
1001 if (simulate) {
1002 simulate_progress_bar();
1003 } else {
1004 string Password;
1005 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001006 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001007 if (op_status != 0)
1008 op_status = 1;
1009 else {
1010 int load_theme = 1;
1011
1012 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001013
1014 if (load_theme) {
1015 int has_datamedia;
1016
1017 // Check for a custom theme and load it if exists
1018 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1019 if (has_datamedia != 0) {
1020 struct stat st;
1021 int check = 0;
1022 std::string theme_path;
1023
1024 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001025 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001026 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1027 check = 1;
1028 }
1029
1030 theme_path += "/TWRP/theme/ui.zip";
1031 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1032 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1033 {
1034 // Loading the custom theme failed - try loading the stock theme
1035 LOGI("Attempting to reload stock theme...\n");
1036 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1037 {
1038 LOGE("Failed to load base packages.\n");
1039 }
1040 }
1041 }
1042 }
1043 }
1044 }
1045 }
1046
1047 operation_end(op_status, simulate);
1048 return 0;
1049 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001050 if (function == "adbsideload")
1051 {
1052 int ret = 0;
1053
1054 operation_start("Sideload");
1055 if (simulate) {
1056 simulate_progress_bar();
1057 } else {
1058 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001059 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001060 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001061
1062 if (!PartitionManager.Mount_Current_Storage(true)) {
1063 operation_end(1, simulate);
1064 return 0;
1065 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001066 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1067 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001068 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001069 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001070 ui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001071 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy9a4b5692012-09-19 15:09:45 -04001072 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001073 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001074 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001075 } else {
1076 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1077 PartitionManager.Wipe_By_Path("/cache");
1078 if (wipe_dalvik)
1079 PartitionManager.Wipe_Dalvik_Cache();
1080 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001081 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1082 operation_start("ReinjectTWRP");
1083 ui_print("Injecting TWRP into boot image...\n");
1084 if (simulate) {
1085 simulate_progress_bar();
1086 } else {
1087 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1088 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001089 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001090 else {
1091 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001092 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001093 }
1094 ui_print("TWRP injection complete.\n");
1095 }
1096 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001097 }
1098 operation_end(ret, simulate);
1099 return 0;
1100 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001101 if (function == "adbsideloadcancel")
1102 {
1103 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001104 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001105 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001106 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001107 DataManager::GetValue("tw_child_pid", child_pid);
1108 ui_print("Cancelling ADB sideload...\n");
1109 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001110 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001111 return 0;
1112 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001113 if (function == "openrecoveryscript") {
1114 operation_start("OpenRecoveryScript");
1115 if (simulate) {
1116 simulate_progress_bar();
1117 } else {
1118 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1119 // that we converted to ORS commands during boot in recovery.cpp.
1120 // Run those first.
1121 int reboot = 0;
1122 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1123 ui_print("Processing AOSP recovery commands...\n");
1124 if (OpenRecoveryScript::run_script_file() == 0) {
1125 reboot = 1;
1126 }
1127 }
1128 // Check for the ORS file in /cache and attempt to run those commands.
1129 if (OpenRecoveryScript::check_for_script_file()) {
1130 ui_print("Processing OpenRecoveryScript file...\n");
1131 if (OpenRecoveryScript::run_script_file() == 0) {
1132 reboot = 1;
1133 }
1134 }
1135 if (reboot) {
1136 usleep(2000000); // Sleep for 2 seconds before rebooting
1137 TWFunc::tw_reboot(rb_system);
1138 } else {
1139 DataManager::SetValue("tw_page_done", 1);
1140 }
1141 }
1142 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001143 }
1144 else
1145 {
1146 pthread_t t;
1147 pthread_create(&t, NULL, thread_start, this);
1148 return 0;
1149 }
1150 return -1;
1151}
1152
1153int GUIAction::getKeyByName(std::string key)
1154{
1155 if (key == "home") return KEY_HOME;
1156 else if (key == "menu") return KEY_MENU;
1157 else if (key == "back") return KEY_BACK;
1158 else if (key == "search") return KEY_SEARCH;
1159 else if (key == "voldown") return KEY_VOLUMEDOWN;
1160 else if (key == "volup") return KEY_VOLUMEUP;
1161 else if (key == "power") {
1162 int ret_val;
1163 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1164 if (!ret_val)
1165 return KEY_POWER;
1166 else
1167 return ret_val;
1168 }
1169
1170 return atol(key.c_str());
1171}
1172
1173void* GUIAction::command_thread(void *cookie)
1174{
1175 string command;
1176 FILE* fp;
1177 char line[512];
1178
1179 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001180 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001181 if (fp == NULL) {
1182 LOGE("Error opening command to run.\n");
1183 } else {
1184 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1185 struct timeval timeout;
1186 fd_set fdset;
1187
1188 while(keep_going)
1189 {
1190 FD_ZERO(&fdset);
1191 FD_SET(fd, &fdset);
1192 timeout.tv_sec = 0;
1193 timeout.tv_usec = 400000;
1194 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1195 if (has_data == 0) {
1196 // Timeout reached
1197 DataManager::GetValue("tw_terminal_state", check);
1198 if (check == 0) {
1199 keep_going = 0;
1200 }
1201 } else if (has_data < 0) {
1202 // End of execution
1203 keep_going = 0;
1204 } else {
1205 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001206 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001207 bytes_read = read(fd, line, sizeof(line));
1208 if (bytes_read > 0)
1209 ui_print("%s", line); // Display output
1210 else
1211 keep_going = 0; // Done executing
1212 }
1213 }
1214 fclose(fp);
1215 }
1216 DataManager::SetValue("tw_operation_status", 0);
1217 DataManager::SetValue("tw_operation_state", 1);
1218 DataManager::SetValue("tw_terminal_state", 0);
1219 DataManager::SetValue("tw_background_thread_running", 0);
1220 DataManager::SetValue(TW_ACTION_BUSY, 0);
1221 return NULL;
1222}