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