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