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