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