Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1 | /* |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 2 | Copyright 2013 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <linux/input.h> |
| 30 | #include <time.h> |
| 31 | #include <unistd.h> |
| 32 | #include <stdlib.h> |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 34 | #include <dirent.h> |
Matt Mower | 0c88b84 | 2017-01-15 16:00:49 -0600 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | |
| 37 | #include <string> |
| 38 | #include <sstream> |
| 39 | #include "../partitions.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 40 | #include "../twrp-functions.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 41 | #include "../openrecoveryscript.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 43 | #include "install/adb_install.h" |
| 44 | |
| 45 | #include "fuse_sideload.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 46 | #include "blanktimer.hpp" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 47 | #include "../twinstall.h" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 48 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 50 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | #include "../variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 52 | #include "cutils/properties.h" |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 53 | #include "install/adb_install.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | }; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 55 | #include "set_metadata.h" |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 56 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
| 58 | #include "rapidxml.hpp" |
| 59 | #include "objects.hpp" |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 60 | #include "tw_atomic.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 62 | GUIAction::mapFunc GUIAction::mf; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 63 | std::set<string> GUIAction::setActionsRunningInCallerThread; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 64 | static string zip_queue[10]; |
| 65 | static int zip_queue_index; |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 66 | pid_t sideload_child_pid; |
Noah Jacobson | 0b25b1d | 2019-04-28 00:10:07 -0400 | [diff] [blame^] | 67 | extern std::vector<users_struct> Users_List; |
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; |
| 400 | if (stat("/sbin/installTwrp", &st) == 0) |
| 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..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 405 | if (TWFunc::Exec_Cmd("/sbin/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 bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 504 | bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR; |
| 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); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 925 | if (TWFunc::Path_Exists("/sbin/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); |
Matt Mower | 80f7b36 | 2015-12-12 15:38:01 -0600 | [diff] [blame] | 929 | if (TWFunc::Path_Exists("/sbin/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); |
| 933 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) |
| 934 | DataManager::SetValue("tw_partition_f2fs", 1); |
| 935 | else |
| 936 | DataManager::SetValue("tw_partition_f2fs", 0); |
| 937 | if (TWFunc::Path_Exists("/sbin/mke2fs")) |
| 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 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1033 | int GUIAction::flash(std::string arg) |
| 1034 | { |
| 1035 | int i, ret_val = 0, wipe_cache = 0; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1036 | // We're going to jump to this page first, like a loading page |
| 1037 | gui_changePage(arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1038 | for (i=0; i<zip_queue_index; i++) { |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1039 | string zip_path = zip_queue[i]; |
| 1040 | size_t slashpos = zip_path.find_last_of('/'); |
| 1041 | string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1042 | operation_start("Flashing"); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1043 | DataManager::SetValue("tw_filename", zip_path); |
| 1044 | DataManager::SetValue("tw_file", zip_filename); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1045 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 1046 | |
| 1047 | TWFunc::SetPerformanceMode(true); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1048 | ret_val = flash_zip(zip_path, &wipe_cache); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1049 | TWFunc::SetPerformanceMode(false); |
| 1050 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1051 | 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] | 1052 | ret_val = 1; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1053 | break; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | zip_queue_index = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1057 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1058 | if (wipe_cache) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1059 | 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] | 1060 | PartitionManager.Wipe_By_Path("/cache"); |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1061 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1062 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1063 | reinject_after_flash(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1064 | PartitionManager.Update_System_Details(); |
| 1065 | operation_end(ret_val); |
bigbiff | a869fc7 | 2016-03-01 19:40:36 -0500 | [diff] [blame] | 1066 | // 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] | 1067 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | int GUIAction::wipe(std::string arg) |
| 1072 | { |
| 1073 | operation_start("Format"); |
| 1074 | DataManager::SetValue("tw_partition", arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1075 | int ret_val = false; |
| 1076 | |
| 1077 | if (simulate) { |
| 1078 | simulate_progress_bar(); |
| 1079 | } else { |
| 1080 | if (arg == "data") |
| 1081 | ret_val = PartitionManager.Factory_Reset(); |
| 1082 | else if (arg == "battery") |
| 1083 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
| 1084 | else if (arg == "rotate") |
| 1085 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
| 1086 | else if (arg == "dalvik") |
| 1087 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
| 1088 | else if (arg == "DATAMEDIA") { |
| 1089 | ret_val = PartitionManager.Format_Data(); |
| 1090 | } else if (arg == "INTERNAL") { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1091 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1092 | |
| 1093 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1094 | if (has_datamedia) { |
| 1095 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
| 1096 | } else { |
| 1097 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
| 1098 | } |
| 1099 | } else if (arg == "EXTERNAL") { |
| 1100 | string External_Path; |
| 1101 | |
| 1102 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 1103 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
| 1104 | } else if (arg == "ANDROIDSECURE") { |
| 1105 | ret_val = PartitionManager.Wipe_Android_Secure(); |
| 1106 | } else if (arg == "LIST") { |
| 1107 | string Wipe_List, wipe_path; |
| 1108 | bool skip = false; |
| 1109 | ret_val = true; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1110 | |
| 1111 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 1112 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
| 1113 | if (!Wipe_List.empty()) { |
| 1114 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 1115 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 1116 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 1117 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
| 1118 | if (wipe_path == "/and-sec") { |
| 1119 | if (!PartitionManager.Wipe_Android_Secure()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1120 | gui_msg("and_sec_wipe_err=Unable to wipe android secure"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1121 | ret_val = false; |
| 1122 | break; |
| 1123 | } else { |
| 1124 | skip = true; |
| 1125 | } |
| 1126 | } else if (wipe_path == "DALVIK") { |
| 1127 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1128 | gui_err("dalvik_wipe_err=Failed to wipe dalvik"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1129 | ret_val = false; |
| 1130 | break; |
| 1131 | } else { |
| 1132 | skip = true; |
| 1133 | } |
| 1134 | } else if (wipe_path == "INTERNAL") { |
| 1135 | if (!PartitionManager.Wipe_Media_From_Data()) { |
| 1136 | ret_val = false; |
| 1137 | break; |
| 1138 | } else { |
| 1139 | skip = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1140 | } |
| 1141 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1142 | if (!skip) { |
| 1143 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1144 | 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] | 1145 | ret_val = false; |
| 1146 | break; |
| 1147 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 1148 | arg = wipe_path; |
| 1149 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1150 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1151 | skip = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1152 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1153 | start_pos = end_pos + 1; |
| 1154 | end_pos = Wipe_List.find(";", start_pos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1155 | } |
| 1156 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1157 | } else |
| 1158 | ret_val = PartitionManager.Wipe_By_Path(arg); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1159 | #ifndef TW_OEM_BUILD |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1160 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 1161 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 1162 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1163 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1164 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 1165 | LOGINFO("Making TWRP folder and saving settings.\n"); |
| 1166 | Storage_Path += "/TWRP"; |
| 1167 | mkdir(Storage_Path.c_str(), 0777); |
| 1168 | DataManager::Flush(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1169 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1170 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
| 1171 | } |
| 1172 | } |
| 1173 | #endif |
| 1174 | } |
| 1175 | PartitionManager.Update_System_Details(); |
| 1176 | if (ret_val) |
| 1177 | ret_val = 0; // 0 is success |
| 1178 | else |
| 1179 | ret_val = 1; // 1 is failure |
| 1180 | operation_end(ret_val); |
| 1181 | return 0; |
| 1182 | } |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 1183 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1184 | int GUIAction::refreshsizes(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1185 | { |
| 1186 | operation_start("Refreshing Sizes"); |
| 1187 | if (simulate) { |
| 1188 | simulate_progress_bar(); |
| 1189 | } else |
| 1190 | PartitionManager.Update_System_Details(); |
| 1191 | operation_end(0); |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | int GUIAction::nandroid(std::string arg) |
| 1196 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1197 | if (simulate) { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1198 | PartitionManager.stop_backup.set_value(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1199 | DataManager::SetValue("tw_partition", "Simulation"); |
| 1200 | simulate_progress_bar(); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1201 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1202 | } else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1203 | operation_start("Nandroid"); |
| 1204 | int ret = 0; |
| 1205 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1206 | if (arg == "backup") { |
| 1207 | string Backup_Name; |
| 1208 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 1209 | string auto_gen = gui_lookup("auto_generate", "(Auto Generate)"); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 1210 | 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] | 1211 | ret = PartitionManager.Run_Backup(false); |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1212 | DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup |
| 1213 | if (!PartitionManager.stop_backup.get_value()) { |
| 1214 | if (ret == false) |
| 1215 | ret = 1; // 1 for failure |
| 1216 | else |
| 1217 | ret = 0; // 0 for success |
| 1218 | DataManager::SetValue("tw_cancel_backup", 0); |
| 1219 | } else { |
| 1220 | DataManager::SetValue("tw_cancel_backup", 1); |
| 1221 | gui_msg("backup_cancel=Backup Cancelled"); |
| 1222 | ret = 0; |
| 1223 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1224 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1225 | operation_end(1); |
| 1226 | return -1; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1227 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1228 | DataManager::SetValue(TW_BACKUP_NAME, auto_gen); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1229 | } else if (arg == "restore") { |
| 1230 | string Restore_Name; |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1231 | int gui_adb_backup; |
| 1232 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1233 | DataManager::GetValue("tw_restore", Restore_Name); |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1234 | DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup); |
| 1235 | if (gui_adb_backup) { |
| 1236 | DataManager::SetValue("tw_operation_state", 1); |
| 1237 | if (TWFunc::stream_adb_backup(Restore_Name) == 0) |
| 1238 | ret = 0; // success |
| 1239 | else |
| 1240 | ret = 1; // failure |
| 1241 | DataManager::SetValue("tw_enable_adb_backup", 0); |
| 1242 | ret = 0; // assume success??? |
| 1243 | } else { |
| 1244 | if (PartitionManager.Run_Restore(Restore_Name)) |
| 1245 | ret = 0; // success |
| 1246 | else |
| 1247 | ret = 1; // failure |
| 1248 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1249 | } else { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1250 | operation_end(1); // invalid arg specified, fail |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1251 | return -1; |
| 1252 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1253 | operation_end(ret); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1254 | return ret; |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1259 | int GUIAction::cancelbackup(std::string arg __unused) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1260 | if (simulate) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1261 | PartitionManager.stop_backup.set_value(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1262 | } |
| 1263 | else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1264 | int op_status = PartitionManager.Cancel_Backup(); |
| 1265 | if (op_status != 0) |
| 1266 | op_status = 1; // failure |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1270 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1271 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1272 | int GUIAction::fixcontexts(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1273 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1274 | int op_status = 0; |
| 1275 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1276 | operation_start("Fix Contexts"); |
| 1277 | LOGINFO("fix contexts started!\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1278 | if (simulate) { |
| 1279 | simulate_progress_bar(); |
| 1280 | } else { |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1281 | op_status = PartitionManager.Fix_Contexts(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1282 | if (op_status != 0) |
| 1283 | op_status = 1; // failure |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1284 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1285 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
| 1288 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1289 | int GUIAction::fixpermissions(std::string arg) |
| 1290 | { |
| 1291 | return fixcontexts(arg); |
| 1292 | } |
| 1293 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1294 | int GUIAction::dd(std::string arg) |
| 1295 | { |
| 1296 | operation_start("imaging"); |
| 1297 | |
| 1298 | if (simulate) { |
| 1299 | simulate_progress_bar(); |
| 1300 | } else { |
| 1301 | string cmd = "dd " + arg; |
| 1302 | TWFunc::Exec_Cmd(cmd); |
| 1303 | } |
| 1304 | operation_end(0); |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1308 | int GUIAction::partitionsd(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1309 | { |
| 1310 | operation_start("Partition SD Card"); |
| 1311 | int ret_val = 0; |
| 1312 | |
| 1313 | if (simulate) { |
| 1314 | simulate_progress_bar(); |
| 1315 | } else { |
| 1316 | int allow_partition; |
| 1317 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 1318 | if (allow_partition == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1319 | 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] | 1320 | } else { |
| 1321 | if (!PartitionManager.Partition_SDCard()) |
| 1322 | ret_val = 1; // failed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1323 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1324 | } |
| 1325 | operation_end(ret_val); |
| 1326 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1327 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1328 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1329 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1330 | int GUIAction::installhtcdumlock(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1331 | { |
| 1332 | operation_start("Install HTC Dumlock"); |
| 1333 | if (simulate) { |
| 1334 | simulate_progress_bar(); |
| 1335 | } else |
| 1336 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1337 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1338 | operation_end(0); |
| 1339 | return 0; |
| 1340 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1341 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1342 | int GUIAction::htcdumlockrestoreboot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1343 | { |
| 1344 | operation_start("HTC Dumlock Restore Boot"); |
| 1345 | if (simulate) { |
| 1346 | simulate_progress_bar(); |
| 1347 | } else |
| 1348 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1349 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1350 | operation_end(0); |
| 1351 | return 0; |
| 1352 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1353 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1354 | int GUIAction::htcdumlockreflashrecovery(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1355 | { |
| 1356 | operation_start("HTC Dumlock Reflash Recovery"); |
| 1357 | if (simulate) { |
| 1358 | simulate_progress_bar(); |
| 1359 | } else |
| 1360 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1361 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1362 | operation_end(0); |
| 1363 | return 0; |
| 1364 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1365 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1366 | int GUIAction::cmd(std::string arg) |
| 1367 | { |
| 1368 | int op_status = 0; |
| 1369 | |
| 1370 | operation_start("Command"); |
| 1371 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
| 1372 | if (simulate) { |
| 1373 | simulate_progress_bar(); |
| 1374 | } else { |
| 1375 | op_status = TWFunc::Exec_Cmd(arg); |
| 1376 | if (op_status != 0) |
| 1377 | op_status = 1; |
| 1378 | } |
| 1379 | |
| 1380 | operation_end(op_status); |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | int GUIAction::terminalcommand(std::string arg) |
| 1385 | { |
| 1386 | int op_status = 0; |
| 1387 | string cmdpath, command; |
| 1388 | |
| 1389 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1390 | operation_start("CommandOutput"); |
| 1391 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 1392 | if (simulate) { |
| 1393 | simulate_progress_bar(); |
| 1394 | operation_end(op_status); |
Matt Mower | 5aa29ab | 2015-02-25 23:50:55 -0600 | [diff] [blame] | 1395 | } else if (arg == "exit") { |
| 1396 | LOGINFO("Exiting terminal\n"); |
| 1397 | operation_end(op_status); |
| 1398 | page("main"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1399 | } else { |
| 1400 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
| 1401 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1402 | DataManager::SetValue("tw_terminal_state", 1); |
| 1403 | DataManager::SetValue("tw_background_thread_running", 1); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1404 | FILE* fp; |
| 1405 | char line[512]; |
| 1406 | |
| 1407 | fp = popen(command.c_str(), "r"); |
| 1408 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1409 | LOGERR("Error opening command to run (%s).\n", strerror(errno)); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1410 | } else { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1411 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1412 | struct timeval timeout; |
| 1413 | fd_set fdset; |
| 1414 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1415 | while (keep_going) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1416 | { |
| 1417 | FD_ZERO(&fdset); |
| 1418 | FD_SET(fd, &fdset); |
| 1419 | timeout.tv_sec = 0; |
| 1420 | timeout.tv_usec = 400000; |
| 1421 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1422 | if (has_data == 0) { |
| 1423 | // Timeout reached |
| 1424 | DataManager::GetValue("tw_terminal_state", check); |
| 1425 | if (check == 0) { |
| 1426 | keep_going = 0; |
| 1427 | } |
| 1428 | } else if (has_data < 0) { |
| 1429 | // End of execution |
| 1430 | keep_going = 0; |
| 1431 | } else { |
| 1432 | // Try to read output |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1433 | if (fgets(line, sizeof(line), fp) != NULL) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1434 | gui_print("%s", line); // Display output |
| 1435 | else |
| 1436 | keep_going = 0; // Done executing |
| 1437 | } |
| 1438 | } |
| 1439 | fclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1440 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1441 | DataManager::SetValue("tw_operation_status", 0); |
| 1442 | DataManager::SetValue("tw_operation_state", 1); |
| 1443 | DataManager::SetValue("tw_terminal_state", 0); |
| 1444 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1445 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1446 | } |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1450 | int GUIAction::killterminal(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1451 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1452 | LOGINFO("Sending kill command...\n"); |
| 1453 | operation_start("KillCommand"); |
| 1454 | DataManager::SetValue("tw_operation_status", 0); |
| 1455 | DataManager::SetValue("tw_operation_state", 1); |
| 1456 | DataManager::SetValue("tw_terminal_state", 0); |
| 1457 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1458 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1462 | int GUIAction::reinjecttwrp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1463 | { |
| 1464 | int op_status = 0; |
| 1465 | operation_start("ReinjectTWRP"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1466 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1467 | if (simulate) { |
| 1468 | simulate_progress_bar(); |
| 1469 | } else { |
| 1470 | 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] | 1471 | gui_msg("done=Done."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | operation_end(op_status); |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1478 | int GUIAction::checkbackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1479 | { |
| 1480 | int op_status = 0; |
| 1481 | |
| 1482 | operation_start("CheckBackupName"); |
| 1483 | if (simulate) { |
| 1484 | simulate_progress_bar(); |
| 1485 | } else { |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 1486 | string Backup_Name; |
| 1487 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 1488 | op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1489 | if (op_status != 0) |
| 1490 | op_status = 1; |
| 1491 | } |
| 1492 | |
| 1493 | operation_end(op_status); |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1497 | int GUIAction::decrypt(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1498 | { |
| 1499 | int op_status = 0; |
| 1500 | |
| 1501 | operation_start("Decrypt"); |
| 1502 | if (simulate) { |
| 1503 | simulate_progress_bar(); |
| 1504 | } else { |
| 1505 | string Password; |
Noah Jacobson | 0b25b1d | 2019-04-28 00:10:07 -0400 | [diff] [blame^] | 1506 | string userID; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1507 | DataManager::GetValue("tw_crypto_password", Password); |
Noah Jacobson | 0b25b1d | 2019-04-28 00:10:07 -0400 | [diff] [blame^] | 1508 | |
| 1509 | if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE |
| 1510 | DataManager::GetValue("tw_crypto_user_id", userID); |
| 1511 | if (userID != "") { |
| 1512 | op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str())); |
| 1513 | if (userID != "0") { |
| 1514 | if (op_status != 0) |
| 1515 | op_status = 1; |
| 1516 | operation_end(op_status); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | } else { |
| 1520 | LOGINFO("User ID not found\n"); |
| 1521 | op_status = 1; |
| 1522 | } |
| 1523 | ::sleep(1); |
| 1524 | } else { // for FDE |
| 1525 | op_status = PartitionManager.Decrypt_Device(Password); |
| 1526 | } |
| 1527 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1528 | if (op_status != 0) |
| 1529 | op_status = 1; |
| 1530 | else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1531 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1532 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1533 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1534 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1535 | // Check for a custom theme and load it if exists |
| 1536 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1537 | if (has_datamedia != 0) { |
| 1538 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 1539 | 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] | 1540 | } else { |
| 1541 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1542 | } |
| 1543 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1544 | PartitionManager.Decrypt_Adopted(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | operation_end(op_status); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1552 | int GUIAction::adbsideload(std::string arg __unused) |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1553 | { |
| 1554 | operation_start("Sideload"); |
| 1555 | if (simulate) { |
| 1556 | simulate_progress_bar(); |
| 1557 | operation_end(0); |
| 1558 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1559 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1560 | bool mtp_was_enabled = TWFunc::Toggle_MTP(false); |
| 1561 | |
| 1562 | // wait for the adb connection |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1563 | // int ret = apply_from_adb("/", &sideload_child_pid); |
| 1564 | Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER; |
| 1565 | int ret = ApplyFromAdb("/", &reboot_action); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1566 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
| 1567 | |
| 1568 | if (ret != 0) { |
| 1569 | if (ret == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1570 | 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] | 1571 | ret = 1; // failure |
| 1572 | } else { |
| 1573 | int wipe_cache = 0; |
| 1574 | int wipe_dalvik = 0; |
| 1575 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
| 1576 | |
| 1577 | if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 1578 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1579 | PartitionManager.Wipe_By_Path("/cache"); |
| 1580 | if (wipe_dalvik) |
| 1581 | PartitionManager.Wipe_Dalvik_Cache(); |
| 1582 | } else { |
| 1583 | ret = 1; // failure |
| 1584 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1585 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1586 | if (sideload_child_pid) { |
| 1587 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1588 | struct stat st; |
| 1589 | // Calling stat() on this magic filename signals the minadbd |
| 1590 | // subprocess to shut down. |
| 1591 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1592 | int status; |
| 1593 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1594 | waitpid(sideload_child_pid, &status, 0); |
| 1595 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 1596 | property_set("ctl.start", "adbd"); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1597 | TWFunc::Toggle_MTP(mtp_was_enabled); |
| 1598 | reinject_after_flash(); |
| 1599 | operation_end(ret); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1600 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1604 | int GUIAction::adbsideloadcancel(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1605 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1606 | struct stat st; |
| 1607 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1608 | gui_msg("cancel_sideload=Cancelling ADB sideload..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1609 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1610 | // Calling stat() on this magic filename signals the minadbd |
| 1611 | // subprocess to shut down. |
| 1612 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1613 | if (!sideload_child_pid) { |
| 1614 | LOGERR("Unable to get child ID\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1615 | return 0; |
| 1616 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1617 | ::sleep(1); |
| 1618 | LOGINFO("Killing child sideload process.\n"); |
| 1619 | kill(sideload_child_pid, SIGTERM); |
| 1620 | int status; |
| 1621 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1622 | waitpid(sideload_child_pid, &status, 0); |
| 1623 | sideload_child_pid = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1624 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1628 | int GUIAction::openrecoveryscript(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1629 | { |
| 1630 | operation_start("OpenRecoveryScript"); |
| 1631 | if (simulate) { |
| 1632 | simulate_progress_bar(); |
Ethan Yonker | 24e7eb7 | 2015-01-03 16:13:06 -0600 | [diff] [blame] | 1633 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1634 | } else { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1635 | int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action(); |
Ethan Yonker | e1abe61 | 2015-06-09 11:09:32 -0500 | [diff] [blame] | 1636 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1637 | } |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1641 | int GUIAction::installsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1642 | { |
| 1643 | int op_status = 0; |
| 1644 | |
| 1645 | operation_start("Install SuperSU"); |
| 1646 | if (simulate) { |
| 1647 | simulate_progress_bar(); |
| 1648 | } else { |
Ethan Yonker | fa67cbf | 2018-07-20 12:22:33 -0500 | [diff] [blame] | 1649 | LOGERR("Installing SuperSU was deprecated from TWRP.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | operation_end(op_status); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1656 | int GUIAction::fixsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1657 | { |
| 1658 | int op_status = 0; |
| 1659 | |
| 1660 | operation_start("Fixing Superuser Permissions"); |
| 1661 | if (simulate) { |
| 1662 | simulate_progress_bar(); |
| 1663 | } else { |
| 1664 | LOGERR("Fixing su permissions was deprecated from TWRP.\n"); |
| 1665 | LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n"); |
| 1666 | } |
| 1667 | |
| 1668 | operation_end(op_status); |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1672 | int GUIAction::decrypt_backup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1673 | { |
| 1674 | int op_status = 0; |
| 1675 | |
| 1676 | operation_start("Try Restore Decrypt"); |
| 1677 | if (simulate) { |
| 1678 | simulate_progress_bar(); |
| 1679 | } else { |
| 1680 | string Restore_Path, Filename, Password; |
| 1681 | DataManager::GetValue("tw_restore", Restore_Path); |
| 1682 | Restore_Path += "/"; |
| 1683 | DataManager::GetValue("tw_restore_password", Password); |
| 1684 | TWFunc::SetPerformanceMode(true); |
| 1685 | if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password)) |
| 1686 | op_status = 0; // success |
| 1687 | else |
| 1688 | op_status = 1; // fail |
| 1689 | TWFunc::SetPerformanceMode(false); |
| 1690 | } |
| 1691 | |
| 1692 | operation_end(op_status); |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1696 | int GUIAction::repair(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1697 | { |
| 1698 | int op_status = 0; |
| 1699 | |
| 1700 | operation_start("Repair Partition"); |
| 1701 | if (simulate) { |
| 1702 | simulate_progress_bar(); |
| 1703 | } else { |
| 1704 | string part_path; |
| 1705 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1706 | if (PartitionManager.Repair_By_Path(part_path, true)) { |
| 1707 | op_status = 0; // success |
| 1708 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1709 | op_status = 1; // fail |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | operation_end(op_status); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1717 | int GUIAction::resize(std::string arg __unused) |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1718 | { |
| 1719 | int op_status = 0; |
| 1720 | |
| 1721 | operation_start("Resize Partition"); |
| 1722 | if (simulate) { |
| 1723 | simulate_progress_bar(); |
| 1724 | } else { |
| 1725 | string part_path; |
| 1726 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1727 | if (PartitionManager.Resize_By_Path(part_path, true)) { |
| 1728 | op_status = 0; // success |
| 1729 | } else { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1730 | op_status = 1; // fail |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | operation_end(op_status); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1738 | int GUIAction::changefilesystem(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1739 | { |
| 1740 | int op_status = 0; |
| 1741 | |
| 1742 | operation_start("Change File System"); |
| 1743 | if (simulate) { |
| 1744 | simulate_progress_bar(); |
| 1745 | } else { |
| 1746 | string part_path, file_system; |
| 1747 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1748 | DataManager::GetValue("tw_action_new_file_system", file_system); |
| 1749 | if (PartitionManager.Wipe_By_Path(part_path, file_system)) { |
| 1750 | op_status = 0; // success |
| 1751 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1752 | gui_err("change_fs_err=Error changing file system."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1753 | op_status = 1; // fail |
| 1754 | } |
| 1755 | } |
| 1756 | PartitionManager.Update_System_Details(); |
| 1757 | operation_end(op_status); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1761 | int GUIAction::startmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1762 | { |
| 1763 | int op_status = 0; |
| 1764 | |
| 1765 | operation_start("Start MTP"); |
| 1766 | if (PartitionManager.Enable_MTP()) |
| 1767 | op_status = 0; // success |
| 1768 | else |
| 1769 | op_status = 1; // fail |
| 1770 | |
| 1771 | operation_end(op_status); |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1775 | int GUIAction::stopmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1776 | { |
| 1777 | int op_status = 0; |
| 1778 | |
| 1779 | operation_start("Stop MTP"); |
| 1780 | if (PartitionManager.Disable_MTP()) |
| 1781 | op_status = 0; // success |
| 1782 | else |
| 1783 | op_status = 1; // fail |
| 1784 | |
| 1785 | operation_end(op_status); |
| 1786 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1789 | int GUIAction::flashimage(std::string arg __unused) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1790 | { |
| 1791 | int op_status = 0; |
| 1792 | |
| 1793 | operation_start("Flash Image"); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1794 | string path, filename; |
| 1795 | DataManager::GetValue("tw_zip_location", path); |
| 1796 | DataManager::GetValue("tw_file", filename); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 1797 | if (PartitionManager.Flash_Image(path, filename)) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1798 | op_status = 0; // success |
| 1799 | else |
| 1800 | op_status = 1; // fail |
| 1801 | |
| 1802 | operation_end(op_status); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1806 | int GUIAction::twcmd(std::string arg) |
| 1807 | { |
| 1808 | operation_start("TWRP CLI Command"); |
| 1809 | if (simulate) |
| 1810 | simulate_progress_bar(); |
| 1811 | else |
| 1812 | OpenRecoveryScript::Run_CLI_Command(arg.c_str()); |
| 1813 | operation_end(0); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1817 | int GUIAction::getKeyByName(std::string key) |
| 1818 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1819 | 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] | 1820 | else if (key == "menu") return KEY_MENU; |
| 1821 | else if (key == "back") return KEY_BACK; |
| 1822 | else if (key == "search") return KEY_SEARCH; |
| 1823 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1824 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1825 | else if (key == "power") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1826 | int ret_val; |
| 1827 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1828 | if (!ret_val) |
| 1829 | return KEY_POWER; |
| 1830 | else |
| 1831 | return ret_val; |
| 1832 | } |
| 1833 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1834 | return atol(key.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1835 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1836 | |
| 1837 | int GUIAction::checkpartitionlifetimewrites(std::string arg) |
| 1838 | { |
| 1839 | int op_status = 0; |
| 1840 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg); |
| 1841 | |
| 1842 | operation_start("Check Partition Lifetime Writes"); |
| 1843 | if (sys) { |
| 1844 | if (sys->Check_Lifetime_Writes() != 0) |
| 1845 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1846 | else |
| 1847 | DataManager::SetValue("tw_lifetime_writes", 0); |
| 1848 | op_status = 0; // success |
| 1849 | } else { |
| 1850 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1851 | op_status = 1; // fail |
| 1852 | } |
| 1853 | |
| 1854 | operation_end(op_status); |
| 1855 | return 0; |
| 1856 | } |
| 1857 | |
| 1858 | int GUIAction::mountsystemtoggle(std::string arg) |
| 1859 | { |
| 1860 | int op_status = 0; |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1861 | 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] | 1862 | bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor"); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1863 | |
| 1864 | operation_start("Toggle System Mount"); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1865 | if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1866 | op_status = 1; // fail |
| 1867 | } else { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1868 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1869 | if (Part) { |
Ethan Yonker | d6966f4 | 2015-05-30 14:52:16 -0500 | [diff] [blame] | 1870 | if (arg == "0") { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1871 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 1872 | Part->Change_Mount_Read_Only(false); |
| 1873 | } else { |
| 1874 | DataManager::SetValue("tw_mount_system_ro", 1); |
| 1875 | Part->Change_Mount_Read_Only(true); |
| 1876 | } |
| 1877 | if (remount_system) { |
| 1878 | Part->Mount(true); |
| 1879 | } |
| 1880 | op_status = 0; // success |
| 1881 | } else { |
| 1882 | op_status = 1; // fail |
| 1883 | } |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1884 | Part = PartitionManager.Find_Partition_By_Path("/vendor"); |
| 1885 | if (Part) { |
| 1886 | if (arg == "0") { |
| 1887 | Part->Change_Mount_Read_Only(false); |
| 1888 | } else { |
| 1889 | Part->Change_Mount_Read_Only(true); |
| 1890 | } |
| 1891 | if (remount_vendor) { |
| 1892 | Part->Mount(true); |
| 1893 | } |
| 1894 | op_status = 0; // success |
| 1895 | } else { |
| 1896 | op_status = 1; // fail |
| 1897 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | operation_end(op_status); |
| 1901 | return 0; |
| 1902 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1903 | |
| 1904 | int GUIAction::setlanguage(std::string arg __unused) |
| 1905 | { |
| 1906 | int op_status = 0; |
| 1907 | |
| 1908 | operation_start("Set Language"); |
| 1909 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1910 | PageManager::RequestReload(); |
| 1911 | op_status = 0; // success |
| 1912 | |
| 1913 | operation_end(op_status); |
| 1914 | return 0; |
| 1915 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1916 | |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 1917 | int GUIAction::togglebacklight(std::string arg __unused) |
| 1918 | { |
| 1919 | blankTimer.toggleBlank(); |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1923 | int GUIAction::setbootslot(std::string arg) |
| 1924 | { |
| 1925 | operation_start("Set Boot Slot"); |
| 1926 | if (!simulate) |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1927 | PartitionManager.Set_Active_Slot(arg); |
Matt Mower | 029a82d | 2017-01-08 13:12:38 -0600 | [diff] [blame] | 1928 | else |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1929 | simulate_progress_bar(); |
| 1930 | operation_end(0); |
| 1931 | return 0; |
| 1932 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1933 | |
| 1934 | int GUIAction::checkforapp(std::string arg __unused) |
| 1935 | { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1936 | operation_start("Check for TWRP App"); |
| 1937 | if (!simulate) |
| 1938 | { |
| 1939 | string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk"); |
| 1940 | int sdkver = 0; |
| 1941 | if (!sdkverstr.empty()) { |
| 1942 | sdkver = atoi(sdkverstr.c_str()); |
| 1943 | } |
| 1944 | if (sdkver <= 13) { |
| 1945 | if (sdkver == 0) |
| 1946 | LOGINFO("Unable to read sdk version from build prop\n"); |
| 1947 | else |
| 1948 | LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver); |
Ethan Yonker | 64c5c0a | 2017-03-06 12:42:54 -0600 | [diff] [blame] | 1949 | 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] | 1950 | goto exit; |
| 1951 | } |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1952 | if (TWFunc::Is_TWRP_App_In_System()) { |
| 1953 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1954 | goto exit; |
Ethan Yonker | 5e1a7f9 | 2017-01-18 16:44:54 -0600 | [diff] [blame] | 1955 | } |
| 1956 | if (PartitionManager.Mount_By_Path("/data", false)) { |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1957 | const char parent_path[] = "/data/app"; |
| 1958 | const char app_prefix[] = "me.twrp.twrpapp-"; |
| 1959 | DIR *d = opendir(parent_path); |
| 1960 | if (d) { |
| 1961 | struct dirent *p; |
| 1962 | while ((p = readdir(d))) { |
| 1963 | if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix))) |
| 1964 | continue; |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1965 | closedir(d); |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1966 | LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name); |
Ethan Yonker | 64c5c0a | 2017-03-06 12:42:54 -0600 | [diff] [blame] | 1967 | 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] | 1968 | goto exit; |
| 1969 | } |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1970 | closedir(d); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1971 | } |
Ethan Yonker | 64c5c0a | 2017-03-06 12:42:54 -0600 | [diff] [blame] | 1972 | } else { |
| 1973 | LOGINFO("Data partition cannot be mounted during app check\n"); |
| 1974 | 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] | 1975 | } |
| 1976 | } else |
| 1977 | simulate_progress_bar(); |
| 1978 | LOGINFO("App not installed\n"); |
| 1979 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1980 | exit: |
| 1981 | operation_end(0); |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | int GUIAction::installapp(std::string arg __unused) |
| 1986 | { |
| 1987 | int op_status = 1; |
| 1988 | operation_start("Install TWRP App"); |
| 1989 | if (!simulate) |
| 1990 | { |
| 1991 | if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) { |
| 1992 | if (PartitionManager.Mount_By_Path("/data", true)) { |
| 1993 | string install_path = "/data/app"; |
| 1994 | string context = "u:object_r:apk_data_file:s0"; |
| 1995 | if (!TWFunc::Path_Exists(install_path)) { |
| 1996 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 1997 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 1998 | goto exit; |
| 1999 | } |
| 2000 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2001 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2002 | goto exit; |
| 2003 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2004 | 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] | 2005 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2006 | goto exit; |
| 2007 | } |
| 2008 | } |
| 2009 | install_path += "/me.twrp.twrpapp-1"; |
| 2010 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 2011 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 2012 | goto exit; |
| 2013 | } |
| 2014 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2015 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2016 | goto exit; |
| 2017 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2018 | 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] | 2019 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2020 | goto exit; |
| 2021 | } |
| 2022 | install_path += "/base.apk"; |
| 2023 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2024 | LOGERR("Error copying apk file\n"); |
| 2025 | goto exit; |
| 2026 | } |
| 2027 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 2028 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2029 | goto exit; |
| 2030 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2031 | 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] | 2032 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2033 | goto exit; |
| 2034 | } |
| 2035 | sync(); |
| 2036 | sync(); |
| 2037 | } |
| 2038 | } else { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 2039 | if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
| 2040 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 2041 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2042 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 2043 | string install_path = base_path + "/priv-app"; |
| 2044 | string context = "u:object_r:system_file:s0"; |
| 2045 | if (!TWFunc::Path_Exists(install_path)) |
| 2046 | install_path = base_path + "/app"; |
| 2047 | if (TWFunc::Path_Exists(install_path)) { |
| 2048 | install_path += "/twrpapp"; |
| 2049 | LOGINFO("Installing app to '%s'\n", install_path.c_str()); |
| 2050 | 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] | 2051 | 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] | 2052 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2053 | goto exit; |
| 2054 | } |
| 2055 | install_path += "/me.twrp.twrpapp.apk"; |
| 2056 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2057 | LOGERR("Error copying apk file\n"); |
| 2058 | goto exit; |
| 2059 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2060 | 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] | 2061 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2062 | goto exit; |
| 2063 | } |
| 2064 | sync(); |
| 2065 | sync(); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 2066 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2067 | op_status = 0; |
| 2068 | } else { |
| 2069 | LOGERR("Error making app directory '%s': %s\n", strerror(errno)); |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | } else |
| 2075 | simulate_progress_bar(); |
| 2076 | exit: |
| 2077 | operation_end(0); |
| 2078 | return 0; |
| 2079 | } |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2080 | |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 2081 | int GUIAction::uninstalltwrpsystemapp(std::string arg __unused) |
| 2082 | { |
| 2083 | int op_status = 1; |
| 2084 | operation_start("Uninstall TWRP System App"); |
| 2085 | if (!simulate) |
| 2086 | { |
| 2087 | int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro"); |
| 2088 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path()); |
| 2089 | if (!Part) { |
| 2090 | LOGERR("Unabled to find system partition.\n"); |
| 2091 | goto exit; |
| 2092 | } |
| 2093 | if (!Part->UnMount(true)) { |
| 2094 | goto exit; |
| 2095 | } |
| 2096 | if (Mount_System_RO > 0) { |
| 2097 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 2098 | Part->Change_Mount_Read_Only(false); |
| 2099 | } |
| 2100 | if (Part->Mount(true)) { |
| 2101 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 2102 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
| 2103 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 2104 | string uninstall_path = base_path + "/priv-app"; |
| 2105 | if (!TWFunc::Path_Exists(uninstall_path)) |
| 2106 | uninstall_path = base_path + "/app"; |
| 2107 | uninstall_path += "/twrpapp"; |
| 2108 | if (TWFunc::Path_Exists(uninstall_path)) { |
| 2109 | LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str()); |
| 2110 | if (TWFunc::removeDir(uninstall_path, false) == 0) { |
| 2111 | sync(); |
| 2112 | op_status = 0; |
| 2113 | DataManager::SetValue("tw_app_installed_in_system", 0); |
| 2114 | DataManager::SetValue("tw_app_install_status", 0); |
| 2115 | } else { |
| 2116 | LOGERR("Unable to remove TWRP app from system.\n"); |
| 2117 | } |
| 2118 | } else { |
| 2119 | LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str()); |
| 2120 | } |
| 2121 | } |
| 2122 | Part->UnMount(true); |
| 2123 | if (Mount_System_RO > 0) { |
| 2124 | DataManager::SetValue("tw_mount_system_ro", Mount_System_RO); |
| 2125 | Part->Change_Mount_Read_Only(true); |
| 2126 | } |
| 2127 | } else |
| 2128 | simulate_progress_bar(); |
| 2129 | exit: |
| 2130 | operation_end(0); |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 2134 | int GUIAction::repackimage(std::string arg __unused) |
| 2135 | { |
| 2136 | int op_status = 1; |
| 2137 | operation_start("Repack Image"); |
| 2138 | if (!simulate) |
| 2139 | { |
| 2140 | std::string path = DataManager::GetStrValue("tw_filename"); |
| 2141 | Repack_Options_struct Repack_Options; |
| 2142 | Repack_Options.Disable_Verity = false; |
| 2143 | Repack_Options.Disable_Force_Encrypt = false; |
| 2144 | Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0; |
| 2145 | if (DataManager::GetIntValue("tw_repack_kernel") == 1) |
| 2146 | Repack_Options.Type = REPLACE_KERNEL; |
| 2147 | else |
| 2148 | Repack_Options.Type = REPLACE_RAMDISK; |
| 2149 | if (!PartitionManager.Repack_Images(path, Repack_Options)) |
| 2150 | goto exit; |
| 2151 | } else |
| 2152 | simulate_progress_bar(); |
| 2153 | op_status = 0; |
| 2154 | exit: |
| 2155 | operation_end(op_status); |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | int GUIAction::fixabrecoverybootloop(std::string arg __unused) |
| 2160 | { |
| 2161 | int op_status = 1; |
| 2162 | operation_start("Repack Image"); |
| 2163 | if (!simulate) |
| 2164 | { |
| 2165 | if (!TWFunc::Path_Exists("/sbin/magiskboot")) { |
| 2166 | LOGERR("Image repacking tool not present in this TWRP build!"); |
| 2167 | goto exit; |
| 2168 | } |
| 2169 | DataManager::SetProgress(0); |
| 2170 | TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot"); |
| 2171 | if (part) |
| 2172 | gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name)); |
| 2173 | else { |
| 2174 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot")); |
| 2175 | goto exit; |
| 2176 | } |
| 2177 | if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack"))) |
| 2178 | goto exit; |
| 2179 | DataManager::SetProgress(.25); |
| 2180 | gui_msg("fixing_recovery_loop_patch=Patching kernel..."); |
| 2181 | std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300"; |
| 2182 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2183 | gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel.")); |
| 2184 | goto exit; |
| 2185 | } |
| 2186 | std::string header_path = REPACK_ORIG_DIR; |
| 2187 | header_path += "header"; |
| 2188 | if (TWFunc::Path_Exists(header_path)) { |
| 2189 | 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"; |
| 2190 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2191 | gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel.")); |
| 2192 | goto exit; |
| 2193 | } |
| 2194 | } |
| 2195 | DataManager::SetProgress(.5); |
| 2196 | gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name)); |
| 2197 | command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --repack " REPACK_ORIG_DIR "boot.img"; |
| 2198 | if (TWFunc::Exec_Cmd(command) != 0) { |
| 2199 | gui_msg(Msg(msg::kError, "repack_error=Error repacking image.")); |
| 2200 | goto exit; |
| 2201 | } |
| 2202 | DataManager::SetProgress(.75); |
| 2203 | std::string path = REPACK_ORIG_DIR; |
| 2204 | std::string file = "new-boot.img"; |
| 2205 | DataManager::SetValue("tw_flash_partition", "/boot;"); |
| 2206 | if (!PartitionManager.Flash_Image(path, file)) { |
| 2207 | LOGINFO("Error flashing new image\n"); |
| 2208 | goto exit; |
| 2209 | } |
| 2210 | DataManager::SetProgress(1); |
| 2211 | TWFunc::removeDir(REPACK_ORIG_DIR, false); |
| 2212 | } else |
| 2213 | simulate_progress_bar(); |
| 2214 | op_status = 0; |
| 2215 | exit: |
| 2216 | operation_end(op_status); |
| 2217 | return 0; |
| 2218 | } |