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