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