Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 17 | #include <sys/wait.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | #include <sstream> |
| 21 | #include "../partitions.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 22 | #include "../twrp-functions.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | |
| 24 | extern "C" { |
| 25 | #include "../common.h" |
| 26 | #include "../roots.h" |
| 27 | #include "../tw_reboot.h" |
| 28 | #include "../minuitwrp/minui.h" |
| 29 | #include "../recovery_ui.h" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 30 | #include "../extra-functions.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | #include "../variables.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 32 | #include "../twinstall.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 34 | int TWinstall_zip(const char* path, int* wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | void wipe_dalvik_cache(void); |
| 36 | int check_backup_name(int show_error); |
| 37 | void wipe_battery_stats(void); |
| 38 | void wipe_rotate_data(void); |
| 39 | int usb_storage_enable(void); |
| 40 | int usb_storage_disable(void); |
| 41 | int __system(const char *command); |
| 42 | FILE * __popen(const char *program, const char *type); |
| 43 | int __pclose(FILE *iop); |
| 44 | void 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); |
| 45 | void update_tz_environment_variables(); |
| 46 | void install_htc_dumlock(void); |
| 47 | void htc_dumlock_restore_original_boot(void); |
| 48 | void htc_dumlock_reflash_recovery_to_boot(void); |
| 49 | int check_for_script_file(void); |
| 50 | int gui_console_only(); |
| 51 | int run_script_file(void); |
| 52 | int gui_start(); |
| 53 | }; |
| 54 | |
| 55 | #include "rapidxml.hpp" |
| 56 | #include "objects.hpp" |
| 57 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | void curtainClose(void); |
| 59 | |
| 60 | GUIAction::GUIAction(xml_node<>* node) |
| 61 | : Conditional(node) |
| 62 | { |
| 63 | xml_node<>* child; |
| 64 | xml_node<>* actions; |
| 65 | xml_attribute<>* attr; |
| 66 | |
| 67 | mKey = 0; |
| 68 | |
| 69 | if (!node) return; |
| 70 | |
| 71 | // First, get the action |
| 72 | actions = node->first_node("actions"); |
| 73 | if (actions) child = actions->first_node("action"); |
| 74 | else child = node->first_node("action"); |
| 75 | |
| 76 | if (!child) return; |
| 77 | |
| 78 | while (child) |
| 79 | { |
| 80 | Action action; |
| 81 | |
| 82 | attr = child->first_attribute("function"); |
| 83 | if (!attr) return; |
| 84 | |
| 85 | action.mFunction = attr->value(); |
| 86 | action.mArg = child->value(); |
| 87 | mActions.push_back(action); |
| 88 | |
| 89 | child = child->next_sibling("action"); |
| 90 | } |
| 91 | |
| 92 | // Now, let's get either the key or region |
| 93 | child = node->first_node("touch"); |
| 94 | if (child) |
| 95 | { |
| 96 | attr = child->first_attribute("key"); |
| 97 | if (attr) |
| 98 | { |
| 99 | std::string key = attr->value(); |
| 100 | |
| 101 | mKey = getKeyByName(key); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | attr = child->first_attribute("x"); |
| 106 | if (!attr) return; |
| 107 | mActionX = atol(attr->value()); |
| 108 | attr = child->first_attribute("y"); |
| 109 | if (!attr) return; |
| 110 | mActionY = atol(attr->value()); |
| 111 | attr = child->first_attribute("w"); |
| 112 | if (!attr) return; |
| 113 | mActionW = atol(attr->value()); |
| 114 | attr = child->first_attribute("h"); |
| 115 | if (!attr) return; |
| 116 | mActionH = atol(attr->value()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 122 | { |
| 123 | if (state == TOUCH_RELEASE) |
| 124 | doActions(); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int GUIAction::NotifyKey(int key) |
| 130 | { |
| 131 | if (!mKey || key != mKey) |
| 132 | return 1; |
| 133 | |
| 134 | doActions(); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int GUIAction::NotifyVarChange(std::string varName, std::string value) |
| 139 | { |
| 140 | if (varName.empty() && !isConditionValid() && !mKey && !mActionW) |
| 141 | doActions(); |
| 142 | |
| 143 | // This handles notifying the condition system of page start |
| 144 | if (varName.empty() && isConditionValid()) |
| 145 | NotifyPageSet(); |
| 146 | |
| 147 | if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue()) |
| 148 | doActions(); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void GUIAction::simulate_progress_bar(void) |
| 154 | { |
| 155 | ui_print("Simulating actions...\n"); |
| 156 | for (int i = 0; i < 5; i++) |
| 157 | { |
| 158 | usleep(500000); |
| 159 | DataManager::SetValue("ui_progress", i * 20); |
| 160 | } |
| 161 | } |
| 162 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 163 | int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | { |
| 165 | int ret_val = 0; |
| 166 | |
| 167 | DataManager::SetValue("ui_progress", 0); |
| 168 | |
| 169 | if (filename.empty()) |
| 170 | { |
| 171 | LOGE("No file specified.\n"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | // We're going to jump to this page first, like a loading page |
| 176 | gui_changePage(pageName); |
| 177 | |
| 178 | int fd = -1; |
| 179 | ZipArchive zip; |
| 180 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 181 | if (!PartitionManager.Mount_By_Path(filename, true)) |
| 182 | return -1; |
| 183 | |
| 184 | if (mzOpenZipArchive(filename.c_str(), &zip)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | { |
| 186 | LOGE("Unable to open zip file.\n"); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | // Check the zip to see if it has a custom installer theme |
| 191 | const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip"); |
| 192 | if (twrp != NULL) |
| 193 | { |
| 194 | unlink("/tmp/twrp.zip"); |
| 195 | fd = creat("/tmp/twrp.zip", 0666); |
| 196 | } |
| 197 | if (fd >= 0 && twrp != NULL && |
| 198 | mzExtractZipEntryToFile(&zip, twrp, fd) && |
| 199 | !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main")) |
| 200 | { |
| 201 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 202 | PageManager::SelectPackage("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | gui_changePage("main"); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | // In this case, we just use the default page |
| 208 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 209 | gui_changePage(pageName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | } |
| 211 | if (fd >= 0) |
| 212 | close(fd); |
| 213 | |
| 214 | if (simulate) { |
| 215 | simulate_progress_bar(); |
| 216 | } else { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 217 | ret_val = TWinstall_zip(filename.c_str(), wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | |
| 219 | // Now, check if we need to ensure TWRP remains installed... |
| 220 | struct stat st; |
| 221 | if (stat("/sbin/installTwrp", &st) == 0) |
| 222 | { |
| 223 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 224 | DataManager::SetValue("tw_partition", ""); |
| 225 | ui_print("Configuring TWRP...\n"); |
| 226 | if (__system("/sbin/installTwrp reinstall") < 0) |
| 227 | { |
| 228 | ui_print("Unable to configure TWRP with this kernel.\n"); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Done |
| 234 | DataManager::SetValue("ui_progress", 100); |
| 235 | DataManager::SetValue("ui_progress", 0); |
| 236 | return ret_val; |
| 237 | } |
| 238 | |
| 239 | int GUIAction::doActions() |
| 240 | { |
| 241 | if (mActions.size() < 1) return -1; |
| 242 | if (mActions.size() == 1) |
| 243 | return doAction(mActions.at(0), 0); |
| 244 | |
| 245 | // For multi-action, we always use a thread |
| 246 | pthread_t t; |
| 247 | pthread_create(&t, NULL, thread_start, this); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | void* GUIAction::thread_start(void *cookie) |
| 253 | { |
| 254 | GUIAction* ourThis = (GUIAction*) cookie; |
| 255 | |
| 256 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 257 | |
| 258 | if (ourThis->mActions.size() > 1) |
| 259 | { |
| 260 | std::vector<Action>::iterator iter; |
| 261 | for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++) |
| 262 | ourThis->doAction(*iter, 1); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | ourThis->doAction(ourThis->mActions.at(0), 1); |
| 267 | } |
| 268 | int check = 0; |
| 269 | DataManager::GetValue("tw_background_thread_running", check); |
| 270 | if (check == 0) |
| 271 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | void GUIAction::operation_start(const string operation_name) |
| 276 | { |
| 277 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 278 | DataManager::SetValue("ui_progress", 0); |
| 279 | DataManager::SetValue("tw_operation", operation_name); |
| 280 | DataManager::SetValue("tw_operation_status", 0); |
| 281 | DataManager::SetValue("tw_operation_state", 0); |
| 282 | } |
| 283 | |
| 284 | void GUIAction::operation_end(const int operation_status, const int simulate) |
| 285 | { |
| 286 | int simulate_fail; |
| 287 | |
| 288 | DataManager::SetValue("ui_progress", 100); |
| 289 | if (simulate) { |
| 290 | DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); |
| 291 | if (simulate_fail != 0) |
| 292 | DataManager::SetValue("tw_operation_status", 1); |
| 293 | else |
| 294 | DataManager::SetValue("tw_operation_status", 0); |
| 295 | } else { |
| 296 | if (operation_status != 0) |
| 297 | DataManager::SetValue("tw_operation_status", 1); |
| 298 | else |
| 299 | DataManager::SetValue("tw_operation_status", 0); |
| 300 | } |
| 301 | DataManager::SetValue("tw_operation_state", 1); |
| 302 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 303 | } |
| 304 | |
| 305 | int GUIAction::doAction(Action action, int isThreaded /* = 0 */) |
| 306 | { |
| 307 | static string zip_queue[10]; |
| 308 | static int zip_queue_index; |
| 309 | static pthread_t terminal_command; |
| 310 | int simulate; |
| 311 | |
| 312 | std::string arg = gui_parse_text(action.mArg); |
| 313 | |
| 314 | std::string function = gui_parse_text(action.mFunction); |
| 315 | |
| 316 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
| 317 | |
| 318 | if (function == "reboot") |
| 319 | { |
| 320 | //curtainClose(); this sometimes causes a crash |
| 321 | |
| 322 | sync(); |
| 323 | |
| 324 | if (arg == "recovery") |
| 325 | tw_reboot(rb_recovery); |
| 326 | else if (arg == "poweroff") |
| 327 | tw_reboot(rb_poweroff); |
| 328 | else if (arg == "bootloader") |
| 329 | tw_reboot(rb_bootloader); |
| 330 | else if (arg == "download") |
| 331 | tw_reboot(rb_download); |
| 332 | else |
| 333 | tw_reboot(rb_system); |
| 334 | |
| 335 | // This should never occur |
| 336 | return -1; |
| 337 | } |
| 338 | if (function == "home") |
| 339 | { |
| 340 | PageManager::SelectPackage("TWRP"); |
| 341 | gui_changePage("main"); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | if (function == "key") |
| 346 | { |
| 347 | PageManager::NotifyKey(getKeyByName(arg)); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | if (function == "page") { |
| 352 | std::string page_name = gui_parse_text(arg); |
| 353 | return gui_changePage(page_name); |
| 354 | } |
| 355 | |
| 356 | if (function == "reload") { |
| 357 | int check = 0, ret_val = 0; |
| 358 | std::string theme_path; |
| 359 | |
| 360 | operation_start("Reload Theme"); |
| 361 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 362 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 363 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 364 | check = 1; |
| 365 | } |
| 366 | |
| 367 | theme_path += "/TWRP/theme/ui.zip"; |
| 368 | if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 369 | { |
| 370 | // Loading the custom theme failed - try loading the stock theme |
| 371 | LOGI("Attempting to reload stock theme...\n"); |
| 372 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 373 | { |
| 374 | LOGE("Failed to load base packages.\n"); |
| 375 | ret_val = 1; |
| 376 | } |
| 377 | } |
| 378 | operation_end(ret_val, simulate); |
| 379 | } |
| 380 | |
| 381 | if (function == "readBackup") |
| 382 | { |
| 383 | string Restore_Name; |
| 384 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 385 | PartitionManager.Set_Restore_Files(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | if (function == "set") |
| 390 | { |
| 391 | if (arg.find('=') != string::npos) |
| 392 | { |
| 393 | string varName = arg.substr(0, arg.find('=')); |
| 394 | string value = arg.substr(arg.find('=') + 1, string::npos); |
| 395 | |
| 396 | DataManager::GetValue(value, value); |
| 397 | DataManager::SetValue(varName, value); |
| 398 | } |
| 399 | else |
| 400 | DataManager::SetValue(arg, "1"); |
| 401 | return 0; |
| 402 | } |
| 403 | if (function == "clear") |
| 404 | { |
| 405 | DataManager::SetValue(arg, "0"); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | if (function == "mount") |
| 410 | { |
| 411 | if (arg == "usb") |
| 412 | { |
| 413 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 414 | if (!simulate) |
| 415 | usb_storage_enable(); |
| 416 | else |
| 417 | ui_print("Simulating actions...\n"); |
| 418 | } |
| 419 | else if (!simulate) |
| 420 | { |
| 421 | string cmd; |
| 422 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 423 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 424 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 425 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 426 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 427 | PartitionManager.Mount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 428 | } else |
| 429 | ui_print("Simulating actions...\n"); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | if (function == "umount" || function == "unmount") |
| 434 | { |
| 435 | if (arg == "usb") |
| 436 | { |
| 437 | if (!simulate) |
| 438 | usb_storage_disable(); |
| 439 | else |
| 440 | ui_print("Simulating actions...\n"); |
| 441 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 442 | } |
| 443 | else if (!simulate) |
| 444 | { |
| 445 | string cmd; |
| 446 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 447 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 448 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 449 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 450 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 451 | PartitionManager.UnMount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 452 | } else |
| 453 | ui_print("Simulating actions...\n"); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | if (function == "restoredefaultsettings") |
| 458 | { |
| 459 | operation_start("Restore Defaults"); |
| 460 | if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting |
| 461 | ui_print("Simulating actions...\n"); |
| 462 | else { |
| 463 | DataManager::ResetDefaults(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 464 | PartitionManager.Update_System_Details(); |
| 465 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 466 | } |
| 467 | operation_end(0, simulate); |
| 468 | } |
| 469 | |
| 470 | if (function == "copylog") |
| 471 | { |
| 472 | operation_start("Copy Log"); |
| 473 | if (!simulate) |
| 474 | { |
| 475 | char command[255]; |
| 476 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 477 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 478 | sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str()); |
| 479 | __system(command); |
| 480 | sync(); |
| 481 | ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str()); |
| 482 | } else |
| 483 | simulate_progress_bar(); |
| 484 | operation_end(0, simulate); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | if (function == "compute" || function == "addsubtract") |
| 489 | { |
| 490 | if (arg.find("+") != string::npos) |
| 491 | { |
| 492 | string varName = arg.substr(0, arg.find('+')); |
| 493 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 494 | int amount_to_add = atoi(string_to_add.c_str()); |
| 495 | int value; |
| 496 | |
| 497 | DataManager::GetValue(varName, value); |
| 498 | DataManager::SetValue(varName, value + amount_to_add); |
| 499 | return 0; |
| 500 | } |
| 501 | if (arg.find("-") != string::npos) |
| 502 | { |
| 503 | string varName = arg.substr(0, arg.find('-')); |
| 504 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 505 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 506 | int value; |
| 507 | |
| 508 | DataManager::GetValue(varName, value); |
| 509 | value -= amount_to_subtract; |
| 510 | if (value <= 0) |
| 511 | value = 0; |
| 512 | DataManager::SetValue(varName, value); |
| 513 | return 0; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (function == "setguitimezone") |
| 518 | { |
| 519 | string SelectedZone; |
| 520 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 521 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 522 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 523 | |
| 524 | int dst; |
| 525 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 526 | |
| 527 | string offset; |
| 528 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 529 | |
| 530 | string NewTimeZone = Zone; |
| 531 | if (offset != "0") |
| 532 | NewTimeZone += ":" + offset; |
| 533 | |
| 534 | if (dst != 0) |
| 535 | NewTimeZone += DSTZone; |
| 536 | |
| 537 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
| 538 | update_tz_environment_variables(); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | if (function == "togglestorage") { |
| 543 | if (arg == "internal") { |
| 544 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 545 | } else if (arg == "external") { |
| 546 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 547 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 548 | if (PartitionManager.Mount_Current_Storage(true)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 549 | if (arg == "internal") { |
| 550 | // Save the current zip location to the external variable |
| 551 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 552 | // Change the current zip location to the internal variable |
| 553 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR)); |
| 554 | } else if (arg == "external") { |
| 555 | // Save the current zip location to the internal variable |
| 556 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 557 | // Change the current zip location to the external variable |
| 558 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR)); |
| 559 | } |
| 560 | } else { |
| 561 | // We weren't able to toggle for some reason, restore original setting |
| 562 | if (arg == "internal") { |
| 563 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 564 | } else if (arg == "external") { |
| 565 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 566 | } |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | if (function == "overlay") |
| 572 | return gui_changeOverlay(arg); |
| 573 | |
| 574 | if (function == "queuezip") |
| 575 | { |
| 576 | if (zip_queue_index >= 10) { |
| 577 | ui_print("Maximum zip queue reached!\n"); |
| 578 | return 0; |
| 579 | } |
| 580 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 581 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 582 | zip_queue_index++; |
| 583 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | if (function == "cancelzip") |
| 589 | { |
| 590 | if (zip_queue_index <= 0) { |
| 591 | ui_print("Minimum zip queue reached!\n"); |
| 592 | return 0; |
| 593 | } else { |
| 594 | zip_queue_index--; |
| 595 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | if (function == "queueclear") |
| 601 | { |
| 602 | zip_queue_index = 0; |
| 603 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | if (function == "sleep") |
| 608 | { |
| 609 | operation_start("Sleep"); |
| 610 | usleep(atoi(arg.c_str())); |
| 611 | operation_end(0, simulate); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | if (isThreaded) |
| 616 | { |
| 617 | if (function == "fileexists") |
| 618 | { |
| 619 | struct stat st; |
| 620 | string newpath = arg + "/."; |
| 621 | |
| 622 | operation_start("FileExists"); |
| 623 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 624 | operation_end(0, simulate); |
| 625 | else |
| 626 | operation_end(1, simulate); |
| 627 | } |
| 628 | |
| 629 | if (function == "flash") |
| 630 | { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 631 | int i, ret_val = 0, wipe_cache = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 632 | |
| 633 | for (i=0; i<zip_queue_index; i++) { |
| 634 | operation_start("Flashing"); |
| 635 | DataManager::SetValue("tw_filename", zip_queue[i]); |
| 636 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 637 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 638 | ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 639 | if (ret_val != 0) { |
| 640 | ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str()); |
| 641 | i = 10; // Error flashing zip - exit queue |
| 642 | ret_val = 1; |
| 643 | } |
| 644 | } |
| 645 | zip_queue_index = 0; |
| 646 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 647 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 648 | if (wipe_cache) |
| 649 | PartitionManager.Wipe_By_Path("/cache"); |
| 650 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 651 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 652 | operation_start("ReinjectTWRP"); |
| 653 | ui_print("Injecting TWRP into boot image...\n"); |
| 654 | if (simulate) { |
| 655 | simulate_progress_bar(); |
| 656 | } else { |
| 657 | __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 658 | ui_print("TWRP injection complete.\n"); |
| 659 | } |
| 660 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 661 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 662 | operation_end(ret_val, simulate); |
| 663 | return 0; |
| 664 | } |
| 665 | if (function == "wipe") |
| 666 | { |
| 667 | operation_start("Format"); |
| 668 | DataManager::SetValue("tw_partition", arg); |
| 669 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 670 | int ret_val = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 671 | |
| 672 | if (simulate) { |
| 673 | simulate_progress_bar(); |
| 674 | } else { |
| 675 | if (arg == "data") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 676 | ret_val = PartitionManager.Factory_Reset(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 677 | else if (arg == "battery") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 678 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 679 | else if (arg == "rotate") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 680 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 681 | else if (arg == "dalvik") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 682 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 683 | else if (arg == "DATAMEDIA") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 684 | ret_val = PartitionManager.Format_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 685 | } else if (arg == "INTERNAL") { |
| 686 | int has_datamedia, dual_storage; |
| 687 | |
| 688 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 689 | if (has_datamedia) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 690 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 691 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 692 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 693 | } |
| 694 | } else if (arg == "EXTERNAL") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 695 | string External_Path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 696 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 697 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 698 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
| 699 | } else |
| 700 | ret_val = PartitionManager.Wipe_By_Path(arg); |
| 701 | |
| 702 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 703 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 704 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
| 705 | |
| 706 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 707 | LOGI("Making TWRP folder and saving settings.\n"); |
| 708 | Storage_Path += "/TWRP"; |
| 709 | mkdir(Storage_Path.c_str(), 0777); |
| 710 | DataManager::Flush(); |
| 711 | } else { |
| 712 | LOGE("Unable to recreate TWRP folder and save settings.\n"); |
| 713 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 714 | } |
| 715 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 716 | PartitionManager.Update_System_Details(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 717 | if (ret_val) |
| 718 | ret_val = 0; // 0 is success |
| 719 | else |
| 720 | ret_val = 1; // 1 is failure |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 721 | operation_end(ret_val, simulate); |
| 722 | return 0; |
| 723 | } |
| 724 | if (function == "refreshsizes") |
| 725 | { |
| 726 | operation_start("Refreshing Sizes"); |
| 727 | if (simulate) { |
| 728 | simulate_progress_bar(); |
| 729 | } else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 730 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 731 | operation_end(0, simulate); |
| 732 | } |
| 733 | if (function == "nandroid") |
| 734 | { |
| 735 | operation_start("Nandroid"); |
| 736 | |
| 737 | if (simulate) { |
| 738 | DataManager::SetValue("tw_partition", "Simulation"); |
| 739 | simulate_progress_bar(); |
| 740 | } else { |
| 741 | if (arg == "backup") { |
| 742 | string Backup_Name; |
| 743 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 744 | if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 745 | PartitionManager.Run_Backup(Backup_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 746 | else |
| 747 | return -1; |
| 748 | DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)"); |
| 749 | } else if (arg == "restore") { |
| 750 | string Restore_Name; |
| 751 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 752 | PartitionManager.Run_Restore(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 753 | } else { |
| 754 | operation_end(1, simulate); |
| 755 | return -1; |
| 756 | } |
| 757 | } |
| 758 | operation_end(0, simulate); |
| 759 | return 0; |
| 760 | } |
| 761 | if (function == "fixpermissions") |
| 762 | { |
| 763 | operation_start("Fix Permissions"); |
| 764 | LOGI("fix permissions started!\n"); |
| 765 | if (simulate) { |
| 766 | simulate_progress_bar(); |
| 767 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 768 | PartitionManager.Fix_Permissions(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 769 | |
| 770 | LOGI("fix permissions DONE!\n"); |
| 771 | operation_end(0, simulate); |
| 772 | return 0; |
| 773 | } |
| 774 | if (function == "dd") |
| 775 | { |
| 776 | operation_start("imaging"); |
| 777 | |
| 778 | if (simulate) { |
| 779 | simulate_progress_bar(); |
| 780 | } else { |
| 781 | char cmd[512]; |
| 782 | sprintf(cmd, "dd %s", arg.c_str()); |
| 783 | __system(cmd); |
| 784 | } |
| 785 | operation_end(0, simulate); |
| 786 | return 0; |
| 787 | } |
| 788 | if (function == "partitionsd") |
| 789 | { |
| 790 | operation_start("Partition SD Card"); |
| 791 | |
| 792 | if (simulate) { |
| 793 | simulate_progress_bar(); |
| 794 | } else { |
| 795 | int allow_partition; |
| 796 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 797 | if (allow_partition == 0) { |
| 798 | ui_print("This device does not have a real SD Card!\nAborting!\n"); |
| 799 | } else { |
| 800 | // Below seen in Koush's recovery |
| 801 | char sddevice[256]; |
| 802 | char mkdir_path[255]; |
| 803 | Volume *vol = volume_for_path("/sdcard"); |
| 804 | strcpy(sddevice, vol->device); |
| 805 | // Just need block not whole partition |
| 806 | sddevice[strlen("/dev/block/mmcblkX")] = NULL; |
| 807 | |
| 808 | char es[64]; |
| 809 | std::string ext_format, sd_path; |
| 810 | int ext, swap; |
| 811 | DataManager::GetValue("tw_sdext_size", ext); |
| 812 | DataManager::GetValue("tw_swap_size", swap); |
| 813 | DataManager::GetValue("tw_sdpart_file_system", ext_format); |
| 814 | sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap); |
| 815 | LOGI("\nrunning script: %s\n", es); |
| 816 | run_script("\nContinue partitioning?", |
| 817 | "\nPartitioning sdcard : ", |
| 818 | es, |
| 819 | "\nunable to execute parted!\n(%s)\n", |
| 820 | "\nOops... something went wrong!\nPlease check the recovery log!\n", |
| 821 | "\nPartitioning complete!\n\n", |
| 822 | "\nPartitioning aborted!\n\n", 0); |
| 823 | |
| 824 | // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned |
| 825 | #ifdef TW_EXTERNAL_STORAGE_PATH |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 826 | PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 827 | DataManager::GetValue(TW_EXTERNAL_PATH, sd_path); |
| 828 | memset(mkdir_path, 0, sizeof(mkdir_path)); |
| 829 | sprintf(mkdir_path, "%s/TWRP", sd_path.c_str()); |
| 830 | #else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 831 | PartitionManager.Mount_By_Path("/sdcard", 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 832 | strcpy(mkdir_path, "/sdcard/TWRP"); |
| 833 | #endif |
| 834 | mkdir(mkdir_path, 0777); |
| 835 | DataManager::Flush(); |
| 836 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 837 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 838 | if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1) |
| 839 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 840 | #else |
| 841 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard"); |
| 842 | if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1) |
| 843 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard"); |
| 844 | #endif |
| 845 | // This is sometimes needed to make a healthy ext4 partition |
| 846 | if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) { |
| 847 | char command[256]; |
| 848 | LOGE("Fix this format command!\n"); |
| 849 | //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk); |
| 850 | ui_print("Formatting sd-ext as ext4...\n"); |
| 851 | LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command); |
| 852 | __system(command); |
| 853 | ui_print("DONE\n"); |
| 854 | } |
| 855 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 856 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | operation_end(0, simulate); |
| 860 | return 0; |
| 861 | } |
| 862 | if (function == "installhtcdumlock") |
| 863 | { |
| 864 | operation_start("Install HTC Dumlock"); |
| 865 | if (simulate) { |
| 866 | simulate_progress_bar(); |
| 867 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 868 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 869 | |
| 870 | operation_end(0, simulate); |
| 871 | return 0; |
| 872 | } |
| 873 | if (function == "htcdumlockrestoreboot") |
| 874 | { |
| 875 | operation_start("HTC Dumlock Restore Boot"); |
| 876 | if (simulate) { |
| 877 | simulate_progress_bar(); |
| 878 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 879 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 880 | |
| 881 | operation_end(0, simulate); |
| 882 | return 0; |
| 883 | } |
| 884 | if (function == "htcdumlockreflashrecovery") |
| 885 | { |
| 886 | operation_start("HTC Dumlock Reflash Recovery"); |
| 887 | if (simulate) { |
| 888 | simulate_progress_bar(); |
| 889 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 890 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | |
| 892 | operation_end(0, simulate); |
| 893 | return 0; |
| 894 | } |
| 895 | if (function == "cmd") |
| 896 | { |
| 897 | int op_status = 0; |
| 898 | |
| 899 | operation_start("Command"); |
| 900 | LOGI("Running command: '%s'\n", arg.c_str()); |
| 901 | if (simulate) { |
| 902 | simulate_progress_bar(); |
| 903 | } else { |
| 904 | op_status = __system(arg.c_str()); |
| 905 | if (op_status != 0) |
| 906 | op_status = 1; |
| 907 | } |
| 908 | |
| 909 | operation_end(op_status, simulate); |
| 910 | return 0; |
| 911 | } |
| 912 | if (function == "terminalcommand") |
| 913 | { |
| 914 | int op_status = 0; |
| 915 | string cmdpath, command; |
| 916 | |
| 917 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 918 | operation_start("CommandOutput"); |
| 919 | ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 920 | if (simulate) { |
| 921 | simulate_progress_bar(); |
| 922 | operation_end(op_status, simulate); |
| 923 | } else { |
| 924 | command = "cd \""; |
| 925 | command += cmdpath; |
| 926 | command += "\" && "; |
| 927 | command += arg; |
| 928 | LOGI("Actual command is: '%s'\n", command.c_str()); |
| 929 | DataManager::SetValue("tw_terminal_command_thread", command); |
| 930 | DataManager::SetValue("tw_terminal_state", 1); |
| 931 | DataManager::SetValue("tw_background_thread_running", 1); |
| 932 | op_status = pthread_create(&terminal_command, NULL, command_thread, NULL); |
| 933 | if (op_status != 0) { |
| 934 | LOGE("Error starting terminal command thread, %i.\n", op_status); |
| 935 | DataManager::SetValue("tw_terminal_state", 0); |
| 936 | DataManager::SetValue("tw_background_thread_running", 0); |
| 937 | operation_end(1, simulate); |
| 938 | } |
| 939 | } |
| 940 | return 0; |
| 941 | } |
| 942 | if (function == "killterminal") |
| 943 | { |
| 944 | int op_status = 0; |
| 945 | |
| 946 | LOGI("Sending kill command...\n"); |
| 947 | operation_start("KillCommand"); |
| 948 | DataManager::SetValue("tw_operation_status", 0); |
| 949 | DataManager::SetValue("tw_operation_state", 1); |
| 950 | DataManager::SetValue("tw_terminal_state", 0); |
| 951 | DataManager::SetValue("tw_background_thread_running", 0); |
| 952 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 953 | return 0; |
| 954 | } |
| 955 | if (function == "reinjecttwrp") |
| 956 | { |
| 957 | int op_status = 0; |
| 958 | |
| 959 | operation_start("ReinjectTWRP"); |
| 960 | ui_print("Injecting TWRP into boot image...\n"); |
| 961 | if (simulate) { |
| 962 | simulate_progress_bar(); |
| 963 | } else { |
| 964 | __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 965 | ui_print("TWRP injection complete.\n"); |
| 966 | } |
| 967 | |
| 968 | operation_end(op_status, simulate); |
| 969 | return 0; |
| 970 | } |
| 971 | if (function == "checkbackupname") |
| 972 | { |
| 973 | int op_status = 0; |
| 974 | |
| 975 | operation_start("CheckBackupName"); |
| 976 | if (simulate) { |
| 977 | simulate_progress_bar(); |
| 978 | } else { |
| 979 | op_status = check_backup_name(1); |
| 980 | if (op_status != 0) |
| 981 | op_status = 1; |
| 982 | } |
| 983 | |
| 984 | operation_end(op_status, simulate); |
| 985 | return 0; |
| 986 | } |
| 987 | if (function == "decrypt") |
| 988 | { |
| 989 | int op_status = 0; |
| 990 | |
| 991 | operation_start("Decrypt"); |
| 992 | if (simulate) { |
| 993 | simulate_progress_bar(); |
| 994 | } else { |
| 995 | string Password; |
| 996 | DataManager::GetValue("tw_crypto_password", Password); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 997 | op_status = PartitionManager.Decrypt_Device(Password); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 998 | if (op_status != 0) |
| 999 | op_status = 1; |
| 1000 | else { |
| 1001 | int load_theme = 1; |
| 1002 | |
| 1003 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1004 | DataManager::ReadSettingsFile(); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1005 | LOGE("TODO: Implement ORS support\n"); |
| 1006 | if (0/*check_for_script_file()*/) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1007 | ui_print("Processing OpenRecoveryScript file...\n"); |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 1008 | if (/*run_script_file() ==*/ 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1009 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 1010 | tw_reboot(rb_system); |
| 1011 | load_theme = 0; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (load_theme) { |
| 1016 | int has_datamedia; |
| 1017 | |
| 1018 | // Check for a custom theme and load it if exists |
| 1019 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1020 | if (has_datamedia != 0) { |
| 1021 | struct stat st; |
| 1022 | int check = 0; |
| 1023 | std::string theme_path; |
| 1024 | |
| 1025 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1026 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1027 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 1028 | check = 1; |
| 1029 | } |
| 1030 | |
| 1031 | theme_path += "/TWRP/theme/ui.zip"; |
| 1032 | if (check == 0 && stat(theme_path.c_str(), &st) == 0) { |
| 1033 | if (PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 1034 | { |
| 1035 | // Loading the custom theme failed - try loading the stock theme |
| 1036 | LOGI("Attempting to reload stock theme...\n"); |
| 1037 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 1038 | { |
| 1039 | LOGE("Failed to load base packages.\n"); |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | operation_end(op_status, simulate); |
| 1049 | return 0; |
| 1050 | } |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | pthread_t t; |
| 1055 | pthread_create(&t, NULL, thread_start, this); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | int GUIAction::getKeyByName(std::string key) |
| 1062 | { |
| 1063 | if (key == "home") return KEY_HOME; |
| 1064 | else if (key == "menu") return KEY_MENU; |
| 1065 | else if (key == "back") return KEY_BACK; |
| 1066 | else if (key == "search") return KEY_SEARCH; |
| 1067 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1068 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1069 | else if (key == "power") { |
| 1070 | int ret_val; |
| 1071 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1072 | if (!ret_val) |
| 1073 | return KEY_POWER; |
| 1074 | else |
| 1075 | return ret_val; |
| 1076 | } |
| 1077 | |
| 1078 | return atol(key.c_str()); |
| 1079 | } |
| 1080 | |
| 1081 | void* GUIAction::command_thread(void *cookie) |
| 1082 | { |
| 1083 | string command; |
| 1084 | FILE* fp; |
| 1085 | char line[512]; |
| 1086 | |
| 1087 | DataManager::GetValue("tw_terminal_command_thread", command); |
| 1088 | fp = __popen(command.c_str(), "r"); |
| 1089 | if (fp == NULL) { |
| 1090 | LOGE("Error opening command to run.\n"); |
| 1091 | } else { |
| 1092 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0; |
| 1093 | struct timeval timeout; |
| 1094 | fd_set fdset; |
| 1095 | |
| 1096 | while(keep_going) |
| 1097 | { |
| 1098 | FD_ZERO(&fdset); |
| 1099 | FD_SET(fd, &fdset); |
| 1100 | timeout.tv_sec = 0; |
| 1101 | timeout.tv_usec = 400000; |
| 1102 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1103 | if (has_data == 0) { |
| 1104 | // Timeout reached |
| 1105 | DataManager::GetValue("tw_terminal_state", check); |
| 1106 | if (check == 0) { |
| 1107 | keep_going = 0; |
| 1108 | } |
| 1109 | } else if (has_data < 0) { |
| 1110 | // End of execution |
| 1111 | keep_going = 0; |
| 1112 | } else { |
| 1113 | // Try to read output |
| 1114 | bytes_read = read(fd, line, sizeof(line)); |
| 1115 | if (bytes_read > 0) |
| 1116 | ui_print("%s", line); // Display output |
| 1117 | else |
| 1118 | keep_going = 0; // Done executing |
| 1119 | } |
| 1120 | } |
| 1121 | fclose(fp); |
| 1122 | } |
| 1123 | DataManager::SetValue("tw_operation_status", 0); |
| 1124 | DataManager::SetValue("tw_operation_state", 1); |
| 1125 | DataManager::SetValue("tw_terminal_state", 0); |
| 1126 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1127 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1128 | return NULL; |
| 1129 | } |