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> |
Matt Mower | 0c88b84 | 2017-01-15 16:00:49 -0600 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | |
| 37 | #include <string> |
| 38 | #include <sstream> |
| 39 | #include "../partitions.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 40 | #include "../twrp-functions.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 41 | #include "../openrecoveryscript.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | #include "../adb_install.h" |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 44 | #include "../fuse_sideload.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 45 | #include "blanktimer.hpp" |
Ethan Yonker | 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; |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 65 | pid_t sideload_child_pid; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 66 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 67 | static void *ActionThread_work_wrapper(void *data); |
| 68 | |
| 69 | class ActionThread |
| 70 | { |
| 71 | public: |
| 72 | ActionThread(); |
| 73 | ~ActionThread(); |
| 74 | |
| 75 | void threadActions(GUIAction *act); |
| 76 | void run(void *data); |
| 77 | private: |
| 78 | friend void *ActionThread_work_wrapper(void*); |
| 79 | struct ThreadData |
| 80 | { |
| 81 | ActionThread *this_; |
| 82 | GUIAction *act; |
| 83 | ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {} |
| 84 | }; |
| 85 | |
| 86 | pthread_t m_thread; |
| 87 | bool m_thread_running; |
| 88 | pthread_mutex_t m_act_lock; |
| 89 | }; |
| 90 | |
| 91 | static ActionThread action_thread; // for all kinds of longer running actions |
| 92 | static ActionThread cancel_thread; // for longer running "cancel" actions |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 93 | |
| 94 | static void *ActionThread_work_wrapper(void *data) |
| 95 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 96 | static_cast<ActionThread::ThreadData*>(data)->this_->run(data); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | ActionThread::ActionThread() |
| 101 | { |
| 102 | m_thread_running = false; |
| 103 | pthread_mutex_init(&m_act_lock, NULL); |
| 104 | } |
| 105 | |
| 106 | ActionThread::~ActionThread() |
| 107 | { |
| 108 | pthread_mutex_lock(&m_act_lock); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 109 | if (m_thread_running) { |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 110 | pthread_mutex_unlock(&m_act_lock); |
| 111 | pthread_join(m_thread, NULL); |
| 112 | } else { |
| 113 | pthread_mutex_unlock(&m_act_lock); |
| 114 | } |
| 115 | pthread_mutex_destroy(&m_act_lock); |
| 116 | } |
| 117 | |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 118 | void ActionThread::threadActions(GUIAction *act) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 119 | { |
| 120 | pthread_mutex_lock(&m_act_lock); |
| 121 | if (m_thread_running) { |
| 122 | pthread_mutex_unlock(&m_act_lock); |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 123 | LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n", |
| 124 | act->mActions.size(), act->mActions[0].mFunction.c_str()); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 125 | } else { |
| 126 | m_thread_running = true; |
| 127 | pthread_mutex_unlock(&m_act_lock); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 128 | ThreadData *d = new ThreadData(this, act); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 129 | pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void ActionThread::run(void *data) |
| 134 | { |
| 135 | ThreadData *d = (ThreadData*)data; |
| 136 | GUIAction* act = d->act; |
| 137 | |
| 138 | std::vector<GUIAction::Action>::iterator it; |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 139 | for (it = act->mActions.begin(); it != act->mActions.end(); ++it) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 140 | act->doAction(*it); |
| 141 | |
| 142 | pthread_mutex_lock(&m_act_lock); |
| 143 | m_thread_running = false; |
| 144 | pthread_mutex_unlock(&m_act_lock); |
| 145 | delete d; |
| 146 | } |
| 147 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | GUIAction::GUIAction(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 149 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | xml_node<>* child; |
| 152 | xml_node<>* actions; |
| 153 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 154 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | if (!node) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 157 | if (mf.empty()) { |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 158 | #define ADD_ACTION(n) mf[#n] = &GUIAction::n |
| 159 | #define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 160 | // These actions will be run in the caller's thread |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 161 | ADD_ACTION(reboot); |
| 162 | ADD_ACTION(home); |
| 163 | ADD_ACTION(key); |
| 164 | ADD_ACTION(page); |
| 165 | ADD_ACTION(reload); |
| 166 | ADD_ACTION(readBackup); |
| 167 | ADD_ACTION(set); |
| 168 | ADD_ACTION(clear); |
| 169 | ADD_ACTION(mount); |
| 170 | ADD_ACTION(unmount); |
| 171 | ADD_ACTION_EX("umount", unmount); |
| 172 | ADD_ACTION(restoredefaultsettings); |
| 173 | ADD_ACTION(copylog); |
| 174 | ADD_ACTION(compute); |
| 175 | ADD_ACTION_EX("addsubtract", compute); |
| 176 | ADD_ACTION(setguitimezone); |
| 177 | ADD_ACTION(overlay); |
| 178 | ADD_ACTION(queuezip); |
| 179 | ADD_ACTION(cancelzip); |
| 180 | ADD_ACTION(queueclear); |
| 181 | ADD_ACTION(sleep); |
Matt Mower | 9a2a205 | 2016-05-31 21:31:22 -0500 | [diff] [blame] | 182 | ADD_ACTION(sleepcounter); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 183 | ADD_ACTION(appenddatetobackupname); |
| 184 | ADD_ACTION(generatebackupname); |
| 185 | ADD_ACTION(checkpartitionlist); |
| 186 | ADD_ACTION(getpartitiondetails); |
| 187 | ADD_ACTION(screenshot); |
| 188 | ADD_ACTION(setbrightness); |
| 189 | ADD_ACTION(fileexists); |
| 190 | ADD_ACTION(killterminal); |
| 191 | ADD_ACTION(checkbackupname); |
| 192 | ADD_ACTION(adbsideloadcancel); |
| 193 | ADD_ACTION(fixsu); |
| 194 | ADD_ACTION(startmtp); |
| 195 | ADD_ACTION(stopmtp); |
| 196 | ADD_ACTION(cancelbackup); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 197 | ADD_ACTION(checkpartitionlifetimewrites); |
| 198 | ADD_ACTION(mountsystemtoggle); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 199 | ADD_ACTION(setlanguage); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 200 | ADD_ACTION(checkforapp); |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 201 | ADD_ACTION(togglebacklight); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 202 | |
| 203 | // remember actions that run in the caller thread |
| 204 | for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it) |
| 205 | setActionsRunningInCallerThread.insert(it->first); |
| 206 | |
| 207 | // These actions will run in a separate thread |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 208 | ADD_ACTION(flash); |
| 209 | ADD_ACTION(wipe); |
| 210 | ADD_ACTION(refreshsizes); |
| 211 | ADD_ACTION(nandroid); |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 212 | ADD_ACTION(fixcontexts); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 213 | ADD_ACTION(fixpermissions); |
| 214 | ADD_ACTION(dd); |
| 215 | ADD_ACTION(partitionsd); |
| 216 | ADD_ACTION(installhtcdumlock); |
| 217 | ADD_ACTION(htcdumlockrestoreboot); |
| 218 | ADD_ACTION(htcdumlockreflashrecovery); |
| 219 | ADD_ACTION(cmd); |
| 220 | ADD_ACTION(terminalcommand); |
| 221 | ADD_ACTION(reinjecttwrp); |
| 222 | ADD_ACTION(decrypt); |
| 223 | ADD_ACTION(adbsideload); |
| 224 | ADD_ACTION(openrecoveryscript); |
| 225 | ADD_ACTION(installsu); |
| 226 | ADD_ACTION(decrypt_backup); |
| 227 | ADD_ACTION(repair); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 228 | ADD_ACTION(resize); |
Vojtech Bocek | ecd8918 | 2015-01-15 16:28:54 +0100 | [diff] [blame] | 229 | ADD_ACTION(changefilesystem); |
| 230 | ADD_ACTION(flashimage); |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 231 | ADD_ACTION(twcmd); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 232 | ADD_ACTION(setbootslot); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 233 | ADD_ACTION(installapp); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 236 | // First, get the action |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 237 | actions = FindNode(node, "actions"); |
| 238 | if (actions) child = FindNode(actions, "action"); |
| 239 | else child = FindNode(node, "action"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 240 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 241 | if (!child) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 242 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 243 | while (child) |
| 244 | { |
| 245 | Action action; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 247 | attr = child->first_attribute("function"); |
| 248 | if (!attr) return; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 249 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 250 | action.mFunction = attr->value(); |
| 251 | action.mArg = child->value(); |
| 252 | mActions.push_back(action); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 254 | child = child->next_sibling("action"); |
| 255 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 256 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 257 | // Now, let's get either the key or region |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 258 | child = FindNode(node, "touch"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 259 | if (child) |
| 260 | { |
| 261 | attr = child->first_attribute("key"); |
| 262 | if (attr) |
| 263 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 264 | std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 265 | for (size_t i = 0; i < keys.size(); ++i) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 266 | { |
| 267 | const int key = getKeyByName(keys[i]); |
| 268 | mKeys[key] = false; |
| 269 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 270 | } |
| 271 | else |
| 272 | { |
| 273 | attr = child->first_attribute("x"); |
| 274 | if (!attr) return; |
| 275 | mActionX = atol(attr->value()); |
| 276 | attr = child->first_attribute("y"); |
| 277 | if (!attr) return; |
| 278 | mActionY = atol(attr->value()); |
| 279 | attr = child->first_attribute("w"); |
| 280 | if (!attr) return; |
| 281 | mActionW = atol(attr->value()); |
| 282 | attr = child->first_attribute("h"); |
| 283 | if (!attr) return; |
| 284 | mActionH = atol(attr->value()); |
| 285 | } |
| 286 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Matt Mower | 25036b7 | 2016-06-24 12:30:18 -0500 | [diff] [blame] | 289 | int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 290 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 291 | if (state == TOUCH_RELEASE) |
| 292 | doActions(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 293 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 294 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 297 | int GUIAction::NotifyKey(int key, bool down) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 298 | { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 299 | std::map<int, bool>::iterator itr = mKeys.find(key); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 300 | if (itr == mKeys.end()) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 301 | return 1; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 302 | |
| 303 | bool prevState = itr->second; |
| 304 | itr->second = down; |
| 305 | |
| 306 | // If there is only one key for this action, wait for key up so it |
| 307 | // doesn't trigger with multi-key actions. |
| 308 | // Else, check if all buttons are pressed, then consume their release events |
| 309 | // so they don't trigger one-button actions and reset mKeys pressed status |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 310 | if (mKeys.size() == 1) { |
| 311 | if (!down && prevState) { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 312 | doActions(); |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 313 | return 0; |
| 314 | } |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 315 | } else if (down) { |
| 316 | for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) { |
| 317 | if (!itr->second) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 318 | return 1; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Passed, all req buttons are pressed, reset them and consume release events |
| 322 | HardwareKeyboard *kb = PageManager::GetHardwareKeyboard(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 323 | for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 324 | kb->ConsumeKeyRelease(itr->first); |
| 325 | itr->second = false; |
| 326 | } |
| 327 | |
| 328 | doActions(); |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 329 | return 0; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 330 | } |
| 331 | |
that | d4725ca | 2016-01-19 00:15:21 +0100 | [diff] [blame] | 332 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 335 | int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 337 | GUIObject::NotifyVarChange(varName, value); |
| 338 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 339 | if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 340 | doActions(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 341 | else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 342 | doActions(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 343 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 344 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void GUIAction::simulate_progress_bar(void) |
| 348 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 349 | gui_msg("simulating=Simulating actions..."); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 350 | for (int i = 0; i < 5; i++) |
| 351 | { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 352 | if (PartitionManager.stop_backup.get_value()) { |
| 353 | DataManager::SetValue("tw_cancel_backup", 1); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 354 | gui_msg("backup_cancel=Backup Cancelled"); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 355 | DataManager::SetValue("ui_progress", 0); |
| 356 | PartitionManager.stop_backup.set_value(0); |
| 357 | return; |
| 358 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 359 | usleep(500000); |
| 360 | DataManager::SetValue("ui_progress", i * 20); |
| 361 | } |
| 362 | } |
| 363 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 364 | int GUIAction::flash_zip(std::string filename, int* wipe_cache) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 365 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 366 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | |
| 368 | DataManager::SetValue("ui_progress", 0); |
| 369 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 370 | if (filename.empty()) |
| 371 | { |
| 372 | LOGERR("No file specified.\n"); |
| 373 | return -1; |
| 374 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 375 | |
Ethan Yonker | 9598c07 | 2016-01-15 21:54:34 -0600 | [diff] [blame] | 376 | if (!TWFunc::Path_Exists(filename)) { |
| 377 | if (!PartitionManager.Mount_By_Path(filename, true)) { |
| 378 | return -1; |
| 379 | } |
| 380 | if (!TWFunc::Path_Exists(filename)) { |
| 381 | gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename)); |
| 382 | return -1; |
| 383 | } |
| 384 | } |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 385 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | if (simulate) { |
| 387 | simulate_progress_bar(); |
| 388 | } else { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 389 | ret_val = TWinstall_zip(filename.c_str(), wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 390 | |
| 391 | // Now, check if we need to ensure TWRP remains installed... |
| 392 | struct stat st; |
| 393 | if (stat("/sbin/installTwrp", &st) == 0) |
| 394 | { |
| 395 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 396 | DataManager::SetValue("tw_partition", ""); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 397 | gui_msg("config_twrp=Configuring TWRP..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 398 | if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 399 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 400 | gui_msg("config_twrp_err=Unable to configure TWRP with this kernel."); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 405 | // Done |
| 406 | DataManager::SetValue("ui_progress", 100); |
| 407 | DataManager::SetValue("ui_progress", 0); |
| 408 | return ret_val; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | } |
| 410 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 411 | GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 412 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 413 | string func = gui_parse_text(action.mFunction); |
| 414 | bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end(); |
| 415 | if (needsThread) { |
| 416 | if (func == "cancelbackup") |
| 417 | return THREAD_CANCEL; |
| 418 | else |
| 419 | return THREAD_ACTION; |
| 420 | } |
| 421 | return THREAD_NONE; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 424 | int GUIAction::doActions() |
| 425 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 426 | if (mActions.size() < 1) |
| 427 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 428 | |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 429 | // Determine in which thread to run the actions. |
| 430 | // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy. |
| 431 | ThreadType threadType = THREAD_NONE; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 432 | std::vector<Action>::iterator it; |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 433 | for (it = mActions.begin(); it != mActions.end(); ++it) { |
| 434 | ThreadType tt = getThreadType(*it); |
| 435 | if (tt == THREAD_NONE) |
| 436 | continue; |
| 437 | if (threadType == THREAD_NONE) |
| 438 | threadType = tt; |
| 439 | else if (threadType != tt) { |
| 440 | LOGERR("Can't mix normal and cancel actions in the same list.\n" |
| 441 | "Running the whole batch in the cancel thread.\n"); |
| 442 | threadType = THREAD_CANCEL; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 443 | break; |
| 444 | } |
that | 7d3b54f | 2015-01-09 22:52:51 +0100 | [diff] [blame] | 445 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 446 | |
| 447 | // Now run the actions in the desired thread. |
| 448 | switch (threadType) { |
| 449 | case THREAD_ACTION: |
| 450 | action_thread.threadActions(this); |
| 451 | break; |
| 452 | |
| 453 | case THREAD_CANCEL: |
| 454 | cancel_thread.threadActions(this); |
| 455 | break; |
| 456 | |
| 457 | default: { |
| 458 | // no iterators here because theme reloading might kill our object |
| 459 | const size_t cnt = mActions.size(); |
| 460 | for (size_t i = 0; i < cnt; ++i) |
| 461 | doAction(mActions[i]); |
| 462 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 463 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 464 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 465 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 466 | } |
| 467 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 468 | int GUIAction::doAction(Action action) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 469 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 470 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 471 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 472 | std::string function = gui_parse_text(action.mFunction); |
| 473 | std::string arg = gui_parse_text(action.mArg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 474 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 475 | // find function and execute it |
| 476 | mapFunc::const_iterator funcitr = mf.find(function); |
| 477 | if (funcitr != mf.end()) |
| 478 | return (this->*funcitr->second)(arg); |
| 479 | |
| 480 | LOGERR("Unknown action '%s'\n", function.c_str()); |
| 481 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void GUIAction::operation_start(const string operation_name) |
| 485 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 486 | LOGINFO("operation_start: '%s'\n", operation_name.c_str()); |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 487 | time(&Start); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 488 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 489 | DataManager::SetValue("ui_progress", 0); |
| 490 | DataManager::SetValue("tw_operation", operation_name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 491 | DataManager::SetValue("tw_operation_state", 0); |
Ethan Yonker | d83c9ea | 2015-01-02 15:22:31 -0600 | [diff] [blame] | 492 | DataManager::SetValue("tw_operation_status", 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 493 | } |
| 494 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 495 | void GUIAction::operation_end(const int operation_status) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 496 | { |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 497 | time_t Stop; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 498 | int simulate_fail; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 499 | DataManager::SetValue("ui_progress", 100); |
| 500 | if (simulate) { |
| 501 | DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); |
| 502 | if (simulate_fail != 0) |
| 503 | DataManager::SetValue("tw_operation_status", 1); |
| 504 | else |
| 505 | DataManager::SetValue("tw_operation_status", 0); |
| 506 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 507 | if (operation_status != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 508 | DataManager::SetValue("tw_operation_status", 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 509 | } |
| 510 | else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 511 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 512 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 513 | } |
| 514 | DataManager::SetValue("tw_operation_state", 1); |
| 515 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 516 | blankTimer.resetTimerAndUnblank(); |
LuK1337 | 62326f4 | 2015-09-30 19:57:46 +0200 | [diff] [blame] | 517 | property_set("twrp.action_complete", "1"); |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 518 | time(&Stop); |
| 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 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 710 | if (divide_by != 0) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 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 | } |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 858 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 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; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 864 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 865 | if (arg.empty()) |
| 866 | arg = "tw_wipe_list"; |
| 867 | DataManager::GetValue(arg, List); |
| 868 | LOGINFO("getpartitiondetails list '%s'\n", List.c_str()); |
| 869 | if (!List.empty()) { |
| 870 | size_t start_pos = 0, end_pos = List.find(";", start_pos); |
| 871 | part_path = List; |
| 872 | while (end_pos != string::npos && start_pos < List.size()) { |
| 873 | part_path = List.substr(start_pos, end_pos - start_pos); |
| 874 | LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str()); |
| 875 | if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 876 | // Do nothing |
| 877 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 878 | DataManager::SetValue("tw_partition_path", part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | start_pos = end_pos + 1; |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 882 | end_pos = List.find(";", start_pos); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 883 | } |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 884 | if (!part_path.empty()) { |
| 885 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 886 | if (Part) { |
| 887 | unsigned long long mb = 1048576; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 888 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 889 | DataManager::SetValue("tw_partition_name", Part->Display_Name); |
| 890 | DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point); |
| 891 | DataManager::SetValue("tw_partition_file_system", Part->Current_File_System); |
| 892 | DataManager::SetValue("tw_partition_size", Part->Size / mb); |
| 893 | DataManager::SetValue("tw_partition_used", Part->Used / mb); |
| 894 | DataManager::SetValue("tw_partition_free", Part->Free / mb); |
| 895 | DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb); |
| 896 | DataManager::SetValue("tw_partition_removable", Part->Removable); |
| 897 | DataManager::SetValue("tw_partition_is_present", Part->Is_Present); |
| 898 | |
| 899 | if (Part->Can_Repair()) |
| 900 | DataManager::SetValue("tw_partition_can_repair", 1); |
| 901 | else |
| 902 | DataManager::SetValue("tw_partition_can_repair", 0); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 903 | if (Part->Can_Resize()) |
| 904 | DataManager::SetValue("tw_partition_can_resize", 1); |
| 905 | else |
| 906 | DataManager::SetValue("tw_partition_can_resize", 0); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 907 | if (TWFunc::Path_Exists("/sbin/mkfs.fat")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 908 | DataManager::SetValue("tw_partition_vfat", 1); |
| 909 | else |
| 910 | DataManager::SetValue("tw_partition_vfat", 0); |
Matt Mower | 80f7b36 | 2015-12-12 15:38:01 -0600 | [diff] [blame] | 911 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 912 | DataManager::SetValue("tw_partition_exfat", 1); |
| 913 | else |
| 914 | DataManager::SetValue("tw_partition_exfat", 0); |
| 915 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) |
| 916 | DataManager::SetValue("tw_partition_f2fs", 1); |
| 917 | else |
| 918 | DataManager::SetValue("tw_partition_f2fs", 0); |
| 919 | if (TWFunc::Path_Exists("/sbin/mke2fs")) |
| 920 | DataManager::SetValue("tw_partition_ext", 1); |
| 921 | else |
| 922 | DataManager::SetValue("tw_partition_ext", 0); |
| 923 | return 0; |
| 924 | } else { |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 925 | LOGERR("Unable to locate partition: '%s'\n", part_path.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | DataManager::SetValue("tw_partition_name", ""); |
| 930 | DataManager::SetValue("tw_partition_file_system", ""); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 931 | // Set this to 0 to prevent trying to partition this device, just in case |
| 932 | DataManager::SetValue("tw_partition_removable", 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 936 | int GUIAction::screenshot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 937 | { |
| 938 | time_t tm; |
| 939 | char path[256]; |
| 940 | int path_len; |
Matt Mower | 0c88b84 | 2017-01-15 16:00:49 -0600 | [diff] [blame] | 941 | uid_t uid = AID_MEDIA_RW; |
| 942 | gid_t gid = AID_MEDIA_RW; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 943 | |
| 944 | const std::string storage = DataManager::GetCurrentStoragePath(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 945 | if (PartitionManager.Is_Mounted_By_Path(storage)) { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 946 | snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str()); |
| 947 | } else { |
| 948 | strcpy(path, "/tmp/"); |
| 949 | } |
| 950 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 951 | if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid)) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 952 | return 0; |
| 953 | |
| 954 | tm = time(NULL); |
| 955 | path_len = strlen(path); |
| 956 | |
| 957 | // Screenshot_2014-01-01-18-21-38.png |
| 958 | strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm)); |
| 959 | |
| 960 | int res = gr_save_screenshot(path); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 961 | if (res == 0) { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 962 | chmod(path, 0666); |
| 963 | chown(path, uid, gid); |
| 964 | |
that | 677b13f | 2015-12-26 23:57:31 +0100 | [diff] [blame] | 965 | gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path)); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 966 | |
| 967 | // blink to notify that the screenshow was taken |
| 968 | gr_color(255, 255, 255, 255); |
| 969 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 970 | gr_flip(); |
| 971 | gui_forceRender(); |
| 972 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 973 | gui_err("screenshot_err=Failed to take a screenshot!"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 974 | } |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | int GUIAction::setbrightness(std::string arg) |
| 979 | { |
| 980 | return TWFunc::Set_Brightness(arg); |
| 981 | } |
| 982 | |
| 983 | int GUIAction::fileexists(std::string arg) |
| 984 | { |
| 985 | struct stat st; |
| 986 | string newpath = arg + "/."; |
| 987 | |
| 988 | operation_start("FileExists"); |
| 989 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 990 | operation_end(0); |
| 991 | else |
| 992 | operation_end(1); |
| 993 | return 0; |
| 994 | } |
| 995 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 996 | void GUIAction::reinject_after_flash() |
| 997 | { |
| 998 | 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] | 999 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1000 | if (simulate) { |
| 1001 | simulate_progress_bar(); |
| 1002 | } else { |
| 1003 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1004 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
| 1005 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); |
| 1006 | else { |
| 1007 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
| 1008 | TWFunc::Exec_Cmd(injectcmd); |
| 1009 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1010 | gui_msg("done=Done."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1015 | int GUIAction::flash(std::string arg) |
| 1016 | { |
| 1017 | int i, ret_val = 0, wipe_cache = 0; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1018 | // We're going to jump to this page first, like a loading page |
| 1019 | gui_changePage(arg); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1020 | for (i=0; i<zip_queue_index; i++) { |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1021 | string zip_path = zip_queue[i]; |
| 1022 | size_t slashpos = zip_path.find_last_of('/'); |
| 1023 | string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1024 | operation_start("Flashing"); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1025 | DataManager::SetValue("tw_filename", zip_path); |
| 1026 | DataManager::SetValue("tw_file", zip_filename); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1027 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 1028 | |
| 1029 | TWFunc::SetPerformanceMode(true); |
that | c7b631b | 2015-06-01 23:36:57 +0200 | [diff] [blame] | 1030 | ret_val = flash_zip(zip_path, &wipe_cache); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1031 | TWFunc::SetPerformanceMode(false); |
| 1032 | if (ret_val != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1033 | 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] | 1034 | ret_val = 1; |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1035 | break; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | zip_queue_index = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1039 | |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1040 | if (wipe_cache) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1041 | 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] | 1042 | PartitionManager.Wipe_By_Path("/cache"); |
Ethan Yonker | 0d47eb5 | 2015-01-09 11:23:19 -0600 | [diff] [blame] | 1043 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1044 | |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1045 | reinject_after_flash(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1046 | PartitionManager.Update_System_Details(); |
| 1047 | operation_end(ret_val); |
bigbiff | a869fc7 | 2016-03-01 19:40:36 -0500 | [diff] [blame] | 1048 | // 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] | 1049 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | int GUIAction::wipe(std::string arg) |
| 1054 | { |
| 1055 | operation_start("Format"); |
| 1056 | DataManager::SetValue("tw_partition", arg); |
| 1057 | |
| 1058 | int ret_val = false; |
| 1059 | |
| 1060 | if (simulate) { |
| 1061 | simulate_progress_bar(); |
| 1062 | } else { |
| 1063 | if (arg == "data") |
| 1064 | ret_val = PartitionManager.Factory_Reset(); |
| 1065 | else if (arg == "battery") |
| 1066 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
| 1067 | else if (arg == "rotate") |
| 1068 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
| 1069 | else if (arg == "dalvik") |
| 1070 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
| 1071 | else if (arg == "DATAMEDIA") { |
| 1072 | ret_val = PartitionManager.Format_Data(); |
| 1073 | } else if (arg == "INTERNAL") { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1074 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1075 | |
| 1076 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1077 | if (has_datamedia) { |
| 1078 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
| 1079 | } else { |
| 1080 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
| 1081 | } |
| 1082 | } else if (arg == "EXTERNAL") { |
| 1083 | string External_Path; |
| 1084 | |
| 1085 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 1086 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
| 1087 | } else if (arg == "ANDROIDSECURE") { |
| 1088 | ret_val = PartitionManager.Wipe_Android_Secure(); |
| 1089 | } else if (arg == "LIST") { |
| 1090 | string Wipe_List, wipe_path; |
| 1091 | bool skip = false; |
| 1092 | ret_val = true; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1093 | |
| 1094 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
| 1095 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
| 1096 | if (!Wipe_List.empty()) { |
| 1097 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 1098 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 1099 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
| 1100 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
| 1101 | if (wipe_path == "/and-sec") { |
| 1102 | if (!PartitionManager.Wipe_Android_Secure()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1103 | gui_msg("and_sec_wipe_err=Unable to wipe android secure"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1104 | ret_val = false; |
| 1105 | break; |
| 1106 | } else { |
| 1107 | skip = true; |
| 1108 | } |
| 1109 | } else if (wipe_path == "DALVIK") { |
| 1110 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1111 | gui_err("dalvik_wipe_err=Failed to wipe dalvik"); |
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 == "INTERNAL") { |
| 1118 | if (!PartitionManager.Wipe_Media_From_Data()) { |
| 1119 | ret_val = false; |
| 1120 | break; |
| 1121 | } else { |
| 1122 | skip = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1123 | } |
| 1124 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1125 | if (!skip) { |
| 1126 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1127 | 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] | 1128 | ret_val = false; |
| 1129 | break; |
| 1130 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 1131 | arg = wipe_path; |
| 1132 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1133 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1134 | skip = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1135 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1136 | start_pos = end_pos + 1; |
| 1137 | end_pos = Wipe_List.find(";", start_pos); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1138 | } |
| 1139 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1140 | } else |
| 1141 | ret_val = PartitionManager.Wipe_By_Path(arg); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1142 | #ifndef TW_OEM_BUILD |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1143 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 1144 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 1145 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1146 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1147 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 1148 | LOGINFO("Making TWRP folder and saving settings.\n"); |
| 1149 | Storage_Path += "/TWRP"; |
| 1150 | mkdir(Storage_Path.c_str(), 0777); |
| 1151 | DataManager::Flush(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1152 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1153 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
| 1154 | } |
| 1155 | } |
| 1156 | #endif |
| 1157 | } |
| 1158 | PartitionManager.Update_System_Details(); |
| 1159 | if (ret_val) |
| 1160 | ret_val = 0; // 0 is success |
| 1161 | else |
| 1162 | ret_val = 1; // 1 is failure |
| 1163 | operation_end(ret_val); |
| 1164 | return 0; |
| 1165 | } |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 1166 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1167 | int GUIAction::refreshsizes(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1168 | { |
| 1169 | operation_start("Refreshing Sizes"); |
| 1170 | if (simulate) { |
| 1171 | simulate_progress_bar(); |
| 1172 | } else |
| 1173 | PartitionManager.Update_System_Details(); |
| 1174 | operation_end(0); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | int GUIAction::nandroid(std::string arg) |
| 1179 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1180 | if (simulate) { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1181 | PartitionManager.stop_backup.set_value(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1182 | DataManager::SetValue("tw_partition", "Simulation"); |
| 1183 | simulate_progress_bar(); |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1184 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1185 | } else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1186 | operation_start("Nandroid"); |
| 1187 | int ret = 0; |
| 1188 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1189 | if (arg == "backup") { |
| 1190 | string Backup_Name; |
| 1191 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 4adc33e | 2016-01-22 16:07:11 -0600 | [diff] [blame] | 1192 | string auto_gen = gui_lookup("auto_generate", "(Auto Generate)"); |
| 1193 | 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] | 1194 | ret = PartitionManager.Run_Backup(false); |
| 1195 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1196 | operation_end(1); |
| 1197 | return -1; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1198 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1199 | DataManager::SetValue(TW_BACKUP_NAME, auto_gen); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1200 | } else if (arg == "restore") { |
| 1201 | string Restore_Name; |
| 1202 | DataManager::GetValue("tw_restore", Restore_Name); |
| 1203 | ret = PartitionManager.Run_Restore(Restore_Name); |
| 1204 | } else { |
| 1205 | operation_end(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1206 | return -1; |
| 1207 | } |
| 1208 | DataManager::SetValue("tw_encrypt_backup", 0); |
| 1209 | if (!PartitionManager.stop_backup.get_value()) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1210 | if (ret == false) |
| 1211 | ret = 1; // 1 for failure |
| 1212 | else |
| 1213 | ret = 0; // 0 for success |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1214 | DataManager::SetValue("tw_cancel_backup", 0); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1215 | } |
| 1216 | else { |
| 1217 | DataManager::SetValue("tw_cancel_backup", 1); |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1218 | gui_msg("backup_cancel=Backup Cancelled"); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1219 | ret = 0; |
| 1220 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1221 | operation_end(ret); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1222 | return ret; |
| 1223 | } |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1227 | int GUIAction::cancelbackup(std::string arg __unused) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1228 | if (simulate) { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1229 | PartitionManager.stop_backup.set_value(1); |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1230 | } |
| 1231 | else { |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1232 | int op_status = PartitionManager.Cancel_Backup(); |
| 1233 | if (op_status != 0) |
| 1234 | op_status = 1; // failure |
bigbiff | 7abc5fe | 2015-01-17 16:53:12 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | return 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1238 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1239 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1240 | int GUIAction::fixcontexts(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1241 | { |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1242 | int op_status = 0; |
| 1243 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1244 | operation_start("Fix Contexts"); |
| 1245 | LOGINFO("fix contexts started!\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1246 | if (simulate) { |
| 1247 | simulate_progress_bar(); |
| 1248 | } else { |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1249 | op_status = PartitionManager.Fix_Contexts(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1250 | if (op_status != 0) |
| 1251 | op_status = 1; // failure |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1252 | } |
that | 73a5295 | 2015-01-28 23:07:34 +0100 | [diff] [blame] | 1253 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | |
Ethan Yonker | b5fab76 | 2016-01-28 14:03:33 -0600 | [diff] [blame] | 1257 | int GUIAction::fixpermissions(std::string arg) |
| 1258 | { |
| 1259 | return fixcontexts(arg); |
| 1260 | } |
| 1261 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1262 | int GUIAction::dd(std::string arg) |
| 1263 | { |
| 1264 | operation_start("imaging"); |
| 1265 | |
| 1266 | if (simulate) { |
| 1267 | simulate_progress_bar(); |
| 1268 | } else { |
| 1269 | string cmd = "dd " + arg; |
| 1270 | TWFunc::Exec_Cmd(cmd); |
| 1271 | } |
| 1272 | operation_end(0); |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1276 | int GUIAction::partitionsd(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1277 | { |
| 1278 | operation_start("Partition SD Card"); |
| 1279 | int ret_val = 0; |
| 1280 | |
| 1281 | if (simulate) { |
| 1282 | simulate_progress_bar(); |
| 1283 | } else { |
| 1284 | int allow_partition; |
| 1285 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 1286 | if (allow_partition == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1287 | 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] | 1288 | } else { |
| 1289 | if (!PartitionManager.Partition_SDCard()) |
| 1290 | ret_val = 1; // failed |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1291 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1292 | } |
| 1293 | operation_end(ret_val); |
| 1294 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1295 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1296 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1297 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1298 | int GUIAction::installhtcdumlock(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1299 | { |
| 1300 | operation_start("Install HTC Dumlock"); |
| 1301 | if (simulate) { |
| 1302 | simulate_progress_bar(); |
| 1303 | } else |
| 1304 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1305 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1306 | operation_end(0); |
| 1307 | return 0; |
| 1308 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1309 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1310 | int GUIAction::htcdumlockrestoreboot(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1311 | { |
| 1312 | operation_start("HTC Dumlock Restore Boot"); |
| 1313 | if (simulate) { |
| 1314 | simulate_progress_bar(); |
| 1315 | } else |
| 1316 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1317 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1318 | operation_end(0); |
| 1319 | return 0; |
| 1320 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1321 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1322 | int GUIAction::htcdumlockreflashrecovery(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1323 | { |
| 1324 | operation_start("HTC Dumlock Reflash Recovery"); |
| 1325 | if (simulate) { |
| 1326 | simulate_progress_bar(); |
| 1327 | } else |
| 1328 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1329 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1330 | operation_end(0); |
| 1331 | return 0; |
| 1332 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1333 | |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1334 | int GUIAction::cmd(std::string arg) |
| 1335 | { |
| 1336 | int op_status = 0; |
| 1337 | |
| 1338 | operation_start("Command"); |
| 1339 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
| 1340 | if (simulate) { |
| 1341 | simulate_progress_bar(); |
| 1342 | } else { |
| 1343 | op_status = TWFunc::Exec_Cmd(arg); |
| 1344 | if (op_status != 0) |
| 1345 | op_status = 1; |
| 1346 | } |
| 1347 | |
| 1348 | operation_end(op_status); |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | int GUIAction::terminalcommand(std::string arg) |
| 1353 | { |
| 1354 | int op_status = 0; |
| 1355 | string cmdpath, command; |
| 1356 | |
| 1357 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1358 | operation_start("CommandOutput"); |
| 1359 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 1360 | if (simulate) { |
| 1361 | simulate_progress_bar(); |
| 1362 | operation_end(op_status); |
Matt Mower | 5aa29ab | 2015-02-25 23:50:55 -0600 | [diff] [blame] | 1363 | } else if (arg == "exit") { |
| 1364 | LOGINFO("Exiting terminal\n"); |
| 1365 | operation_end(op_status); |
| 1366 | page("main"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1367 | } else { |
| 1368 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
| 1369 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1370 | DataManager::SetValue("tw_terminal_state", 1); |
| 1371 | DataManager::SetValue("tw_background_thread_running", 1); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1372 | FILE* fp; |
| 1373 | char line[512]; |
| 1374 | |
| 1375 | fp = popen(command.c_str(), "r"); |
| 1376 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1377 | LOGERR("Error opening command to run (%s).\n", strerror(errno)); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1378 | } else { |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 1379 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1; |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1380 | struct timeval timeout; |
| 1381 | fd_set fdset; |
| 1382 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1383 | while (keep_going) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1384 | { |
| 1385 | FD_ZERO(&fdset); |
| 1386 | FD_SET(fd, &fdset); |
| 1387 | timeout.tv_sec = 0; |
| 1388 | timeout.tv_usec = 400000; |
| 1389 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1390 | if (has_data == 0) { |
| 1391 | // Timeout reached |
| 1392 | DataManager::GetValue("tw_terminal_state", check); |
| 1393 | if (check == 0) { |
| 1394 | keep_going = 0; |
| 1395 | } |
| 1396 | } else if (has_data < 0) { |
| 1397 | // End of execution |
| 1398 | keep_going = 0; |
| 1399 | } else { |
| 1400 | // Try to read output |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1401 | if (fgets(line, sizeof(line), fp) != NULL) |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1402 | gui_print("%s", line); // Display output |
| 1403 | else |
| 1404 | keep_going = 0; // Done executing |
| 1405 | } |
| 1406 | } |
| 1407 | fclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1408 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1409 | DataManager::SetValue("tw_operation_status", 0); |
| 1410 | DataManager::SetValue("tw_operation_state", 1); |
| 1411 | DataManager::SetValue("tw_terminal_state", 0); |
| 1412 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1413 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1414 | } |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1418 | int GUIAction::killterminal(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1419 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1420 | LOGINFO("Sending kill command...\n"); |
| 1421 | operation_start("KillCommand"); |
| 1422 | DataManager::SetValue("tw_operation_status", 0); |
| 1423 | DataManager::SetValue("tw_operation_state", 1); |
| 1424 | DataManager::SetValue("tw_terminal_state", 0); |
| 1425 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1426 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1430 | int GUIAction::reinjecttwrp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1431 | { |
| 1432 | int op_status = 0; |
| 1433 | operation_start("ReinjectTWRP"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1434 | gui_msg("injecttwrp=Injecting TWRP into boot image..."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1435 | if (simulate) { |
| 1436 | simulate_progress_bar(); |
| 1437 | } else { |
| 1438 | 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] | 1439 | gui_msg("done=Done."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | operation_end(op_status); |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1446 | int GUIAction::checkbackupname(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1447 | { |
| 1448 | int op_status = 0; |
| 1449 | |
| 1450 | operation_start("CheckBackupName"); |
| 1451 | if (simulate) { |
| 1452 | simulate_progress_bar(); |
| 1453 | } else { |
| 1454 | op_status = PartitionManager.Check_Backup_Name(true); |
| 1455 | if (op_status != 0) |
| 1456 | op_status = 1; |
| 1457 | } |
| 1458 | |
| 1459 | operation_end(op_status); |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1463 | int GUIAction::decrypt(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1464 | { |
| 1465 | int op_status = 0; |
| 1466 | |
| 1467 | operation_start("Decrypt"); |
| 1468 | if (simulate) { |
| 1469 | simulate_progress_bar(); |
| 1470 | } else { |
| 1471 | string Password; |
| 1472 | DataManager::GetValue("tw_crypto_password", Password); |
| 1473 | op_status = PartitionManager.Decrypt_Device(Password); |
| 1474 | if (op_status != 0) |
| 1475 | op_status = 1; |
| 1476 | else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1477 | |
| 1478 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1479 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1480 | int has_datamedia; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1481 | |
Ethan Yonker | cf50da5 | 2015-01-12 21:59:07 -0600 | [diff] [blame] | 1482 | // Check for a custom theme and load it if exists |
| 1483 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1484 | if (has_datamedia != 0) { |
| 1485 | if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) { |
Ethan Yonker | d9ff3c5 | 2015-01-21 21:51:20 -0600 | [diff] [blame] | 1486 | 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] | 1487 | } else { |
| 1488 | LOGINFO("Got default contexts and file mode for storage files.\n"); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1489 | } |
| 1490 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1491 | PartitionManager.Decrypt_Adopted(); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | operation_end(op_status); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1499 | int GUIAction::adbsideload(std::string arg __unused) |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1500 | { |
| 1501 | operation_start("Sideload"); |
| 1502 | if (simulate) { |
| 1503 | simulate_progress_bar(); |
| 1504 | operation_end(0); |
| 1505 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1506 | gui_msg("start_sideload=Starting ADB sideload feature..."); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1507 | bool mtp_was_enabled = TWFunc::Toggle_MTP(false); |
| 1508 | |
| 1509 | // wait for the adb connection |
| 1510 | int ret = apply_from_adb("/", &sideload_child_pid); |
| 1511 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
| 1512 | |
| 1513 | if (ret != 0) { |
| 1514 | if (ret == -2) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1515 | 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] | 1516 | ret = 1; // failure |
| 1517 | } else { |
| 1518 | int wipe_cache = 0; |
| 1519 | int wipe_dalvik = 0; |
| 1520 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
| 1521 | |
| 1522 | if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) { |
| 1523 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1524 | PartitionManager.Wipe_By_Path("/cache"); |
| 1525 | if (wipe_dalvik) |
| 1526 | PartitionManager.Wipe_Dalvik_Cache(); |
| 1527 | } else { |
| 1528 | ret = 1; // failure |
| 1529 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1530 | } |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1531 | if (sideload_child_pid) { |
| 1532 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1533 | struct stat st; |
| 1534 | // Calling stat() on this magic filename signals the minadbd |
| 1535 | // subprocess to shut down. |
| 1536 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1537 | int status; |
| 1538 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1539 | waitpid(sideload_child_pid, &status, 0); |
| 1540 | } |
Dees Troy | 28a85d2 | 2015-04-28 02:03:16 +0000 | [diff] [blame] | 1541 | property_set("ctl.start", "adbd"); |
that | c608548 | 2015-01-09 22:12:43 +0100 | [diff] [blame] | 1542 | TWFunc::Toggle_MTP(mtp_was_enabled); |
| 1543 | reinject_after_flash(); |
| 1544 | operation_end(ret); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1545 | } |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1549 | int GUIAction::adbsideloadcancel(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1550 | { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1551 | struct stat st; |
| 1552 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1553 | gui_msg("cancel_sideload=Cancelling ADB sideload..."); |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1554 | LOGINFO("Signaling child sideload process to exit.\n"); |
| 1555 | // Calling stat() on this magic filename signals the minadbd |
| 1556 | // subprocess to shut down. |
| 1557 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); |
| 1558 | if (!sideload_child_pid) { |
| 1559 | LOGERR("Unable to get child ID\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1560 | return 0; |
| 1561 | } |
that | cc8ddca | 2015-01-03 01:59:36 +0100 | [diff] [blame] | 1562 | ::sleep(1); |
| 1563 | LOGINFO("Killing child sideload process.\n"); |
| 1564 | kill(sideload_child_pid, SIGTERM); |
| 1565 | int status; |
| 1566 | LOGINFO("Waiting for child sideload process to exit.\n"); |
| 1567 | waitpid(sideload_child_pid, &status, 0); |
| 1568 | sideload_child_pid = 0; |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1569 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
| 1570 | return 0; |
| 1571 | } |
| 1572 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1573 | int GUIAction::openrecoveryscript(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1574 | { |
| 1575 | operation_start("OpenRecoveryScript"); |
| 1576 | if (simulate) { |
| 1577 | simulate_progress_bar(); |
Ethan Yonker | 24e7eb7 | 2015-01-03 16:13:06 -0600 | [diff] [blame] | 1578 | operation_end(0); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1579 | } else { |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1580 | int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action(); |
Ethan Yonker | e1abe61 | 2015-06-09 11:09:32 -0500 | [diff] [blame] | 1581 | operation_end(op_status); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1582 | } |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1586 | int GUIAction::installsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1587 | { |
| 1588 | int op_status = 0; |
| 1589 | |
| 1590 | operation_start("Install SuperSU"); |
| 1591 | if (simulate) { |
| 1592 | simulate_progress_bar(); |
| 1593 | } else { |
| 1594 | if (!TWFunc::Install_SuperSU()) |
| 1595 | op_status = 1; |
| 1596 | } |
| 1597 | |
| 1598 | operation_end(op_status); |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1602 | int GUIAction::fixsu(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1603 | { |
| 1604 | int op_status = 0; |
| 1605 | |
| 1606 | operation_start("Fixing Superuser Permissions"); |
| 1607 | if (simulate) { |
| 1608 | simulate_progress_bar(); |
| 1609 | } else { |
| 1610 | LOGERR("Fixing su permissions was deprecated from TWRP.\n"); |
| 1611 | LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n"); |
| 1612 | } |
| 1613 | |
| 1614 | operation_end(op_status); |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1618 | int GUIAction::decrypt_backup(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1619 | { |
| 1620 | int op_status = 0; |
| 1621 | |
| 1622 | operation_start("Try Restore Decrypt"); |
| 1623 | if (simulate) { |
| 1624 | simulate_progress_bar(); |
| 1625 | } else { |
| 1626 | string Restore_Path, Filename, Password; |
| 1627 | DataManager::GetValue("tw_restore", Restore_Path); |
| 1628 | Restore_Path += "/"; |
| 1629 | DataManager::GetValue("tw_restore_password", Password); |
| 1630 | TWFunc::SetPerformanceMode(true); |
| 1631 | if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password)) |
| 1632 | op_status = 0; // success |
| 1633 | else |
| 1634 | op_status = 1; // fail |
| 1635 | TWFunc::SetPerformanceMode(false); |
| 1636 | } |
| 1637 | |
| 1638 | operation_end(op_status); |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1642 | int GUIAction::repair(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1643 | { |
| 1644 | int op_status = 0; |
| 1645 | |
| 1646 | operation_start("Repair Partition"); |
| 1647 | if (simulate) { |
| 1648 | simulate_progress_bar(); |
| 1649 | } else { |
| 1650 | string part_path; |
| 1651 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1652 | if (PartitionManager.Repair_By_Path(part_path, true)) { |
| 1653 | op_status = 0; // success |
| 1654 | } else { |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1655 | op_status = 1; // fail |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | operation_end(op_status); |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1663 | int GUIAction::resize(std::string arg __unused) |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1664 | { |
| 1665 | int op_status = 0; |
| 1666 | |
| 1667 | operation_start("Resize Partition"); |
| 1668 | if (simulate) { |
| 1669 | simulate_progress_bar(); |
| 1670 | } else { |
| 1671 | string part_path; |
| 1672 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1673 | if (PartitionManager.Resize_By_Path(part_path, true)) { |
| 1674 | op_status = 0; // success |
| 1675 | } else { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1676 | op_status = 1; // fail |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | operation_end(op_status); |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1684 | int GUIAction::changefilesystem(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1685 | { |
| 1686 | int op_status = 0; |
| 1687 | |
| 1688 | operation_start("Change File System"); |
| 1689 | if (simulate) { |
| 1690 | simulate_progress_bar(); |
| 1691 | } else { |
| 1692 | string part_path, file_system; |
| 1693 | DataManager::GetValue("tw_partition_mount_point", part_path); |
| 1694 | DataManager::GetValue("tw_action_new_file_system", file_system); |
| 1695 | if (PartitionManager.Wipe_By_Path(part_path, file_system)) { |
| 1696 | op_status = 0; // success |
| 1697 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1698 | gui_err("change_fs_err=Error changing file system."); |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1699 | op_status = 1; // fail |
| 1700 | } |
| 1701 | } |
| 1702 | PartitionManager.Update_System_Details(); |
| 1703 | operation_end(op_status); |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1707 | int GUIAction::startmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1708 | { |
| 1709 | int op_status = 0; |
| 1710 | |
| 1711 | operation_start("Start MTP"); |
| 1712 | if (PartitionManager.Enable_MTP()) |
| 1713 | op_status = 0; // success |
| 1714 | else |
| 1715 | op_status = 1; // fail |
| 1716 | |
| 1717 | operation_end(op_status); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1721 | int GUIAction::stopmtp(std::string arg __unused) |
that | 3f7b1ac | 2014-12-30 11:30:13 +0100 | [diff] [blame] | 1722 | { |
| 1723 | int op_status = 0; |
| 1724 | |
| 1725 | operation_start("Stop MTP"); |
| 1726 | if (PartitionManager.Disable_MTP()) |
| 1727 | op_status = 0; // success |
| 1728 | else |
| 1729 | op_status = 1; // fail |
| 1730 | |
| 1731 | operation_end(op_status); |
| 1732 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1733 | } |
| 1734 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 1735 | int GUIAction::flashimage(std::string arg __unused) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1736 | { |
| 1737 | int op_status = 0; |
| 1738 | |
| 1739 | operation_start("Flash Image"); |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1740 | string path, filename; |
| 1741 | DataManager::GetValue("tw_zip_location", path); |
| 1742 | DataManager::GetValue("tw_file", filename); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 1743 | if (PartitionManager.Flash_Image(path, filename)) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1744 | op_status = 0; // success |
| 1745 | else |
| 1746 | op_status = 1; // fail |
| 1747 | |
| 1748 | operation_end(op_status); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
that | 10ae24f | 2015-12-26 20:53:51 +0100 | [diff] [blame] | 1752 | int GUIAction::twcmd(std::string arg) |
| 1753 | { |
| 1754 | operation_start("TWRP CLI Command"); |
| 1755 | if (simulate) |
| 1756 | simulate_progress_bar(); |
| 1757 | else |
| 1758 | OpenRecoveryScript::Run_CLI_Command(arg.c_str()); |
| 1759 | operation_end(0); |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1763 | int GUIAction::getKeyByName(std::string key) |
| 1764 | { |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 1765 | 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] | 1766 | else if (key == "menu") return KEY_MENU; |
| 1767 | else if (key == "back") return KEY_BACK; |
| 1768 | else if (key == "search") return KEY_SEARCH; |
| 1769 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1770 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1771 | else if (key == "power") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1772 | int ret_val; |
| 1773 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1774 | if (!ret_val) |
| 1775 | return KEY_POWER; |
| 1776 | else |
| 1777 | return ret_val; |
| 1778 | } |
| 1779 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1780 | return atol(key.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1781 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1782 | |
| 1783 | int GUIAction::checkpartitionlifetimewrites(std::string arg) |
| 1784 | { |
| 1785 | int op_status = 0; |
| 1786 | TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg); |
| 1787 | |
| 1788 | operation_start("Check Partition Lifetime Writes"); |
| 1789 | if (sys) { |
| 1790 | if (sys->Check_Lifetime_Writes() != 0) |
| 1791 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1792 | else |
| 1793 | DataManager::SetValue("tw_lifetime_writes", 0); |
| 1794 | op_status = 0; // success |
| 1795 | } else { |
| 1796 | DataManager::SetValue("tw_lifetime_writes", 1); |
| 1797 | op_status = 1; // fail |
| 1798 | } |
| 1799 | |
| 1800 | operation_end(op_status); |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | int GUIAction::mountsystemtoggle(std::string arg) |
| 1805 | { |
| 1806 | int op_status = 0; |
| 1807 | bool remount_system = PartitionManager.Is_Mounted_By_Path("/system"); |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1808 | bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor"); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1809 | |
| 1810 | operation_start("Toggle System Mount"); |
| 1811 | if (!PartitionManager.UnMount_By_Path("/system", true)) { |
| 1812 | op_status = 1; // fail |
| 1813 | } else { |
| 1814 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system"); |
| 1815 | if (Part) { |
Ethan Yonker | d6966f4 | 2015-05-30 14:52:16 -0500 | [diff] [blame] | 1816 | if (arg == "0") { |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1817 | DataManager::SetValue("tw_mount_system_ro", 0); |
| 1818 | Part->Change_Mount_Read_Only(false); |
| 1819 | } else { |
| 1820 | DataManager::SetValue("tw_mount_system_ro", 1); |
| 1821 | Part->Change_Mount_Read_Only(true); |
| 1822 | } |
| 1823 | if (remount_system) { |
| 1824 | Part->Mount(true); |
| 1825 | } |
| 1826 | op_status = 0; // success |
| 1827 | } else { |
| 1828 | op_status = 1; // fail |
| 1829 | } |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 1830 | Part = PartitionManager.Find_Partition_By_Path("/vendor"); |
| 1831 | if (Part) { |
| 1832 | if (arg == "0") { |
| 1833 | Part->Change_Mount_Read_Only(false); |
| 1834 | } else { |
| 1835 | Part->Change_Mount_Read_Only(true); |
| 1836 | } |
| 1837 | if (remount_vendor) { |
| 1838 | Part->Mount(true); |
| 1839 | } |
| 1840 | op_status = 0; // success |
| 1841 | } else { |
| 1842 | op_status = 1; // fail |
| 1843 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | operation_end(op_status); |
| 1847 | return 0; |
| 1848 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1849 | |
| 1850 | int GUIAction::setlanguage(std::string arg __unused) |
| 1851 | { |
| 1852 | int op_status = 0; |
| 1853 | |
| 1854 | operation_start("Set Language"); |
| 1855 | PageManager::LoadLanguage(DataManager::GetStrValue("tw_language")); |
| 1856 | PageManager::RequestReload(); |
| 1857 | op_status = 0; // success |
| 1858 | |
| 1859 | operation_end(op_status); |
| 1860 | return 0; |
| 1861 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1862 | |
Matt Mower | 9472ba1 | 2016-01-20 18:12:47 -0600 | [diff] [blame] | 1863 | int GUIAction::togglebacklight(std::string arg __unused) |
| 1864 | { |
| 1865 | blankTimer.toggleBlank(); |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1869 | int GUIAction::setbootslot(std::string arg) |
| 1870 | { |
| 1871 | operation_start("Set Boot Slot"); |
| 1872 | if (!simulate) |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1873 | PartitionManager.Set_Active_Slot(arg); |
Matt Mower | 029a82d | 2017-01-08 13:12:38 -0600 | [diff] [blame] | 1874 | else |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1875 | simulate_progress_bar(); |
| 1876 | operation_end(0); |
| 1877 | return 0; |
| 1878 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1879 | |
| 1880 | int GUIAction::checkforapp(std::string arg __unused) |
| 1881 | { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1882 | operation_start("Check for TWRP App"); |
| 1883 | if (!simulate) |
| 1884 | { |
| 1885 | string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk"); |
| 1886 | int sdkver = 0; |
| 1887 | if (!sdkverstr.empty()) { |
| 1888 | sdkver = atoi(sdkverstr.c_str()); |
| 1889 | } |
| 1890 | if (sdkver <= 13) { |
| 1891 | if (sdkver == 0) |
| 1892 | LOGINFO("Unable to read sdk version from build prop\n"); |
| 1893 | else |
| 1894 | LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver); |
| 1895 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1896 | goto exit; |
| 1897 | } |
| 1898 | if (PartitionManager.Mount_By_Path("/system", false)) { |
| 1899 | string base_path = "/system"; |
| 1900 | if (TWFunc::Path_Exists("/system/system")) |
| 1901 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 1902 | string install_path = base_path + "/priv-app"; |
| 1903 | if (!TWFunc::Path_Exists(install_path)) |
| 1904 | install_path = base_path + "/app"; |
| 1905 | install_path += "/twrpapp"; |
| 1906 | if (TWFunc::Path_Exists(install_path)) { |
| 1907 | LOGINFO("App found at '%s'\n", install_path.c_str()); |
| 1908 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed |
| 1909 | goto exit; |
| 1910 | } |
Ethan Yonker | 5e1a7f9 | 2017-01-18 16:44:54 -0600 | [diff] [blame] | 1911 | } |
| 1912 | if (PartitionManager.Mount_By_Path("/data", false)) { |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1913 | const char parent_path[] = "/data/app"; |
| 1914 | const char app_prefix[] = "me.twrp.twrpapp-"; |
| 1915 | DIR *d = opendir(parent_path); |
| 1916 | if (d) { |
| 1917 | struct dirent *p; |
| 1918 | while ((p = readdir(d))) { |
| 1919 | if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix))) |
| 1920 | continue; |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1921 | closedir(d); |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1922 | LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1923 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed |
| 1924 | goto exit; |
| 1925 | } |
Ethan Yonker | 5128d29 | 2017-02-04 19:52:56 -0600 | [diff] [blame] | 1926 | closedir(d); |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1927 | } |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1928 | } |
| 1929 | } else |
| 1930 | simulate_progress_bar(); |
| 1931 | LOGINFO("App not installed\n"); |
| 1932 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1933 | exit: |
| 1934 | operation_end(0); |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
| 1938 | int GUIAction::installapp(std::string arg __unused) |
| 1939 | { |
| 1940 | int op_status = 1; |
| 1941 | operation_start("Install TWRP App"); |
| 1942 | if (!simulate) |
| 1943 | { |
| 1944 | if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) { |
| 1945 | if (PartitionManager.Mount_By_Path("/data", true)) { |
| 1946 | string install_path = "/data/app"; |
| 1947 | string context = "u:object_r:apk_data_file:s0"; |
| 1948 | if (!TWFunc::Path_Exists(install_path)) { |
| 1949 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 1950 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 1951 | goto exit; |
| 1952 | } |
| 1953 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1954 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1955 | goto exit; |
| 1956 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 1957 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1958 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1959 | goto exit; |
| 1960 | } |
| 1961 | } |
| 1962 | install_path += "/me.twrp.twrpapp-1"; |
| 1963 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { |
| 1964 | LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno)); |
| 1965 | goto exit; |
| 1966 | } |
| 1967 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1968 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1969 | goto exit; |
| 1970 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 1971 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1972 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1973 | goto exit; |
| 1974 | } |
| 1975 | install_path += "/base.apk"; |
| 1976 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 1977 | LOGERR("Error copying apk file\n"); |
| 1978 | goto exit; |
| 1979 | } |
| 1980 | if (chown(install_path.c_str(), 1000, 1000)) { |
| 1981 | LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1982 | goto exit; |
| 1983 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 1984 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 1985 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 1986 | goto exit; |
| 1987 | } |
| 1988 | sync(); |
| 1989 | sync(); |
| 1990 | } |
| 1991 | } else { |
| 1992 | if (PartitionManager.Mount_By_Path("/system", true)) { |
| 1993 | string base_path = "/system"; |
| 1994 | if (TWFunc::Path_Exists("/system/system")) |
| 1995 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 1996 | string install_path = base_path + "/priv-app"; |
| 1997 | string context = "u:object_r:system_file:s0"; |
| 1998 | if (!TWFunc::Path_Exists(install_path)) |
| 1999 | install_path = base_path + "/app"; |
| 2000 | if (TWFunc::Path_Exists(install_path)) { |
| 2001 | install_path += "/twrpapp"; |
| 2002 | LOGINFO("Installing app to '%s'\n", install_path.c_str()); |
| 2003 | if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) { |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2004 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2005 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2006 | goto exit; |
| 2007 | } |
| 2008 | install_path += "/me.twrp.twrpapp.apk"; |
| 2009 | if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) { |
| 2010 | LOGERR("Error copying apk file\n"); |
| 2011 | goto exit; |
| 2012 | } |
Ethan Yonker | 4767caf | 2017-01-11 10:45:04 -0600 | [diff] [blame] | 2013 | if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) { |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 2014 | LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno)); |
| 2015 | goto exit; |
| 2016 | } |
| 2017 | sync(); |
| 2018 | sync(); |
| 2019 | PartitionManager.UnMount_By_Path("/system", true); |
| 2020 | op_status = 0; |
| 2021 | } else { |
| 2022 | LOGERR("Error making app directory '%s': %s\n", strerror(errno)); |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | } else |
| 2028 | simulate_progress_bar(); |
| 2029 | exit: |
| 2030 | operation_end(0); |
| 2031 | return 0; |
| 2032 | } |