blob: 7d34da8a016083b01679c92a0bb57a025d552f03 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 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_Troy51a0e822012-09-05 15:24:24 -040018
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_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053};
Ethan Yonkerf1179622016-08-25 15:32:21 -050054#include "../set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
that3f7b1ac2014-12-30 11:30:13 +010061GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010062std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010063static string zip_queue[10];
64static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010065pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010066
that73a52952015-01-28 23:07:34 +010067static void *ActionThread_work_wrapper(void *data);
68
69class ActionThread
70{
71public:
72 ActionThread();
73 ~ActionThread();
74
75 void threadActions(GUIAction *act);
76 void run(void *data);
77private:
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
91static ActionThread action_thread; // for all kinds of longer running actions
92static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010093
94static void *ActionThread_work_wrapper(void *data)
95{
that73a52952015-01-28 23:07:34 +010096 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010097 return NULL;
98}
99
100ActionThread::ActionThread()
101{
102 m_thread_running = false;
103 pthread_mutex_init(&m_act_lock, NULL);
104}
105
106ActionThread::~ActionThread()
107{
108 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600109 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100110 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
that7d3b54f2015-01-09 22:52:51 +0100118void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100119{
120 pthread_mutex_lock(&m_act_lock);
121 if (m_thread_running) {
122 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100123 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());
thatc6085482015-01-09 22:12:43 +0100125 } else {
126 m_thread_running = true;
127 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100128 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100129 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
130 }
131}
132
133void ActionThread::run(void *data)
134{
135 ThreadData *d = (ThreadData*)data;
136 GUIAction* act = d->act;
137
138 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100139 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100140 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_Troy51a0e822012-09-05 15:24:24 -0400148GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100149 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400150{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 xml_node<>* child;
152 xml_node<>* actions;
153 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
that3f7b1ac2014-12-30 11:30:13 +0100157 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100158#define ADD_ACTION(n) mf[#n] = &GUIAction::n
159#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100160 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100161 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 Mower9a2a2052016-05-31 21:31:22 -0500182 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100183 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 Yonkereb32b1f2015-05-18 10:23:03 -0500197 ADD_ACTION(checkpartitionlifetimewrites);
198 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500199 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600200 ADD_ACTION(checkforapp);
thatc6085482015-01-09 22:12:43 +0100201
202 // remember actions that run in the caller thread
203 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
204 setActionsRunningInCallerThread.insert(it->first);
205
206 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100207 ADD_ACTION(flash);
208 ADD_ACTION(wipe);
209 ADD_ACTION(refreshsizes);
210 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600211 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100212 ADD_ACTION(fixpermissions);
213 ADD_ACTION(dd);
214 ADD_ACTION(partitionsd);
215 ADD_ACTION(installhtcdumlock);
216 ADD_ACTION(htcdumlockrestoreboot);
217 ADD_ACTION(htcdumlockreflashrecovery);
218 ADD_ACTION(cmd);
219 ADD_ACTION(terminalcommand);
220 ADD_ACTION(reinjecttwrp);
221 ADD_ACTION(decrypt);
222 ADD_ACTION(adbsideload);
223 ADD_ACTION(openrecoveryscript);
224 ADD_ACTION(installsu);
225 ADD_ACTION(decrypt_backup);
226 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500227 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100228 ADD_ACTION(changefilesystem);
229 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100230 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600231 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600232 ADD_ACTION(installapp);
that3f7b1ac2014-12-30 11:30:13 +0100233 }
234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600236 actions = FindNode(node, "actions");
237 if (actions) child = FindNode(actions, "action");
238 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 while (child)
243 {
244 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 attr = child->first_attribute("function");
247 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 action.mFunction = attr->value();
250 action.mArg = child->value();
251 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 child = child->next_sibling("action");
254 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600257 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (child)
259 {
260 attr = child->first_attribute("key");
261 if (attr)
262 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100263 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600264 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100265 {
266 const int key = getKeyByName(keys[i]);
267 mKeys[key] = false;
268 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 }
270 else
271 {
272 attr = child->first_attribute("x");
273 if (!attr) return;
274 mActionX = atol(attr->value());
275 attr = child->first_attribute("y");
276 if (!attr) return;
277 mActionY = atol(attr->value());
278 attr = child->first_attribute("w");
279 if (!attr) return;
280 mActionW = atol(attr->value());
281 attr = child->first_attribute("h");
282 if (!attr) return;
283 mActionH = atol(attr->value());
284 }
285 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400286}
287
Matt Mower25036b72016-06-24 12:30:18 -0500288int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400289{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 if (state == TOUCH_RELEASE)
291 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100296int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400297{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100298 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600299 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100300 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100301
302 bool prevState = itr->second;
303 itr->second = down;
304
305 // If there is only one key for this action, wait for key up so it
306 // doesn't trigger with multi-key actions.
307 // Else, check if all buttons are pressed, then consume their release events
308 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600309 if (mKeys.size() == 1) {
310 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100311 doActions();
thatd4725ca2016-01-19 00:15:21 +0100312 return 0;
313 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600314 } else if (down) {
315 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
316 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100317 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100318 }
319
320 // Passed, all req buttons are pressed, reset them and consume release events
321 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600322 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100323 kb->ConsumeKeyRelease(itr->first);
324 itr->second = false;
325 }
326
327 doActions();
thatd4725ca2016-01-19 00:15:21 +0100328 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100329 }
330
thatd4725ca2016-01-19 00:15:21 +0100331 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400332}
333
Vojtech Bocek07220562014-02-08 02:05:33 +0100334int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400335{
Vojtech Bocek07220562014-02-08 02:05:33 +0100336 GUIObject::NotifyVarChange(varName, value);
337
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100338 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600340 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200341 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400342
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200343 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
346void GUIAction::simulate_progress_bar(void)
347{
Ethan Yonker74db1572015-10-28 12:44:49 -0500348 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 for (int i = 0; i < 5; i++)
350 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500351 if (PartitionManager.stop_backup.get_value()) {
352 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600353 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500354 DataManager::SetValue("ui_progress", 0);
355 PartitionManager.stop_backup.set_value(0);
356 return;
357 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400358 usleep(500000);
359 DataManager::SetValue("ui_progress", i * 20);
360 }
361}
362
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600363int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400364{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366
367 DataManager::SetValue("ui_progress", 0);
368
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 if (filename.empty())
370 {
371 LOGERR("No file specified.\n");
372 return -1;
373 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400374
Ethan Yonker9598c072016-01-15 21:54:34 -0600375 if (!TWFunc::Path_Exists(filename)) {
376 if (!PartitionManager.Mount_By_Path(filename, true)) {
377 return -1;
378 }
379 if (!TWFunc::Path_Exists(filename)) {
380 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
381 return -1;
382 }
383 }
Dees_Troy657c3092012-09-10 20:32:10 -0400384
Dees_Troy51a0e822012-09-05 15:24:24 -0400385 if (simulate) {
386 simulate_progress_bar();
387 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400388 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400389
390 // Now, check if we need to ensure TWRP remains installed...
391 struct stat st;
392 if (stat("/sbin/installTwrp", &st) == 0)
393 {
394 DataManager::SetValue("tw_operation", "Configuring TWRP");
395 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500396 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200397 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500399 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 }
401 }
402 }
403
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 // Done
405 DataManager::SetValue("ui_progress", 100);
406 DataManager::SetValue("ui_progress", 0);
407 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400408}
409
that73a52952015-01-28 23:07:34 +0100410GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100411{
that73a52952015-01-28 23:07:34 +0100412 string func = gui_parse_text(action.mFunction);
413 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
414 if (needsThread) {
415 if (func == "cancelbackup")
416 return THREAD_CANCEL;
417 else
418 return THREAD_ACTION;
419 }
420 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100421}
422
Dees_Troy51a0e822012-09-05 15:24:24 -0400423int GUIAction::doActions()
424{
that3f7b1ac2014-12-30 11:30:13 +0100425 if (mActions.size() < 1)
426 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427
that73a52952015-01-28 23:07:34 +0100428 // Determine in which thread to run the actions.
429 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
430 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100431 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100432 for (it = mActions.begin(); it != mActions.end(); ++it) {
433 ThreadType tt = getThreadType(*it);
434 if (tt == THREAD_NONE)
435 continue;
436 if (threadType == THREAD_NONE)
437 threadType = tt;
438 else if (threadType != tt) {
439 LOGERR("Can't mix normal and cancel actions in the same list.\n"
440 "Running the whole batch in the cancel thread.\n");
441 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100442 break;
443 }
that7d3b54f2015-01-09 22:52:51 +0100444 }
that73a52952015-01-28 23:07:34 +0100445
446 // Now run the actions in the desired thread.
447 switch (threadType) {
448 case THREAD_ACTION:
449 action_thread.threadActions(this);
450 break;
451
452 case THREAD_CANCEL:
453 cancel_thread.threadActions(this);
454 break;
455
456 default: {
457 // no iterators here because theme reloading might kill our object
458 const size_t cnt = mActions.size();
459 for (size_t i = 0; i < cnt; ++i)
460 doAction(mActions[i]);
461 }
thatc6085482015-01-09 22:12:43 +0100462 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400463
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200464 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400465}
466
that3f7b1ac2014-12-30 11:30:13 +0100467int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400468{
that3f7b1ac2014-12-30 11:30:13 +0100469 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400470
that3f7b1ac2014-12-30 11:30:13 +0100471 std::string function = gui_parse_text(action.mFunction);
472 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473
that3f7b1ac2014-12-30 11:30:13 +0100474 // find function and execute it
475 mapFunc::const_iterator funcitr = mf.find(function);
476 if (funcitr != mf.end())
477 return (this->*funcitr->second)(arg);
478
479 LOGERR("Unknown action '%s'\n", function.c_str());
480 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481}
482
483void GUIAction::operation_start(const string operation_name)
484{
that3f7b1ac2014-12-30 11:30:13 +0100485 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000486 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 DataManager::SetValue(TW_ACTION_BUSY, 1);
488 DataManager::SetValue("ui_progress", 0);
489 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400490 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600491 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492}
493
that3f7b1ac2014-12-30 11:30:13 +0100494void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400495{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000496 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 DataManager::SetValue("ui_progress", 100);
499 if (simulate) {
500 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
501 if (simulate_fail != 0)
502 DataManager::SetValue("tw_operation_status", 1);
503 else
504 DataManager::SetValue("tw_operation_status", 0);
505 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500506 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400507 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500508 }
509 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500511 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 }
513 DataManager::SetValue("tw_operation_state", 1);
514 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500515 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200516 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000517 time(&Stop);
518 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600519 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100520 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400521}
522
that3f7b1ac2014-12-30 11:30:13 +0100523int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400524{
that3f7b1ac2014-12-30 11:30:13 +0100525 sync();
526 DataManager::SetValue("tw_gui_done", 1);
527 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400528
that3f7b1ac2014-12-30 11:30:13 +0100529 return 0;
530}
Dees_Troy51a0e822012-09-05 15:24:24 -0400531
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500532int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100533{
that3f7b1ac2014-12-30 11:30:13 +0100534 gui_changePage("main");
535 return 0;
536}
Dees_Troy51a0e822012-09-05 15:24:24 -0400537
that3f7b1ac2014-12-30 11:30:13 +0100538int GUIAction::key(std::string arg)
539{
540 const int key = getKeyByName(arg);
541 PageManager::NotifyKey(key, true);
542 PageManager::NotifyKey(key, false);
543 return 0;
544}
545
546int GUIAction::page(std::string arg)
547{
LuK133762326f42015-09-30 19:57:46 +0200548 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100549 std::string page_name = gui_parse_text(arg);
550 return gui_changePage(page_name);
551}
552
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500553int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100554{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500555 PageManager::RequestReload();
556 // The actual reload is handled in pages.cpp in PageManager::RunReload()
557 // The reload will occur on the next Update or Render call and will
558 // be performed in the rendoer thread instead of the action thread
559 // to prevent crashing which could occur when we start deleting
560 // GUI resources in the action thread while we attempt to render
561 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100562 return 0;
563}
Dees_Troy51a0e822012-09-05 15:24:24 -0400564
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500565int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100566{
567 string Restore_Name;
568 DataManager::GetValue("tw_restore", Restore_Name);
569 PartitionManager.Set_Restore_Files(Restore_Name);
570 return 0;
571}
572
573int GUIAction::set(std::string arg)
574{
575 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200576 {
that3f7b1ac2014-12-30 11:30:13 +0100577 string varName = arg.substr(0, arg.find('='));
578 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
that3f7b1ac2014-12-30 11:30:13 +0100580 DataManager::GetValue(value, value);
581 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200582 }
that3f7b1ac2014-12-30 11:30:13 +0100583 else
584 DataManager::SetValue(arg, "1");
585 return 0;
586}
Dees_Troy51a0e822012-09-05 15:24:24 -0400587
that3f7b1ac2014-12-30 11:30:13 +0100588int GUIAction::clear(std::string arg)
589{
590 DataManager::SetValue(arg, "0");
591 return 0;
592}
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
that3f7b1ac2014-12-30 11:30:13 +0100594int GUIAction::mount(std::string arg)
595{
596 if (arg == "usb") {
597 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100599 PartitionManager.usb_storage_enable();
600 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500601 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100602 } else if (!simulate) {
603 PartitionManager.Mount_By_Path(arg, true);
604 PartitionManager.Add_MTP_Storage(arg);
605 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100607 return 0;
608}
609
610int GUIAction::unmount(std::string arg)
611{
612 if (arg == "usb") {
613 if (!simulate)
614 PartitionManager.usb_storage_disable();
615 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500616 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100617 DataManager::SetValue(TW_ACTION_BUSY, 0);
618 } else if (!simulate) {
619 PartitionManager.UnMount_By_Path(arg, true);
620 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500621 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100622 return 0;
623}
624
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500625int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100626{
627 operation_start("Restore Defaults");
628 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500629 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100630 else {
631 DataManager::ResetDefaults();
632 PartitionManager.Update_System_Details();
633 PartitionManager.Mount_Current_Storage(true);
634 }
635 operation_end(0);
636 return 0;
637}
638
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500639int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100640{
641 operation_start("Copy Log");
642 if (!simulate)
643 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400644 string dst, curr_storage;
645 int copy_kernel_log = 0;
646
647 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100648 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400649 curr_storage = DataManager::GetCurrentStoragePath();
650 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100651 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
652 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400653 if (copy_kernel_log)
654 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100655 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400656 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100657 } else
658 simulate_progress_bar();
659 operation_end(0);
660 return 0;
661}
662
663
664int GUIAction::compute(std::string arg)
665{
666 if (arg.find("+") != string::npos)
667 {
668 string varName = arg.substr(0, arg.find('+'));
669 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
670 int amount_to_add = atoi(string_to_add.c_str());
671 int value;
672
673 DataManager::GetValue(varName, value);
674 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 return 0;
676 }
that3f7b1ac2014-12-30 11:30:13 +0100677 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400678 {
that3f7b1ac2014-12-30 11:30:13 +0100679 string varName = arg.substr(0, arg.find('-'));
680 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
681 int amount_to_subtract = atoi(string_to_subtract.c_str());
682 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400683
that3f7b1ac2014-12-30 11:30:13 +0100684 DataManager::GetValue(varName, value);
685 value -= amount_to_subtract;
686 if (value <= 0)
687 value = 0;
688 DataManager::SetValue(varName, value);
689 return 0;
690 }
691 if (arg.find("*") != string::npos)
692 {
693 string varName = arg.substr(0, arg.find('*'));
694 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
695 int multiply_by = atoi(multiply_by_str.c_str());
696 int value;
697
698 DataManager::GetValue(varName, value);
699 DataManager::SetValue(varName, value*multiply_by);
700 return 0;
701 }
702 if (arg.find("/") != string::npos)
703 {
704 string varName = arg.substr(0, arg.find('/'));
705 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
706 int divide_by = atoi(divide_by_str.c_str());
707 int value;
708
Matt Mowera8a89d12016-12-30 18:10:37 -0600709 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100710 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100712 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 }
714 return 0;
715 }
that3f7b1ac2014-12-30 11:30:13 +0100716 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
717 return -1;
718}
Dees_Troy51a0e822012-09-05 15:24:24 -0400719
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500720int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100721{
722 string SelectedZone;
723 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
724 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
725 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
726
727 int dst;
728 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
729
730 string offset;
731 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
732
733 string NewTimeZone = Zone;
734 if (offset != "0")
735 NewTimeZone += ":" + offset;
736
737 if (dst != 0)
738 NewTimeZone += DSTZone;
739
740 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
741 DataManager::update_tz_environment_variables();
742 return 0;
743}
744
745int GUIAction::overlay(std::string arg)
746{
747 return gui_changeOverlay(arg);
748}
749
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500750int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100751{
752 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400754 return 0;
755 }
that3f7b1ac2014-12-30 11:30:13 +0100756 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
757 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
758 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400759 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100760 }
761 return 0;
762}
763
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500764int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100765{
766 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500767 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100769 } else {
770 zip_queue_index--;
771 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 }
that3f7b1ac2014-12-30 11:30:13 +0100773 return 0;
774}
Dees_Troy51a0e822012-09-05 15:24:24 -0400775
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500776int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100777{
778 zip_queue_index = 0;
779 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
780 return 0;
781}
Dees_Troy51a0e822012-09-05 15:24:24 -0400782
that3f7b1ac2014-12-30 11:30:13 +0100783int GUIAction::sleep(std::string arg)
784{
785 operation_start("Sleep");
786 usleep(atoi(arg.c_str()));
787 operation_end(0);
788 return 0;
789}
Dees Troyb21cc642013-09-10 17:36:41 +0000790
Matt Mower9a2a2052016-05-31 21:31:22 -0500791int GUIAction::sleepcounter(std::string arg)
792{
793 operation_start("SleepCounter");
794 // Ensure user notices countdown in case it needs to be cancelled
795 blankTimer.resetTimerAndUnblank();
796 int total = atoi(arg.c_str());
797 for (int t = total; t > 0; t--) {
798 int progress = (int)(((float)(total-t)/(float)total)*100.0);
799 DataManager::SetValue("ui_progress", progress);
800 ::sleep(1);
801 DataManager::SetValue("tw_sleep", t-1);
802 }
803 DataManager::SetValue("ui_progress", 100);
804 operation_end(0);
805 return 0;
806}
807
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500808int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100809{
810 operation_start("AppendDateToBackupName");
811 string Backup_Name;
812 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
813 Backup_Name += TWFunc::Get_Current_Date();
814 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
815 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
816 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500817 PageManager::NotifyKey(KEY_END, true);
818 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100819 operation_end(0);
820 return 0;
821}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500822
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500823int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100824{
825 operation_start("GenerateBackupName");
826 TWFunc::Auto_Generate_Backup_Name();
827 operation_end(0);
828 return 0;
829}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500830
Ethan Yonker483e9f42016-01-11 22:21:18 -0600831int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100832{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600833 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100834 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500835
Ethan Yonker483e9f42016-01-11 22:21:18 -0600836 if (arg.empty())
837 arg = "tw_wipe_list";
838 DataManager::GetValue(arg, List);
839 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
840 if (!List.empty()) {
841 size_t start_pos = 0, end_pos = List.find(";", start_pos);
842 while (end_pos != string::npos && start_pos < List.size()) {
843 part_path = List.substr(start_pos, end_pos - start_pos);
844 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
845 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100846 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400847 } else {
that3f7b1ac2014-12-30 11:30:13 +0100848 count++;
849 }
850 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600851 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100852 }
853 DataManager::SetValue("tw_check_partition_list", count);
854 } else {
855 DataManager::SetValue("tw_check_partition_list", 0);
856 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600857 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100858}
Dees_Troy51a0e822012-09-05 15:24:24 -0400859
Ethan Yonker483e9f42016-01-11 22:21:18 -0600860int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100861{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600862 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400863
Ethan Yonker483e9f42016-01-11 22:21:18 -0600864 if (arg.empty())
865 arg = "tw_wipe_list";
866 DataManager::GetValue(arg, List);
867 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
868 if (!List.empty()) {
869 size_t start_pos = 0, end_pos = List.find(";", start_pos);
870 part_path = List;
871 while (end_pos != string::npos && start_pos < List.size()) {
872 part_path = List.substr(start_pos, end_pos - start_pos);
873 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
874 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100875 // Do nothing
876 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600877 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100878 break;
879 }
880 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100882 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 if (!part_path.empty()) {
884 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100885 if (Part) {
886 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500887
that3f7b1ac2014-12-30 11:30:13 +0100888 DataManager::SetValue("tw_partition_name", Part->Display_Name);
889 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
890 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
891 DataManager::SetValue("tw_partition_size", Part->Size / mb);
892 DataManager::SetValue("tw_partition_used", Part->Used / mb);
893 DataManager::SetValue("tw_partition_free", Part->Free / mb);
894 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
895 DataManager::SetValue("tw_partition_removable", Part->Removable);
896 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
897
898 if (Part->Can_Repair())
899 DataManager::SetValue("tw_partition_can_repair", 1);
900 else
901 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500902 if (Part->Can_Resize())
903 DataManager::SetValue("tw_partition_can_resize", 1);
904 else
905 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600906 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100907 DataManager::SetValue("tw_partition_vfat", 1);
908 else
909 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600910 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100911 DataManager::SetValue("tw_partition_exfat", 1);
912 else
913 DataManager::SetValue("tw_partition_exfat", 0);
914 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
915 DataManager::SetValue("tw_partition_f2fs", 1);
916 else
917 DataManager::SetValue("tw_partition_f2fs", 0);
918 if (TWFunc::Path_Exists("/sbin/mke2fs"))
919 DataManager::SetValue("tw_partition_ext", 1);
920 else
921 DataManager::SetValue("tw_partition_ext", 0);
922 return 0;
923 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600924 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100925 }
926 }
927 }
928 DataManager::SetValue("tw_partition_name", "");
929 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600930 // Set this to 0 to prevent trying to partition this device, just in case
931 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100932 return 0;
933}
934
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500935int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100936{
937 time_t tm;
938 char path[256];
939 int path_len;
940 uid_t uid = -1;
941 gid_t gid = -1;
942
943 struct passwd *pwd = getpwnam("media_rw");
Matt Mowera8a89d12016-12-30 18:10:37 -0600944 if (pwd) {
that3f7b1ac2014-12-30 11:30:13 +0100945 uid = pwd->pw_uid;
946 gid = pwd->pw_gid;
947 }
948
949 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600950 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100951 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
952 } else {
953 strcpy(path, "/tmp/");
954 }
955
Matt Mowera8a89d12016-12-30 18:10:37 -0600956 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100957 return 0;
958
959 tm = time(NULL);
960 path_len = strlen(path);
961
962 // Screenshot_2014-01-01-18-21-38.png
963 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
964
965 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600966 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100967 chmod(path, 0666);
968 chown(path, uid, gid);
969
that677b13f2015-12-26 23:57:31 +0100970 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100971
972 // blink to notify that the screenshow was taken
973 gr_color(255, 255, 255, 255);
974 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
975 gr_flip();
976 gui_forceRender();
977 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500978 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100979 }
980 return 0;
981}
982
983int GUIAction::setbrightness(std::string arg)
984{
985 return TWFunc::Set_Brightness(arg);
986}
987
988int GUIAction::fileexists(std::string arg)
989{
990 struct stat st;
991 string newpath = arg + "/.";
992
993 operation_start("FileExists");
994 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
995 operation_end(0);
996 else
997 operation_end(1);
998 return 0;
999}
1000
thatcc8ddca2015-01-03 01:59:36 +01001001void GUIAction::reinject_after_flash()
1002{
1003 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001004 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001005 if (simulate) {
1006 simulate_progress_bar();
1007 } else {
1008 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1009 if (Boot == NULL || Boot->Current_File_System != "emmc")
1010 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1011 else {
1012 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1013 TWFunc::Exec_Cmd(injectcmd);
1014 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001015 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001016 }
1017 }
1018}
1019
that3f7b1ac2014-12-30 11:30:13 +01001020int GUIAction::flash(std::string arg)
1021{
1022 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001023 // We're going to jump to this page first, like a loading page
1024 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001025 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001026 string zip_path = zip_queue[i];
1027 size_t slashpos = zip_path.find_last_of('/');
1028 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001029 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001030 DataManager::SetValue("tw_filename", zip_path);
1031 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001032 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1033
1034 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001035 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001036 TWFunc::SetPerformanceMode(false);
1037 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001038 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001039 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001040 break;
that3f7b1ac2014-12-30 11:30:13 +01001041 }
1042 }
1043 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001044
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001045 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001046 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001047 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001048 }
that3f7b1ac2014-12-30 11:30:13 +01001049
thatcc8ddca2015-01-03 01:59:36 +01001050 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001051 PartitionManager.Update_System_Details();
1052 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001053 // This needs to be after the operation_end call so we change pages before we change variables that we display on the screen
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001054 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001055 return 0;
1056}
1057
1058int GUIAction::wipe(std::string arg)
1059{
1060 operation_start("Format");
1061 DataManager::SetValue("tw_partition", arg);
1062
1063 int ret_val = false;
1064
1065 if (simulate) {
1066 simulate_progress_bar();
1067 } else {
1068 if (arg == "data")
1069 ret_val = PartitionManager.Factory_Reset();
1070 else if (arg == "battery")
1071 ret_val = PartitionManager.Wipe_Battery_Stats();
1072 else if (arg == "rotate")
1073 ret_val = PartitionManager.Wipe_Rotate_Data();
1074 else if (arg == "dalvik")
1075 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1076 else if (arg == "DATAMEDIA") {
1077 ret_val = PartitionManager.Format_Data();
1078 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001079 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001080
1081 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1082 if (has_datamedia) {
1083 ret_val = PartitionManager.Wipe_Media_From_Data();
1084 } else {
1085 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1086 }
1087 } else if (arg == "EXTERNAL") {
1088 string External_Path;
1089
1090 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1091 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1092 } else if (arg == "ANDROIDSECURE") {
1093 ret_val = PartitionManager.Wipe_Android_Secure();
1094 } else if (arg == "LIST") {
1095 string Wipe_List, wipe_path;
1096 bool skip = false;
1097 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001098
1099 DataManager::GetValue("tw_wipe_list", Wipe_List);
1100 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1101 if (!Wipe_List.empty()) {
1102 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1103 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1104 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1105 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1106 if (wipe_path == "/and-sec") {
1107 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001108 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001109 ret_val = false;
1110 break;
1111 } else {
1112 skip = true;
1113 }
1114 } else if (wipe_path == "DALVIK") {
1115 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001116 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001117 ret_val = false;
1118 break;
1119 } else {
1120 skip = true;
1121 }
1122 } else if (wipe_path == "INTERNAL") {
1123 if (!PartitionManager.Wipe_Media_From_Data()) {
1124 ret_val = false;
1125 break;
1126 } else {
1127 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001128 }
1129 }
that3f7b1ac2014-12-30 11:30:13 +01001130 if (!skip) {
1131 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001132 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001133 ret_val = false;
1134 break;
1135 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1136 arg = wipe_path;
1137 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 } else {
that3f7b1ac2014-12-30 11:30:13 +01001139 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 }
that3f7b1ac2014-12-30 11:30:13 +01001141 start_pos = end_pos + 1;
1142 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001143 }
1144 }
that3f7b1ac2014-12-30 11:30:13 +01001145 } else
1146 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001147#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001148 if (arg == DataManager::GetSettingsStoragePath()) {
1149 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1150 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001151
that3f7b1ac2014-12-30 11:30:13 +01001152 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1153 LOGINFO("Making TWRP folder and saving settings.\n");
1154 Storage_Path += "/TWRP";
1155 mkdir(Storage_Path.c_str(), 0777);
1156 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001157 } else {
that3f7b1ac2014-12-30 11:30:13 +01001158 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1159 }
1160 }
1161#endif
1162 }
1163 PartitionManager.Update_System_Details();
1164 if (ret_val)
1165 ret_val = 0; // 0 is success
1166 else
1167 ret_val = 1; // 1 is failure
1168 operation_end(ret_val);
1169 return 0;
1170}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001171
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001172int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001173{
1174 operation_start("Refreshing Sizes");
1175 if (simulate) {
1176 simulate_progress_bar();
1177 } else
1178 PartitionManager.Update_System_Details();
1179 operation_end(0);
1180 return 0;
1181}
1182
1183int GUIAction::nandroid(std::string arg)
1184{
that3f7b1ac2014-12-30 11:30:13 +01001185 if (simulate) {
that73a52952015-01-28 23:07:34 +01001186 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001187 DataManager::SetValue("tw_partition", "Simulation");
1188 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001189 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001190 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001191 operation_start("Nandroid");
1192 int ret = 0;
1193
that3f7b1ac2014-12-30 11:30:13 +01001194 if (arg == "backup") {
1195 string Backup_Name;
1196 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001197 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1198 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001199 ret = PartitionManager.Run_Backup(false);
1200 } else {
that3f7b1ac2014-12-30 11:30:13 +01001201 operation_end(1);
1202 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001203 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001204 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001205 } else if (arg == "restore") {
1206 string Restore_Name;
1207 DataManager::GetValue("tw_restore", Restore_Name);
1208 ret = PartitionManager.Run_Restore(Restore_Name);
1209 } else {
1210 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001211 return -1;
1212 }
1213 DataManager::SetValue("tw_encrypt_backup", 0);
1214 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 if (ret == false)
1216 ret = 1; // 1 for failure
1217 else
1218 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001219 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001220 }
1221 else {
1222 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001223 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001224 ret = 0;
1225 }
that73a52952015-01-28 23:07:34 +01001226 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001227 return ret;
1228 }
1229 return 0;
1230}
1231
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001232int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001233 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001234 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001235 }
1236 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001237 int op_status = PartitionManager.Cancel_Backup();
1238 if (op_status != 0)
1239 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001240 }
1241
1242 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001243}
Dees_Troy51a0e822012-09-05 15:24:24 -04001244
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001245int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001246{
that73a52952015-01-28 23:07:34 +01001247 int op_status = 0;
1248
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001249 operation_start("Fix Contexts");
1250 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001251 if (simulate) {
1252 simulate_progress_bar();
1253 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001254 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001255 if (op_status != 0)
1256 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001257 }
that73a52952015-01-28 23:07:34 +01001258 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001259 return 0;
1260}
1261
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001262int GUIAction::fixpermissions(std::string arg)
1263{
1264 return fixcontexts(arg);
1265}
1266
that3f7b1ac2014-12-30 11:30:13 +01001267int GUIAction::dd(std::string arg)
1268{
1269 operation_start("imaging");
1270
1271 if (simulate) {
1272 simulate_progress_bar();
1273 } else {
1274 string cmd = "dd " + arg;
1275 TWFunc::Exec_Cmd(cmd);
1276 }
1277 operation_end(0);
1278 return 0;
1279}
1280
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001281int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001282{
1283 operation_start("Partition SD Card");
1284 int ret_val = 0;
1285
1286 if (simulate) {
1287 simulate_progress_bar();
1288 } else {
1289 int allow_partition;
1290 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1291 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001293 } else {
1294 if (!PartitionManager.Partition_SDCard())
1295 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001296 }
that3f7b1ac2014-12-30 11:30:13 +01001297 }
1298 operation_end(ret_val);
1299 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
that3f7b1ac2014-12-30 11:30:13 +01001301}
Dees_Troy51a0e822012-09-05 15:24:24 -04001302
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001303int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001304{
1305 operation_start("Install HTC Dumlock");
1306 if (simulate) {
1307 simulate_progress_bar();
1308 } else
1309 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001310
that3f7b1ac2014-12-30 11:30:13 +01001311 operation_end(0);
1312 return 0;
1313}
Dees_Troy51a0e822012-09-05 15:24:24 -04001314
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001315int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001316{
1317 operation_start("HTC Dumlock Restore Boot");
1318 if (simulate) {
1319 simulate_progress_bar();
1320 } else
1321 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001322
that3f7b1ac2014-12-30 11:30:13 +01001323 operation_end(0);
1324 return 0;
1325}
Dees_Troy51a0e822012-09-05 15:24:24 -04001326
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001327int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001328{
1329 operation_start("HTC Dumlock Reflash Recovery");
1330 if (simulate) {
1331 simulate_progress_bar();
1332 } else
1333 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001334
that3f7b1ac2014-12-30 11:30:13 +01001335 operation_end(0);
1336 return 0;
1337}
Dees_Troy51a0e822012-09-05 15:24:24 -04001338
that3f7b1ac2014-12-30 11:30:13 +01001339int GUIAction::cmd(std::string arg)
1340{
1341 int op_status = 0;
1342
1343 operation_start("Command");
1344 LOGINFO("Running command: '%s'\n", arg.c_str());
1345 if (simulate) {
1346 simulate_progress_bar();
1347 } else {
1348 op_status = TWFunc::Exec_Cmd(arg);
1349 if (op_status != 0)
1350 op_status = 1;
1351 }
1352
1353 operation_end(op_status);
1354 return 0;
1355}
1356
1357int GUIAction::terminalcommand(std::string arg)
1358{
1359 int op_status = 0;
1360 string cmdpath, command;
1361
1362 DataManager::GetValue("tw_terminal_location", cmdpath);
1363 operation_start("CommandOutput");
1364 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1365 if (simulate) {
1366 simulate_progress_bar();
1367 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001368 } else if (arg == "exit") {
1369 LOGINFO("Exiting terminal\n");
1370 operation_end(op_status);
1371 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001372 } else {
1373 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1374 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001375 DataManager::SetValue("tw_terminal_state", 1);
1376 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001377 FILE* fp;
1378 char line[512];
1379
1380 fp = popen(command.c_str(), "r");
1381 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001382 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001383 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001384 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001385 struct timeval timeout;
1386 fd_set fdset;
1387
Matt Mowera8a89d12016-12-30 18:10:37 -06001388 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001389 {
1390 FD_ZERO(&fdset);
1391 FD_SET(fd, &fdset);
1392 timeout.tv_sec = 0;
1393 timeout.tv_usec = 400000;
1394 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1395 if (has_data == 0) {
1396 // Timeout reached
1397 DataManager::GetValue("tw_terminal_state", check);
1398 if (check == 0) {
1399 keep_going = 0;
1400 }
1401 } else if (has_data < 0) {
1402 // End of execution
1403 keep_going = 0;
1404 } else {
1405 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001406 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001407 gui_print("%s", line); // Display output
1408 else
1409 keep_going = 0; // Done executing
1410 }
1411 }
1412 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001413 }
thatc6085482015-01-09 22:12:43 +01001414 DataManager::SetValue("tw_operation_status", 0);
1415 DataManager::SetValue("tw_operation_state", 1);
1416 DataManager::SetValue("tw_terminal_state", 0);
1417 DataManager::SetValue("tw_background_thread_running", 0);
1418 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001419 }
1420 return 0;
1421}
1422
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001423int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001424{
that3f7b1ac2014-12-30 11:30:13 +01001425 LOGINFO("Sending kill command...\n");
1426 operation_start("KillCommand");
1427 DataManager::SetValue("tw_operation_status", 0);
1428 DataManager::SetValue("tw_operation_state", 1);
1429 DataManager::SetValue("tw_terminal_state", 0);
1430 DataManager::SetValue("tw_background_thread_running", 0);
1431 DataManager::SetValue(TW_ACTION_BUSY, 0);
1432 return 0;
1433}
1434
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001435int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001436{
1437 int op_status = 0;
1438 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001439 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001440 if (simulate) {
1441 simulate_progress_bar();
1442 } else {
1443 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001445 }
1446
1447 operation_end(op_status);
1448 return 0;
1449}
1450
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001451int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001452{
1453 int op_status = 0;
1454
1455 operation_start("CheckBackupName");
1456 if (simulate) {
1457 simulate_progress_bar();
1458 } else {
1459 op_status = PartitionManager.Check_Backup_Name(true);
1460 if (op_status != 0)
1461 op_status = 1;
1462 }
1463
1464 operation_end(op_status);
1465 return 0;
1466}
1467
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001468int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001469{
1470 int op_status = 0;
1471
1472 operation_start("Decrypt");
1473 if (simulate) {
1474 simulate_progress_bar();
1475 } else {
1476 string Password;
1477 DataManager::GetValue("tw_crypto_password", Password);
1478 op_status = PartitionManager.Decrypt_Device(Password);
1479 if (op_status != 0)
1480 op_status = 1;
1481 else {
that3f7b1ac2014-12-30 11:30:13 +01001482
1483 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1484
Ethan Yonkercf50da52015-01-12 21:59:07 -06001485 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001486
Ethan Yonkercf50da52015-01-12 21:59:07 -06001487 // Check for a custom theme and load it if exists
1488 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1489 if (has_datamedia != 0) {
1490 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001491 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001492 } else {
1493 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001494 }
1495 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001496 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001497 }
1498 }
1499
1500 operation_end(op_status);
1501 return 0;
1502}
1503
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001504int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001505{
1506 operation_start("Sideload");
1507 if (simulate) {
1508 simulate_progress_bar();
1509 operation_end(0);
1510 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001511 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001512 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1513
1514 // wait for the adb connection
1515 int ret = apply_from_adb("/", &sideload_child_pid);
1516 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1517
1518 if (ret != 0) {
1519 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001520 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001521 ret = 1; // failure
1522 } else {
1523 int wipe_cache = 0;
1524 int wipe_dalvik = 0;
1525 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1526
1527 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1528 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1529 PartitionManager.Wipe_By_Path("/cache");
1530 if (wipe_dalvik)
1531 PartitionManager.Wipe_Dalvik_Cache();
1532 } else {
1533 ret = 1; // failure
1534 }
thatcc8ddca2015-01-03 01:59:36 +01001535 }
thatc6085482015-01-09 22:12:43 +01001536 if (sideload_child_pid) {
1537 LOGINFO("Signaling child sideload process to exit.\n");
1538 struct stat st;
1539 // Calling stat() on this magic filename signals the minadbd
1540 // subprocess to shut down.
1541 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1542 int status;
1543 LOGINFO("Waiting for child sideload process to exit.\n");
1544 waitpid(sideload_child_pid, &status, 0);
1545 }
Dees Troy28a85d22015-04-28 02:03:16 +00001546 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001547 TWFunc::Toggle_MTP(mtp_was_enabled);
1548 reinject_after_flash();
1549 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001550 }
that3f7b1ac2014-12-30 11:30:13 +01001551 return 0;
1552}
1553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001554int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001555{
that3f7b1ac2014-12-30 11:30:13 +01001556 struct stat st;
1557 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001558 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001559 LOGINFO("Signaling child sideload process to exit.\n");
1560 // Calling stat() on this magic filename signals the minadbd
1561 // subprocess to shut down.
1562 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1563 if (!sideload_child_pid) {
1564 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001565 return 0;
1566 }
thatcc8ddca2015-01-03 01:59:36 +01001567 ::sleep(1);
1568 LOGINFO("Killing child sideload process.\n");
1569 kill(sideload_child_pid, SIGTERM);
1570 int status;
1571 LOGINFO("Waiting for child sideload process to exit.\n");
1572 waitpid(sideload_child_pid, &status, 0);
1573 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001574 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1575 return 0;
1576}
1577
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001578int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001579{
1580 operation_start("OpenRecoveryScript");
1581 if (simulate) {
1582 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001583 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001584 } else {
that10ae24f2015-12-26 20:53:51 +01001585 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001586 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001587 }
1588 return 0;
1589}
1590
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001591int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001592{
1593 int op_status = 0;
1594
1595 operation_start("Install SuperSU");
1596 if (simulate) {
1597 simulate_progress_bar();
1598 } else {
1599 if (!TWFunc::Install_SuperSU())
1600 op_status = 1;
1601 }
1602
1603 operation_end(op_status);
1604 return 0;
1605}
1606
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001607int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001608{
1609 int op_status = 0;
1610
1611 operation_start("Fixing Superuser Permissions");
1612 if (simulate) {
1613 simulate_progress_bar();
1614 } else {
1615 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1616 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1617 }
1618
1619 operation_end(op_status);
1620 return 0;
1621}
1622
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001623int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001624{
1625 int op_status = 0;
1626
1627 operation_start("Try Restore Decrypt");
1628 if (simulate) {
1629 simulate_progress_bar();
1630 } else {
1631 string Restore_Path, Filename, Password;
1632 DataManager::GetValue("tw_restore", Restore_Path);
1633 Restore_Path += "/";
1634 DataManager::GetValue("tw_restore_password", Password);
1635 TWFunc::SetPerformanceMode(true);
1636 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1637 op_status = 0; // success
1638 else
1639 op_status = 1; // fail
1640 TWFunc::SetPerformanceMode(false);
1641 }
1642
1643 operation_end(op_status);
1644 return 0;
1645}
1646
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001647int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001648{
1649 int op_status = 0;
1650
1651 operation_start("Repair Partition");
1652 if (simulate) {
1653 simulate_progress_bar();
1654 } else {
1655 string part_path;
1656 DataManager::GetValue("tw_partition_mount_point", part_path);
1657 if (PartitionManager.Repair_By_Path(part_path, true)) {
1658 op_status = 0; // success
1659 } else {
that3f7b1ac2014-12-30 11:30:13 +01001660 op_status = 1; // fail
1661 }
1662 }
1663
1664 operation_end(op_status);
1665 return 0;
1666}
1667
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001668int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001669{
1670 int op_status = 0;
1671
1672 operation_start("Resize Partition");
1673 if (simulate) {
1674 simulate_progress_bar();
1675 } else {
1676 string part_path;
1677 DataManager::GetValue("tw_partition_mount_point", part_path);
1678 if (PartitionManager.Resize_By_Path(part_path, true)) {
1679 op_status = 0; // success
1680 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001681 op_status = 1; // fail
1682 }
1683 }
1684
1685 operation_end(op_status);
1686 return 0;
1687}
1688
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001689int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001690{
1691 int op_status = 0;
1692
1693 operation_start("Change File System");
1694 if (simulate) {
1695 simulate_progress_bar();
1696 } else {
1697 string part_path, file_system;
1698 DataManager::GetValue("tw_partition_mount_point", part_path);
1699 DataManager::GetValue("tw_action_new_file_system", file_system);
1700 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1701 op_status = 0; // success
1702 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001703 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001704 op_status = 1; // fail
1705 }
1706 }
1707 PartitionManager.Update_System_Details();
1708 operation_end(op_status);
1709 return 0;
1710}
1711
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001712int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001713{
1714 int op_status = 0;
1715
1716 operation_start("Start MTP");
1717 if (PartitionManager.Enable_MTP())
1718 op_status = 0; // success
1719 else
1720 op_status = 1; // fail
1721
1722 operation_end(op_status);
1723 return 0;
1724}
1725
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001726int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001727{
1728 int op_status = 0;
1729
1730 operation_start("Stop MTP");
1731 if (PartitionManager.Disable_MTP())
1732 op_status = 0; // success
1733 else
1734 op_status = 1; // fail
1735
1736 operation_end(op_status);
1737 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001738}
1739
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001740int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001741{
1742 int op_status = 0;
1743
1744 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001745 string path, filename;
1746 DataManager::GetValue("tw_zip_location", path);
1747 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001748 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001749 op_status = 0; // success
1750 else
1751 op_status = 1; // fail
1752
1753 operation_end(op_status);
1754 return 0;
1755}
1756
that10ae24f2015-12-26 20:53:51 +01001757int GUIAction::twcmd(std::string arg)
1758{
1759 operation_start("TWRP CLI Command");
1760 if (simulate)
1761 simulate_progress_bar();
1762 else
1763 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1764 operation_end(0);
1765 return 0;
1766}
1767
Dees_Troy51a0e822012-09-05 15:24:24 -04001768int GUIAction::getKeyByName(std::string key)
1769{
that8834a0f2016-01-05 23:29:30 +01001770 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001771 else if (key == "menu") return KEY_MENU;
1772 else if (key == "back") return KEY_BACK;
1773 else if (key == "search") return KEY_SEARCH;
1774 else if (key == "voldown") return KEY_VOLUMEDOWN;
1775 else if (key == "volup") return KEY_VOLUMEUP;
1776 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001777 int ret_val;
1778 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1779 if (!ret_val)
1780 return KEY_POWER;
1781 else
1782 return ret_val;
1783 }
1784
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001785 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001786}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001787
1788int GUIAction::checkpartitionlifetimewrites(std::string arg)
1789{
1790 int op_status = 0;
1791 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1792
1793 operation_start("Check Partition Lifetime Writes");
1794 if (sys) {
1795 if (sys->Check_Lifetime_Writes() != 0)
1796 DataManager::SetValue("tw_lifetime_writes", 1);
1797 else
1798 DataManager::SetValue("tw_lifetime_writes", 0);
1799 op_status = 0; // success
1800 } else {
1801 DataManager::SetValue("tw_lifetime_writes", 1);
1802 op_status = 1; // fail
1803 }
1804
1805 operation_end(op_status);
1806 return 0;
1807}
1808
1809int GUIAction::mountsystemtoggle(std::string arg)
1810{
1811 int op_status = 0;
1812 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001813 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001814
1815 operation_start("Toggle System Mount");
1816 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1817 op_status = 1; // fail
1818 } else {
1819 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1820 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001821 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001822 DataManager::SetValue("tw_mount_system_ro", 0);
1823 Part->Change_Mount_Read_Only(false);
1824 } else {
1825 DataManager::SetValue("tw_mount_system_ro", 1);
1826 Part->Change_Mount_Read_Only(true);
1827 }
1828 if (remount_system) {
1829 Part->Mount(true);
1830 }
1831 op_status = 0; // success
1832 } else {
1833 op_status = 1; // fail
1834 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001835 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1836 if (Part) {
1837 if (arg == "0") {
1838 Part->Change_Mount_Read_Only(false);
1839 } else {
1840 Part->Change_Mount_Read_Only(true);
1841 }
1842 if (remount_vendor) {
1843 Part->Mount(true);
1844 }
1845 op_status = 0; // success
1846 } else {
1847 op_status = 1; // fail
1848 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001849 }
1850
1851 operation_end(op_status);
1852 return 0;
1853}
Ethan Yonker74db1572015-10-28 12:44:49 -05001854
1855int GUIAction::setlanguage(std::string arg __unused)
1856{
1857 int op_status = 0;
1858
1859 operation_start("Set Language");
1860 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1861 PageManager::RequestReload();
1862 op_status = 0; // success
1863
1864 operation_end(op_status);
1865 return 0;
1866}
Ethan Yonker1b190162016-12-05 15:25:19 -06001867
1868int GUIAction::setbootslot(std::string arg)
1869{
1870 operation_start("Set Boot Slot");
1871 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001872 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001873 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001874 simulate_progress_bar();
1875 operation_end(0);
1876 return 0;
1877}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001878
1879int GUIAction::checkforapp(std::string arg __unused)
1880{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001881 operation_start("Check for TWRP App");
1882 if (!simulate)
1883 {
1884 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1885 int sdkver = 0;
1886 if (!sdkverstr.empty()) {
1887 sdkver = atoi(sdkverstr.c_str());
1888 }
1889 if (sdkver <= 13) {
1890 if (sdkver == 0)
1891 LOGINFO("Unable to read sdk version from build prop\n");
1892 else
1893 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
1894 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1895 goto exit;
1896 }
1897 if (PartitionManager.Mount_By_Path("/system", false)) {
1898 string base_path = "/system";
1899 if (TWFunc::Path_Exists("/system/system"))
1900 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1901 string install_path = base_path + "/priv-app";
1902 if (!TWFunc::Path_Exists(install_path))
1903 install_path = base_path + "/app";
1904 install_path += "/twrpapp";
1905 if (TWFunc::Path_Exists(install_path)) {
1906 LOGINFO("App found at '%s'\n", install_path.c_str());
1907 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1908 goto exit;
1909 }
1910 } else if (PartitionManager.Mount_By_Path("/data", false)) {
1911 string parent_path = "/data/app";
1912 DIR *d = opendir("/data/app");
1913 struct dirent *p;
Ethan Yonker4767caf2017-01-11 10:45:04 -06001914 size_t len = strlen("me.twrp.twrpapp-");
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001915 while ((p = readdir(d))) {
1916 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
1917 continue;
1918 if (p->d_type == DT_DIR && strlen(p->d_name) >= len && strncmp(p->d_name, "me.twrp.twrpapp-", len) == 0) {
1919 LOGINFO("App found at %s/%s\n", parent_path.c_str(), p->d_name);
1920 closedir(d);
1921 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1922 goto exit;
1923 }
1924 }
1925 closedir(d);
1926 }
1927 } else
1928 simulate_progress_bar();
1929 LOGINFO("App not installed\n");
1930 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1931exit:
1932 operation_end(0);
1933 return 0;
1934}
1935
1936int GUIAction::installapp(std::string arg __unused)
1937{
1938 int op_status = 1;
1939 operation_start("Install TWRP App");
1940 if (!simulate)
1941 {
1942 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1943 if (PartitionManager.Mount_By_Path("/data", true)) {
1944 string install_path = "/data/app";
1945 string context = "u:object_r:apk_data_file:s0";
1946 if (!TWFunc::Path_Exists(install_path)) {
1947 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1948 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1949 goto exit;
1950 }
1951 if (chown(install_path.c_str(), 1000, 1000)) {
1952 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1953 goto exit;
1954 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001955 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001956 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1957 goto exit;
1958 }
1959 }
1960 install_path += "/me.twrp.twrpapp-1";
1961 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1962 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1963 goto exit;
1964 }
1965 if (chown(install_path.c_str(), 1000, 1000)) {
1966 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1967 goto exit;
1968 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001969 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001970 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1971 goto exit;
1972 }
1973 install_path += "/base.apk";
1974 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1975 LOGERR("Error copying apk file\n");
1976 goto exit;
1977 }
1978 if (chown(install_path.c_str(), 1000, 1000)) {
1979 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1980 goto exit;
1981 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001982 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001983 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1984 goto exit;
1985 }
1986 sync();
1987 sync();
1988 }
1989 } else {
1990 if (PartitionManager.Mount_By_Path("/system", true)) {
1991 string base_path = "/system";
1992 if (TWFunc::Path_Exists("/system/system"))
1993 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1994 string install_path = base_path + "/priv-app";
1995 string context = "u:object_r:system_file:s0";
1996 if (!TWFunc::Path_Exists(install_path))
1997 install_path = base_path + "/app";
1998 if (TWFunc::Path_Exists(install_path)) {
1999 install_path += "/twrpapp";
2000 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2001 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002002 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002003 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
2006 install_path += "/me.twrp.twrpapp.apk";
2007 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2008 LOGERR("Error copying apk file\n");
2009 goto exit;
2010 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002011 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002012 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2013 goto exit;
2014 }
2015 sync();
2016 sync();
2017 PartitionManager.UnMount_By_Path("/system", true);
2018 op_status = 0;
2019 } else {
2020 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2021 }
2022 }
2023 }
2024 }
2025 } else
2026 simulate_progress_bar();
2027exit:
2028 operation_end(0);
2029 return 0;
2030}