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