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