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