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