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