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