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