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