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