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