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