jrior001 | aad0311 | 2014-07-05 10:31:21 -0400 | [diff] [blame] | 1 | /*update |
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> |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 35 | #include <pwd.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 | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 46 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 48 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | #include "../variables.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 50 | #include "../twinstall.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; |
| 65 | static pthread_t terminal_command; |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 66 | pid_t sideload_child_pid; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 67 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 68 | static void *ActionThread_work_wrapper(void *data); |
| 69 | |
| 70 | class ActionThread |
| 71 | { |
| 72 | public: |
| 73 | ActionThread(); |
| 74 | ~ActionThread(); |
| 75 | |
| 76 | void threadActions(GUIAction *act); |
| 77 | void run(void *data); |
| 78 | private: |
| 79 | friend void *ActionThread_work_wrapper(void*); |
| 80 | struct ThreadData |
| 81 | { |
| 82 | ActionThread *this_; |
| 83 | GUIAction *act; |
| 84 | ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {} |
| 85 | }; |
| 86 | |
| 87 | pthread_t m_thread; |
| 88 | bool m_thread_running; |
| 89 | pthread_mutex_t m_act_lock; |
| 90 | }; |
| 91 | |
| 92 | static ActionThread action_thread; // for all kinds of longer running actions |
| 93 | static ActionThread cancel_thread; // for longer running "cancel" actions |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 94 | |
| 95 | static void *ActionThread_work_wrapper(void *data) |
| 96 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 97 | static_cast<ActionThread::ThreadData*>(data)->this_->run(data); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | ActionThread::ActionThread() |
| 102 | { |
| 103 | m_thread_running = false; |
| 104 | pthread_mutex_init(&m_act_lock, NULL); |
| 105 | } |
| 106 | |
| 107 | ActionThread::~ActionThread() |
| 108 | { |
| 109 | pthread_mutex_lock(&m_act_lock); |
| 110 | if(m_thread_running) { |
| 111 | pthread_mutex_unlock(&m_act_lock); |
| 112 | pthread_join(m_thread, NULL); |
| 113 | } else { |
| 114 | pthread_mutex_unlock(&m_act_lock); |
| 115 | } |
| 116 | pthread_mutex_destroy(&m_act_lock); |
| 117 | } |
| 118 | |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 119 | void ActionThread::threadActions(GUIAction *act) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 120 | { |
| 121 | pthread_mutex_lock(&m_act_lock); |
| 122 | if (m_thread_running) { |
| 123 | pthread_mutex_unlock(&m_act_lock); |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 124 | LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n", |
| 125 | act->mActions.size(), act->mActions[0].mFunction.c_str()); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 126 | } else { |
| 127 | m_thread_running = true; |
| 128 | pthread_mutex_unlock(&m_act_lock); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 129 | ThreadData *d = new ThreadData(this, act); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 130 | pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void ActionThread::run(void *data) |
| 135 | { |
| 136 | ThreadData *d = (ThreadData*)data; |
| 137 | GUIAction* act = d->act; |
| 138 | |
| 139 | std::vector<GUIAction::Action>::iterator it; |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 140 | for (it = act->mActions.begin(); it != act->mActions.end(); ++it) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 141 | act->doAction(*it); |
| 142 | |
| 143 | pthread_mutex_lock(&m_act_lock); |
| 144 | m_thread_running = false; |
| 145 | pthread_mutex_unlock(&m_act_lock); |
| 146 | delete d; |
| 147 | } |
| 148 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | GUIAction::GUIAction(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 150 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 151 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | xml_node<>* child; |
| 153 | xml_node<>* actions; |
| 154 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | if (!node) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 158 | if (mf.empty()) { |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 159 | #define ADD_ACTION(n) mf[#n] = &GUIAction::n |
| 160 | #define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 161 | // These actions will be run in the caller's thread |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 162 | ADD_ACTION(reboot); |
| 163 | ADD_ACTION(home); |
| 164 | ADD_ACTION(key); |
| 165 | ADD_ACTION(page); |
| 166 | ADD_ACTION(reload); |
| 167 | ADD_ACTION(readBackup); |
| 168 | ADD_ACTION(set); |
| 169 | ADD_ACTION(clear); |
| 170 | ADD_ACTION(mount); |
| 171 | ADD_ACTION(unmount); |
| 172 | ADD_ACTION_EX("umount", unmount); |
| 173 | ADD_ACTION(restoredefaultsettings); |
| 174 | ADD_ACTION(copylog); |
| 175 | ADD_ACTION(compute); |
| 176 | ADD_ACTION_EX("addsubtract", compute); |
| 177 | ADD_ACTION(setguitimezone); |
| 178 | ADD_ACTION(overlay); |
| 179 | ADD_ACTION(queuezip); |
| 180 | ADD_ACTION(cancelzip); |
| 181 | ADD_ACTION(queueclear); |
| 182 | ADD_ACTION(sleep); |
Matt Mower | 9a2a205 | 2016-05-31 21:31:22 -0500 | [diff] [blame] | 183 | ADD_ACTION(sleepcounter); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 184 | ADD_ACTION(appenddatetobackupname); |
| 185 | ADD_ACTION(generatebackupname); |
| 186 | ADD_ACTION(checkpartitionlist); |
| 187 | ADD_ACTION(getpartitiondetails); |
| 188 | ADD_ACTION(screenshot); |
| 189 | ADD_ACTION(setbrightness); |
| 190 | ADD_ACTION(fileexists); |
| 191 | ADD_ACTION(killterminal); |
| 192 | ADD_ACTION(checkbackupname); |
| 193 | ADD_ACTION(adbsideloadcancel); |
| 194 | ADD_ACTION(fixsu); |
| 195 | ADD_ACTION(startmtp); |
| 196 | ADD_ACTION(stopmtp); |
| 197 | ADD_ACTION(cancelbackup); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 198 | ADD_ACTION(checkpartitionlifetimewrites); |
| 199 | ADD_ACTION(mountsystemtoggle); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 200 | ADD_ACTION(setlanguage); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 201 | ADD_ACTION(checkforapp); |
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(), "+"); |
| 265 | for(size_t i = 0; i < keys.size(); ++i) |
| 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); |
| 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 |
| 310 | if(mKeys.size() == 1) { |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 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 | } |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [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(); |
| 323 | for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) { |
| 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(); |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [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); |
| 519 | if ((int) difftime(Stop, Start) > 10) |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 520 | DataManager::Vibrate("tw_action_vibrate"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 521 | LOGINFO("operation_end - status=%d\n", operation_status); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 522 | } |
| 523 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 524 | int GUIAction::reboot(std::string arg) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 525 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 526 | sync(); |
| 527 | DataManager::SetValue("tw_gui_done", 1); |
| 528 | DataManager::SetValue("tw_reboot_arg", 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 | return 0; |
| 531 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 532 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 533 | int GUIAction::home(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 534 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 535 | gui_changePage("main"); |
| 536 | return 0; |
| 537 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 538 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 539 | int GUIAction::key(std::string arg) |
| 540 | { |
| 541 | const int key = getKeyByName(arg); |
| 542 | PageManager::NotifyKey(key, true); |
| 543 | PageManager::NotifyKey(key, false); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int GUIAction::page(std::string arg) |
| 548 | { |
LuK1337 | 62326f4 | 2015-09-30 19:57:46 +0200 | [diff] [blame] | 549 | property_set("twrp.action_complete", "0"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 550 | std::string page_name = gui_parse_text(arg); |
| 551 | return gui_changePage(page_name); |
| 552 | } |
| 553 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 554 | int GUIAction::reload(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 555 | { |
Ethan Yonker | e0f1f3b | 2015-10-27 09:49:01 -0500 | [diff] [blame] | 556 | PageManager::RequestReload(); |
| 557 | // The actual reload is handled in pages.cpp in PageManager::RunReload() |
| 558 | // The reload will occur on the next Update or Render call and will |
| 559 | // be performed in the rendoer thread instead of the action thread |
| 560 | // to prevent crashing which could occur when we start deleting |
| 561 | // GUI resources in the action thread while we attempt to render |
| 562 | // with those same resources in another thread. |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 563 | return 0; |
| 564 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 565 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 566 | int GUIAction::readBackup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 567 | { |
| 568 | string Restore_Name; |
| 569 | DataManager::GetValue("tw_restore", Restore_Name); |
| 570 | PartitionManager.Set_Restore_Files(Restore_Name); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | int GUIAction::set(std::string arg) |
| 575 | { |
| 576 | if (arg.find('=') != string::npos) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 577 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 578 | string varName = arg.substr(0, arg.find('=')); |
| 579 | string value = arg.substr(arg.find('=') + 1, string::npos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 580 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 581 | DataManager::GetValue(value, value); |
| 582 | DataManager::SetValue(varName, value); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 583 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 584 | else |
| 585 | DataManager::SetValue(arg, "1"); |
| 586 | return 0; |
| 587 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 588 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 589 | int GUIAction::clear(std::string arg) |
| 590 | { |
| 591 | DataManager::SetValue(arg, "0"); |
| 592 | return 0; |
| 593 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 594 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 595 | int GUIAction::mount(std::string arg) |
| 596 | { |
| 597 | if (arg == "usb") { |
| 598 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 599 | if (!simulate) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 600 | PartitionManager.usb_storage_enable(); |
| 601 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 602 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 603 | } else if (!simulate) { |
| 604 | PartitionManager.Mount_By_Path(arg, true); |
| 605 | PartitionManager.Add_MTP_Storage(arg); |
| 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 | return 0; |
| 609 | } |
| 610 | |
| 611 | int GUIAction::unmount(std::string arg) |
| 612 | { |
| 613 | if (arg == "usb") { |
| 614 | if (!simulate) |
| 615 | PartitionManager.usb_storage_disable(); |
| 616 | else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 617 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 618 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 619 | } else if (!simulate) { |
| 620 | PartitionManager.UnMount_By_Path(arg, true); |
| 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 | return 0; |
| 624 | } |
| 625 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 626 | int GUIAction::restoredefaultsettings(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 627 | { |
| 628 | operation_start("Restore Defaults"); |
| 629 | 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] | 630 | gui_msg("simulating=Simulating actions..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 631 | else { |
| 632 | DataManager::ResetDefaults(); |
| 633 | PartitionManager.Update_System_Details(); |
| 634 | PartitionManager.Mount_Current_Storage(true); |
| 635 | } |
| 636 | operation_end(0); |
| 637 | return 0; |
| 638 | } |
| 639 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 640 | int GUIAction::copylog(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 641 | { |
| 642 | operation_start("Copy Log"); |
| 643 | if (!simulate) |
| 644 | { |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 645 | string dst, curr_storage; |
| 646 | int copy_kernel_log = 0; |
| 647 | |
| 648 | DataManager::GetValue("tw_include_kernel_log", copy_kernel_log); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 649 | PartitionManager.Mount_Current_Storage(true); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 650 | curr_storage = DataManager::GetCurrentStoragePath(); |
| 651 | dst = curr_storage + "/recovery.log"; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 652 | TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); |
| 653 | tw_set_default_metadata(dst.c_str()); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 654 | if (copy_kernel_log) |
| 655 | TWFunc::copy_kernel_log(curr_storage); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 656 | sync(); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 657 | gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 658 | } else |
| 659 | simulate_progress_bar(); |
| 660 | operation_end(0); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | int GUIAction::compute(std::string arg) |
| 666 | { |
| 667 | if (arg.find("+") != string::npos) |
| 668 | { |
| 669 | string varName = arg.substr(0, arg.find('+')); |
| 670 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 671 | int amount_to_add = atoi(string_to_add.c_str()); |
| 672 | int value; |
| 673 | |
| 674 | DataManager::GetValue(varName, value); |
| 675 | DataManager::SetValue(varName, value + amount_to_add); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 676 | return 0; |
| 677 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 678 | if (arg.find("-") != string::npos) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 679 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 680 | string varName = arg.substr(0, arg.find('-')); |
| 681 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 682 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 683 | int value; |
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 | DataManager::GetValue(varName, value); |
| 686 | value -= amount_to_subtract; |
| 687 | if (value <= 0) |
| 688 | value = 0; |
| 689 | DataManager::SetValue(varName, value); |
| 690 | return 0; |
| 691 | } |
| 692 | if (arg.find("*") != string::npos) |
| 693 | { |
| 694 | string varName = arg.substr(0, arg.find('*')); |
| 695 | string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos)); |
| 696 | int multiply_by = atoi(multiply_by_str.c_str()); |
| 697 | int value; |
| 698 | |
| 699 | DataManager::GetValue(varName, value); |
| 700 | DataManager::SetValue(varName, value*multiply_by); |
| 701 | return 0; |
| 702 | } |
| 703 | if (arg.find("/") != string::npos) |
| 704 | { |
| 705 | string varName = arg.substr(0, arg.find('/')); |
| 706 | string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos)); |
| 707 | int divide_by = atoi(divide_by_str.c_str()); |
| 708 | int value; |
| 709 | |
| 710 | if(divide_by != 0) |
| 711 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 712 | DataManager::GetValue(varName, value); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 713 | DataManager::SetValue(varName, value/divide_by); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 714 | } |
| 715 | return 0; |
| 716 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 717 | LOGERR("Unable to perform compute '%s'\n", arg.c_str()); |
| 718 | return -1; |
| 719 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 720 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 721 | int GUIAction::setguitimezone(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 722 | { |
| 723 | string SelectedZone; |
| 724 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 725 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 726 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 727 | |
| 728 | int dst; |
| 729 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 730 | |
| 731 | string offset; |
| 732 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 733 | |
| 734 | string NewTimeZone = Zone; |
| 735 | if (offset != "0") |
| 736 | NewTimeZone += ":" + offset; |
| 737 | |
| 738 | if (dst != 0) |
| 739 | NewTimeZone += DSTZone; |
| 740 | |
| 741 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
| 742 | DataManager::update_tz_environment_variables(); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | int GUIAction::overlay(std::string arg) |
| 747 | { |
| 748 | return gui_changeOverlay(arg); |
| 749 | } |
| 750 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 751 | int GUIAction::queuezip(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 752 | { |
| 753 | if (zip_queue_index >= 10) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 754 | gui_msg("max_queue=Maximum zip queue reached!"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 755 | return 0; |
| 756 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 757 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 758 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 759 | zip_queue_index++; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 760 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 761 | } |
| 762 | return 0; |
| 763 | } |
| 764 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 765 | int GUIAction::cancelzip(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 766 | { |
| 767 | if (zip_queue_index <= 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 768 | gui_msg("min_queue=Minimum zip queue reached!"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 769 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 770 | } else { |
| 771 | zip_queue_index--; |
| 772 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 773 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 774 | return 0; |
| 775 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 776 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 777 | int GUIAction::queueclear(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 778 | { |
| 779 | zip_queue_index = 0; |
| 780 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 781 | return 0; |
| 782 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 783 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 784 | int GUIAction::sleep(std::string arg) |
| 785 | { |
| 786 | operation_start("Sleep"); |
| 787 | usleep(atoi(arg.c_str())); |
| 788 | operation_end(0); |
| 789 | return 0; |
| 790 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 791 | |
Matt Mower | 9a2a205 | 2016-05-31 21:31:22 -0500 | [diff] [blame] | 792 | int GUIAction::sleepcounter(std::string arg) |
| 793 | { |
| 794 | operation_start("SleepCounter"); |
| 795 | // Ensure user notices countdown in case it needs to be cancelled |
| 796 | blankTimer.resetTimerAndUnblank(); |
| 797 | int total = atoi(arg.c_str()); |
| 798 | for (int t = total; t > 0; t--) { |
| 799 | int progress = (int)(((float)(total-t)/(float)total)*100.0); |
| 800 | DataManager::SetValue("ui_progress", progress); |
| 801 | ::sleep(1); |
| 802 | DataManager::SetValue("tw_sleep", t-1); |
| 803 | } |
| 804 | DataManager::SetValue("ui_progress", 100); |
| 805 | operation_end(0); |
| 806 | return 0; |
| 807 | } |
| 808 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 809 | int GUIAction::appenddatetobackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 810 | { |
| 811 | operation_start("AppendDateToBackupName"); |
| 812 | string Backup_Name; |
| 813 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 814 | Backup_Name += TWFunc::Get_Current_Date(); |
| 815 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 816 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 817 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Matt Mower | 76b8afc | 2016-06-24 12:04:27 -0500 | [diff] [blame] | 818 | PageManager::NotifyKey(KEY_END, true); |
| 819 | PageManager::NotifyKey(KEY_END, false); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 820 | operation_end(0); |
| 821 | return 0; |
| 822 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 823 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 824 | int GUIAction::generatebackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 825 | { |
| 826 | operation_start("GenerateBackupName"); |
| 827 | TWFunc::Auto_Generate_Backup_Name(); |
| 828 | operation_end(0); |
| 829 | return 0; |
| 830 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 831 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 832 | int GUIAction::checkpartitionlist(std::string arg) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 833 | { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 834 | string List, part_path; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 835 | int count = 0; |
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 | if (arg.empty()) |
| 838 | arg = "tw_wipe_list"; |
| 839 | DataManager::GetValue(arg, List); |
| 840 | LOGINFO("checkpartitionlist list '%s'\n", List.c_str()); |
| 841 | if (!List.empty()) { |
| 842 | size_t start_pos = 0, end_pos = List.find(";", start_pos); |
| 843 | while (end_pos != string::npos && start_pos < List.size()) { |
| 844 | part_path = List.substr(start_pos, end_pos - start_pos); |
| 845 | LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str()); |
| 846 | if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 847 | // Do nothing |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 848 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 849 | count++; |
| 850 | } |
| 851 | start_pos = end_pos + 1; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 852 | end_pos = List.find(";", start_pos); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 853 | } |
| 854 | DataManager::SetValue("tw_check_partition_list", count); |
| 855 | } else { |
| 856 | DataManager::SetValue("tw_check_partition_list", 0); |
| 857 | } |
| 858 | return 0; |
| 859 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 860 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 861 | int GUIAction::getpartitiondetails(std::string arg) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 862 | { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 863 | string List, part_path; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 864 | int count = 0; |
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 | if (arg.empty()) |
| 867 | arg = "tw_wipe_list"; |
| 868 | DataManager::GetValue(arg, List); |
| 869 | LOGINFO("getpartitiondetails list '%s'\n", List.c_str()); |
| 870 | if (!List.empty()) { |
| 871 | size_t start_pos = 0, end_pos = List.find(";", start_pos); |
| 872 | part_path = List; |
| 873 | while (end_pos != string::npos && start_pos < List.size()) { |
| 874 | part_path = List.substr(start_pos, end_pos - start_pos); |
| 875 | LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str()); |
| 876 | if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 877 | // Do nothing |
| 878 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 879 | DataManager::SetValue("tw_partition_path", part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | start_pos = end_pos + 1; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 883 | end_pos = List.find(";", start_pos); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 884 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 885 | if (!part_path.empty()) { |
| 886 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 887 | if (Part) { |
| 888 | unsigned long long mb = 1048576; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 889 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 890 | DataManager::SetValue("tw_partition_name", Part->Display_Name); |
| 891 | DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point); |
| 892 | DataManager::SetValue("tw_partition_file_system", Part->Current_File_System); |
| 893 | DataManager::SetValue("tw_partition_size", Part->Size / mb); |
| 894 | DataManager::SetValue("tw_partition_used", Part->Used / mb); |
| 895 | DataManager::SetValue("tw_partition_free", Part->Free / mb); |
| 896 | DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb); |
| 897 | DataManager::SetValue("tw_partition_removable", Part->Removable); |
| 898 | DataManager::SetValue("tw_partition_is_present", Part->Is_Present); |
| 899 | |
| 900 | if (Part->Can_Repair()) |
| 901 | DataManager::SetValue("tw_partition_can_repair", 1); |
| 902 | else |
| 903 | DataManager::SetValue("tw_partition_can_repair", 0); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 904 | if (Part->Can_Resize()) |
| 905 | DataManager::SetValue("tw_partition_can_resize", 1); |
| 906 | else |
| 907 | DataManager::SetValue("tw_partition_can_resize", 0); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 908 | if (TWFunc::Path_Exists("/sbin/mkfs.fat")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 909 | DataManager::SetValue("tw_partition_vfat", 1); |
| 910 | else |
| 911 | DataManager::SetValue("tw_partition_vfat", 0); |
Matt Mower | 80f7b36 | 2015-12-12 15:38:01 -0600 | [diff] [blame] | 912 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 913 | DataManager::SetValue("tw_partition_exfat", 1); |
| 914 | else |
| 915 | DataManager::SetValue("tw_partition_exfat", 0); |
| 916 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) |
| 917 | DataManager::SetValue("tw_partition_f2fs", 1); |
| 918 | else |
| 919 | DataManager::SetValue("tw_partition_f2fs", 0); |
| 920 | if (TWFunc::Path_Exists("/sbin/mke2fs")) |
| 921 | DataManager::SetValue("tw_partition_ext", 1); |
| 922 | else |
| 923 | DataManager::SetValue("tw_partition_ext", 0); |
| 924 | return 0; |
| 925 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 926 | LOGERR("Unable to locate partition: '%s'\n", part_path.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | } |
| 930 | DataManager::SetValue("tw_partition_name", ""); |
| 931 | DataManager::SetValue("tw_partition_file_system", ""); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 932 | // Set this to 0 to prevent trying to partition this device, just in case |
| 933 | DataManager::SetValue("tw_partition_removable", 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 937 | int GUIAction::screenshot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 938 | { |
| 939 | time_t tm; |
| 940 | char path[256]; |
| 941 | int path_len; |
| 942 | uid_t uid = -1; |
| 943 | gid_t gid = -1; |
| 944 | |
| 945 | struct passwd *pwd = getpwnam("media_rw"); |
| 946 | if(pwd) { |
| 947 | uid = pwd->pw_uid; |
| 948 | gid = pwd->pw_gid; |
| 949 | } |
| 950 | |
| 951 | const std::string storage = DataManager::GetCurrentStoragePath(); |
| 952 | if(PartitionManager.Is_Mounted_By_Path(storage)) { |
| 953 | snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str()); |
| 954 | } else { |
| 955 | strcpy(path, "/tmp/"); |
| 956 | } |
| 957 | |
Xuefer | 579e907 | 2016-01-22 13:35:50 +0800 | [diff] [blame] | 958 | if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid)) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 959 | return 0; |
| 960 | |
| 961 | tm = time(NULL); |
| 962 | path_len = strlen(path); |
| 963 | |
| 964 | // Screenshot_2014-01-01-18-21-38.png |
| 965 | strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm)); |
| 966 | |
| 967 | int res = gr_save_screenshot(path); |
| 968 | if(res == 0) { |
| 969 | chmod(path, 0666); |
| 970 | chown(path, uid, gid); |
| 971 | |
that | 677b13f | 2015-12-26 23:57:31 +0100 | [diff] [blame] | 972 | gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 973 | |
| 974 | // blink to notify that the screenshow was taken |
| 975 | gr_color(255, 255, 255, 255); |
| 976 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 977 | gr_flip(); |
| 978 | gui_forceRender(); |
| 979 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 980 | gui_err("screenshot_err=Failed to take a screenshot!"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | int GUIAction::setbrightness(std::string arg) |
| 986 | { |
| 987 | return TWFunc::Set_Brightness(arg); |
| 988 | } |
| 989 | |
| 990 | int GUIAction::fileexists(std::string arg) |
| 991 | { |
| 992 | struct stat st; |
| 993 | string newpath = arg + "/."; |
| 994 | |
| 995 | operation_start("FileExists"); |
| 996 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 997 | operation_end(0); |
| 998 | else |
| 999 | operation_end(1); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1003 | void GUIAction::reinject_after_flash() |
| 1004 | { |
| 1005 | 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] | 1006 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1007 | if (simulate) { |
| 1008 | simulate_progress_bar(); |
| 1009 | } else { |
| 1010 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1011 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
| 1012 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 1013 | else { |
| 1014 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
| 1015 | TWFunc::Exec_Cmd(injectcmd); |
| 1016 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1017 | gui_msg("done=Done."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1022 | int GUIAction::flash(std::string arg) |
| 1023 | { |
| 1024 | int i, ret_val = 0, wipe_cache = 0; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1025 | // We're going to jump to this page first, like a loading page |
| 1026 | gui_changePage(arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1027 | for (i=0; i<zip_queue_index; i++) { |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1028 | string zip_path = zip_queue[i]; |
| 1029 | size_t slashpos = zip_path.find_last_of('/'); |
| 1030 | string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1031 | operation_start("Flashing"); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1032 | DataManager::SetValue("tw_filename", zip_path); |
| 1033 | DataManager::SetValue("tw_file", zip_filename); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1034 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 1035 | |
| 1036 | TWFunc::SetPerformanceMode(true); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1037 | ret_val = flash_zip(zip_path, &wipe_cache); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1038 | TWFunc::SetPerformanceMode(false); |
| 1039 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1040 | 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] | 1041 | ret_val = 1; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1042 | break; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | zip_queue_index = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1046 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1047 | if (wipe_cache) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1048 | 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] | 1049 | PartitionManager.Wipe_By_Path("/cache"); |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1050 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1051 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1052 | reinject_after_flash(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1053 | PartitionManager.Update_System_Details(); |
| 1054 | operation_end(ret_val); |
bigbiff | a869fc7 | 2016-03-01 19:40:36 -0500 | [diff] [blame] | 1055 | // 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] | 1056 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | int GUIAction::wipe(std::string arg) |
| 1061 | { |
| 1062 | operation_start("Format"); |
| 1063 | DataManager::SetValue("tw_partition", arg); |
| 1064 | |
| 1065 | int ret_val = false; |
| 1066 | |
| 1067 | if (simulate) { |
| 1068 | simulate_progress_bar(); |
| 1069 | } else { |
| 1070 | if (arg == "data") |
| 1071 | ret_val = PartitionManager.Factory_Reset(); |
| 1072 | else if (arg == "battery") |
| 1073 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
| 1074 | else if (arg == "rotate") |
| 1075 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
| 1076 | else if (arg == "dalvik") |
| 1077 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
| 1078 | else if (arg == "DATAMEDIA") { |
| 1079 | ret_val = PartitionManager.Format_Data(); |
| 1080 | } else if (arg == "INTERNAL") { |
| 1081 | int has_datamedia, dual_storage; |
| 1082 | |
| 1083 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1084 | if (has_datamedia) { |
| 1085 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
| 1086 | } else { |
| 1087 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
| 1088 | } |
| 1089 | } else if (arg == "EXTERNAL") { |
| 1090 | string External_Path; |
| 1091 | |
| 1092 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 1093 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
| 1094 | } else if (arg == "ANDROIDSECURE") { |
| 1095 | ret_val = PartitionManager.Wipe_Android_Secure(); |
| 1096 | } else if (arg == "LIST") { |
| 1097 | string Wipe_List, wipe_path; |
| 1098 | bool skip = false; |
| 1099 | ret_val = true; |
| 1100 | TWPartition* wipe_part = NULL; |
| 1101 | |
| 1102 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 1103 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
| 1104 | if (!Wipe_List.empty()) { |
| 1105 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 1106 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 1107 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 1108 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
| 1109 | if (wipe_path == "/and-sec") { |
| 1110 | if (!PartitionManager.Wipe_Android_Secure()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1111 | gui_msg("and_sec_wipe_err=Unable to wipe android secure"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1112 | ret_val = false; |
| 1113 | break; |
| 1114 | } else { |
| 1115 | skip = true; |
| 1116 | } |
| 1117 | } else if (wipe_path == "DALVIK") { |
| 1118 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1119 | gui_err("dalvik_wipe_err=Failed to wipe dalvik"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1120 | ret_val = false; |
| 1121 | break; |
| 1122 | } else { |
| 1123 | skip = true; |
| 1124 | } |
| 1125 | } else if (wipe_path == "INTERNAL") { |
| 1126 | if (!PartitionManager.Wipe_Media_From_Data()) { |
| 1127 | ret_val = false; |
| 1128 | break; |
| 1129 | } else { |
| 1130 | skip = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1131 | } |
| 1132 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1133 | if (!skip) { |
| 1134 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1135 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1136 | ret_val = false; |
| 1137 | break; |
| 1138 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 1139 | arg = wipe_path; |
| 1140 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1141 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1142 | skip = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1143 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1144 | start_pos = end_pos + 1; |
| 1145 | end_pos = Wipe_List.find(";", start_pos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1146 | } |
| 1147 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1148 | } else |
| 1149 | ret_val = PartitionManager.Wipe_By_Path(arg); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1150 | #ifndef TW_OEM_BUILD |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1151 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 1152 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 1153 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1154 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1155 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 1156 | LOGINFO("Making TWRP folder and saving settings.\n"); |
| 1157 | Storage_Path += "/TWRP"; |
| 1158 | mkdir(Storage_Path.c_str(), 0777); |
| 1159 | DataManager::Flush(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1160 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1161 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
| 1162 | } |
| 1163 | } |
| 1164 | #endif |
| 1165 | } |
| 1166 | PartitionManager.Update_System_Details(); |
| 1167 | if (ret_val) |
| 1168 | ret_val = 0; // 0 is success |
| 1169 | else |
| 1170 | ret_val = 1; // 1 is failure |
| 1171 | operation_end(ret_val); |
| 1172 | return 0; |
| 1173 | } |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 1174 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1175 | int GUIAction::refreshsizes(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1176 | { |
| 1177 | operation_start("Refreshing Sizes"); |
| 1178 | if (simulate) { |
| 1179 | simulate_progress_bar(); |
| 1180 | } else |
| 1181 | PartitionManager.Update_System_Details(); |
| 1182 | operation_end(0); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | int GUIAction::nandroid(std::string arg) |
| 1187 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1188 | if (simulate) { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1189 | PartitionManager.stop_backup.set_value(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1190 | DataManager::SetValue("tw_partition", "Simulation"); |
| 1191 | simulate_progress_bar(); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1192 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1193 | } else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1194 | operation_start("Nandroid"); |
| 1195 | int ret = 0; |
| 1196 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1197 | if (arg == "backup") { |
| 1198 | string Backup_Name; |
| 1199 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 1200 | string auto_gen = gui_lookup("auto_generate", "(Auto Generate)"); |
| 1201 | 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] | 1202 | ret = PartitionManager.Run_Backup(false); |
| 1203 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1204 | operation_end(1); |
| 1205 | return -1; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1206 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1207 | DataManager::SetValue(TW_BACKUP_NAME, auto_gen); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1208 | } else if (arg == "restore") { |
| 1209 | string Restore_Name; |
| 1210 | DataManager::GetValue("tw_restore", Restore_Name); |
| 1211 | ret = PartitionManager.Run_Restore(Restore_Name); |
| 1212 | } else { |
| 1213 | operation_end(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1214 | return -1; |
| 1215 | } |
| 1216 | DataManager::SetValue("tw_encrypt_backup", 0); |
| 1217 | if (!PartitionManager.stop_backup.get_value()) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1218 | if (ret == false) |
| 1219 | ret = 1; // 1 for failure |
| 1220 | else |
| 1221 | ret = 0; // 0 for success |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1222 | DataManager::SetValue("tw_cancel_backup", 0); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1223 | } |
| 1224 | else { |
| 1225 | DataManager::SetValue("tw_cancel_backup", 1); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1226 | gui_msg("backup_cancel=Backup Cancelled"); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1227 | ret = 0; |
| 1228 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1229 | operation_end(ret); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1230 | return ret; |
| 1231 | } |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1235 | int GUIAction::cancelbackup(std::string arg __unused) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1236 | if (simulate) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1237 | PartitionManager.stop_backup.set_value(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1238 | } |
| 1239 | else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1240 | int op_status = PartitionManager.Cancel_Backup(); |
| 1241 | if (op_status != 0) |
| 1242 | op_status = 1; // failure |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1246 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1247 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1248 | int GUIAction::fixcontexts(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1249 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1250 | int op_status = 0; |
| 1251 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1252 | operation_start("Fix Contexts"); |
| 1253 | LOGINFO("fix contexts started!\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1254 | if (simulate) { |
| 1255 | simulate_progress_bar(); |
| 1256 | } else { |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1257 | op_status = PartitionManager.Fix_Contexts(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1258 | if (op_status != 0) |
| 1259 | op_status = 1; // failure |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1260 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1261 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1265 | int GUIAction::fixpermissions(std::string arg) |
| 1266 | { |
| 1267 | return fixcontexts(arg); |
| 1268 | } |
| 1269 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1270 | int GUIAction::dd(std::string arg) |
| 1271 | { |
| 1272 | operation_start("imaging"); |
| 1273 | |
| 1274 | if (simulate) { |
| 1275 | simulate_progress_bar(); |
| 1276 | } else { |
| 1277 | string cmd = "dd " + arg; |
| 1278 | TWFunc::Exec_Cmd(cmd); |
| 1279 | } |
| 1280 | operation_end(0); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1284 | int GUIAction::partitionsd(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1285 | { |
| 1286 | operation_start("Partition SD Card"); |
| 1287 | int ret_val = 0; |
| 1288 | |
| 1289 | if (simulate) { |
| 1290 | simulate_progress_bar(); |
| 1291 | } else { |
| 1292 | int allow_partition; |
| 1293 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 1294 | if (allow_partition == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1295 | 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] | 1296 | } else { |
| 1297 | if (!PartitionManager.Partition_SDCard()) |
| 1298 | ret_val = 1; // failed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1299 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1300 | } |
| 1301 | operation_end(ret_val); |
| 1302 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1303 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1304 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1305 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1306 | int GUIAction::installhtcdumlock(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1307 | { |
| 1308 | operation_start("Install HTC Dumlock"); |
| 1309 | if (simulate) { |
| 1310 | simulate_progress_bar(); |
| 1311 | } else |
| 1312 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1313 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1314 | operation_end(0); |
| 1315 | return 0; |
| 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::htcdumlockrestoreboot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1319 | { |
| 1320 | operation_start("HTC Dumlock Restore Boot"); |
| 1321 | if (simulate) { |
| 1322 | simulate_progress_bar(); |
| 1323 | } else |
| 1324 | TWFunc::htc_dumlock_restore_original_boot(); |
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::htcdumlockreflashrecovery(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1331 | { |
| 1332 | operation_start("HTC Dumlock Reflash Recovery"); |
| 1333 | if (simulate) { |
| 1334 | simulate_progress_bar(); |
| 1335 | } else |
| 1336 | TWFunc::htc_dumlock_reflash_recovery_to_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 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1342 | int GUIAction::cmd(std::string arg) |
| 1343 | { |
| 1344 | int op_status = 0; |
| 1345 | |
| 1346 | operation_start("Command"); |
| 1347 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
| 1348 | if (simulate) { |
| 1349 | simulate_progress_bar(); |
| 1350 | } else { |
| 1351 | op_status = TWFunc::Exec_Cmd(arg); |
| 1352 | if (op_status != 0) |
| 1353 | op_status = 1; |
| 1354 | } |
| 1355 | |
| 1356 | operation_end(op_status); |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | int GUIAction::terminalcommand(std::string arg) |
| 1361 | { |
| 1362 | int op_status = 0; |
| 1363 | string cmdpath, command; |
| 1364 | |
| 1365 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1366 | operation_start("CommandOutput"); |
| 1367 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 1368 | if (simulate) { |
| 1369 | simulate_progress_bar(); |
| 1370 | operation_end(op_status); |
Matt Mower | 5aa29ab | 2015-02-25 23:50:55 -0600 | [diff] [blame] | 1371 | } else if (arg == "exit") { |
| 1372 | LOGINFO("Exiting terminal\n"); |
| 1373 | operation_end(op_status); |
| 1374 | page("main"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1375 | } else { |
| 1376 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
| 1377 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1378 | DataManager::SetValue("tw_terminal_state", 1); |
| 1379 | DataManager::SetValue("tw_background_thread_running", 1); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1380 | FILE* fp; |
| 1381 | char line[512]; |
| 1382 | |
| 1383 | fp = popen(command.c_str(), "r"); |
| 1384 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1385 | LOGERR("Error opening command to run (%s).\n", strerror(errno)); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1386 | } else { |
| 1387 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0; |
| 1388 | struct timeval timeout; |
| 1389 | fd_set fdset; |
| 1390 | |
| 1391 | while(keep_going) |
| 1392 | { |
| 1393 | FD_ZERO(&fdset); |
| 1394 | FD_SET(fd, &fdset); |
| 1395 | timeout.tv_sec = 0; |
| 1396 | timeout.tv_usec = 400000; |
| 1397 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1398 | if (has_data == 0) { |
| 1399 | // Timeout reached |
| 1400 | DataManager::GetValue("tw_terminal_state", check); |
| 1401 | if (check == 0) { |
| 1402 | keep_going = 0; |
| 1403 | } |
| 1404 | } else if (has_data < 0) { |
| 1405 | // End of execution |
| 1406 | keep_going = 0; |
| 1407 | } else { |
| 1408 | // Try to read output |
xiaolu | e738da5 | 2015-02-22 20:49:35 +0800 | [diff] [blame] | 1409 | if(fgets(line, sizeof(line), fp) != NULL) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1410 | gui_print("%s", line); // Display output |
| 1411 | else |
| 1412 | keep_going = 0; // Done executing |
| 1413 | } |
| 1414 | } |
| 1415 | fclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1416 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1417 | DataManager::SetValue("tw_operation_status", 0); |
| 1418 | DataManager::SetValue("tw_operation_state", 1); |
| 1419 | DataManager::SetValue("tw_terminal_state", 0); |
| 1420 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1421 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1422 | } |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1426 | int GUIAction::killterminal(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1427 | { |
| 1428 | int op_status = 0; |
| 1429 | |
| 1430 | LOGINFO("Sending kill command...\n"); |
| 1431 | operation_start("KillCommand"); |
| 1432 | DataManager::SetValue("tw_operation_status", 0); |
| 1433 | DataManager::SetValue("tw_operation_state", 1); |
| 1434 | DataManager::SetValue("tw_terminal_state", 0); |
| 1435 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1436 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1440 | int GUIAction::reinjecttwrp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1441 | { |
| 1442 | int op_status = 0; |
| 1443 | operation_start("ReinjectTWRP"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1444 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1445 | if (simulate) { |
| 1446 | simulate_progress_bar(); |
| 1447 | } else { |
| 1448 | 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] | 1449 | gui_msg("done=Done."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | operation_end(op_status); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1456 | int GUIAction::checkbackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1457 | { |
| 1458 | int op_status = 0; |
| 1459 | |
| 1460 | operation_start("CheckBackupName"); |
| 1461 | if (simulate) { |
| 1462 | simulate_progress_bar(); |
| 1463 | } else { |
| 1464 | op_status = PartitionManager.Check_Backup_Name(true); |
| 1465 | if (op_status != 0) |
| 1466 | op_status = 1; |
| 1467 | } |
| 1468 | |
| 1469 | operation_end(op_status); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1473 | int GUIAction::decrypt(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1474 | { |
| 1475 | int op_status = 0; |
| 1476 | |
| 1477 | operation_start("Decrypt"); |
| 1478 | if (simulate) { |
| 1479 | simulate_progress_bar(); |
| 1480 | } else { |
| 1481 | string Password; |
| 1482 | DataManager::GetValue("tw_crypto_password", Password); |
| 1483 | op_status = PartitionManager.Decrypt_Device(Password); |
| 1484 | if (op_status != 0) |
| 1485 | op_status = 1; |
| 1486 | else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1487 | |
| 1488 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1489 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1490 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1491 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1492 | // Check for a custom theme and load it if exists |
| 1493 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1494 | if (has_datamedia != 0) { |
| 1495 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 1496 | 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] | 1497 | } else { |
| 1498 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1499 | } |
| 1500 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1501 | PartitionManager.Decrypt_Adopted(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | operation_end(op_status); |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1509 | int GUIAction::adbsideload(std::string arg __unused) |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1510 | { |
| 1511 | operation_start("Sideload"); |
| 1512 | if (simulate) { |
| 1513 | simulate_progress_bar(); |
| 1514 | operation_end(0); |
| 1515 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1516 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1517 | bool mtp_was_enabled = TWFunc::Toggle_MTP(false); |
| 1518 | |
| 1519 | // wait for the adb connection |
| 1520 | int ret = apply_from_adb("/", &sideload_child_pid); |
| 1521 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
| 1522 | |
| 1523 | if (ret != 0) { |
| 1524 | if (ret == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1525 | 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] | 1526 | ret = 1; // failure |
| 1527 | } else { |
| 1528 | int wipe_cache = 0; |
| 1529 | int wipe_dalvik = 0; |
| 1530 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
| 1531 | |
| 1532 | if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 1533 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1534 | PartitionManager.Wipe_By_Path("/cache"); |
| 1535 | if (wipe_dalvik) |
| 1536 | PartitionManager.Wipe_Dalvik_Cache(); |
| 1537 | } else { |
| 1538 | ret = 1; // failure |
| 1539 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1540 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1541 | if (sideload_child_pid) { |
| 1542 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1543 | struct stat st; |
| 1544 | // Calling stat() on this magic filename signals the minadbd |
| 1545 | // subprocess to shut down. |
| 1546 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1547 | int status; |
| 1548 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1549 | waitpid(sideload_child_pid, &status, 0); |
| 1550 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 1551 | property_set("ctl.start", "adbd"); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1552 | TWFunc::Toggle_MTP(mtp_was_enabled); |
| 1553 | reinject_after_flash(); |
| 1554 | operation_end(ret); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1555 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1559 | int GUIAction::adbsideloadcancel(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1560 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1561 | struct stat st; |
| 1562 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1563 | gui_msg("cancel_sideload=Cancelling ADB sideload..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1564 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1565 | // Calling stat() on this magic filename signals the minadbd |
| 1566 | // subprocess to shut down. |
| 1567 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1568 | if (!sideload_child_pid) { |
| 1569 | LOGERR("Unable to get child ID\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1572 | ::sleep(1); |
| 1573 | LOGINFO("Killing child sideload process.\n"); |
| 1574 | kill(sideload_child_pid, SIGTERM); |
| 1575 | int status; |
| 1576 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1577 | waitpid(sideload_child_pid, &status, 0); |
| 1578 | sideload_child_pid = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1579 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1583 | int GUIAction::openrecoveryscript(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1584 | { |
| 1585 | operation_start("OpenRecoveryScript"); |
| 1586 | if (simulate) { |
| 1587 | simulate_progress_bar(); |
Ethan Yonker | 24e7eb7 | 2015-01-03 16:13:06 -0600 | [diff] [blame] | 1588 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1589 | } else { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1590 | int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action(); |
Ethan Yonker | e1abe61 | 2015-06-09 11:09:32 -0500 | [diff] [blame] | 1591 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1592 | } |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1596 | int GUIAction::installsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1597 | { |
| 1598 | int op_status = 0; |
| 1599 | |
| 1600 | operation_start("Install SuperSU"); |
| 1601 | if (simulate) { |
| 1602 | simulate_progress_bar(); |
| 1603 | } else { |
| 1604 | if (!TWFunc::Install_SuperSU()) |
| 1605 | op_status = 1; |
| 1606 | } |
| 1607 | |
| 1608 | operation_end(op_status); |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1612 | int GUIAction::fixsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1613 | { |
| 1614 | int op_status = 0; |
| 1615 | |
| 1616 | operation_start("Fixing Superuser Permissions"); |
| 1617 | if (simulate) { |
| 1618 | simulate_progress_bar(); |
| 1619 | } else { |
| 1620 | LOGERR("Fixing su permissions was deprecated from TWRP.\n"); |
| 1621 | LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n"); |
| 1622 | } |
| 1623 | |
| 1624 | operation_end(op_status); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1628 | int GUIAction::decrypt_backup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1629 | { |
| 1630 | int op_status = 0; |
| 1631 | |
| 1632 | operation_start("Try Restore Decrypt"); |
| 1633 | if (simulate) { |
| 1634 | simulate_progress_bar(); |
| 1635 | } else { |
| 1636 | string Restore_Path, Filename, Password; |
| 1637 | DataManager::GetValue("tw_restore", Restore_Path); |
| 1638 | Restore_Path += "/"; |
| 1639 | DataManager::GetValue("tw_restore_password", Password); |
| 1640 | TWFunc::SetPerformanceMode(true); |
| 1641 | if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password)) |
| 1642 | op_status = 0; // success |
| 1643 | else |
| 1644 | op_status = 1; // fail |
| 1645 | TWFunc::SetPerformanceMode(false); |
| 1646 | } |
| 1647 | |
| 1648 | operation_end(op_status); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1652 | int GUIAction::repair(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1653 | { |
| 1654 | int op_status = 0; |
| 1655 | |
| 1656 | operation_start("Repair Partition"); |
| 1657 | if (simulate) { |
| 1658 | simulate_progress_bar(); |
| 1659 | } else { |
| 1660 | string part_path; |
| 1661 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1662 | if (PartitionManager.Repair_By_Path(part_path, true)) { |
| 1663 | op_status = 0; // success |
| 1664 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1665 | op_status = 1; // fail |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | operation_end(op_status); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1673 | int GUIAction::resize(std::string arg __unused) |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1674 | { |
| 1675 | int op_status = 0; |
| 1676 | |
| 1677 | operation_start("Resize Partition"); |
| 1678 | if (simulate) { |
| 1679 | simulate_progress_bar(); |
| 1680 | } else { |
| 1681 | string part_path; |
| 1682 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1683 | if (PartitionManager.Resize_By_Path(part_path, true)) { |
| 1684 | op_status = 0; // success |
| 1685 | } else { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1686 | op_status = 1; // fail |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | operation_end(op_status); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1694 | int GUIAction::changefilesystem(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1695 | { |
| 1696 | int op_status = 0; |
| 1697 | |
| 1698 | operation_start("Change File System"); |
| 1699 | if (simulate) { |
| 1700 | simulate_progress_bar(); |
| 1701 | } else { |
| 1702 | string part_path, file_system; |
| 1703 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1704 | DataManager::GetValue("tw_action_new_file_system", file_system); |
| 1705 | if (PartitionManager.Wipe_By_Path(part_path, file_system)) { |
| 1706 | op_status = 0; // success |
| 1707 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1708 | gui_err("change_fs_err=Error changing file system."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1709 | op_status = 1; // fail |
| 1710 | } |
| 1711 | } |
| 1712 | PartitionManager.Update_System_Details(); |
| 1713 | operation_end(op_status); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1717 | int GUIAction::startmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1718 | { |
| 1719 | int op_status = 0; |
| 1720 | |
| 1721 | operation_start("Start MTP"); |
| 1722 | if (PartitionManager.Enable_MTP()) |
| 1723 | op_status = 0; // success |
| 1724 | else |
| 1725 | op_status = 1; // fail |
| 1726 | |
| 1727 | operation_end(op_status); |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1731 | int GUIAction::stopmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1732 | { |
| 1733 | int op_status = 0; |
| 1734 | |
| 1735 | operation_start("Stop MTP"); |
| 1736 | if (PartitionManager.Disable_MTP()) |
| 1737 | op_status = 0; // success |
| 1738 | else |
| 1739 | op_status = 1; // fail |
| 1740 | |
| 1741 | operation_end(op_status); |
| 1742 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1745 | int GUIAction::flashimage(std::string arg __unused) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1746 | { |
| 1747 | int op_status = 0; |
| 1748 | |
| 1749 | operation_start("Flash Image"); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1750 | string path, filename; |
| 1751 | DataManager::GetValue("tw_zip_location", path); |
| 1752 | DataManager::GetValue("tw_file", filename); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 1753 | if (PartitionManager.Flash_Image(path, filename)) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1754 | op_status = 0; // success |
| 1755 | else |
| 1756 | op_status = 1; // fail |
| 1757 | |
| 1758 | operation_end(op_status); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1762 | int GUIAction::twcmd(std::string arg) |
| 1763 | { |
| 1764 | operation_start("TWRP CLI Command"); |
| 1765 | if (simulate) |
| 1766 | simulate_progress_bar(); |
| 1767 | else |
| 1768 | OpenRecoveryScript::Run_CLI_Command(arg.c_str()); |
| 1769 | operation_end(0); |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1773 | int GUIAction::getKeyByName(std::string key) |
| 1774 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1775 | 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] | 1776 | else if (key == "menu") return KEY_MENU; |
| 1777 | else if (key == "back") return KEY_BACK; |
| 1778 | else if (key == "search") return KEY_SEARCH; |
| 1779 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1780 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1781 | else if (key == "power") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1782 | int ret_val; |
| 1783 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1784 | if (!ret_val) |
| 1785 | return KEY_POWER; |
| 1786 | else |
| 1787 | return ret_val; |
| 1788 | } |
| 1789 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1790 | return atol(key.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1791 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1792 | |
| 1793 | int GUIAction::checkpartitionlifetimewrites(std::string arg) |
| 1794 | { |
| 1795 | int op_status = 0; |
| 1796 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg); |
| 1797 | |
| 1798 | operation_start("Check Partition Lifetime Writes"); |
| 1799 | if (sys) { |
| 1800 | if (sys->Check_Lifetime_Writes() != 0) |
| 1801 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1802 | else |
| 1803 | DataManager::SetValue("tw_lifetime_writes", 0); |
| 1804 | op_status = 0; // success |
| 1805 | } else { |
| 1806 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1807 | op_status = 1; // fail |
| 1808 | } |
| 1809 | |
| 1810 | operation_end(op_status); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | int GUIAction::mountsystemtoggle(std::string arg) |
| 1815 | { |
| 1816 | int op_status = 0; |
| 1817 | bool remount_system = PartitionManager.Is_Mounted_By_Path("/system"); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1818 | bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor"); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1819 | |
| 1820 | operation_start("Toggle System Mount"); |
| 1821 | if (!PartitionManager.UnMount_By_Path("/system", true)) { |
| 1822 | op_status = 1; // fail |
| 1823 | } else { |
| 1824 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system"); |
| 1825 | if (Part) { |
Ethan Yonker | d6966f4 | 2015-05-30 14:52:16 -0500 | [diff] [blame] | 1826 | if (arg == "0") { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1827 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 1828 | Part->Change_Mount_Read_Only(false); |
| 1829 | } else { |
| 1830 | DataManager::SetValue("tw_mount_system_ro", 1); |
| 1831 | Part->Change_Mount_Read_Only(true); |
| 1832 | } |
| 1833 | if (remount_system) { |
| 1834 | Part->Mount(true); |
| 1835 | } |
| 1836 | op_status = 0; // success |
| 1837 | } else { |
| 1838 | op_status = 1; // fail |
| 1839 | } |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1840 | Part = PartitionManager.Find_Partition_By_Path("/vendor"); |
| 1841 | if (Part) { |
| 1842 | if (arg == "0") { |
| 1843 | Part->Change_Mount_Read_Only(false); |
| 1844 | } else { |
| 1845 | Part->Change_Mount_Read_Only(true); |
| 1846 | } |
| 1847 | if (remount_vendor) { |
| 1848 | Part->Mount(true); |
| 1849 | } |
| 1850 | op_status = 0; // success |
| 1851 | } else { |
| 1852 | op_status = 1; // fail |
| 1853 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | operation_end(op_status); |
| 1857 | return 0; |
| 1858 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1859 | |
| 1860 | int GUIAction::setlanguage(std::string arg __unused) |
| 1861 | { |
| 1862 | int op_status = 0; |
| 1863 | |
| 1864 | operation_start("Set Language"); |
| 1865 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1866 | PageManager::RequestReload(); |
| 1867 | op_status = 0; // success |
| 1868 | |
| 1869 | operation_end(op_status); |
| 1870 | return 0; |
| 1871 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1872 | |
| 1873 | int GUIAction::setbootslot(std::string arg) |
| 1874 | { |
| 1875 | operation_start("Set Boot Slot"); |
| 1876 | if (!simulate) |
| 1877 | { |
| 1878 | PartitionManager.Set_Active_Slot(arg); |
| 1879 | } else |
| 1880 | simulate_progress_bar(); |
| 1881 | operation_end(0); |
| 1882 | return 0; |
| 1883 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1884 | |
| 1885 | int GUIAction::checkforapp(std::string arg __unused) |
| 1886 | { |
| 1887 | int op_status = 1; |
| 1888 | operation_start("Check for TWRP App"); |
| 1889 | if (!simulate) |
| 1890 | { |
| 1891 | string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk"); |
| 1892 | int sdkver = 0; |
| 1893 | if (!sdkverstr.empty()) { |
| 1894 | sdkver = atoi(sdkverstr.c_str()); |
| 1895 | } |
| 1896 | if (sdkver <= 13) { |
| 1897 | if (sdkver == 0) |
| 1898 | LOGINFO("Unable to read sdk version from build prop\n"); |
| 1899 | else |
| 1900 | LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver); |
| 1901 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1902 | goto exit; |
| 1903 | } |
| 1904 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 1905 | string base_path = "/system"; |
| 1906 | if (TWFunc::Path_Exists("/system/system")) |
| 1907 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 1908 | string install_path = base_path + "/priv-app"; |
| 1909 | if (!TWFunc::Path_Exists(install_path)) |
| 1910 | install_path = base_path + "/app"; |
| 1911 | install_path += "/twrpapp"; |
| 1912 | if (TWFunc::Path_Exists(install_path)) { |
| 1913 | LOGINFO("App found at '%s'\n", install_path.c_str()); |
| 1914 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed |
| 1915 | goto exit; |
| 1916 | } |
| 1917 | } else if (PartitionManager.Mount_By_Path("/data", false)) { |
| 1918 | string parent_path = "/data/app"; |
| 1919 | DIR *d = opendir("/data/app"); |
| 1920 | struct dirent *p; |
| 1921 | int len = strlen("me.twrp.twrpapp-"); |
| 1922 | while ((p = readdir(d))) { |
| 1923 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 1924 | continue; |
| 1925 | if (p->d_type == DT_DIR && strlen(p->d_name) >= len && strncmp(p->d_name, "me.twrp.twrpapp-", len) == 0) { |
| 1926 | LOGINFO("App found at %s/%s\n", parent_path.c_str(), p->d_name); |
| 1927 | closedir(d); |
| 1928 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed |
| 1929 | goto exit; |
| 1930 | } |
| 1931 | } |
| 1932 | closedir(d); |
| 1933 | } |
| 1934 | } else |
| 1935 | simulate_progress_bar(); |
| 1936 | LOGINFO("App not installed\n"); |
| 1937 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1938 | exit: |
| 1939 | operation_end(0); |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
| 1943 | int GUIAction::installapp(std::string arg __unused) |
| 1944 | { |
| 1945 | int op_status = 1; |
| 1946 | operation_start("Install TWRP App"); |
| 1947 | if (!simulate) |
| 1948 | { |
| 1949 | if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) { |
| 1950 | if (PartitionManager.Mount_By_Path("/data", true)) { |
| 1951 | string install_path = "/data/app"; |
| 1952 | string context = "u:object_r:apk_data_file:s0"; |
| 1953 | if (!TWFunc::Path_Exists(install_path)) { |
| 1954 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 1955 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 1956 | goto exit; |
| 1957 | } |
| 1958 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1959 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1960 | goto exit; |
| 1961 | } |
| 1962 | if (setfilecon(install_path.c_str(), context.c_str()) < 0) { |
| 1963 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1964 | goto exit; |
| 1965 | } |
| 1966 | } |
| 1967 | install_path += "/me.twrp.twrpapp-1"; |
| 1968 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 1969 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 1970 | goto exit; |
| 1971 | } |
| 1972 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1973 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1974 | goto exit; |
| 1975 | } |
| 1976 | if (setfilecon(install_path.c_str(), context.c_str()) < 0) { |
| 1977 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1978 | goto exit; |
| 1979 | } |
| 1980 | install_path += "/base.apk"; |
| 1981 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 1982 | LOGERR("Error copying apk file\n"); |
| 1983 | goto exit; |
| 1984 | } |
| 1985 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1986 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1987 | goto exit; |
| 1988 | } |
| 1989 | if (setfilecon(install_path.c_str(), context.c_str()) < 0) { |
| 1990 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1991 | goto exit; |
| 1992 | } |
| 1993 | sync(); |
| 1994 | sync(); |
| 1995 | } |
| 1996 | } else { |
| 1997 | if (PartitionManager.Mount_By_Path("/system", true)) { |
| 1998 | string base_path = "/system"; |
| 1999 | if (TWFunc::Path_Exists("/system/system")) |
| 2000 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 2001 | string install_path = base_path + "/priv-app"; |
| 2002 | string context = "u:object_r:system_file:s0"; |
| 2003 | if (!TWFunc::Path_Exists(install_path)) |
| 2004 | install_path = base_path + "/app"; |
| 2005 | if (TWFunc::Path_Exists(install_path)) { |
| 2006 | install_path += "/twrpapp"; |
| 2007 | LOGINFO("Installing app to '%s'\n", install_path.c_str()); |
| 2008 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) { |
| 2009 | if (setfilecon(install_path.c_str(), context.c_str()) < 0) { |
| 2010 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2011 | goto exit; |
| 2012 | } |
| 2013 | install_path += "/me.twrp.twrpapp.apk"; |
| 2014 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2015 | LOGERR("Error copying apk file\n"); |
| 2016 | goto exit; |
| 2017 | } |
| 2018 | if (setfilecon(install_path.c_str(), context.c_str()) < 0) { |
| 2019 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2020 | goto exit; |
| 2021 | } |
| 2022 | sync(); |
| 2023 | sync(); |
| 2024 | PartitionManager.UnMount_By_Path("/system", true); |
| 2025 | op_status = 0; |
| 2026 | } else { |
| 2027 | LOGERR("Error making app directory '%s': %s\n", strerror(errno)); |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | } else |
| 2033 | simulate_progress_bar(); |
| 2034 | exit: |
| 2035 | operation_end(0); |
| 2036 | return 0; |
| 2037 | } |