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