Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1 | /* |
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> |
Matt Mower | 0c88b84 | 2017-01-15 16:00:49 -0600 | [diff] [blame] | 35 | #include <private/android_filesystem_config.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" |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 41 | #include "../twrpRepacker.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 42 | #include "../openrecoveryscript.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 44 | #include "../adb_install.h" |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 45 | #include "../fuse_sideload.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 46 | #include "blanktimer.hpp" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 47 | #include "../twinstall.h" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 48 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 50 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | #include "../variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 52 | #include "cutils/properties.h" |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 53 | #include "../adb_install.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | }; |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 55 | #include "../set_metadata.h" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 56 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
| 58 | #include "rapidxml.hpp" |
| 59 | #include "objects.hpp" |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 60 | #include "../tw_atomic.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 62 | GUIAction::mapFunc GUIAction::mf; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 63 | std::set<string> GUIAction::setActionsRunningInCallerThread; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 64 | static string zip_queue[10]; |
| 65 | static int zip_queue_index; |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 66 | pid_t sideload_child_pid; |
Noah Jacobson | 5a79f67 | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 67 | extern std::vector<users_struct> Users_List; |
Mohd Faraz | 3c25ab3 | 2020-08-12 12:29:42 +0000 | [diff] [blame] | 68 | extern GUITerminal* term; |
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); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 112 | if (m_thread_running) { |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 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); |
Matt Mower | 9a2a205 | 2016-05-31 21:31:22 -0500 | [diff] [blame] | 185 | ADD_ACTION(sleepcounter); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 186 | ADD_ACTION(appenddatetobackupname); |
| 187 | ADD_ACTION(generatebackupname); |
| 188 | ADD_ACTION(checkpartitionlist); |
| 189 | ADD_ACTION(getpartitiondetails); |
| 190 | ADD_ACTION(screenshot); |
| 191 | ADD_ACTION(setbrightness); |
| 192 | ADD_ACTION(fileexists); |
| 193 | ADD_ACTION(killterminal); |
| 194 | ADD_ACTION(checkbackupname); |
| 195 | ADD_ACTION(adbsideloadcancel); |
| 196 | ADD_ACTION(fixsu); |
| 197 | ADD_ACTION(startmtp); |
| 198 | ADD_ACTION(stopmtp); |
| 199 | ADD_ACTION(cancelbackup); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 200 | ADD_ACTION(checkpartitionlifetimewrites); |
| 201 | ADD_ACTION(mountsystemtoggle); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 202 | ADD_ACTION(setlanguage); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 203 | ADD_ACTION(checkforapp); |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 204 | ADD_ACTION(togglebacklight); |
Mohd Faraz | 3c25ab3 | 2020-08-12 12:29:42 +0000 | [diff] [blame] | 205 | ADD_ACTION(changeterminal); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 206 | |
| 207 | // remember actions that run in the caller thread |
| 208 | for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it) |
| 209 | setActionsRunningInCallerThread.insert(it->first); |
| 210 | |
| 211 | // These actions will run in a separate thread |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 212 | ADD_ACTION(flash); |
| 213 | ADD_ACTION(wipe); |
| 214 | ADD_ACTION(refreshsizes); |
| 215 | ADD_ACTION(nandroid); |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 216 | ADD_ACTION(fixcontexts); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 217 | ADD_ACTION(fixpermissions); |
| 218 | ADD_ACTION(dd); |
| 219 | ADD_ACTION(partitionsd); |
| 220 | ADD_ACTION(installhtcdumlock); |
| 221 | ADD_ACTION(htcdumlockrestoreboot); |
| 222 | ADD_ACTION(htcdumlockreflashrecovery); |
| 223 | ADD_ACTION(cmd); |
| 224 | ADD_ACTION(terminalcommand); |
| 225 | ADD_ACTION(reinjecttwrp); |
| 226 | ADD_ACTION(decrypt); |
| 227 | ADD_ACTION(adbsideload); |
| 228 | ADD_ACTION(openrecoveryscript); |
| 229 | ADD_ACTION(installsu); |
| 230 | ADD_ACTION(decrypt_backup); |
| 231 | ADD_ACTION(repair); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 232 | ADD_ACTION(resize); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 233 | ADD_ACTION(changefilesystem); |
| 234 | ADD_ACTION(flashimage); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 235 | ADD_ACTION(twcmd); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 236 | ADD_ACTION(setbootslot); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 237 | ADD_ACTION(installapp); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 238 | ADD_ACTION(uninstalltwrpsystemapp); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 239 | ADD_ACTION(repackimage); |
nebrassy | fe1c237 | 2021-05-20 13:03:30 +0200 | [diff] [blame] | 240 | ADD_ACTION(reflashtwrp); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 241 | ADD_ACTION(fixabrecoverybootloop); |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 242 | ADD_ACTION(applycustomtwrpfolder); |
Captain Throwback | 3b55610 | 2021-02-12 19:32:36 -0500 | [diff] [blame] | 243 | #ifndef TW_EXCLUDE_NANO |
| 244 | ADD_ACTION(editfile); |
| 245 | #endif |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 248 | // First, get the action |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 249 | actions = FindNode(node, "actions"); |
| 250 | if (actions) child = FindNode(actions, "action"); |
| 251 | else child = FindNode(node, "action"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 252 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 253 | if (!child) return; |
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 | while (child) |
| 256 | { |
| 257 | Action action; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 258 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 259 | attr = child->first_attribute("function"); |
| 260 | if (!attr) return; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 261 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 262 | action.mFunction = attr->value(); |
| 263 | action.mArg = child->value(); |
| 264 | mActions.push_back(action); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 265 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 266 | child = child->next_sibling("action"); |
| 267 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 268 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 269 | // Now, let's get either the key or region |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 270 | child = FindNode(node, "touch"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 271 | if (child) |
| 272 | { |
| 273 | attr = child->first_attribute("key"); |
| 274 | if (attr) |
| 275 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 276 | std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 277 | for (size_t i = 0; i < keys.size(); ++i) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 278 | { |
| 279 | const int key = getKeyByName(keys[i]); |
| 280 | mKeys[key] = false; |
| 281 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 282 | } |
| 283 | else |
| 284 | { |
| 285 | attr = child->first_attribute("x"); |
| 286 | if (!attr) return; |
| 287 | mActionX = atol(attr->value()); |
| 288 | attr = child->first_attribute("y"); |
| 289 | if (!attr) return; |
| 290 | mActionY = atol(attr->value()); |
| 291 | attr = child->first_attribute("w"); |
| 292 | if (!attr) return; |
| 293 | mActionW = atol(attr->value()); |
| 294 | attr = child->first_attribute("h"); |
| 295 | if (!attr) return; |
| 296 | mActionH = atol(attr->value()); |
| 297 | } |
| 298 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Matt Mower | 25036b7 | 2016-06-24 12:30:18 -0500 | [diff] [blame] | 301 | int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 302 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 303 | if (state == TOUCH_RELEASE) |
| 304 | doActions(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 305 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 306 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 309 | int GUIAction::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 310 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 311 | std::map<int, bool>::iterator itr = mKeys.find(key); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 312 | if (itr == mKeys.end()) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 313 | return 1; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 314 | |
| 315 | bool prevState = itr->second; |
| 316 | itr->second = down; |
| 317 | |
| 318 | // If there is only one key for this action, wait for key up so it |
| 319 | // doesn't trigger with multi-key actions. |
| 320 | // Else, check if all buttons are pressed, then consume their release events |
| 321 | // so they don't trigger one-button actions and reset mKeys pressed status |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 322 | if (mKeys.size() == 1) { |
| 323 | if (!down && prevState) { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 324 | doActions(); |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 325 | return 0; |
| 326 | } |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 327 | } else if (down) { |
| 328 | for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) { |
| 329 | if (!itr->second) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 330 | return 1; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Passed, all req buttons are pressed, reset them and consume release events |
| 334 | HardwareKeyboard *kb = PageManager::GetHardwareKeyboard(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 335 | for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 336 | kb->ConsumeKeyRelease(itr->first); |
| 337 | itr->second = false; |
| 338 | } |
| 339 | |
| 340 | doActions(); |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 341 | return 0; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 342 | } |
| 343 | |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 344 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 347 | int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 348 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 349 | GUIObject::NotifyVarChange(varName, value); |
| 350 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 351 | if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 352 | doActions(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 353 | else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 354 | doActions(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 356 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void GUIAction::simulate_progress_bar(void) |
| 360 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 361 | gui_msg("simulating=Simulating actions..."); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 362 | for (int i = 0; i < 5; i++) |
| 363 | { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 364 | if (PartitionManager.stop_backup.get_value()) { |
| 365 | DataManager::SetValue("tw_cancel_backup", 1); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 366 | gui_msg("backup_cancel=Backup Cancelled"); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 367 | DataManager::SetValue("ui_progress", 0); |
| 368 | PartitionManager.stop_backup.set_value(0); |
| 369 | return; |
| 370 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 371 | usleep(500000); |
| 372 | DataManager::SetValue("ui_progress", i * 20); |
| 373 | } |
| 374 | } |
| 375 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 376 | int GUIAction::flash_zip(std::string filename, int* wipe_cache) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 377 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 378 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 379 | |
| 380 | DataManager::SetValue("ui_progress", 0); |
Chaosmaster | ab16437 | 2020-02-03 15:38:02 +0100 | [diff] [blame] | 381 | DataManager::SetValue("ui_portion_size", 0); |
| 382 | DataManager::SetValue("ui_portion_start", 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 383 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 384 | if (filename.empty()) |
| 385 | { |
| 386 | LOGERR("No file specified.\n"); |
| 387 | return -1; |
| 388 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 389 | |
Ethan Yonker | 9598c07 | 2016-01-15 21:54:34 -0600 | [diff] [blame] | 390 | if (!TWFunc::Path_Exists(filename)) { |
| 391 | if (!PartitionManager.Mount_By_Path(filename, true)) { |
| 392 | return -1; |
| 393 | } |
| 394 | if (!TWFunc::Path_Exists(filename)) { |
| 395 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename)); |
| 396 | return -1; |
| 397 | } |
| 398 | } |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 399 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 400 | if (simulate) { |
| 401 | simulate_progress_bar(); |
| 402 | } else { |
epicX | 40b7f7a | 2021-03-20 21:58:17 +0530 | [diff] [blame] | 403 | ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 404 | |
| 405 | // Now, check if we need to ensure TWRP remains installed... |
| 406 | struct stat st; |
| 407 | if (stat("/sbin/installTwrp", &st) == 0) |
| 408 | { |
| 409 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 410 | DataManager::SetValue("tw_partition", ""); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 411 | gui_msg("config_twrp=Configuring TWRP..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 412 | if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 413 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 414 | gui_msg("config_twrp_err=Unable to configure TWRP with this kernel."); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 419 | // Done |
| 420 | DataManager::SetValue("ui_progress", 100); |
| 421 | DataManager::SetValue("ui_progress", 0); |
Chaosmaster | ab16437 | 2020-02-03 15:38:02 +0100 | [diff] [blame] | 422 | DataManager::SetValue("ui_portion_size", 0); |
| 423 | DataManager::SetValue("ui_portion_start", 0); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 424 | return ret_val; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 425 | } |
| 426 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 427 | GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 428 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 429 | string func = gui_parse_text(action.mFunction); |
| 430 | bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end(); |
| 431 | if (needsThread) { |
| 432 | if (func == "cancelbackup") |
| 433 | return THREAD_CANCEL; |
| 434 | else |
| 435 | return THREAD_ACTION; |
| 436 | } |
| 437 | return THREAD_NONE; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 440 | int GUIAction::doActions() |
| 441 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 442 | if (mActions.size() < 1) |
| 443 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 444 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 445 | // Determine in which thread to run the actions. |
| 446 | // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy. |
| 447 | ThreadType threadType = THREAD_NONE; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 448 | std::vector<Action>::iterator it; |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 449 | for (it = mActions.begin(); it != mActions.end(); ++it) { |
| 450 | ThreadType tt = getThreadType(*it); |
| 451 | if (tt == THREAD_NONE) |
| 452 | continue; |
| 453 | if (threadType == THREAD_NONE) |
| 454 | threadType = tt; |
| 455 | else if (threadType != tt) { |
| 456 | LOGERR("Can't mix normal and cancel actions in the same list.\n" |
| 457 | "Running the whole batch in the cancel thread.\n"); |
| 458 | threadType = THREAD_CANCEL; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 459 | break; |
| 460 | } |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 461 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 462 | |
| 463 | // Now run the actions in the desired thread. |
| 464 | switch (threadType) { |
| 465 | case THREAD_ACTION: |
| 466 | action_thread.threadActions(this); |
| 467 | break; |
| 468 | |
| 469 | case THREAD_CANCEL: |
| 470 | cancel_thread.threadActions(this); |
| 471 | break; |
| 472 | |
| 473 | default: { |
| 474 | // no iterators here because theme reloading might kill our object |
| 475 | const size_t cnt = mActions.size(); |
| 476 | for (size_t i = 0; i < cnt; ++i) |
| 477 | doAction(mActions[i]); |
| 478 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 479 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 480 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 481 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 482 | } |
| 483 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 484 | int GUIAction::doAction(Action action) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 485 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 486 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 487 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 488 | std::string function = gui_parse_text(action.mFunction); |
| 489 | std::string arg = gui_parse_text(action.mArg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 490 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 491 | // find function and execute it |
| 492 | mapFunc::const_iterator funcitr = mf.find(function); |
| 493 | if (funcitr != mf.end()) |
| 494 | return (this->*funcitr->second)(arg); |
| 495 | |
| 496 | LOGERR("Unknown action '%s'\n", function.c_str()); |
| 497 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void GUIAction::operation_start(const string operation_name) |
| 501 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 502 | 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] | 503 | time(&Start); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 504 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 505 | DataManager::SetValue("ui_progress", 0); |
Chaosmaster | ab16437 | 2020-02-03 15:38:02 +0100 | [diff] [blame] | 506 | DataManager::SetValue("ui_portion_size", 0); |
| 507 | DataManager::SetValue("ui_portion_start", 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 508 | DataManager::SetValue("tw_operation", operation_name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 509 | DataManager::SetValue("tw_operation_state", 0); |
Ethan Yonker | d83c9ea | 2015-01-02 15:22:31 -0600 | [diff] [blame] | 510 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 511 | bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 512 | DataManager::SetValue("tw_ab_device", tw_ab_device); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 513 | } |
| 514 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 515 | void GUIAction::operation_end(const int operation_status) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 516 | { |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 517 | time_t Stop; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 518 | int simulate_fail; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 519 | DataManager::SetValue("ui_progress", 100); |
| 520 | if (simulate) { |
| 521 | DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); |
| 522 | if (simulate_fail != 0) |
| 523 | DataManager::SetValue("tw_operation_status", 1); |
| 524 | else |
| 525 | DataManager::SetValue("tw_operation_status", 0); |
| 526 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 527 | if (operation_status != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 528 | DataManager::SetValue("tw_operation_status", 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 529 | } |
| 530 | else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 531 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 532 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 533 | } |
| 534 | DataManager::SetValue("tw_operation_state", 1); |
| 535 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 536 | blankTimer.resetTimerAndUnblank(); |
LuK1337 | 62326f4 | 2015-09-30 19:57:46 +0200 | [diff] [blame] | 537 | property_set("twrp.action_complete", "1"); |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 538 | time(&Stop); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 539 | |
| 540 | #ifndef TW_NO_HAPTICS |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 541 | if ((int) difftime(Stop, Start) > 10) |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 542 | DataManager::Vibrate("tw_action_vibrate"); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 543 | #endif |
| 544 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 545 | LOGINFO("operation_end - status=%d\n", operation_status); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 546 | } |
| 547 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 548 | int GUIAction::reboot(std::string arg) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 549 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 550 | sync(); |
| 551 | DataManager::SetValue("tw_gui_done", 1); |
| 552 | DataManager::SetValue("tw_reboot_arg", arg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 553 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 556 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 557 | int GUIAction::home(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 558 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 559 | gui_changePage("main"); |
| 560 | return 0; |
| 561 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 562 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 563 | int GUIAction::key(std::string arg) |
| 564 | { |
| 565 | const int key = getKeyByName(arg); |
| 566 | PageManager::NotifyKey(key, true); |
| 567 | PageManager::NotifyKey(key, false); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | int GUIAction::page(std::string arg) |
| 572 | { |
LuK1337 | 62326f4 | 2015-09-30 19:57:46 +0200 | [diff] [blame] | 573 | property_set("twrp.action_complete", "0"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 574 | std::string page_name = gui_parse_text(arg); |
| 575 | return gui_changePage(page_name); |
| 576 | } |
| 577 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 578 | int GUIAction::reload(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 579 | { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 580 | PageManager::RequestReload(); |
| 581 | // The actual reload is handled in pages.cpp in PageManager::RunReload() |
| 582 | // The reload will occur on the next Update or Render call and will |
| 583 | // be performed in the rendoer thread instead of the action thread |
| 584 | // to prevent crashing which could occur when we start deleting |
| 585 | // GUI resources in the action thread while we attempt to render |
| 586 | // with those same resources in another thread. |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 587 | return 0; |
| 588 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 589 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 590 | int GUIAction::readBackup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 591 | { |
| 592 | string Restore_Name; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 593 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 594 | DataManager::GetValue("tw_restore", Restore_Name); |
| 595 | PartitionManager.Set_Restore_Files(Restore_Name); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | int GUIAction::set(std::string arg) |
| 600 | { |
| 601 | if (arg.find('=') != string::npos) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 602 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 603 | string varName = arg.substr(0, arg.find('=')); |
| 604 | string value = arg.substr(arg.find('=') + 1, string::npos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 606 | DataManager::GetValue(value, value); |
| 607 | DataManager::SetValue(varName, value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 608 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 609 | else |
| 610 | DataManager::SetValue(arg, "1"); |
| 611 | return 0; |
| 612 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 613 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 614 | int GUIAction::clear(std::string arg) |
| 615 | { |
| 616 | DataManager::SetValue(arg, "0"); |
| 617 | return 0; |
| 618 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 619 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 620 | int GUIAction::mount(std::string arg) |
| 621 | { |
| 622 | if (arg == "usb") { |
| 623 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 624 | if (!simulate) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 625 | PartitionManager.usb_storage_enable(); |
| 626 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 627 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 628 | } else if (!simulate) { |
| 629 | PartitionManager.Mount_By_Path(arg, true); |
| 630 | PartitionManager.Add_MTP_Storage(arg); |
| 631 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 632 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | int GUIAction::unmount(std::string arg) |
| 637 | { |
| 638 | if (arg == "usb") { |
| 639 | if (!simulate) |
| 640 | PartitionManager.usb_storage_disable(); |
| 641 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 642 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 643 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 644 | } else if (!simulate) { |
| 645 | PartitionManager.UnMount_By_Path(arg, true); |
| 646 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 647 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 651 | int GUIAction::restoredefaultsettings(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 652 | { |
| 653 | operation_start("Restore Defaults"); |
| 654 | 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] | 655 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 656 | else { |
| 657 | DataManager::ResetDefaults(); |
| 658 | PartitionManager.Update_System_Details(); |
| 659 | PartitionManager.Mount_Current_Storage(true); |
| 660 | } |
| 661 | operation_end(0); |
| 662 | return 0; |
| 663 | } |
| 664 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 665 | int GUIAction::copylog(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 666 | { |
| 667 | operation_start("Copy Log"); |
| 668 | if (!simulate) |
| 669 | { |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 670 | string dst, curr_storage; |
| 671 | int copy_kernel_log = 0; |
| 672 | |
| 673 | DataManager::GetValue("tw_include_kernel_log", copy_kernel_log); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 674 | PartitionManager.Mount_Current_Storage(true); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 675 | curr_storage = DataManager::GetCurrentStoragePath(); |
| 676 | dst = curr_storage + "/recovery.log"; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 677 | TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); |
| 678 | tw_set_default_metadata(dst.c_str()); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 679 | if (copy_kernel_log) |
| 680 | TWFunc::copy_kernel_log(curr_storage); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 681 | sync(); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 682 | gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 683 | } else |
| 684 | simulate_progress_bar(); |
| 685 | operation_end(0); |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | int GUIAction::compute(std::string arg) |
| 691 | { |
| 692 | if (arg.find("+") != string::npos) |
| 693 | { |
| 694 | string varName = arg.substr(0, arg.find('+')); |
| 695 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 696 | int amount_to_add = atoi(string_to_add.c_str()); |
| 697 | int value; |
| 698 | |
| 699 | DataManager::GetValue(varName, value); |
| 700 | DataManager::SetValue(varName, value + amount_to_add); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 701 | return 0; |
| 702 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 703 | if (arg.find("-") != string::npos) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 704 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 705 | string varName = arg.substr(0, arg.find('-')); |
| 706 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 707 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 708 | int value; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 709 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 710 | DataManager::GetValue(varName, value); |
| 711 | value -= amount_to_subtract; |
| 712 | if (value <= 0) |
| 713 | value = 0; |
| 714 | DataManager::SetValue(varName, value); |
| 715 | return 0; |
| 716 | } |
| 717 | if (arg.find("*") != string::npos) |
| 718 | { |
| 719 | string varName = arg.substr(0, arg.find('*')); |
| 720 | string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos)); |
| 721 | int multiply_by = atoi(multiply_by_str.c_str()); |
| 722 | int value; |
| 723 | |
| 724 | DataManager::GetValue(varName, value); |
| 725 | DataManager::SetValue(varName, value*multiply_by); |
| 726 | return 0; |
| 727 | } |
| 728 | if (arg.find("/") != string::npos) |
| 729 | { |
| 730 | string varName = arg.substr(0, arg.find('/')); |
| 731 | string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos)); |
| 732 | int divide_by = atoi(divide_by_str.c_str()); |
| 733 | int value; |
| 734 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 735 | if (divide_by != 0) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 736 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 737 | DataManager::GetValue(varName, value); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 738 | DataManager::SetValue(varName, value/divide_by); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 739 | } |
| 740 | return 0; |
| 741 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 742 | LOGERR("Unable to perform compute '%s'\n", arg.c_str()); |
| 743 | return -1; |
| 744 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 745 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 746 | int GUIAction::setguitimezone(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 747 | { |
| 748 | string SelectedZone; |
| 749 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 750 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 751 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 752 | |
| 753 | int dst; |
| 754 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 755 | |
| 756 | string offset; |
| 757 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 758 | |
| 759 | string NewTimeZone = Zone; |
| 760 | if (offset != "0") |
| 761 | NewTimeZone += ":" + offset; |
| 762 | |
| 763 | if (dst != 0) |
| 764 | NewTimeZone += DSTZone; |
| 765 | |
| 766 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
| 767 | DataManager::update_tz_environment_variables(); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | int GUIAction::overlay(std::string arg) |
| 772 | { |
| 773 | return gui_changeOverlay(arg); |
| 774 | } |
| 775 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 776 | int GUIAction::queuezip(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 777 | { |
| 778 | if (zip_queue_index >= 10) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 779 | gui_msg("max_queue=Maximum zip queue reached!"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 780 | return 0; |
| 781 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 782 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 783 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 784 | zip_queue_index++; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 785 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 786 | } |
| 787 | return 0; |
| 788 | } |
| 789 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 790 | int GUIAction::cancelzip(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 791 | { |
| 792 | if (zip_queue_index <= 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 793 | gui_msg("min_queue=Minimum zip queue reached!"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 794 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 795 | } else { |
| 796 | zip_queue_index--; |
| 797 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 798 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 799 | return 0; |
| 800 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 801 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 802 | int GUIAction::queueclear(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 803 | { |
| 804 | zip_queue_index = 0; |
| 805 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 806 | return 0; |
| 807 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 808 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 809 | int GUIAction::sleep(std::string arg) |
| 810 | { |
| 811 | operation_start("Sleep"); |
| 812 | usleep(atoi(arg.c_str())); |
| 813 | operation_end(0); |
| 814 | return 0; |
| 815 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 816 | |
Matt Mower | 9a2a205 | 2016-05-31 21:31:22 -0500 | [diff] [blame] | 817 | int GUIAction::sleepcounter(std::string arg) |
| 818 | { |
| 819 | operation_start("SleepCounter"); |
| 820 | // Ensure user notices countdown in case it needs to be cancelled |
| 821 | blankTimer.resetTimerAndUnblank(); |
| 822 | int total = atoi(arg.c_str()); |
| 823 | for (int t = total; t > 0; t--) { |
| 824 | int progress = (int)(((float)(total-t)/(float)total)*100.0); |
| 825 | DataManager::SetValue("ui_progress", progress); |
| 826 | ::sleep(1); |
| 827 | DataManager::SetValue("tw_sleep", t-1); |
| 828 | } |
| 829 | DataManager::SetValue("ui_progress", 100); |
| 830 | operation_end(0); |
| 831 | return 0; |
| 832 | } |
| 833 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 834 | int GUIAction::appenddatetobackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 835 | { |
| 836 | operation_start("AppendDateToBackupName"); |
| 837 | string Backup_Name; |
| 838 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 839 | Backup_Name += TWFunc::Get_Current_Date(); |
| 840 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 841 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 842 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Matt Mower | 76b8afc | 2016-06-24 12:04:27 -0500 | [diff] [blame] | 843 | PageManager::NotifyKey(KEY_END, true); |
| 844 | PageManager::NotifyKey(KEY_END, false); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 845 | operation_end(0); |
| 846 | return 0; |
| 847 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 848 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 849 | int GUIAction::generatebackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 850 | { |
| 851 | operation_start("GenerateBackupName"); |
| 852 | TWFunc::Auto_Generate_Backup_Name(); |
| 853 | operation_end(0); |
| 854 | return 0; |
| 855 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 856 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 857 | int GUIAction::checkpartitionlist(std::string arg) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 858 | { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 859 | string List, part_path; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 860 | int count = 0; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 861 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 862 | if (arg.empty()) |
| 863 | arg = "tw_wipe_list"; |
| 864 | DataManager::GetValue(arg, List); |
| 865 | LOGINFO("checkpartitionlist list '%s'\n", List.c_str()); |
| 866 | if (!List.empty()) { |
| 867 | size_t start_pos = 0, end_pos = List.find(";", start_pos); |
| 868 | while (end_pos != string::npos && start_pos < List.size()) { |
| 869 | part_path = List.substr(start_pos, end_pos - start_pos); |
| 870 | LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str()); |
| 871 | if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 872 | // Do nothing |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 873 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 874 | count++; |
| 875 | } |
| 876 | start_pos = end_pos + 1; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 877 | end_pos = List.find(";", start_pos); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 878 | } |
| 879 | DataManager::SetValue("tw_check_partition_list", count); |
| 880 | } else { |
| 881 | DataManager::SetValue("tw_check_partition_list", 0); |
| 882 | } |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 883 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 884 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 885 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 886 | int GUIAction::getpartitiondetails(std::string arg) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 887 | { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 888 | string List, part_path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 889 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 890 | if (arg.empty()) |
| 891 | arg = "tw_wipe_list"; |
| 892 | DataManager::GetValue(arg, List); |
| 893 | LOGINFO("getpartitiondetails list '%s'\n", List.c_str()); |
| 894 | if (!List.empty()) { |
| 895 | size_t start_pos = 0, end_pos = List.find(";", start_pos); |
| 896 | part_path = List; |
| 897 | while (end_pos != string::npos && start_pos < List.size()) { |
| 898 | part_path = List.substr(start_pos, end_pos - start_pos); |
| 899 | LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str()); |
| 900 | if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 901 | // Do nothing |
| 902 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 903 | DataManager::SetValue("tw_partition_path", part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | start_pos = end_pos + 1; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 907 | end_pos = List.find(";", start_pos); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 908 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 909 | if (!part_path.empty()) { |
| 910 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 911 | if (Part) { |
| 912 | unsigned long long mb = 1048576; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 913 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 914 | DataManager::SetValue("tw_partition_name", Part->Display_Name); |
| 915 | DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point); |
| 916 | DataManager::SetValue("tw_partition_file_system", Part->Current_File_System); |
| 917 | DataManager::SetValue("tw_partition_size", Part->Size / mb); |
| 918 | DataManager::SetValue("tw_partition_used", Part->Used / mb); |
| 919 | DataManager::SetValue("tw_partition_free", Part->Free / mb); |
| 920 | DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb); |
| 921 | DataManager::SetValue("tw_partition_removable", Part->Removable); |
| 922 | DataManager::SetValue("tw_partition_is_present", Part->Is_Present); |
| 923 | |
| 924 | if (Part->Can_Repair()) |
| 925 | DataManager::SetValue("tw_partition_can_repair", 1); |
| 926 | else |
| 927 | DataManager::SetValue("tw_partition_can_repair", 0); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 928 | if (Part->Can_Resize()) |
| 929 | DataManager::SetValue("tw_partition_can_resize", 1); |
| 930 | else |
| 931 | DataManager::SetValue("tw_partition_can_resize", 0); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 932 | if (TWFunc::Path_Exists("/sbin/mkfs.fat")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 933 | DataManager::SetValue("tw_partition_vfat", 1); |
| 934 | else |
| 935 | DataManager::SetValue("tw_partition_vfat", 0); |
Matt Mower | 80f7b36 | 2015-12-12 15:38:01 -0600 | [diff] [blame] | 936 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 937 | DataManager::SetValue("tw_partition_exfat", 1); |
| 938 | else |
| 939 | DataManager::SetValue("tw_partition_exfat", 0); |
| 940 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) |
| 941 | DataManager::SetValue("tw_partition_f2fs", 1); |
| 942 | else |
| 943 | DataManager::SetValue("tw_partition_f2fs", 0); |
| 944 | if (TWFunc::Path_Exists("/sbin/mke2fs")) |
| 945 | DataManager::SetValue("tw_partition_ext", 1); |
| 946 | else |
| 947 | DataManager::SetValue("tw_partition_ext", 0); |
| 948 | return 0; |
| 949 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 950 | LOGERR("Unable to locate partition: '%s'\n", part_path.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | } |
| 954 | DataManager::SetValue("tw_partition_name", ""); |
| 955 | DataManager::SetValue("tw_partition_file_system", ""); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 956 | // Set this to 0 to prevent trying to partition this device, just in case |
| 957 | DataManager::SetValue("tw_partition_removable", 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 961 | int GUIAction::screenshot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 962 | { |
| 963 | time_t tm; |
| 964 | char path[256]; |
| 965 | int path_len; |
Matt Mower | 0c88b84 | 2017-01-15 16:00:49 -0600 | [diff] [blame] | 966 | uid_t uid = AID_MEDIA_RW; |
| 967 | gid_t gid = AID_MEDIA_RW; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 968 | |
| 969 | const std::string storage = DataManager::GetCurrentStoragePath(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 970 | if (PartitionManager.Is_Mounted_By_Path(storage)) { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 971 | snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str()); |
| 972 | } else { |
| 973 | strcpy(path, "/tmp/"); |
| 974 | } |
| 975 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 976 | if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid)) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 977 | return 0; |
| 978 | |
| 979 | tm = time(NULL); |
| 980 | path_len = strlen(path); |
| 981 | |
| 982 | // Screenshot_2014-01-01-18-21-38.png |
| 983 | strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm)); |
| 984 | |
| 985 | int res = gr_save_screenshot(path); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 986 | if (res == 0) { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 987 | chmod(path, 0666); |
| 988 | chown(path, uid, gid); |
| 989 | |
that | 677b13f | 2015-12-26 23:57:31 +0100 | [diff] [blame] | 990 | gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 991 | |
VDavid003 | 2034a41 | 2019-10-18 22:43:20 +0200 | [diff] [blame] | 992 | // blink to notify that the screenshot was taken |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 993 | gr_color(255, 255, 255, 255); |
| 994 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 995 | gr_flip(); |
| 996 | gui_forceRender(); |
| 997 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 998 | gui_err("screenshot_err=Failed to take a screenshot!"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | int GUIAction::setbrightness(std::string arg) |
| 1004 | { |
| 1005 | return TWFunc::Set_Brightness(arg); |
| 1006 | } |
| 1007 | |
| 1008 | int GUIAction::fileexists(std::string arg) |
| 1009 | { |
| 1010 | struct stat st; |
| 1011 | string newpath = arg + "/."; |
| 1012 | |
| 1013 | operation_start("FileExists"); |
| 1014 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 1015 | operation_end(0); |
| 1016 | else |
| 1017 | operation_end(1); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1021 | void GUIAction::reinject_after_flash() |
| 1022 | { |
| 1023 | 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] | 1024 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1025 | if (simulate) { |
| 1026 | simulate_progress_bar(); |
| 1027 | } else { |
| 1028 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1029 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
| 1030 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 1031 | else { |
| 1032 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
| 1033 | TWFunc::Exec_Cmd(injectcmd); |
| 1034 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1035 | gui_msg("done=Done."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
mauronofrio | 0ff5984 | 2019-10-26 19:47:55 +0200 | [diff] [blame] | 1040 | int GUIAction::ozip_decrypt(string zip_path) |
| 1041 | { |
| 1042 | if (!TWFunc::Path_Exists("/sbin/ozip_decrypt")) { |
| 1043 | return 1; |
| 1044 | } |
| 1045 | gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption..."); |
| 1046 | TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'"); |
| 1047 | gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!"); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1051 | int GUIAction::flash(std::string arg) |
| 1052 | { |
| 1053 | int i, ret_val = 0, wipe_cache = 0; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1054 | // We're going to jump to this page first, like a loading page |
| 1055 | gui_changePage(arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1056 | for (i=0; i<zip_queue_index; i++) { |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1057 | string zip_path = zip_queue[i]; |
| 1058 | size_t slashpos = zip_path.find_last_of('/'); |
| 1059 | string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1060 | operation_start("Flashing"); |
mauronofrio | 0ff5984 | 2019-10-26 19:47:55 +0200 | [diff] [blame] | 1061 | if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip") |
| 1062 | { |
| 1063 | if((ozip_decrypt(zip_path)) != 0) |
| 1064 | { |
| 1065 | LOGERR("Unable to find ozip_decrypt!"); |
| 1066 | break; |
| 1067 | } |
| 1068 | zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip"); |
| 1069 | zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip"); |
| 1070 | if (!TWFunc::Path_Exists(zip_path)) { |
| 1071 | LOGERR("Unable to find decrypted zip"); |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1075 | DataManager::SetValue("tw_filename", zip_path); |
| 1076 | DataManager::SetValue("tw_file", zip_filename); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1077 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 1078 | |
| 1079 | TWFunc::SetPerformanceMode(true); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1080 | ret_val = flash_zip(zip_path, &wipe_cache); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1081 | TWFunc::SetPerformanceMode(false); |
| 1082 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1083 | 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] | 1084 | ret_val = 1; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1085 | break; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | zip_queue_index = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1089 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1090 | if (wipe_cache) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1091 | 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] | 1092 | PartitionManager.Wipe_By_Path("/cache"); |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1093 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1094 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1095 | reinject_after_flash(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1096 | PartitionManager.Update_System_Details(); |
| 1097 | operation_end(ret_val); |
bigbiff | a869fc7 | 2016-03-01 19:40:36 -0500 | [diff] [blame] | 1098 | // 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] | 1099 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | int GUIAction::wipe(std::string arg) |
| 1104 | { |
| 1105 | operation_start("Format"); |
| 1106 | DataManager::SetValue("tw_partition", arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1107 | int ret_val = false; |
| 1108 | |
| 1109 | if (simulate) { |
| 1110 | simulate_progress_bar(); |
| 1111 | } else { |
| 1112 | if (arg == "data") |
| 1113 | ret_val = PartitionManager.Factory_Reset(); |
| 1114 | else if (arg == "battery") |
| 1115 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
| 1116 | else if (arg == "rotate") |
| 1117 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
| 1118 | else if (arg == "dalvik") |
| 1119 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
| 1120 | else if (arg == "DATAMEDIA") { |
| 1121 | ret_val = PartitionManager.Format_Data(); |
| 1122 | } else if (arg == "INTERNAL") { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1123 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1124 | |
| 1125 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1126 | if (has_datamedia) { |
| 1127 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
| 1128 | } else { |
| 1129 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
| 1130 | } |
| 1131 | } else if (arg == "EXTERNAL") { |
| 1132 | string External_Path; |
| 1133 | |
| 1134 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 1135 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
| 1136 | } else if (arg == "ANDROIDSECURE") { |
| 1137 | ret_val = PartitionManager.Wipe_Android_Secure(); |
| 1138 | } else if (arg == "LIST") { |
| 1139 | string Wipe_List, wipe_path; |
| 1140 | bool skip = false; |
| 1141 | ret_val = true; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1142 | |
| 1143 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 1144 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
| 1145 | if (!Wipe_List.empty()) { |
| 1146 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 1147 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 1148 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 1149 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
| 1150 | if (wipe_path == "/and-sec") { |
| 1151 | if (!PartitionManager.Wipe_Android_Secure()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1152 | gui_msg("and_sec_wipe_err=Unable to wipe android secure"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1153 | ret_val = false; |
| 1154 | break; |
| 1155 | } else { |
| 1156 | skip = true; |
| 1157 | } |
| 1158 | } else if (wipe_path == "DALVIK") { |
| 1159 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1160 | gui_err("dalvik_wipe_err=Failed to wipe dalvik"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1161 | ret_val = false; |
| 1162 | break; |
| 1163 | } else { |
| 1164 | skip = true; |
| 1165 | } |
| 1166 | } else if (wipe_path == "INTERNAL") { |
| 1167 | if (!PartitionManager.Wipe_Media_From_Data()) { |
| 1168 | ret_val = false; |
| 1169 | break; |
| 1170 | } else { |
| 1171 | skip = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1172 | } |
| 1173 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1174 | if (!skip) { |
| 1175 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1176 | 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] | 1177 | ret_val = false; |
| 1178 | break; |
| 1179 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 1180 | arg = wipe_path; |
| 1181 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1182 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1183 | skip = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1184 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1185 | start_pos = end_pos + 1; |
| 1186 | end_pos = Wipe_List.find(";", start_pos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1187 | } |
| 1188 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1189 | } else |
| 1190 | ret_val = PartitionManager.Wipe_By_Path(arg); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1191 | #ifndef TW_OEM_BUILD |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1192 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 1193 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 1194 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1195 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1196 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 1197 | LOGINFO("Making TWRP folder and saving settings.\n"); |
| 1198 | Storage_Path += "/TWRP"; |
| 1199 | mkdir(Storage_Path.c_str(), 0777); |
| 1200 | DataManager::Flush(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1201 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1202 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
| 1203 | } |
| 1204 | } |
| 1205 | #endif |
| 1206 | } |
| 1207 | PartitionManager.Update_System_Details(); |
| 1208 | if (ret_val) |
| 1209 | ret_val = 0; // 0 is success |
| 1210 | else |
| 1211 | ret_val = 1; // 1 is failure |
| 1212 | operation_end(ret_val); |
| 1213 | return 0; |
| 1214 | } |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 1215 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1216 | int GUIAction::refreshsizes(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1217 | { |
| 1218 | operation_start("Refreshing Sizes"); |
| 1219 | if (simulate) { |
| 1220 | simulate_progress_bar(); |
| 1221 | } else |
| 1222 | PartitionManager.Update_System_Details(); |
| 1223 | operation_end(0); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | int GUIAction::nandroid(std::string arg) |
| 1228 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1229 | if (simulate) { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1230 | PartitionManager.stop_backup.set_value(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1231 | DataManager::SetValue("tw_partition", "Simulation"); |
| 1232 | simulate_progress_bar(); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1233 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1234 | } else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1235 | operation_start("Nandroid"); |
| 1236 | int ret = 0; |
| 1237 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1238 | if (arg == "backup") { |
| 1239 | string Backup_Name; |
| 1240 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 1241 | string auto_gen = gui_lookup("auto_generate", "(Auto Generate)"); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 1242 | if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(Backup_Name, true, true) == 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1243 | ret = PartitionManager.Run_Backup(false); |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1244 | DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup |
| 1245 | if (!PartitionManager.stop_backup.get_value()) { |
| 1246 | if (ret == false) |
| 1247 | ret = 1; // 1 for failure |
| 1248 | else |
| 1249 | ret = 0; // 0 for success |
| 1250 | DataManager::SetValue("tw_cancel_backup", 0); |
| 1251 | } else { |
| 1252 | DataManager::SetValue("tw_cancel_backup", 1); |
| 1253 | gui_msg("backup_cancel=Backup Cancelled"); |
| 1254 | ret = 0; |
| 1255 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1256 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1257 | operation_end(1); |
| 1258 | return -1; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1259 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1260 | DataManager::SetValue(TW_BACKUP_NAME, auto_gen); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1261 | } else if (arg == "restore") { |
| 1262 | string Restore_Name; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1263 | int gui_adb_backup; |
| 1264 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1265 | DataManager::GetValue("tw_restore", Restore_Name); |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1266 | DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup); |
| 1267 | if (gui_adb_backup) { |
| 1268 | DataManager::SetValue("tw_operation_state", 1); |
| 1269 | if (TWFunc::stream_adb_backup(Restore_Name) == 0) |
| 1270 | ret = 0; // success |
| 1271 | else |
| 1272 | ret = 1; // failure |
| 1273 | DataManager::SetValue("tw_enable_adb_backup", 0); |
| 1274 | ret = 0; // assume success??? |
| 1275 | } else { |
| 1276 | if (PartitionManager.Run_Restore(Restore_Name)) |
| 1277 | ret = 0; // success |
| 1278 | else |
| 1279 | ret = 1; // failure |
| 1280 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1281 | } else { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1282 | operation_end(1); // invalid arg specified, fail |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1283 | return -1; |
| 1284 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1285 | operation_end(ret); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1286 | return ret; |
| 1287 | } |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1291 | int GUIAction::cancelbackup(std::string arg __unused) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1292 | if (simulate) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1293 | PartitionManager.stop_backup.set_value(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1294 | } |
| 1295 | else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1296 | int op_status = PartitionManager.Cancel_Backup(); |
| 1297 | if (op_status != 0) |
| 1298 | op_status = 1; // failure |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1302 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1303 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1304 | int GUIAction::fixcontexts(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1305 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1306 | int op_status = 0; |
| 1307 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1308 | operation_start("Fix Contexts"); |
| 1309 | LOGINFO("fix contexts started!\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1310 | if (simulate) { |
| 1311 | simulate_progress_bar(); |
| 1312 | } else { |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1313 | op_status = PartitionManager.Fix_Contexts(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1314 | if (op_status != 0) |
| 1315 | op_status = 1; // failure |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1316 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1317 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
| 1320 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1321 | int GUIAction::fixpermissions(std::string arg) |
| 1322 | { |
| 1323 | return fixcontexts(arg); |
| 1324 | } |
| 1325 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1326 | int GUIAction::dd(std::string arg) |
| 1327 | { |
| 1328 | operation_start("imaging"); |
| 1329 | |
| 1330 | if (simulate) { |
| 1331 | simulate_progress_bar(); |
| 1332 | } else { |
| 1333 | string cmd = "dd " + arg; |
| 1334 | TWFunc::Exec_Cmd(cmd); |
| 1335 | } |
| 1336 | operation_end(0); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1340 | int GUIAction::partitionsd(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1341 | { |
| 1342 | operation_start("Partition SD Card"); |
| 1343 | int ret_val = 0; |
| 1344 | |
| 1345 | if (simulate) { |
| 1346 | simulate_progress_bar(); |
| 1347 | } else { |
| 1348 | int allow_partition; |
| 1349 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 1350 | if (allow_partition == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1351 | 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] | 1352 | } else { |
| 1353 | if (!PartitionManager.Partition_SDCard()) |
| 1354 | ret_val = 1; // failed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1355 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1356 | } |
| 1357 | operation_end(ret_val); |
| 1358 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1359 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1360 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1361 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1362 | int GUIAction::installhtcdumlock(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1363 | { |
| 1364 | operation_start("Install HTC Dumlock"); |
| 1365 | if (simulate) { |
| 1366 | simulate_progress_bar(); |
| 1367 | } else |
| 1368 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1369 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1370 | operation_end(0); |
| 1371 | return 0; |
| 1372 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1373 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1374 | int GUIAction::htcdumlockrestoreboot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1375 | { |
| 1376 | operation_start("HTC Dumlock Restore Boot"); |
| 1377 | if (simulate) { |
| 1378 | simulate_progress_bar(); |
| 1379 | } else |
| 1380 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1381 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1382 | operation_end(0); |
| 1383 | return 0; |
| 1384 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1385 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1386 | int GUIAction::htcdumlockreflashrecovery(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1387 | { |
| 1388 | operation_start("HTC Dumlock Reflash Recovery"); |
| 1389 | if (simulate) { |
| 1390 | simulate_progress_bar(); |
| 1391 | } else |
| 1392 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1393 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1394 | operation_end(0); |
| 1395 | return 0; |
| 1396 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1397 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1398 | int GUIAction::cmd(std::string arg) |
| 1399 | { |
| 1400 | int op_status = 0; |
| 1401 | |
| 1402 | operation_start("Command"); |
| 1403 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
| 1404 | if (simulate) { |
| 1405 | simulate_progress_bar(); |
| 1406 | } else { |
| 1407 | op_status = TWFunc::Exec_Cmd(arg); |
| 1408 | if (op_status != 0) |
| 1409 | op_status = 1; |
| 1410 | } |
| 1411 | |
| 1412 | operation_end(op_status); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | int GUIAction::terminalcommand(std::string arg) |
| 1417 | { |
| 1418 | int op_status = 0; |
| 1419 | string cmdpath, command; |
| 1420 | |
| 1421 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1422 | operation_start("CommandOutput"); |
| 1423 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 1424 | if (simulate) { |
| 1425 | simulate_progress_bar(); |
| 1426 | operation_end(op_status); |
Matt Mower | 5aa29ab | 2015-02-25 23:50:55 -0600 | [diff] [blame] | 1427 | } else if (arg == "exit") { |
| 1428 | LOGINFO("Exiting terminal\n"); |
| 1429 | operation_end(op_status); |
| 1430 | page("main"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1431 | } else { |
| 1432 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
| 1433 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1434 | DataManager::SetValue("tw_terminal_state", 1); |
| 1435 | DataManager::SetValue("tw_background_thread_running", 1); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1436 | FILE* fp; |
| 1437 | char line[512]; |
| 1438 | |
| 1439 | fp = popen(command.c_str(), "r"); |
| 1440 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1441 | LOGERR("Error opening command to run (%s).\n", strerror(errno)); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1442 | } else { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1443 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1444 | struct timeval timeout; |
| 1445 | fd_set fdset; |
| 1446 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1447 | while (keep_going) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1448 | { |
| 1449 | FD_ZERO(&fdset); |
| 1450 | FD_SET(fd, &fdset); |
| 1451 | timeout.tv_sec = 0; |
| 1452 | timeout.tv_usec = 400000; |
| 1453 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1454 | if (has_data == 0) { |
| 1455 | // Timeout reached |
| 1456 | DataManager::GetValue("tw_terminal_state", check); |
| 1457 | if (check == 0) { |
| 1458 | keep_going = 0; |
| 1459 | } |
| 1460 | } else if (has_data < 0) { |
| 1461 | // End of execution |
| 1462 | keep_going = 0; |
| 1463 | } else { |
| 1464 | // Try to read output |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1465 | if (fgets(line, sizeof(line), fp) != NULL) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1466 | gui_print("%s", line); // Display output |
| 1467 | else |
| 1468 | keep_going = 0; // Done executing |
| 1469 | } |
| 1470 | } |
| 1471 | fclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1472 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1473 | DataManager::SetValue("tw_operation_status", 0); |
| 1474 | DataManager::SetValue("tw_operation_state", 1); |
| 1475 | DataManager::SetValue("tw_terminal_state", 0); |
| 1476 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1477 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1478 | } |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1482 | int GUIAction::killterminal(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1483 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1484 | LOGINFO("Sending kill command...\n"); |
| 1485 | operation_start("KillCommand"); |
| 1486 | DataManager::SetValue("tw_operation_status", 0); |
| 1487 | DataManager::SetValue("tw_operation_state", 1); |
| 1488 | DataManager::SetValue("tw_terminal_state", 0); |
| 1489 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1490 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1494 | int GUIAction::reinjecttwrp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1495 | { |
| 1496 | int op_status = 0; |
| 1497 | operation_start("ReinjectTWRP"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1498 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1499 | if (simulate) { |
| 1500 | simulate_progress_bar(); |
| 1501 | } else { |
| 1502 | 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] | 1503 | gui_msg("done=Done."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | operation_end(op_status); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1510 | int GUIAction::checkbackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1511 | { |
| 1512 | int op_status = 0; |
| 1513 | |
| 1514 | operation_start("CheckBackupName"); |
| 1515 | if (simulate) { |
| 1516 | simulate_progress_bar(); |
| 1517 | } else { |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 1518 | string Backup_Name; |
| 1519 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 1520 | op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1521 | if (op_status != 0) |
| 1522 | op_status = 1; |
| 1523 | } |
| 1524 | |
| 1525 | operation_end(op_status); |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1529 | int GUIAction::decrypt(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1530 | { |
| 1531 | int op_status = 0; |
| 1532 | |
| 1533 | operation_start("Decrypt"); |
| 1534 | if (simulate) { |
| 1535 | simulate_progress_bar(); |
| 1536 | } else { |
| 1537 | string Password; |
Noah Jacobson | 5a79f67 | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 1538 | string userID; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1539 | DataManager::GetValue("tw_crypto_password", Password); |
Noah Jacobson | 5a79f67 | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 1540 | |
| 1541 | if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE |
| 1542 | DataManager::GetValue("tw_crypto_user_id", userID); |
| 1543 | if (userID != "") { |
| 1544 | op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str())); |
| 1545 | if (userID != "0") { |
| 1546 | if (op_status != 0) |
| 1547 | op_status = 1; |
| 1548 | operation_end(op_status); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | } else { |
| 1552 | LOGINFO("User ID not found\n"); |
| 1553 | op_status = 1; |
| 1554 | } |
| 1555 | ::sleep(1); |
| 1556 | } else { // for FDE |
| 1557 | op_status = PartitionManager.Decrypt_Device(Password); |
| 1558 | } |
| 1559 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1560 | if (op_status != 0) |
| 1561 | op_status = 1; |
| 1562 | else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1563 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1564 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1565 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1566 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1567 | // Check for a custom theme and load it if exists |
| 1568 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1569 | if (has_datamedia != 0) { |
| 1570 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 1571 | 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] | 1572 | } else { |
| 1573 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1574 | } |
| 1575 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1576 | PartitionManager.Decrypt_Adopted(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | operation_end(op_status); |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1584 | int GUIAction::adbsideload(std::string arg __unused) |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1585 | { |
| 1586 | operation_start("Sideload"); |
| 1587 | if (simulate) { |
| 1588 | simulate_progress_bar(); |
| 1589 | operation_end(0); |
| 1590 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1591 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1592 | bool mtp_was_enabled = TWFunc::Toggle_MTP(false); |
| 1593 | |
| 1594 | // wait for the adb connection |
| 1595 | int ret = apply_from_adb("/", &sideload_child_pid); |
| 1596 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
| 1597 | |
| 1598 | if (ret != 0) { |
| 1599 | if (ret == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1600 | 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] | 1601 | ret = 1; // failure |
| 1602 | } else { |
| 1603 | int wipe_cache = 0; |
| 1604 | int wipe_dalvik = 0; |
| 1605 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
| 1606 | |
| 1607 | if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 1608 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1609 | PartitionManager.Wipe_By_Path("/cache"); |
| 1610 | if (wipe_dalvik) |
| 1611 | PartitionManager.Wipe_Dalvik_Cache(); |
| 1612 | } else { |
| 1613 | ret = 1; // failure |
| 1614 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1615 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1616 | if (sideload_child_pid) { |
| 1617 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1618 | struct stat st; |
| 1619 | // Calling stat() on this magic filename signals the minadbd |
| 1620 | // subprocess to shut down. |
| 1621 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1622 | int status; |
| 1623 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1624 | waitpid(sideload_child_pid, &status, 0); |
| 1625 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 1626 | property_set("ctl.start", "adbd"); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1627 | TWFunc::Toggle_MTP(mtp_was_enabled); |
| 1628 | reinject_after_flash(); |
| 1629 | operation_end(ret); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1630 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1631 | return 0; |
| 1632 | } |
| 1633 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1634 | int GUIAction::adbsideloadcancel(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1635 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1636 | struct stat st; |
| 1637 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1638 | gui_msg("cancel_sideload=Cancelling ADB sideload..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1639 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1640 | // Calling stat() on this magic filename signals the minadbd |
| 1641 | // subprocess to shut down. |
| 1642 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1643 | if (!sideload_child_pid) { |
| 1644 | LOGERR("Unable to get child ID\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1647 | ::sleep(1); |
| 1648 | LOGINFO("Killing child sideload process.\n"); |
| 1649 | kill(sideload_child_pid, SIGTERM); |
| 1650 | int status; |
| 1651 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1652 | waitpid(sideload_child_pid, &status, 0); |
| 1653 | sideload_child_pid = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1654 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1658 | int GUIAction::openrecoveryscript(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1659 | { |
| 1660 | operation_start("OpenRecoveryScript"); |
| 1661 | if (simulate) { |
| 1662 | simulate_progress_bar(); |
Ethan Yonker | 24e7eb7 | 2015-01-03 16:13:06 -0600 | [diff] [blame] | 1663 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1664 | } else { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1665 | int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action(); |
Ethan Yonker | e1abe61 | 2015-06-09 11:09:32 -0500 | [diff] [blame] | 1666 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1667 | } |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1671 | int GUIAction::installsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1672 | { |
| 1673 | int op_status = 0; |
| 1674 | |
| 1675 | operation_start("Install SuperSU"); |
| 1676 | if (simulate) { |
| 1677 | simulate_progress_bar(); |
| 1678 | } else { |
Ethan Yonker | fa67cbf | 2018-07-20 12:22:33 -0500 | [diff] [blame] | 1679 | LOGERR("Installing SuperSU was deprecated from TWRP.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | operation_end(op_status); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1686 | int GUIAction::fixsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1687 | { |
| 1688 | int op_status = 0; |
| 1689 | |
| 1690 | operation_start("Fixing Superuser Permissions"); |
| 1691 | if (simulate) { |
| 1692 | simulate_progress_bar(); |
| 1693 | } else { |
| 1694 | LOGERR("Fixing su permissions was deprecated from TWRP.\n"); |
| 1695 | LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n"); |
| 1696 | } |
| 1697 | |
| 1698 | operation_end(op_status); |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1702 | int GUIAction::decrypt_backup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1703 | { |
| 1704 | int op_status = 0; |
| 1705 | |
| 1706 | operation_start("Try Restore Decrypt"); |
| 1707 | if (simulate) { |
| 1708 | simulate_progress_bar(); |
| 1709 | } else { |
| 1710 | string Restore_Path, Filename, Password; |
| 1711 | DataManager::GetValue("tw_restore", Restore_Path); |
| 1712 | Restore_Path += "/"; |
| 1713 | DataManager::GetValue("tw_restore_password", Password); |
| 1714 | TWFunc::SetPerformanceMode(true); |
| 1715 | if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password)) |
| 1716 | op_status = 0; // success |
| 1717 | else |
| 1718 | op_status = 1; // fail |
| 1719 | TWFunc::SetPerformanceMode(false); |
| 1720 | } |
| 1721 | |
| 1722 | operation_end(op_status); |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1726 | int GUIAction::repair(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1727 | { |
| 1728 | int op_status = 0; |
| 1729 | |
| 1730 | operation_start("Repair Partition"); |
| 1731 | if (simulate) { |
| 1732 | simulate_progress_bar(); |
| 1733 | } else { |
| 1734 | string part_path; |
| 1735 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1736 | if (PartitionManager.Repair_By_Path(part_path, true)) { |
| 1737 | op_status = 0; // success |
| 1738 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1739 | op_status = 1; // fail |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | operation_end(op_status); |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1747 | int GUIAction::resize(std::string arg __unused) |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1748 | { |
| 1749 | int op_status = 0; |
| 1750 | |
| 1751 | operation_start("Resize Partition"); |
| 1752 | if (simulate) { |
| 1753 | simulate_progress_bar(); |
| 1754 | } else { |
| 1755 | string part_path; |
| 1756 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1757 | if (PartitionManager.Resize_By_Path(part_path, true)) { |
| 1758 | op_status = 0; // success |
| 1759 | } else { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1760 | op_status = 1; // fail |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | operation_end(op_status); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1768 | int GUIAction::changefilesystem(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1769 | { |
| 1770 | int op_status = 0; |
| 1771 | |
| 1772 | operation_start("Change File System"); |
| 1773 | if (simulate) { |
| 1774 | simulate_progress_bar(); |
| 1775 | } else { |
| 1776 | string part_path, file_system; |
| 1777 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1778 | DataManager::GetValue("tw_action_new_file_system", file_system); |
| 1779 | if (PartitionManager.Wipe_By_Path(part_path, file_system)) { |
| 1780 | op_status = 0; // success |
| 1781 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1782 | gui_err("change_fs_err=Error changing file system."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1783 | op_status = 1; // fail |
| 1784 | } |
| 1785 | } |
| 1786 | PartitionManager.Update_System_Details(); |
| 1787 | operation_end(op_status); |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1791 | int GUIAction::startmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1792 | { |
| 1793 | int op_status = 0; |
| 1794 | |
| 1795 | operation_start("Start MTP"); |
| 1796 | if (PartitionManager.Enable_MTP()) |
| 1797 | op_status = 0; // success |
| 1798 | else |
| 1799 | op_status = 1; // fail |
| 1800 | |
| 1801 | operation_end(op_status); |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1805 | int GUIAction::stopmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1806 | { |
| 1807 | int op_status = 0; |
| 1808 | |
| 1809 | operation_start("Stop MTP"); |
| 1810 | if (PartitionManager.Disable_MTP()) |
| 1811 | op_status = 0; // success |
| 1812 | else |
| 1813 | op_status = 1; // fail |
| 1814 | |
| 1815 | operation_end(op_status); |
| 1816 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1817 | } |
| 1818 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1819 | int GUIAction::flashimage(std::string arg __unused) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1820 | { |
| 1821 | int op_status = 0; |
epicX | b733737 | 2021-02-24 23:12:08 +0530 | [diff] [blame] | 1822 | bool flag = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1823 | |
| 1824 | operation_start("Flash Image"); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1825 | string path, filename; |
| 1826 | DataManager::GetValue("tw_zip_location", path); |
| 1827 | DataManager::GetValue("tw_file", filename); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1828 | |
epicX | b733737 | 2021-02-24 23:12:08 +0530 | [diff] [blame] | 1829 | #ifdef AB_OTA_UPDATER |
| 1830 | string target = DataManager::GetStrValue("tw_flash_partition"); |
| 1831 | unsigned int pos = target.find_last_of(';'); |
| 1832 | string mount_point = pos != string::npos ? target.substr(0, pos) : ""; |
| 1833 | TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point); |
| 1834 | bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false; |
| 1835 | |
| 1836 | if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect)) |
| 1837 | { |
| 1838 | string current_slot = PartitionManager.Get_Active_Slot_Display(); |
| 1839 | bool pre_op_status = PartitionManager.Flash_Image(path, filename); |
| 1840 | |
| 1841 | PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A"); |
| 1842 | op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename)); |
| 1843 | PartitionManager.Set_Active_Slot(current_slot); |
| 1844 | |
| 1845 | DataManager::SetValue("tw_flash_both_slots", 0); |
| 1846 | flag = false; |
| 1847 | } |
| 1848 | #endif |
| 1849 | if (flag) |
| 1850 | { |
| 1851 | if (PartitionManager.Flash_Image(path, filename)) |
| 1852 | op_status = 0; // success |
| 1853 | else |
| 1854 | op_status = 1; // fail |
| 1855 | } |
| 1856 | |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1857 | operation_end(op_status); |
| 1858 | return 0; |
| 1859 | } |
| 1860 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1861 | int GUIAction::twcmd(std::string arg) |
| 1862 | { |
| 1863 | operation_start("TWRP CLI Command"); |
| 1864 | if (simulate) |
| 1865 | simulate_progress_bar(); |
| 1866 | else |
| 1867 | OpenRecoveryScript::Run_CLI_Command(arg.c_str()); |
| 1868 | operation_end(0); |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1872 | int GUIAction::getKeyByName(std::string key) |
| 1873 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1874 | 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] | 1875 | else if (key == "menu") return KEY_MENU; |
| 1876 | else if (key == "back") return KEY_BACK; |
| 1877 | else if (key == "search") return KEY_SEARCH; |
| 1878 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1879 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1880 | else if (key == "power") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1881 | int ret_val; |
| 1882 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1883 | if (!ret_val) |
| 1884 | return KEY_POWER; |
| 1885 | else |
| 1886 | return ret_val; |
| 1887 | } |
| 1888 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1889 | return atol(key.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1890 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1891 | |
| 1892 | int GUIAction::checkpartitionlifetimewrites(std::string arg) |
| 1893 | { |
| 1894 | int op_status = 0; |
| 1895 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg); |
| 1896 | |
| 1897 | operation_start("Check Partition Lifetime Writes"); |
| 1898 | if (sys) { |
| 1899 | if (sys->Check_Lifetime_Writes() != 0) |
| 1900 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1901 | else |
| 1902 | DataManager::SetValue("tw_lifetime_writes", 0); |
| 1903 | op_status = 0; // success |
| 1904 | } else { |
| 1905 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1906 | op_status = 1; // fail |
| 1907 | } |
| 1908 | |
| 1909 | operation_end(op_status); |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | int GUIAction::mountsystemtoggle(std::string arg) |
| 1914 | { |
| 1915 | int op_status = 0; |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1916 | bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1917 | bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor"); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1918 | |
| 1919 | operation_start("Toggle System Mount"); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1920 | if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1921 | op_status = 1; // fail |
| 1922 | } else { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1923 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1924 | if (Part) { |
Ethan Yonker | d6966f4 | 2015-05-30 14:52:16 -0500 | [diff] [blame] | 1925 | if (arg == "0") { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1926 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 1927 | Part->Change_Mount_Read_Only(false); |
| 1928 | } else { |
| 1929 | DataManager::SetValue("tw_mount_system_ro", 1); |
| 1930 | Part->Change_Mount_Read_Only(true); |
| 1931 | } |
| 1932 | if (remount_system) { |
| 1933 | Part->Mount(true); |
| 1934 | } |
| 1935 | op_status = 0; // success |
| 1936 | } else { |
| 1937 | op_status = 1; // fail |
| 1938 | } |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1939 | Part = PartitionManager.Find_Partition_By_Path("/vendor"); |
| 1940 | if (Part) { |
| 1941 | if (arg == "0") { |
| 1942 | Part->Change_Mount_Read_Only(false); |
| 1943 | } else { |
| 1944 | Part->Change_Mount_Read_Only(true); |
| 1945 | } |
| 1946 | if (remount_vendor) { |
| 1947 | Part->Mount(true); |
| 1948 | } |
| 1949 | op_status = 0; // success |
| 1950 | } else { |
| 1951 | op_status = 1; // fail |
| 1952 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | operation_end(op_status); |
| 1956 | return 0; |
| 1957 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1958 | |
| 1959 | int GUIAction::setlanguage(std::string arg __unused) |
| 1960 | { |
| 1961 | int op_status = 0; |
| 1962 | |
| 1963 | operation_start("Set Language"); |
| 1964 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1965 | PageManager::RequestReload(); |
| 1966 | op_status = 0; // success |
| 1967 | |
| 1968 | operation_end(op_status); |
| 1969 | return 0; |
| 1970 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1971 | |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 1972 | int GUIAction::togglebacklight(std::string arg __unused) |
| 1973 | { |
| 1974 | blankTimer.toggleBlank(); |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1978 | int GUIAction::setbootslot(std::string arg) |
| 1979 | { |
| 1980 | operation_start("Set Boot Slot"); |
| 1981 | if (!simulate) |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1982 | PartitionManager.Set_Active_Slot(arg); |
Matt Mower | 029a82d | 2017-01-08 13:12:38 -0600 | [diff] [blame] | 1983 | else |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1984 | simulate_progress_bar(); |
| 1985 | operation_end(0); |
| 1986 | return 0; |
| 1987 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1988 | |
| 1989 | int GUIAction::checkforapp(std::string arg __unused) |
| 1990 | { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1991 | operation_start("Check for TWRP App"); |
| 1992 | if (!simulate) |
| 1993 | { |
epicX | 9d930a2 | 2020-12-29 00:39:36 +0530 | [diff] [blame] | 1994 | TWFunc::checkforapp(); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1995 | } else |
| 1996 | simulate_progress_bar(); |
epicX | 9d930a2 | 2020-12-29 00:39:36 +0530 | [diff] [blame] | 1997 | |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1998 | operation_end(0); |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | int GUIAction::installapp(std::string arg __unused) |
| 2003 | { |
| 2004 | int op_status = 1; |
| 2005 | operation_start("Install TWRP App"); |
| 2006 | if (!simulate) |
| 2007 | { |
| 2008 | if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) { |
| 2009 | if (PartitionManager.Mount_By_Path("/data", true)) { |
| 2010 | string install_path = "/data/app"; |
| 2011 | string context = "u:object_r:apk_data_file:s0"; |
| 2012 | if (!TWFunc::Path_Exists(install_path)) { |
| 2013 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 2014 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 2015 | goto exit; |
| 2016 | } |
| 2017 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2018 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2019 | goto exit; |
| 2020 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2021 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2022 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2023 | goto exit; |
| 2024 | } |
| 2025 | } |
| 2026 | install_path += "/me.twrp.twrpapp-1"; |
| 2027 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 2028 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 2029 | goto exit; |
| 2030 | } |
| 2031 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2032 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2033 | goto exit; |
| 2034 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2035 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2036 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2037 | goto exit; |
| 2038 | } |
| 2039 | install_path += "/base.apk"; |
| 2040 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2041 | LOGERR("Error copying apk file\n"); |
| 2042 | goto exit; |
| 2043 | } |
| 2044 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2045 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2046 | goto exit; |
| 2047 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2048 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2049 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2050 | goto exit; |
| 2051 | } |
| 2052 | sync(); |
| 2053 | sync(); |
| 2054 | } |
| 2055 | } else { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 2056 | if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
| 2057 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 2058 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2059 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 2060 | string install_path = base_path + "/priv-app"; |
| 2061 | string context = "u:object_r:system_file:s0"; |
| 2062 | if (!TWFunc::Path_Exists(install_path)) |
| 2063 | install_path = base_path + "/app"; |
| 2064 | if (TWFunc::Path_Exists(install_path)) { |
| 2065 | install_path += "/twrpapp"; |
| 2066 | LOGINFO("Installing app to '%s'\n", install_path.c_str()); |
| 2067 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) { |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2068 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2069 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2070 | goto exit; |
| 2071 | } |
| 2072 | install_path += "/me.twrp.twrpapp.apk"; |
| 2073 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2074 | LOGERR("Error copying apk file\n"); |
| 2075 | goto exit; |
| 2076 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2077 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2078 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2079 | goto exit; |
| 2080 | } |
Stefan Tauner | 86a4370 | 2020-01-11 22:28:53 +0100 | [diff] [blame] | 2081 | |
| 2082 | // System apps require their permissions to be pre-set via an XML file in /etc/permissions |
| 2083 | string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml"; |
| 2084 | if (TWFunc::copy_file("/sbin/privapp-permissions-twrpapp.xml", permission_path, 0644)) { |
| 2085 | LOGERR("Error copying permission file\n"); |
| 2086 | goto exit; |
| 2087 | } |
| 2088 | if (chown(permission_path.c_str(), 1000, 1000)) { |
| 2089 | LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno)); |
| 2090 | goto exit; |
| 2091 | } |
| 2092 | if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) { |
| 2093 | LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno)); |
| 2094 | goto exit; |
| 2095 | } |
| 2096 | |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2097 | sync(); |
| 2098 | sync(); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 2099 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2100 | op_status = 0; |
| 2101 | } else { |
| 2102 | LOGERR("Error making app directory '%s': %s\n", strerror(errno)); |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | } |
| 2107 | } else |
| 2108 | simulate_progress_bar(); |
| 2109 | exit: |
epicX | 9d930a2 | 2020-12-29 00:39:36 +0530 | [diff] [blame] | 2110 | TWFunc::checkforapp(); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2111 | operation_end(0); |
| 2112 | return 0; |
| 2113 | } |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2114 | |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 2115 | int GUIAction::uninstalltwrpsystemapp(std::string arg __unused) |
| 2116 | { |
| 2117 | int op_status = 1; |
| 2118 | operation_start("Uninstall TWRP System App"); |
| 2119 | if (!simulate) |
| 2120 | { |
| 2121 | int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro"); |
| 2122 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
| 2123 | if (!Part) { |
| 2124 | LOGERR("Unabled to find system partition.\n"); |
| 2125 | goto exit; |
| 2126 | } |
| 2127 | if (!Part->UnMount(true)) { |
| 2128 | goto exit; |
| 2129 | } |
| 2130 | if (Mount_System_RO > 0) { |
| 2131 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 2132 | Part->Change_Mount_Read_Only(false); |
| 2133 | } |
| 2134 | if (Part->Mount(true)) { |
| 2135 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 2136 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
| 2137 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 2138 | string uninstall_path = base_path + "/priv-app"; |
| 2139 | if (!TWFunc::Path_Exists(uninstall_path)) |
| 2140 | uninstall_path = base_path + "/app"; |
| 2141 | uninstall_path += "/twrpapp"; |
| 2142 | if (TWFunc::Path_Exists(uninstall_path)) { |
| 2143 | LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str()); |
| 2144 | if (TWFunc::removeDir(uninstall_path, false) == 0) { |
| 2145 | sync(); |
| 2146 | op_status = 0; |
| 2147 | DataManager::SetValue("tw_app_installed_in_system", 0); |
| 2148 | DataManager::SetValue("tw_app_install_status", 0); |
| 2149 | } else { |
| 2150 | LOGERR("Unable to remove TWRP app from system.\n"); |
| 2151 | } |
| 2152 | } else { |
| 2153 | LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str()); |
| 2154 | } |
| 2155 | } |
| 2156 | Part->UnMount(true); |
| 2157 | if (Mount_System_RO > 0) { |
| 2158 | DataManager::SetValue("tw_mount_system_ro", Mount_System_RO); |
| 2159 | Part->Change_Mount_Read_Only(true); |
| 2160 | } |
| 2161 | } else |
| 2162 | simulate_progress_bar(); |
| 2163 | exit: |
epicX | 9d930a2 | 2020-12-29 00:39:36 +0530 | [diff] [blame] | 2164 | TWFunc::checkforapp(); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 2165 | operation_end(0); |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2169 | int GUIAction::repackimage(std::string arg __unused) |
| 2170 | { |
| 2171 | int op_status = 1; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 2172 | twrpRepacker repacker; |
| 2173 | |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2174 | operation_start("Repack Image"); |
| 2175 | if (!simulate) |
| 2176 | { |
| 2177 | std::string path = DataManager::GetStrValue("tw_filename"); |
| 2178 | Repack_Options_struct Repack_Options; |
| 2179 | Repack_Options.Disable_Verity = false; |
| 2180 | Repack_Options.Disable_Force_Encrypt = false; |
| 2181 | Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0; |
| 2182 | if (DataManager::GetIntValue("tw_repack_kernel") == 1) |
| 2183 | Repack_Options.Type = REPLACE_KERNEL; |
| 2184 | else |
| 2185 | Repack_Options.Type = REPLACE_RAMDISK; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 2186 | if (!repacker.Repack_Image_And_Flash(path, Repack_Options)) |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2187 | goto exit; |
| 2188 | } else |
| 2189 | simulate_progress_bar(); |
| 2190 | op_status = 0; |
| 2191 | exit: |
| 2192 | operation_end(op_status); |
| 2193 | return 0; |
| 2194 | } |
| 2195 | |
nebrassy | fe1c237 | 2021-05-20 13:03:30 +0200 | [diff] [blame] | 2196 | int GUIAction::reflashtwrp(std::string arg __unused) |
| 2197 | { |
| 2198 | int op_status = 1; |
| 2199 | twrpRepacker repacker; |
| 2200 | |
| 2201 | operation_start("Repack Image"); |
| 2202 | if (!simulate) |
| 2203 | { |
| 2204 | if (!repacker.Flash_Current_Twrp()) |
| 2205 | goto exit; |
| 2206 | } else |
| 2207 | simulate_progress_bar(); |
| 2208 | op_status = 0; |
| 2209 | exit: |
| 2210 | operation_end(op_status); |
| 2211 | return 0; |
| 2212 | } |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2213 | int GUIAction::fixabrecoverybootloop(std::string arg __unused) |
| 2214 | { |
| 2215 | int op_status = 1; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 2216 | twrpRepacker repacker; |
| 2217 | |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2218 | operation_start("Repack Image"); |
| 2219 | if (!simulate) |
| 2220 | { |
| 2221 | if (!TWFunc::Path_Exists("/sbin/magiskboot")) { |
| 2222 | LOGERR("Image repacking tool not present in this TWRP build!"); |
| 2223 | goto exit; |
| 2224 | } |
| 2225 | DataManager::SetProgress(0); |
| 2226 | TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot"); |
| 2227 | if (part) |
| 2228 | gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name)); |
| 2229 | else { |
| 2230 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot")); |
| 2231 | goto exit; |
| 2232 | } |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 2233 | if (!repacker.Backup_Image_For_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack"))) |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2234 | goto exit; |
| 2235 | DataManager::SetProgress(.25); |
| 2236 | gui_msg("fixing_recovery_loop_patch=Patching kernel..."); |
Mohd Faraz | 5738e76 | 2020-05-10 04:18:30 +0530 | [diff] [blame] | 2237 | std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300"; |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2238 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2239 | gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel.")); |
| 2240 | goto exit; |
| 2241 | } |
| 2242 | std::string header_path = REPACK_ORIG_DIR; |
| 2243 | header_path += "header"; |
| 2244 | if (TWFunc::Path_Exists(header_path)) { |
| 2245 | command = "cd " REPACK_ORIG_DIR " && sed -i \"s|$(grep '^cmdline=' header | cut -d= -f2-)|$(grep '^cmdline=' header | cut -d= -f2- | sed -e 's/skip_override//' -e 's/ */ /g' -e 's/[ \t]*$//')|\" header"; |
| 2246 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2247 | gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel.")); |
| 2248 | goto exit; |
| 2249 | } |
| 2250 | } |
| 2251 | DataManager::SetProgress(.5); |
| 2252 | gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name)); |
Mohd Faraz | 5738e76 | 2020-05-10 04:18:30 +0530 | [diff] [blame] | 2253 | command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot repack " REPACK_ORIG_DIR "boot.img"; |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2254 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2255 | gui_msg(Msg(msg::kError, "repack_error=Error repacking image.")); |
| 2256 | goto exit; |
| 2257 | } |
| 2258 | DataManager::SetProgress(.75); |
| 2259 | std::string path = REPACK_ORIG_DIR; |
| 2260 | std::string file = "new-boot.img"; |
| 2261 | DataManager::SetValue("tw_flash_partition", "/boot;"); |
| 2262 | if (!PartitionManager.Flash_Image(path, file)) { |
| 2263 | LOGINFO("Error flashing new image\n"); |
| 2264 | goto exit; |
| 2265 | } |
| 2266 | DataManager::SetProgress(1); |
| 2267 | TWFunc::removeDir(REPACK_ORIG_DIR, false); |
| 2268 | } else |
| 2269 | simulate_progress_bar(); |
| 2270 | op_status = 0; |
| 2271 | exit: |
| 2272 | operation_end(op_status); |
| 2273 | return 0; |
| 2274 | } |
Mohd Faraz | 3c25ab3 | 2020-08-12 12:29:42 +0000 | [diff] [blame] | 2275 | |
| 2276 | int GUIAction::changeterminal(std::string arg) { |
| 2277 | bool res = true; |
| 2278 | std::string resp, cmd = "cd " + arg; |
| 2279 | DataManager::GetValue("tw_terminal_location", resp); |
| 2280 | if (arg.empty() && !resp.empty()) { |
| 2281 | cmd = "cd /"; |
| 2282 | for (uint8_t iter = 0; iter < cmd.size(); iter++) |
| 2283 | term->NotifyCharInput(cmd.at(iter)); |
| 2284 | term->NotifyCharInput(13); |
| 2285 | DataManager::SetValue("tw_terminal_location", ""); |
| 2286 | return 0; |
| 2287 | } |
| 2288 | if (term != NULL && !arg.empty()) { |
| 2289 | DataManager::SetValue("tw_terminal_location", arg); |
| 2290 | if (term->status()) { |
| 2291 | for (uint8_t iter = 0; iter < cmd.size(); iter++) |
| 2292 | term->NotifyCharInput(cmd.at(iter)); |
| 2293 | term->NotifyCharInput(13); |
| 2294 | } |
| 2295 | else if (chdir(arg.c_str()) != 0) { |
| 2296 | LOGINFO("Unable to change dir to %s\n", arg.c_str()); |
| 2297 | res = false; |
| 2298 | } |
| 2299 | } |
| 2300 | else { |
| 2301 | res = false; |
| 2302 | LOGINFO("Unable to switch to Terminal\n"); |
| 2303 | } |
| 2304 | if (res) |
| 2305 | gui_changePage("terminalcommand"); |
| 2306 | return 0; |
| 2307 | } |
Captain Throwback | 3b55610 | 2021-02-12 19:32:36 -0500 | [diff] [blame] | 2308 | #ifndef TW_EXCLUDE_NANO |
| 2309 | int GUIAction::editfile(std::string arg) { |
| 2310 | if (term != NULL) { |
| 2311 | for (uint8_t iter = 0; iter < arg.size(); iter++) |
| 2312 | term->NotifyCharInput(arg.at(iter)); |
| 2313 | term->NotifyCharInput(13); |
| 2314 | } |
| 2315 | else |
| 2316 | LOGINFO("Unable to switch to Terminal\n"); |
| 2317 | return 0; |
| 2318 | } |
| 2319 | #endif |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 2320 | |
| 2321 | int GUIAction::applycustomtwrpfolder(string arg __unused) |
| 2322 | { |
| 2323 | operation_start("ChangingTWRPFolder"); |
| 2324 | string storageFolder = DataManager::GetSettingsStoragePath(); |
| 2325 | string newFolder = storageFolder + '/' + arg; |
| 2326 | string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id"); |
| 2327 | string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR); |
| 2328 | bool ret = false; |
| 2329 | |
| 2330 | if (TWFunc::Path_Exists(newFolder)) { |
| 2331 | gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!")); |
| 2332 | } else { |
| 2333 | ret = true; |
| 2334 | } |
| 2335 | |
| 2336 | if (newFolder != prevFolder && ret) { |
| 2337 | ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true; |
| 2338 | } else { |
| 2339 | gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!")); |
| 2340 | } |
| 2341 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 2342 | if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false; |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 2343 | |
| 2344 | |
| 2345 | if (ret) { |
| 2346 | DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg); |
| 2347 | DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder); |
| 2348 | DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE; |
| 2349 | } |
| 2350 | operation_end((int)!ret); |
| 2351 | return 0; |
| 2352 | } |