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