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