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; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 91 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 505 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 519 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 536 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 592 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 599 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 600 | int dst; |
| 601 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 602 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 603 | string offset; |
| 604 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 605 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 606 | string NewTimeZone = Zone; |
| 607 | if (offset != "0") |
| 608 | NewTimeZone += ":" + offset; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 609 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 610 | if (dst != 0) |
| 611 | NewTimeZone += DSTZone; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 612 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | } |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 622 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 687 | if (function == "checkpartitionlist") { |
| 688 | string Wipe_List, wipe_path; |
| 689 | int count = 0; |
| 690 | |
| 691 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 692 | LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str()); |
| 693 | if (!Wipe_List.empty()) { |
| 694 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 695 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 696 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 697 | LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str()); |
| 698 | if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") { |
| 699 | // Do nothing |
| 700 | } else { |
| 701 | count++; |
| 702 | } |
| 703 | start_pos = end_pos + 1; |
| 704 | end_pos = Wipe_List.find(";", start_pos); |
| 705 | } |
| 706 | DataManager::SetValue("tw_check_partition_list", count); |
| 707 | } else { |
| 708 | DataManager::SetValue("tw_check_partition_list", 0); |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | if (function == "getpartitiondetails") { |
| 713 | string Wipe_List, wipe_path; |
| 714 | int count = 0; |
| 715 | |
| 716 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 717 | LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str()); |
| 718 | if (!Wipe_List.empty()) { |
| 719 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 720 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 721 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 722 | LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str()); |
| 723 | if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") { |
| 724 | // Do nothing |
| 725 | } else { |
| 726 | DataManager::SetValue("tw_partition_path", wipe_path); |
| 727 | break; |
| 728 | } |
| 729 | start_pos = end_pos + 1; |
| 730 | end_pos = Wipe_List.find(";", start_pos); |
| 731 | } |
| 732 | if (!wipe_path.empty()) { |
| 733 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path); |
| 734 | if (Part) { |
| 735 | unsigned long long mb = 1048576; |
| 736 | |
| 737 | DataManager::SetValue("tw_partition_name", Part->Display_Name); |
| 738 | DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point); |
| 739 | DataManager::SetValue("tw_partition_file_system", Part->Current_File_System); |
| 740 | DataManager::SetValue("tw_partition_size", Part->Size / mb); |
| 741 | DataManager::SetValue("tw_partition_used", Part->Used / mb); |
| 742 | DataManager::SetValue("tw_partition_free", Part->Free / mb); |
| 743 | DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb); |
| 744 | DataManager::SetValue("tw_partition_removable", Part->Removable); |
| 745 | DataManager::SetValue("tw_partition_is_present", Part->Is_Present); |
| 746 | |
| 747 | if (Part->Can_Repair()) |
| 748 | DataManager::SetValue("tw_partition_can_repair", 1); |
| 749 | else |
| 750 | DataManager::SetValue("tw_partition_can_repair", 0); |
| 751 | if (TWFunc::Path_Exists("/sbin/mkdosfs")) |
| 752 | DataManager::SetValue("tw_partition_vfat", 1); |
| 753 | else |
| 754 | DataManager::SetValue("tw_partition_vfat", 0); |
| 755 | if (TWFunc::Path_Exists("/sbin/mkfs.exfat")) |
| 756 | DataManager::SetValue("tw_partition_exfat", 1); |
| 757 | else |
| 758 | DataManager::SetValue("tw_partition_exfat", 0); |
| 759 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) |
| 760 | DataManager::SetValue("tw_partition_f2fs", 1); |
| 761 | else |
| 762 | DataManager::SetValue("tw_partition_f2fs", 0); |
| 763 | if (TWFunc::Path_Exists("/sbin/mke2fs")) |
| 764 | DataManager::SetValue("tw_partition_ext", 1); |
| 765 | else |
| 766 | DataManager::SetValue("tw_partition_ext", 0); |
| 767 | return 0; |
| 768 | } else { |
| 769 | LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str()); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | DataManager::SetValue("tw_partition_name", ""); |
| 774 | DataManager::SetValue("tw_partition_file_system", ""); |
| 775 | return 0; |
| 776 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 777 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 778 | if (isThreaded) |
| 779 | { |
| 780 | if (function == "fileexists") |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 781 | { |
| 782 | struct stat st; |
| 783 | string newpath = arg + "/."; |
| 784 | |
| 785 | operation_start("FileExists"); |
| 786 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 787 | operation_end(0, simulate); |
| 788 | else |
| 789 | operation_end(1, simulate); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 790 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (function == "flash") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 794 | { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 795 | int i, ret_val = 0, wipe_cache = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 796 | |
| 797 | for (i=0; i<zip_queue_index; i++) { |
| 798 | operation_start("Flashing"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 799 | DataManager::SetValue("tw_filename", zip_queue[i]); |
| 800 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 801 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 802 | ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 803 | if (ret_val != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 804 | gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 805 | i = 10; // Error flashing zip - exit queue |
| 806 | ret_val = 1; |
| 807 | } |
| 808 | } |
| 809 | zip_queue_index = 0; |
| 810 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 811 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 812 | if (wipe_cache) |
| 813 | PartitionManager.Wipe_By_Path("/cache"); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 814 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 815 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 816 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 817 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 818 | if (simulate) { |
| 819 | simulate_progress_bar(); |
| 820 | } else { |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 821 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 822 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 823 | 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] | 824 | else { |
| 825 | 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] | 826 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 827 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 828 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 829 | } |
| 830 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 831 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 832 | operation_end(ret_val, simulate); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | if (function == "wipe") |
| 836 | { |
| 837 | operation_start("Format"); |
| 838 | DataManager::SetValue("tw_partition", arg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 839 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 840 | int ret_val = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 841 | |
| 842 | if (simulate) { |
| 843 | simulate_progress_bar(); |
| 844 | } else { |
| 845 | if (arg == "data") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 846 | ret_val = PartitionManager.Factory_Reset(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 847 | else if (arg == "battery") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 848 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 849 | else if (arg == "rotate") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 850 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 851 | else if (arg == "dalvik") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 852 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 853 | else if (arg == "DATAMEDIA") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 854 | ret_val = PartitionManager.Format_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 855 | } else if (arg == "INTERNAL") { |
| 856 | int has_datamedia, dual_storage; |
| 857 | |
| 858 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 859 | if (has_datamedia) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 860 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 861 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 862 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 863 | } |
| 864 | } else if (arg == "EXTERNAL") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 865 | string External_Path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 866 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 867 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 868 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 869 | } else if (arg == "ANDROIDSECURE") { |
| 870 | ret_val = PartitionManager.Wipe_Android_Secure(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 871 | } else if (arg == "LIST") { |
| 872 | string Wipe_List, wipe_path; |
| 873 | bool skip = false; |
| 874 | ret_val = true; |
| 875 | TWPartition* wipe_part = NULL; |
| 876 | |
| 877 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 878 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 879 | if (!Wipe_List.empty()) { |
| 880 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 881 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 882 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 883 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 884 | if (wipe_path == "/and-sec") { |
| 885 | if (!PartitionManager.Wipe_Android_Secure()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 886 | LOGERR("Unable to wipe android secure\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 887 | ret_val = false; |
| 888 | break; |
| 889 | } else { |
| 890 | skip = true; |
| 891 | } |
| 892 | } else if (wipe_path == "DALVIK") { |
| 893 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 894 | LOGERR("Failed to wipe dalvik\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 895 | ret_val = false; |
| 896 | break; |
| 897 | } else { |
| 898 | skip = true; |
| 899 | } |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 900 | } else if (wipe_path == "INTERNAL") { |
| 901 | if (!PartitionManager.Wipe_Media_From_Data()) { |
| 902 | ret_val = false; |
| 903 | break; |
| 904 | } else { |
| 905 | skip = true; |
| 906 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 907 | } |
| 908 | if (!skip) { |
| 909 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 910 | LOGERR("Unable to wipe '%s'\n", wipe_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 911 | ret_val = false; |
| 912 | break; |
| 913 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 914 | arg = wipe_path; |
| 915 | } |
| 916 | } else { |
| 917 | skip = false; |
| 918 | } |
| 919 | start_pos = end_pos + 1; |
| 920 | end_pos = Wipe_List.find(";", start_pos); |
| 921 | } |
| 922 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 923 | } else |
| 924 | ret_val = PartitionManager.Wipe_By_Path(arg); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 925 | #ifdef TW_OEM_BUILD |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 926 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 927 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 928 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
| 929 | |
| 930 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 931 | LOGINFO("Making TWRP folder and saving settings.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 932 | Storage_Path += "/TWRP"; |
| 933 | mkdir(Storage_Path.c_str(), 0777); |
| 934 | DataManager::Flush(); |
| 935 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 936 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 937 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 938 | } |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 939 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 940 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 941 | PartitionManager.Update_System_Details(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 942 | if (ret_val) |
| 943 | ret_val = 0; // 0 is success |
| 944 | else |
| 945 | ret_val = 1; // 1 is failure |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 946 | operation_end(ret_val, simulate); |
| 947 | return 0; |
| 948 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 949 | if (function == "refreshsizes") |
| 950 | { |
| 951 | operation_start("Refreshing Sizes"); |
| 952 | if (simulate) { |
| 953 | simulate_progress_bar(); |
| 954 | } else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 955 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 956 | operation_end(0, simulate); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 957 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 958 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 959 | if (function == "nandroid") |
| 960 | { |
| 961 | operation_start("Nandroid"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 962 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 963 | |
| 964 | if (simulate) { |
| 965 | DataManager::SetValue("tw_partition", "Simulation"); |
| 966 | simulate_progress_bar(); |
| 967 | } else { |
| 968 | if (arg == "backup") { |
| 969 | string Backup_Name; |
| 970 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 971 | 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] | 972 | ret = PartitionManager.Run_Backup(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 973 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 974 | else { |
| 975 | operation_end(1, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 976 | return -1; |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 977 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 978 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 979 | DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 980 | } else if (arg == "restore") { |
| 981 | string Restore_Name; |
| 982 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 983 | ret = PartitionManager.Run_Restore(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 984 | } else { |
| 985 | operation_end(1, simulate); |
| 986 | return -1; |
| 987 | } |
| 988 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 989 | DataManager::SetValue("tw_encrypt_backup", 0); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 990 | if (ret == false) |
| 991 | ret = 1; // 1 for failure |
| 992 | else |
| 993 | ret = 0; // 0 for success |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 994 | operation_end(ret, simulate); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 995 | return 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 996 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 997 | if (function == "fixpermissions") |
| 998 | { |
| 999 | operation_start("Fix Permissions"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1000 | LOGINFO("fix permissions started!\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1001 | if (simulate) { |
| 1002 | simulate_progress_bar(); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1003 | } else { |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 1004 | int op_status = PartitionManager.Fix_Permissions(); |
| 1005 | if (op_status != 0) |
| 1006 | op_status = 1; // failure |
| 1007 | operation_end(op_status, simulate); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1008 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1011 | if (function == "dd") |
| 1012 | { |
| 1013 | operation_start("imaging"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1014 | |
| 1015 | if (simulate) { |
| 1016 | simulate_progress_bar(); |
| 1017 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1018 | string cmd = "dd " + arg; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1019 | TWFunc::Exec_Cmd(cmd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1020 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1021 | operation_end(0, simulate); |
| 1022 | return 0; |
| 1023 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1024 | if (function == "partitionsd") |
| 1025 | { |
| 1026 | operation_start("Partition SD Card"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1027 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1028 | |
| 1029 | if (simulate) { |
| 1030 | simulate_progress_bar(); |
| 1031 | } else { |
| 1032 | int allow_partition; |
| 1033 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 1034 | if (allow_partition == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1035 | 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] | 1036 | } else { |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1037 | if (!PartitionManager.Partition_SDCard()) |
| 1038 | ret_val = 1; // failed |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1039 | } |
| 1040 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1041 | operation_end(ret_val, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | if (function == "installhtcdumlock") |
| 1045 | { |
| 1046 | operation_start("Install HTC Dumlock"); |
| 1047 | if (simulate) { |
| 1048 | simulate_progress_bar(); |
| 1049 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1050 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1051 | |
| 1052 | operation_end(0, simulate); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | if (function == "htcdumlockrestoreboot") |
| 1056 | { |
| 1057 | operation_start("HTC Dumlock Restore Boot"); |
| 1058 | if (simulate) { |
| 1059 | simulate_progress_bar(); |
| 1060 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1061 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1062 | |
| 1063 | operation_end(0, simulate); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | if (function == "htcdumlockreflashrecovery") |
| 1067 | { |
| 1068 | operation_start("HTC Dumlock Reflash Recovery"); |
| 1069 | if (simulate) { |
| 1070 | simulate_progress_bar(); |
| 1071 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1072 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1073 | |
| 1074 | operation_end(0, simulate); |
| 1075 | return 0; |
| 1076 | } |
| 1077 | if (function == "cmd") |
| 1078 | { |
| 1079 | int op_status = 0; |
| 1080 | |
| 1081 | operation_start("Command"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1082 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1083 | if (simulate) { |
| 1084 | simulate_progress_bar(); |
| 1085 | } else { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1086 | op_status = TWFunc::Exec_Cmd(arg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1087 | if (op_status != 0) |
| 1088 | op_status = 1; |
| 1089 | } |
| 1090 | |
| 1091 | operation_end(op_status, simulate); |
| 1092 | return 0; |
| 1093 | } |
| 1094 | if (function == "terminalcommand") |
| 1095 | { |
| 1096 | int op_status = 0; |
| 1097 | string cmdpath, command; |
| 1098 | |
| 1099 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1100 | operation_start("CommandOutput"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1101 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1102 | if (simulate) { |
| 1103 | simulate_progress_bar(); |
| 1104 | operation_end(op_status, simulate); |
| 1105 | } else { |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1106 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1107 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1108 | DataManager::SetValue("tw_terminal_command_thread", command); |
| 1109 | DataManager::SetValue("tw_terminal_state", 1); |
| 1110 | DataManager::SetValue("tw_background_thread_running", 1); |
| 1111 | op_status = pthread_create(&terminal_command, NULL, command_thread, NULL); |
| 1112 | if (op_status != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1113 | LOGERR("Error starting terminal command thread, %i.\n", op_status); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1114 | DataManager::SetValue("tw_terminal_state", 0); |
| 1115 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1116 | operation_end(1, simulate); |
| 1117 | } |
| 1118 | } |
| 1119 | return 0; |
| 1120 | } |
| 1121 | if (function == "killterminal") |
| 1122 | { |
| 1123 | int op_status = 0; |
| 1124 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1125 | LOGINFO("Sending kill command...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1126 | operation_start("KillCommand"); |
| 1127 | DataManager::SetValue("tw_operation_status", 0); |
| 1128 | DataManager::SetValue("tw_operation_state", 1); |
| 1129 | DataManager::SetValue("tw_terminal_state", 0); |
| 1130 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1131 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | if (function == "reinjecttwrp") |
| 1135 | { |
| 1136 | int op_status = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1137 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1138 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1139 | if (simulate) { |
| 1140 | simulate_progress_bar(); |
| 1141 | } else { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1142 | 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] | 1143 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | operation_end(op_status, simulate); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | if (function == "checkbackupname") |
| 1150 | { |
| 1151 | int op_status = 0; |
| 1152 | |
| 1153 | operation_start("CheckBackupName"); |
| 1154 | if (simulate) { |
| 1155 | simulate_progress_bar(); |
| 1156 | } else { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 1157 | op_status = PartitionManager.Check_Backup_Name(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1158 | if (op_status != 0) |
| 1159 | op_status = 1; |
| 1160 | } |
| 1161 | |
| 1162 | operation_end(op_status, simulate); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | if (function == "decrypt") |
| 1166 | { |
| 1167 | int op_status = 0; |
| 1168 | |
| 1169 | operation_start("Decrypt"); |
| 1170 | if (simulate) { |
| 1171 | simulate_progress_bar(); |
| 1172 | } else { |
| 1173 | string Password; |
| 1174 | DataManager::GetValue("tw_crypto_password", Password); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1175 | op_status = PartitionManager.Decrypt_Device(Password); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1176 | if (op_status != 0) |
| 1177 | op_status = 1; |
| 1178 | else { |
| 1179 | int load_theme = 1; |
| 1180 | |
| 1181 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1182 | |
| 1183 | if (load_theme) { |
| 1184 | int has_datamedia; |
| 1185 | |
| 1186 | // Check for a custom theme and load it if exists |
| 1187 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1188 | if (has_datamedia != 0) { |
| 1189 | struct stat st; |
| 1190 | int check = 0; |
| 1191 | std::string theme_path; |
| 1192 | |
| 1193 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1194 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1195 | 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] | 1196 | check = 1; |
| 1197 | } |
| 1198 | |
| 1199 | theme_path += "/TWRP/theme/ui.zip"; |
| 1200 | if (check == 0 && stat(theme_path.c_str(), &st) == 0) { |
| 1201 | if (PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 1202 | { |
| 1203 | // Loading the custom theme failed - try loading the stock theme |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1204 | LOGINFO("Attempting to reload stock theme...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1205 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 1206 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1207 | LOGERR("Failed to load base packages.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | operation_end(op_status, simulate); |
| 1217 | return 0; |
| 1218 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1219 | if (function == "adbsideload") |
| 1220 | { |
| 1221 | int ret = 0; |
| 1222 | |
| 1223 | operation_start("Sideload"); |
| 1224 | if (simulate) { |
| 1225 | simulate_progress_bar(); |
| 1226 | } else { |
| 1227 | int wipe_cache = 0; |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1228 | int wipe_dalvik = 0; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1229 | string Sideload_File; |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1230 | |
Ethan Yonker | 45312e5 | 2014-02-26 09:23:53 -0600 | [diff] [blame] | 1231 | if (!PartitionManager.Mount_Current_Storage(false)) { |
| 1232 | gui_print("Using RAM for sideload storage.\n"); |
| 1233 | Sideload_File = "/tmp/sideload.zip"; |
| 1234 | } else { |
| 1235 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1236 | } |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1237 | if (TWFunc::Path_Exists(Sideload_File)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1238 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1239 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1240 | gui_print("Starting ADB sideload feature...\n"); |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1241 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1242 | ret = apply_from_adb(Sideload_File.c_str()); |
| 1243 | 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] | 1244 | if (ret != 0) { |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1245 | ret = 1; // failure |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1246 | } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) { |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1247 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1248 | PartitionManager.Wipe_By_Path("/cache"); |
| 1249 | if (wipe_dalvik) |
| 1250 | PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1251 | } else { |
| 1252 | ret = 1; // failure |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1253 | } |
Dees Troy | 8c0f06b | 2013-11-26 21:21:29 +0000 | [diff] [blame] | 1254 | PartitionManager.Update_System_Details(); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1255 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 1256 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1257 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1258 | if (simulate) { |
| 1259 | simulate_progress_bar(); |
| 1260 | } else { |
| 1261 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1262 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1263 | 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] | 1264 | else { |
| 1265 | 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] | 1266 | TWFunc::Exec_Cmd(injectcmd); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1267 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1268 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1269 | } |
| 1270 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1271 | } |
| 1272 | operation_end(ret, simulate); |
| 1273 | return 0; |
| 1274 | } |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1275 | if (function == "adbsideloadcancel") |
| 1276 | { |
| 1277 | int child_pid; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1278 | char child_prop[PROPERTY_VALUE_MAX]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1279 | string Sideload_File; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1280 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1281 | unlink(Sideload_File.c_str()); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1282 | property_get("tw_child_pid", child_prop, "error"); |
| 1283 | if (strcmp(child_prop, "error") == 0) { |
| 1284 | LOGERR("Unable to get child ID from prop\n"); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | child_pid = atoi(child_prop); |
| 1288 | gui_print("Cancelling ADB sideload...\n"); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1289 | kill(child_pid, SIGTERM); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 1290 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1291 | return 0; |
| 1292 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1293 | if (function == "openrecoveryscript") { |
| 1294 | operation_start("OpenRecoveryScript"); |
| 1295 | if (simulate) { |
| 1296 | simulate_progress_bar(); |
| 1297 | } else { |
| 1298 | // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands |
| 1299 | // that we converted to ORS commands during boot in recovery.cpp. |
| 1300 | // Run those first. |
| 1301 | int reboot = 0; |
| 1302 | if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1303 | gui_print("Processing AOSP recovery commands...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1304 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1305 | reboot = 1; |
| 1306 | } |
| 1307 | } |
| 1308 | // Check for the ORS file in /cache and attempt to run those commands. |
| 1309 | if (OpenRecoveryScript::check_for_script_file()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1310 | gui_print("Processing OpenRecoveryScript file...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1311 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1312 | reboot = 1; |
| 1313 | } |
| 1314 | } |
| 1315 | if (reboot) { |
| 1316 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 1317 | TWFunc::tw_reboot(rb_system); |
| 1318 | } else { |
| 1319 | DataManager::SetValue("tw_page_done", 1); |
| 1320 | } |
| 1321 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1322 | return 0; |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1323 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1324 | if (function == "installsu") |
| 1325 | { |
| 1326 | int op_status = 0; |
| 1327 | |
| 1328 | operation_start("Install SuperSU"); |
| 1329 | if (simulate) { |
| 1330 | simulate_progress_bar(); |
| 1331 | } else { |
| 1332 | if (!TWFunc::Install_SuperSU()) |
| 1333 | op_status = 1; |
| 1334 | } |
| 1335 | |
| 1336 | operation_end(op_status, simulate); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | if (function == "fixsu") |
| 1340 | { |
| 1341 | int op_status = 0; |
| 1342 | |
| 1343 | operation_start("Fixing Superuser Permissions"); |
| 1344 | if (simulate) { |
| 1345 | simulate_progress_bar(); |
| 1346 | } else { |
Dees Troy | f193f88 | 2013-09-11 14:56:20 +0000 | [diff] [blame] | 1347 | LOGERR("Fixing su permissions was deprecated from TWRP.\n"); |
| 1348 | LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | operation_end(op_status, simulate); |
| 1352 | return 0; |
| 1353 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1354 | if (function == "decrypt_backup") |
| 1355 | { |
| 1356 | int op_status = 0; |
| 1357 | |
| 1358 | operation_start("Try Restore Decrypt"); |
| 1359 | if (simulate) { |
| 1360 | simulate_progress_bar(); |
| 1361 | } else { |
| 1362 | string Restore_Path, Filename, Password; |
| 1363 | DataManager::GetValue("tw_restore", Restore_Path); |
| 1364 | Restore_Path += "/"; |
| 1365 | DataManager::GetValue("tw_restore_password", Password); |
| 1366 | if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password)) |
| 1367 | op_status = 0; // success |
| 1368 | else |
| 1369 | op_status = 1; // fail |
| 1370 | } |
| 1371 | |
| 1372 | operation_end(op_status, simulate); |
| 1373 | return 0; |
| 1374 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1375 | if (function == "repair") |
| 1376 | { |
| 1377 | int op_status = 0; |
| 1378 | |
| 1379 | operation_start("Repair Partition"); |
| 1380 | if (simulate) { |
| 1381 | simulate_progress_bar(); |
| 1382 | } else { |
| 1383 | string part_path; |
| 1384 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1385 | if (PartitionManager.Repair_By_Path(part_path, true)) { |
| 1386 | op_status = 0; // success |
| 1387 | } else { |
| 1388 | LOGERR("Error repairing file system.\n"); |
| 1389 | op_status = 1; // fail |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | operation_end(op_status, simulate); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | if (function == "changefilesystem") |
| 1397 | { |
| 1398 | int op_status = 0; |
| 1399 | |
| 1400 | operation_start("Change File System"); |
| 1401 | if (simulate) { |
| 1402 | simulate_progress_bar(); |
| 1403 | } else { |
| 1404 | string part_path, file_system; |
| 1405 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1406 | DataManager::GetValue("tw_action_new_file_system", file_system); |
| 1407 | if (PartitionManager.Wipe_By_Path(part_path, file_system)) { |
| 1408 | op_status = 0; // success |
| 1409 | } else { |
| 1410 | LOGERR("Error changing file system.\n"); |
| 1411 | op_status = 1; // fail |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | operation_end(op_status, simulate); |
| 1416 | return 0; |
| 1417 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1418 | } |
| 1419 | else |
| 1420 | { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1421 | pthread_t t; |
| 1422 | pthread_create(&t, NULL, thread_start, this); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1425 | LOGERR("Unknown action '%s'\n", function.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1426 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | int GUIAction::getKeyByName(std::string key) |
| 1430 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1431 | if (key == "home") return KEY_HOME; |
| 1432 | else if (key == "menu") return KEY_MENU; |
| 1433 | else if (key == "back") return KEY_BACK; |
| 1434 | else if (key == "search") return KEY_SEARCH; |
| 1435 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1436 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1437 | else if (key == "power") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1438 | int ret_val; |
| 1439 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1440 | if (!ret_val) |
| 1441 | return KEY_POWER; |
| 1442 | else |
| 1443 | return ret_val; |
| 1444 | } |
| 1445 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1446 | return atol(key.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | void* GUIAction::command_thread(void *cookie) |
| 1450 | { |
| 1451 | string command; |
| 1452 | FILE* fp; |
| 1453 | char line[512]; |
| 1454 | |
| 1455 | DataManager::GetValue("tw_terminal_command_thread", command); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1456 | fp = popen(command.c_str(), "r"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1457 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1458 | LOGERR("Error opening command to run.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1459 | } else { |
| 1460 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0; |
| 1461 | struct timeval timeout; |
| 1462 | fd_set fdset; |
| 1463 | |
| 1464 | while(keep_going) |
| 1465 | { |
| 1466 | FD_ZERO(&fdset); |
| 1467 | FD_SET(fd, &fdset); |
| 1468 | timeout.tv_sec = 0; |
| 1469 | timeout.tv_usec = 400000; |
| 1470 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1471 | if (has_data == 0) { |
| 1472 | // Timeout reached |
| 1473 | DataManager::GetValue("tw_terminal_state", check); |
| 1474 | if (check == 0) { |
| 1475 | keep_going = 0; |
| 1476 | } |
| 1477 | } else if (has_data < 0) { |
| 1478 | // End of execution |
| 1479 | keep_going = 0; |
| 1480 | } else { |
| 1481 | // Try to read output |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1482 | memset(line, 0, sizeof(line)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1483 | bytes_read = read(fd, line, sizeof(line)); |
| 1484 | if (bytes_read > 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1485 | gui_print("%s", line); // Display output |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1486 | else |
| 1487 | keep_going = 0; // Done executing |
| 1488 | } |
| 1489 | } |
| 1490 | fclose(fp); |
| 1491 | } |
| 1492 | DataManager::SetValue("tw_operation_status", 0); |
| 1493 | DataManager::SetValue("tw_operation_state", 1); |
| 1494 | DataManager::SetValue("tw_terminal_state", 0); |
| 1495 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1496 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1497 | return NULL; |
| 1498 | } |