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