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