blob: 750d73b8d099c9f0358534418e837bba9e5ffaaf [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"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050046#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040048extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000049#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040052#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000053#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040054#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060055#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060056#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040057};
58
59#include "rapidxml.hpp"
60#include "objects.hpp"
61
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070062#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050063extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070064#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040065
Dees_Troy51a0e822012-09-05 15:24:24 -040066void curtainClose(void);
67
that3f7b1ac2014-12-30 11:30:13 +010068GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010069std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010070static string zip_queue[10];
71static int zip_queue_index;
72static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010073pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010074
thatc6085482015-01-09 22:12:43 +010075static ActionThread action_thread;
76
77static void *ActionThread_work_wrapper(void *data)
78{
79 action_thread.run(data);
80 return NULL;
81}
82
83ActionThread::ActionThread()
84{
85 m_thread_running = false;
86 pthread_mutex_init(&m_act_lock, NULL);
87}
88
89ActionThread::~ActionThread()
90{
91 pthread_mutex_lock(&m_act_lock);
92 if(m_thread_running) {
93 pthread_mutex_unlock(&m_act_lock);
94 pthread_join(m_thread, NULL);
95 } else {
96 pthread_mutex_unlock(&m_act_lock);
97 }
98 pthread_mutex_destroy(&m_act_lock);
99}
100
that7d3b54f2015-01-09 22:52:51 +0100101void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100102{
103 pthread_mutex_lock(&m_act_lock);
104 if (m_thread_running) {
105 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100106 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
107 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100108 } else {
109 m_thread_running = true;
110 pthread_mutex_unlock(&m_act_lock);
111 ThreadData *d = new ThreadData;
112 d->act = act;
thatc6085482015-01-09 22:12:43 +0100113
114 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
115 }
116}
117
118void ActionThread::run(void *data)
119{
120 ThreadData *d = (ThreadData*)data;
121 GUIAction* act = d->act;
122
123 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100124 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100125 act->doAction(*it);
126
127 pthread_mutex_lock(&m_act_lock);
128 m_thread_running = false;
129 pthread_mutex_unlock(&m_act_lock);
130 delete d;
131}
132
Dees_Troy51a0e822012-09-05 15:24:24 -0400133GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100134 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400135{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 xml_node<>* child;
137 xml_node<>* actions;
138 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141
that3f7b1ac2014-12-30 11:30:13 +0100142 if (mf.empty()) {
thatc6085482015-01-09 22:12:43 +0100143 // These actions will be run in the caller's thread
that3f7b1ac2014-12-30 11:30:13 +0100144 mf["reboot"] = &GUIAction::reboot;
145 mf["home"] = &GUIAction::home;
146 mf["key"] = &GUIAction::key;
147 mf["page"] = &GUIAction::page;
148 mf["reload"] = &GUIAction::reload;
149 mf["readBackup"] = &GUIAction::readBackup;
150 mf["set"] = &GUIAction::set;
151 mf["clear"] = &GUIAction::clear;
152 mf["mount"] = &GUIAction::mount;
153 mf["unmount"] = &GUIAction::unmount;
154 mf["umount"] = &GUIAction::unmount;
155 mf["restoredefaultsettings"] = &GUIAction::restoredefaultsettings;
156 mf["copylog"] = &GUIAction::copylog;
157 mf["compute"] = &GUIAction::compute;
158 mf["addsubtract"] = &GUIAction::compute;
159 mf["setguitimezone"] = &GUIAction::setguitimezone;
160 mf["overlay"] = &GUIAction::overlay;
161 mf["queuezip"] = &GUIAction::queuezip;
162 mf["cancelzip"] = &GUIAction::cancelzip;
163 mf["queueclear"] = &GUIAction::queueclear;
164 mf["sleep"] = &GUIAction::sleep;
165 mf["appenddatetobackupname"] = &GUIAction::appenddatetobackupname;
166 mf["generatebackupname"] = &GUIAction::generatebackupname;
167 mf["checkpartitionlist"] = &GUIAction::checkpartitionlist;
168 mf["getpartitiondetails"] = &GUIAction::getpartitiondetails;
169 mf["screenshot"] = &GUIAction::screenshot;
170 mf["setbrightness"] = &GUIAction::setbrightness;
that3f7b1ac2014-12-30 11:30:13 +0100171 mf["fileexists"] = &GUIAction::fileexists;
thatc6085482015-01-09 22:12:43 +0100172 mf["killterminal"] = &GUIAction::killterminal;
173 mf["checkbackupname"] = &GUIAction::checkbackupname;
174 mf["adbsideloadcancel"] = &GUIAction::adbsideloadcancel;
175 mf["fixsu"] = &GUIAction::fixsu;
176 mf["startmtp"] = &GUIAction::startmtp;
177 mf["stopmtp"] = &GUIAction::stopmtp;
178
179 // remember actions that run in the caller thread
180 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
181 setActionsRunningInCallerThread.insert(it->first);
182
183 // These actions will run in a separate thread
that3f7b1ac2014-12-30 11:30:13 +0100184 mf["flash"] = &GUIAction::flash;
185 mf["wipe"] = &GUIAction::wipe;
186 mf["refreshsizes"] = &GUIAction::refreshsizes;
187 mf["nandroid"] = &GUIAction::nandroid;
188 mf["fixpermissions"] = &GUIAction::fixpermissions;
189 mf["dd"] = &GUIAction::dd;
190 mf["partitionsd"] = &GUIAction::partitionsd;
191 mf["installhtcdumlock"] = &GUIAction::installhtcdumlock;
192 mf["htcdumlockrestoreboot"] = &GUIAction::htcdumlockrestoreboot;
193 mf["htcdumlockreflashrecovery"] = &GUIAction::htcdumlockreflashrecovery;
194 mf["cmd"] = &GUIAction::cmd;
195 mf["terminalcommand"] = &GUIAction::terminalcommand;
that3f7b1ac2014-12-30 11:30:13 +0100196 mf["reinjecttwrp"] = &GUIAction::reinjecttwrp;
that3f7b1ac2014-12-30 11:30:13 +0100197 mf["decrypt"] = &GUIAction::decrypt;
198 mf["adbsideload"] = &GUIAction::adbsideload;
that3f7b1ac2014-12-30 11:30:13 +0100199 mf["openrecoveryscript"] = &GUIAction::openrecoveryscript;
200 mf["installsu"] = &GUIAction::installsu;
that3f7b1ac2014-12-30 11:30:13 +0100201 mf["decrypt_backup"] = &GUIAction::decrypt_backup;
202 mf["repair"] = &GUIAction::repair;
203 mf["changefilesystem"] = &GUIAction::changefilesystem;
that3f7b1ac2014-12-30 11:30:13 +0100204 }
205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 // First, get the action
207 actions = node->first_node("actions");
208 if (actions) child = actions->first_node("action");
209 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 while (child)
214 {
215 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 attr = child->first_attribute("function");
218 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500219
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 action.mFunction = attr->value();
221 action.mArg = child->value();
222 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400223
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 child = child->next_sibling("action");
225 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 // Now, let's get either the key or region
228 child = node->first_node("touch");
229 if (child)
230 {
231 attr = child->first_attribute("key");
232 if (attr)
233 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100234 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
235 for(size_t i = 0; i < keys.size(); ++i)
236 {
237 const int key = getKeyByName(keys[i]);
238 mKeys[key] = false;
239 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 }
241 else
242 {
243 attr = child->first_attribute("x");
244 if (!attr) return;
245 mActionX = atol(attr->value());
246 attr = child->first_attribute("y");
247 if (!attr) return;
248 mActionY = atol(attr->value());
249 attr = child->first_attribute("w");
250 if (!attr) return;
251 mActionW = atol(attr->value());
252 attr = child->first_attribute("h");
253 if (!attr) return;
254 mActionH = atol(attr->value());
255 }
256 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400257}
258
259int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
260{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (state == TOUCH_RELEASE)
262 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265}
266
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100267int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400268{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100269 if (mKeys.empty())
270 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100272 std::map<int, bool>::iterator itr = mKeys.find(key);
273 if(itr == mKeys.end())
274 return 0;
275
276 bool prevState = itr->second;
277 itr->second = down;
278
279 // If there is only one key for this action, wait for key up so it
280 // doesn't trigger with multi-key actions.
281 // Else, check if all buttons are pressed, then consume their release events
282 // so they don't trigger one-button actions and reset mKeys pressed status
283 if(mKeys.size() == 1) {
284 if(!down && prevState)
285 doActions();
286 } else if(down) {
287 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
288 if(!itr->second)
289 return 0;
290 }
291
292 // Passed, all req buttons are pressed, reset them and consume release events
293 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
294 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
295 kb->ConsumeKeyRelease(itr->first);
296 itr->second = false;
297 }
298
299 doActions();
300 }
301
Dees_Troy51a0e822012-09-05 15:24:24 -0400302 return 0;
303}
304
Vojtech Bocek07220562014-02-08 02:05:33 +0100305int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400306{
Vojtech Bocek07220562014-02-08 02:05:33 +0100307 GUIObject::NotifyVarChange(varName, value);
308
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100309 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200310 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100311 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200314 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400315}
316
317void GUIAction::simulate_progress_bar(void)
318{
Dees_Troy2673cec2013-04-02 20:22:16 +0000319 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400320 for (int i = 0; i < 5; i++)
321 {
322 usleep(500000);
323 DataManager::SetValue("ui_progress", i * 20);
324 }
325}
326
that3f7b1ac2014-12-30 11:30:13 +0100327int GUIAction::flash_zip(std::string filename, std::string pageName, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400328{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200329 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400330
331 DataManager::SetValue("ui_progress", 0);
332
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 if (filename.empty())
334 {
335 LOGERR("No file specified.\n");
336 return -1;
337 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400338
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 // We're going to jump to this page first, like a loading page
340 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400343 return -1;
344
that3f7b1ac2014-12-30 11:30:13 +0100345 // TODO: why again?
Ethan Yonker57e35872014-11-07 14:52:22 -0600346 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
Dees_Troy51a0e822012-09-05 15:24:24 -0400348 if (simulate) {
349 simulate_progress_bar();
350 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400351 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400352
353 // Now, check if we need to ensure TWRP remains installed...
354 struct stat st;
355 if (stat("/sbin/installTwrp", &st) == 0)
356 {
357 DataManager::SetValue("tw_operation", "Configuring TWRP");
358 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200360 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400361 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 }
364 }
365 }
366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 // Done
368 DataManager::SetValue("ui_progress", 100);
369 DataManager::SetValue("ui_progress", 0);
370 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371}
372
thatc6085482015-01-09 22:12:43 +0100373bool GUIAction::needsToRunInSeparateThread(const GUIAction::Action& action)
374{
375 return setActionsRunningInCallerThread.find(action.mFunction) == setActionsRunningInCallerThread.end();
376}
377
Dees_Troy51a0e822012-09-05 15:24:24 -0400378int GUIAction::doActions()
379{
that3f7b1ac2014-12-30 11:30:13 +0100380 if (mActions.size() < 1)
381 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
that7d3b54f2015-01-09 22:52:51 +0100383 bool needThread = false;
that3f7b1ac2014-12-30 11:30:13 +0100384 std::vector<Action>::iterator it;
385 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100386 {
387 if (needsToRunInSeparateThread(*it))
388 {
that7d3b54f2015-01-09 22:52:51 +0100389 needThread = true;
thatc6085482015-01-09 22:12:43 +0100390 break;
391 }
that7d3b54f2015-01-09 22:52:51 +0100392 }
393 if (needThread)
394 {
395 action_thread.threadActions(this);
396 }
397 else
398 {
399 for (it = mActions.begin(); it != mActions.end(); ++it)
400 doAction(*it);
thatc6085482015-01-09 22:12:43 +0100401 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400402
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400404}
405
that3f7b1ac2014-12-30 11:30:13 +0100406int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400407{
that3f7b1ac2014-12-30 11:30:13 +0100408 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400409
that3f7b1ac2014-12-30 11:30:13 +0100410 std::string function = gui_parse_text(action.mFunction);
411 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400412
that3f7b1ac2014-12-30 11:30:13 +0100413 // find function and execute it
414 mapFunc::const_iterator funcitr = mf.find(function);
415 if (funcitr != mf.end())
416 return (this->*funcitr->second)(arg);
417
418 LOGERR("Unknown action '%s'\n", function.c_str());
419 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420}
421
422void GUIAction::operation_start(const string operation_name)
423{
that3f7b1ac2014-12-30 11:30:13 +0100424 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000425 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 DataManager::SetValue(TW_ACTION_BUSY, 1);
427 DataManager::SetValue("ui_progress", 0);
428 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400429 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600430 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400431}
432
that3f7b1ac2014-12-30 11:30:13 +0100433void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400434{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000435 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437 DataManager::SetValue("ui_progress", 100);
438 if (simulate) {
439 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
440 if (simulate_fail != 0)
441 DataManager::SetValue("tw_operation_status", 1);
442 else
443 DataManager::SetValue("tw_operation_status", 0);
444 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500445 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500447 }
448 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500450 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 }
452 DataManager::SetValue("tw_operation_state", 1);
453 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700454#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500455 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700456#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000457 time(&Stop);
458 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600459 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100460 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400461}
462
that3f7b1ac2014-12-30 11:30:13 +0100463int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400464{
that3f7b1ac2014-12-30 11:30:13 +0100465 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400466
that3f7b1ac2014-12-30 11:30:13 +0100467 sync();
468 DataManager::SetValue("tw_gui_done", 1);
469 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400470
that3f7b1ac2014-12-30 11:30:13 +0100471 return 0;
472}
Dees_Troy51a0e822012-09-05 15:24:24 -0400473
that3f7b1ac2014-12-30 11:30:13 +0100474int GUIAction::home(std::string arg)
475{
476 PageManager::SelectPackage("TWRP");
477 gui_changePage("main");
478 return 0;
479}
Dees_Troy51a0e822012-09-05 15:24:24 -0400480
that3f7b1ac2014-12-30 11:30:13 +0100481int GUIAction::key(std::string arg)
482{
483 const int key = getKeyByName(arg);
484 PageManager::NotifyKey(key, true);
485 PageManager::NotifyKey(key, false);
486 return 0;
487}
488
489int GUIAction::page(std::string arg)
490{
491 std::string page_name = gui_parse_text(arg);
492 return gui_changePage(page_name);
493}
494
495int GUIAction::reload(std::string arg)
496{
497 int check = 0, ret_val = 0;
498 std::string theme_path;
499
500 operation_start("Reload Theme");
501 theme_path = DataManager::GetSettingsStoragePath();
502 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
503 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
504 check = 1;
505 }
506
507 theme_path += "/TWRP/theme/ui.zip";
508 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600509 {
that3f7b1ac2014-12-30 11:30:13 +0100510 // Loading the custom theme failed - try loading the stock theme
511 LOGINFO("Attempting to reload stock theme...\n");
512 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 {
that3f7b1ac2014-12-30 11:30:13 +0100514 LOGERR("Failed to load base packages.\n");
515 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 }
that3f7b1ac2014-12-30 11:30:13 +0100518 operation_end(ret_val);
519 return 0;
520}
Dees_Troy51a0e822012-09-05 15:24:24 -0400521
that3f7b1ac2014-12-30 11:30:13 +0100522int GUIAction::readBackup(std::string arg)
523{
524 string Restore_Name;
525 DataManager::GetValue("tw_restore", Restore_Name);
526 PartitionManager.Set_Restore_Files(Restore_Name);
527 return 0;
528}
529
530int GUIAction::set(std::string arg)
531{
532 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533 {
that3f7b1ac2014-12-30 11:30:13 +0100534 string varName = arg.substr(0, arg.find('='));
535 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400536
that3f7b1ac2014-12-30 11:30:13 +0100537 DataManager::GetValue(value, value);
538 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200539 }
that3f7b1ac2014-12-30 11:30:13 +0100540 else
541 DataManager::SetValue(arg, "1");
542 return 0;
543}
Dees_Troy51a0e822012-09-05 15:24:24 -0400544
that3f7b1ac2014-12-30 11:30:13 +0100545int GUIAction::clear(std::string arg)
546{
547 DataManager::SetValue(arg, "0");
548 return 0;
549}
Dees_Troy51a0e822012-09-05 15:24:24 -0400550
that3f7b1ac2014-12-30 11:30:13 +0100551int GUIAction::mount(std::string arg)
552{
553 if (arg == "usb") {
554 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400555 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100556 PartitionManager.usb_storage_enable();
557 else
558 gui_print("Simulating actions...\n");
559 } else if (!simulate) {
560 PartitionManager.Mount_By_Path(arg, true);
561 PartitionManager.Add_MTP_Storage(arg);
562 } else
563 gui_print("Simulating actions...\n");
564 return 0;
565}
566
567int GUIAction::unmount(std::string arg)
568{
569 if (arg == "usb") {
570 if (!simulate)
571 PartitionManager.usb_storage_disable();
572 else
573 gui_print("Simulating actions...\n");
574 DataManager::SetValue(TW_ACTION_BUSY, 0);
575 } else if (!simulate) {
576 PartitionManager.UnMount_By_Path(arg, true);
577 } else
578 gui_print("Simulating actions...\n");
579 return 0;
580}
581
582int GUIAction::restoredefaultsettings(std::string arg)
583{
584 operation_start("Restore Defaults");
585 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
586 gui_print("Simulating actions...\n");
587 else {
588 DataManager::ResetDefaults();
589 PartitionManager.Update_System_Details();
590 PartitionManager.Mount_Current_Storage(true);
591 }
592 operation_end(0);
593 return 0;
594}
595
596int GUIAction::copylog(std::string arg)
597{
598 operation_start("Copy Log");
599 if (!simulate)
600 {
601 string dst;
602 PartitionManager.Mount_Current_Storage(true);
603 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
604 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
605 tw_set_default_metadata(dst.c_str());
606 sync();
607 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
608 } else
609 simulate_progress_bar();
610 operation_end(0);
611 return 0;
612}
613
614
615int GUIAction::compute(std::string arg)
616{
617 if (arg.find("+") != string::npos)
618 {
619 string varName = arg.substr(0, arg.find('+'));
620 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
621 int amount_to_add = atoi(string_to_add.c_str());
622 int value;
623
624 DataManager::GetValue(varName, value);
625 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400626 return 0;
627 }
that3f7b1ac2014-12-30 11:30:13 +0100628 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400629 {
that3f7b1ac2014-12-30 11:30:13 +0100630 string varName = arg.substr(0, arg.find('-'));
631 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
632 int amount_to_subtract = atoi(string_to_subtract.c_str());
633 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400634
that3f7b1ac2014-12-30 11:30:13 +0100635 DataManager::GetValue(varName, value);
636 value -= amount_to_subtract;
637 if (value <= 0)
638 value = 0;
639 DataManager::SetValue(varName, value);
640 return 0;
641 }
642 if (arg.find("*") != string::npos)
643 {
644 string varName = arg.substr(0, arg.find('*'));
645 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
646 int multiply_by = atoi(multiply_by_str.c_str());
647 int value;
648
649 DataManager::GetValue(varName, value);
650 DataManager::SetValue(varName, value*multiply_by);
651 return 0;
652 }
653 if (arg.find("/") != string::npos)
654 {
655 string varName = arg.substr(0, arg.find('/'));
656 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
657 int divide_by = atoi(divide_by_str.c_str());
658 int value;
659
660 if(divide_by != 0)
661 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100663 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 }
665 return 0;
666 }
that3f7b1ac2014-12-30 11:30:13 +0100667 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
668 return -1;
669}
Dees_Troy51a0e822012-09-05 15:24:24 -0400670
that3f7b1ac2014-12-30 11:30:13 +0100671int GUIAction::setguitimezone(std::string arg)
672{
673 string SelectedZone;
674 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
675 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
676 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
677
678 int dst;
679 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
680
681 string offset;
682 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
683
684 string NewTimeZone = Zone;
685 if (offset != "0")
686 NewTimeZone += ":" + offset;
687
688 if (dst != 0)
689 NewTimeZone += DSTZone;
690
691 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
692 DataManager::update_tz_environment_variables();
693 return 0;
694}
695
696int GUIAction::overlay(std::string arg)
697{
698 return gui_changeOverlay(arg);
699}
700
701int GUIAction::queuezip(std::string arg)
702{
703 if (zip_queue_index >= 10) {
704 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 return 0;
706 }
that3f7b1ac2014-12-30 11:30:13 +0100707 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
708 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
709 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100711 }
712 return 0;
713}
714
715int GUIAction::cancelzip(std::string arg)
716{
717 if (zip_queue_index <= 0) {
718 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100720 } else {
721 zip_queue_index--;
722 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400723 }
that3f7b1ac2014-12-30 11:30:13 +0100724 return 0;
725}
Dees_Troy51a0e822012-09-05 15:24:24 -0400726
that3f7b1ac2014-12-30 11:30:13 +0100727int GUIAction::queueclear(std::string arg)
728{
729 zip_queue_index = 0;
730 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
731 return 0;
732}
Dees_Troy51a0e822012-09-05 15:24:24 -0400733
that3f7b1ac2014-12-30 11:30:13 +0100734int GUIAction::sleep(std::string arg)
735{
736 operation_start("Sleep");
737 usleep(atoi(arg.c_str()));
738 operation_end(0);
739 return 0;
740}
Dees Troyb21cc642013-09-10 17:36:41 +0000741
that3f7b1ac2014-12-30 11:30:13 +0100742int GUIAction::appenddatetobackupname(std::string arg)
743{
744 operation_start("AppendDateToBackupName");
745 string Backup_Name;
746 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
747 Backup_Name += TWFunc::Get_Current_Date();
748 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
749 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
750 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
751 operation_end(0);
752 return 0;
753}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500754
that3f7b1ac2014-12-30 11:30:13 +0100755int GUIAction::generatebackupname(std::string arg)
756{
757 operation_start("GenerateBackupName");
758 TWFunc::Auto_Generate_Backup_Name();
759 operation_end(0);
760 return 0;
761}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500762
that3f7b1ac2014-12-30 11:30:13 +0100763int GUIAction::checkpartitionlist(std::string arg)
764{
765 string Wipe_List, wipe_path;
766 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500767
that3f7b1ac2014-12-30 11:30:13 +0100768 DataManager::GetValue("tw_wipe_list", Wipe_List);
769 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
770 if (!Wipe_List.empty()) {
771 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
772 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
773 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
774 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
775 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
776 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 } else {
that3f7b1ac2014-12-30 11:30:13 +0100778 count++;
779 }
780 start_pos = end_pos + 1;
781 end_pos = Wipe_List.find(";", start_pos);
782 }
783 DataManager::SetValue("tw_check_partition_list", count);
784 } else {
785 DataManager::SetValue("tw_check_partition_list", 0);
786 }
787 return 0;
788}
Dees_Troy51a0e822012-09-05 15:24:24 -0400789
that3f7b1ac2014-12-30 11:30:13 +0100790int GUIAction::getpartitiondetails(std::string arg)
791{
792 string Wipe_List, wipe_path;
793 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400794
that3f7b1ac2014-12-30 11:30:13 +0100795 DataManager::GetValue("tw_wipe_list", Wipe_List);
796 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
797 if (!Wipe_List.empty()) {
798 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
799 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
800 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
801 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
802 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
803 // Do nothing
804 } else {
805 DataManager::SetValue("tw_partition_path", wipe_path);
806 break;
807 }
808 start_pos = end_pos + 1;
809 end_pos = Wipe_List.find(";", start_pos);
810 }
811 if (!wipe_path.empty()) {
812 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
813 if (Part) {
814 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500815
that3f7b1ac2014-12-30 11:30:13 +0100816 DataManager::SetValue("tw_partition_name", Part->Display_Name);
817 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
818 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
819 DataManager::SetValue("tw_partition_size", Part->Size / mb);
820 DataManager::SetValue("tw_partition_used", Part->Used / mb);
821 DataManager::SetValue("tw_partition_free", Part->Free / mb);
822 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
823 DataManager::SetValue("tw_partition_removable", Part->Removable);
824 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
825
826 if (Part->Can_Repair())
827 DataManager::SetValue("tw_partition_can_repair", 1);
828 else
829 DataManager::SetValue("tw_partition_can_repair", 0);
830 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
831 DataManager::SetValue("tw_partition_vfat", 1);
832 else
833 DataManager::SetValue("tw_partition_vfat", 0);
834 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
835 DataManager::SetValue("tw_partition_exfat", 1);
836 else
837 DataManager::SetValue("tw_partition_exfat", 0);
838 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
839 DataManager::SetValue("tw_partition_f2fs", 1);
840 else
841 DataManager::SetValue("tw_partition_f2fs", 0);
842 if (TWFunc::Path_Exists("/sbin/mke2fs"))
843 DataManager::SetValue("tw_partition_ext", 1);
844 else
845 DataManager::SetValue("tw_partition_ext", 0);
846 return 0;
847 } else {
848 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
849 }
850 }
851 }
852 DataManager::SetValue("tw_partition_name", "");
853 DataManager::SetValue("tw_partition_file_system", "");
854 return 0;
855}
856
857int GUIAction::screenshot(std::string arg)
858{
859 time_t tm;
860 char path[256];
861 int path_len;
862 uid_t uid = -1;
863 gid_t gid = -1;
864
865 struct passwd *pwd = getpwnam("media_rw");
866 if(pwd) {
867 uid = pwd->pw_uid;
868 gid = pwd->pw_gid;
869 }
870
871 const std::string storage = DataManager::GetCurrentStoragePath();
872 if(PartitionManager.Is_Mounted_By_Path(storage)) {
873 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
874 } else {
875 strcpy(path, "/tmp/");
876 }
877
878 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
879 return 0;
880
881 tm = time(NULL);
882 path_len = strlen(path);
883
884 // Screenshot_2014-01-01-18-21-38.png
885 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
886
887 int res = gr_save_screenshot(path);
888 if(res == 0) {
889 chmod(path, 0666);
890 chown(path, uid, gid);
891
892 gui_print("Screenshot was saved to %s\n", path);
893
894 // blink to notify that the screenshow was taken
895 gr_color(255, 255, 255, 255);
896 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
897 gr_flip();
898 gui_forceRender();
899 } else {
900 LOGERR("Failed to take a screenshot!\n");
901 }
902 return 0;
903}
904
905int GUIAction::setbrightness(std::string arg)
906{
907 return TWFunc::Set_Brightness(arg);
908}
909
910int GUIAction::fileexists(std::string arg)
911{
912 struct stat st;
913 string newpath = arg + "/.";
914
915 operation_start("FileExists");
916 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
917 operation_end(0);
918 else
919 operation_end(1);
920 return 0;
921}
922
thatcc8ddca2015-01-03 01:59:36 +0100923void GUIAction::reinject_after_flash()
924{
925 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
926 operation_start("ReinjectTWRP");
927 gui_print("Injecting TWRP into boot image...\n");
928 if (simulate) {
929 simulate_progress_bar();
930 } else {
931 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
932 if (Boot == NULL || Boot->Current_File_System != "emmc")
933 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
934 else {
935 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
936 TWFunc::Exec_Cmd(injectcmd);
937 }
938 gui_print("TWRP injection complete.\n");
939 }
940 }
941}
942
that3f7b1ac2014-12-30 11:30:13 +0100943int GUIAction::flash(std::string arg)
944{
945 int i, ret_val = 0, wipe_cache = 0;
946 for (i=0; i<zip_queue_index; i++) {
947 operation_start("Flashing");
948 DataManager::SetValue("tw_filename", zip_queue[i]);
949 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
950
951 TWFunc::SetPerformanceMode(true);
952 ret_val = flash_zip(zip_queue[i], arg, &wipe_cache);
953 TWFunc::SetPerformanceMode(false);
954 if (ret_val != 0) {
955 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
956 i = 10; // Error flashing zip - exit queue
957 ret_val = 1;
958 }
959 }
960 zip_queue_index = 0;
961 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
962
963 if (wipe_cache)
964 PartitionManager.Wipe_By_Path("/cache");
965
thatcc8ddca2015-01-03 01:59:36 +0100966 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100967 PartitionManager.Update_System_Details();
968 operation_end(ret_val);
969 return 0;
970}
971
972int GUIAction::wipe(std::string arg)
973{
974 operation_start("Format");
975 DataManager::SetValue("tw_partition", arg);
976
977 int ret_val = false;
978
979 if (simulate) {
980 simulate_progress_bar();
981 } else {
982 if (arg == "data")
983 ret_val = PartitionManager.Factory_Reset();
984 else if (arg == "battery")
985 ret_val = PartitionManager.Wipe_Battery_Stats();
986 else if (arg == "rotate")
987 ret_val = PartitionManager.Wipe_Rotate_Data();
988 else if (arg == "dalvik")
989 ret_val = PartitionManager.Wipe_Dalvik_Cache();
990 else if (arg == "DATAMEDIA") {
991 ret_val = PartitionManager.Format_Data();
992 } else if (arg == "INTERNAL") {
993 int has_datamedia, dual_storage;
994
995 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
996 if (has_datamedia) {
997 ret_val = PartitionManager.Wipe_Media_From_Data();
998 } else {
999 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1000 }
1001 } else if (arg == "EXTERNAL") {
1002 string External_Path;
1003
1004 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1005 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1006 } else if (arg == "ANDROIDSECURE") {
1007 ret_val = PartitionManager.Wipe_Android_Secure();
1008 } else if (arg == "LIST") {
1009 string Wipe_List, wipe_path;
1010 bool skip = false;
1011 ret_val = true;
1012 TWPartition* wipe_part = NULL;
1013
1014 DataManager::GetValue("tw_wipe_list", Wipe_List);
1015 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1016 if (!Wipe_List.empty()) {
1017 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1018 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1019 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1020 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1021 if (wipe_path == "/and-sec") {
1022 if (!PartitionManager.Wipe_Android_Secure()) {
1023 LOGERR("Unable to wipe android secure\n");
1024 ret_val = false;
1025 break;
1026 } else {
1027 skip = true;
1028 }
1029 } else if (wipe_path == "DALVIK") {
1030 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1031 LOGERR("Failed to wipe dalvik\n");
1032 ret_val = false;
1033 break;
1034 } else {
1035 skip = true;
1036 }
1037 } else if (wipe_path == "INTERNAL") {
1038 if (!PartitionManager.Wipe_Media_From_Data()) {
1039 ret_val = false;
1040 break;
1041 } else {
1042 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001043 }
1044 }
that3f7b1ac2014-12-30 11:30:13 +01001045 if (!skip) {
1046 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1047 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1048 ret_val = false;
1049 break;
1050 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1051 arg = wipe_path;
1052 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001053 } else {
that3f7b1ac2014-12-30 11:30:13 +01001054 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001055 }
that3f7b1ac2014-12-30 11:30:13 +01001056 start_pos = end_pos + 1;
1057 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001058 }
1059 }
that3f7b1ac2014-12-30 11:30:13 +01001060 } else
1061 ret_val = PartitionManager.Wipe_By_Path(arg);
1062#ifdef TW_OEM_BUILD
1063 if (arg == DataManager::GetSettingsStoragePath()) {
1064 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1065 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001066
that3f7b1ac2014-12-30 11:30:13 +01001067 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1068 LOGINFO("Making TWRP folder and saving settings.\n");
1069 Storage_Path += "/TWRP";
1070 mkdir(Storage_Path.c_str(), 0777);
1071 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001072 } else {
that3f7b1ac2014-12-30 11:30:13 +01001073 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1074 }
1075 }
1076#endif
1077 }
1078 PartitionManager.Update_System_Details();
1079 if (ret_val)
1080 ret_val = 0; // 0 is success
1081 else
1082 ret_val = 1; // 1 is failure
1083 operation_end(ret_val);
1084 return 0;
1085}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001086
that3f7b1ac2014-12-30 11:30:13 +01001087int GUIAction::refreshsizes(std::string arg)
1088{
1089 operation_start("Refreshing Sizes");
1090 if (simulate) {
1091 simulate_progress_bar();
1092 } else
1093 PartitionManager.Update_System_Details();
1094 operation_end(0);
1095 return 0;
1096}
1097
1098int GUIAction::nandroid(std::string arg)
1099{
1100 operation_start("Nandroid");
1101 int ret = 0;
1102
1103 if (simulate) {
1104 DataManager::SetValue("tw_partition", "Simulation");
1105 simulate_progress_bar();
1106 } else {
1107 if (arg == "backup") {
1108 string Backup_Name;
1109 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1110 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1111 ret = PartitionManager.Run_Backup();
1112 }
1113 else {
1114 operation_end(1);
1115 return -1;
1116
1117 }
1118 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1119 } else if (arg == "restore") {
1120 string Restore_Name;
1121 DataManager::GetValue("tw_restore", Restore_Name);
1122 ret = PartitionManager.Run_Restore(Restore_Name);
1123 } else {
1124 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 return -1;
1126 }
1127 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001128 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001129 if (ret == false)
1130 ret = 1; // 1 for failure
1131 else
1132 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001133 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001134 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001135}
Dees_Troy51a0e822012-09-05 15:24:24 -04001136
that3f7b1ac2014-12-30 11:30:13 +01001137int GUIAction::fixpermissions(std::string arg)
1138{
1139 operation_start("Fix Permissions");
1140 LOGINFO("fix permissions started!\n");
1141 if (simulate) {
1142 simulate_progress_bar();
1143 } else {
1144 int op_status = PartitionManager.Fix_Permissions();
1145 if (op_status != 0)
1146 op_status = 1; // failure
1147 operation_end(op_status);
1148 }
1149 return 0;
1150}
1151
1152int GUIAction::dd(std::string arg)
1153{
1154 operation_start("imaging");
1155
1156 if (simulate) {
1157 simulate_progress_bar();
1158 } else {
1159 string cmd = "dd " + arg;
1160 TWFunc::Exec_Cmd(cmd);
1161 }
1162 operation_end(0);
1163 return 0;
1164}
1165
1166int GUIAction::partitionsd(std::string arg)
1167{
1168 operation_start("Partition SD Card");
1169 int ret_val = 0;
1170
1171 if (simulate) {
1172 simulate_progress_bar();
1173 } else {
1174 int allow_partition;
1175 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1176 if (allow_partition == 0) {
1177 gui_print("This device does not have a real SD Card!\nAborting!\n");
1178 } else {
1179 if (!PartitionManager.Partition_SDCard())
1180 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001181 }
that3f7b1ac2014-12-30 11:30:13 +01001182 }
1183 operation_end(ret_val);
1184 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001185
that3f7b1ac2014-12-30 11:30:13 +01001186}
Dees_Troy51a0e822012-09-05 15:24:24 -04001187
that3f7b1ac2014-12-30 11:30:13 +01001188int GUIAction::installhtcdumlock(std::string arg)
1189{
1190 operation_start("Install HTC Dumlock");
1191 if (simulate) {
1192 simulate_progress_bar();
1193 } else
1194 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001195
that3f7b1ac2014-12-30 11:30:13 +01001196 operation_end(0);
1197 return 0;
1198}
Dees_Troy51a0e822012-09-05 15:24:24 -04001199
that3f7b1ac2014-12-30 11:30:13 +01001200int GUIAction::htcdumlockrestoreboot(std::string arg)
1201{
1202 operation_start("HTC Dumlock Restore Boot");
1203 if (simulate) {
1204 simulate_progress_bar();
1205 } else
1206 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001207
that3f7b1ac2014-12-30 11:30:13 +01001208 operation_end(0);
1209 return 0;
1210}
Dees_Troy51a0e822012-09-05 15:24:24 -04001211
that3f7b1ac2014-12-30 11:30:13 +01001212int GUIAction::htcdumlockreflashrecovery(std::string arg)
1213{
1214 operation_start("HTC Dumlock Reflash Recovery");
1215 if (simulate) {
1216 simulate_progress_bar();
1217 } else
1218 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001219
that3f7b1ac2014-12-30 11:30:13 +01001220 operation_end(0);
1221 return 0;
1222}
Dees_Troy51a0e822012-09-05 15:24:24 -04001223
that3f7b1ac2014-12-30 11:30:13 +01001224int GUIAction::cmd(std::string arg)
1225{
1226 int op_status = 0;
1227
1228 operation_start("Command");
1229 LOGINFO("Running command: '%s'\n", arg.c_str());
1230 if (simulate) {
1231 simulate_progress_bar();
1232 } else {
1233 op_status = TWFunc::Exec_Cmd(arg);
1234 if (op_status != 0)
1235 op_status = 1;
1236 }
1237
1238 operation_end(op_status);
1239 return 0;
1240}
1241
1242int GUIAction::terminalcommand(std::string arg)
1243{
1244 int op_status = 0;
1245 string cmdpath, command;
1246
1247 DataManager::GetValue("tw_terminal_location", cmdpath);
1248 operation_start("CommandOutput");
1249 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1250 if (simulate) {
1251 simulate_progress_bar();
1252 operation_end(op_status);
1253 } else {
1254 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1255 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001256 DataManager::SetValue("tw_terminal_state", 1);
1257 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001258 FILE* fp;
1259 char line[512];
1260
1261 fp = popen(command.c_str(), "r");
1262 if (fp == NULL) {
1263 LOGERR("Error opening command to run.\n");
1264 } else {
1265 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1266 struct timeval timeout;
1267 fd_set fdset;
1268
1269 while(keep_going)
1270 {
1271 FD_ZERO(&fdset);
1272 FD_SET(fd, &fdset);
1273 timeout.tv_sec = 0;
1274 timeout.tv_usec = 400000;
1275 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1276 if (has_data == 0) {
1277 // Timeout reached
1278 DataManager::GetValue("tw_terminal_state", check);
1279 if (check == 0) {
1280 keep_going = 0;
1281 }
1282 } else if (has_data < 0) {
1283 // End of execution
1284 keep_going = 0;
1285 } else {
1286 // Try to read output
1287 memset(line, 0, sizeof(line));
1288 bytes_read = read(fd, line, sizeof(line));
1289 if (bytes_read > 0)
1290 gui_print("%s", line); // Display output
1291 else
1292 keep_going = 0; // Done executing
1293 }
1294 }
1295 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001296 }
thatc6085482015-01-09 22:12:43 +01001297 DataManager::SetValue("tw_operation_status", 0);
1298 DataManager::SetValue("tw_operation_state", 1);
1299 DataManager::SetValue("tw_terminal_state", 0);
1300 DataManager::SetValue("tw_background_thread_running", 0);
1301 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001302 }
1303 return 0;
1304}
1305
1306int GUIAction::killterminal(std::string arg)
1307{
1308 int op_status = 0;
1309
1310 LOGINFO("Sending kill command...\n");
1311 operation_start("KillCommand");
1312 DataManager::SetValue("tw_operation_status", 0);
1313 DataManager::SetValue("tw_operation_state", 1);
1314 DataManager::SetValue("tw_terminal_state", 0);
1315 DataManager::SetValue("tw_background_thread_running", 0);
1316 DataManager::SetValue(TW_ACTION_BUSY, 0);
1317 return 0;
1318}
1319
1320int GUIAction::reinjecttwrp(std::string arg)
1321{
1322 int op_status = 0;
1323 operation_start("ReinjectTWRP");
1324 gui_print("Injecting TWRP into boot image...\n");
1325 if (simulate) {
1326 simulate_progress_bar();
1327 } else {
1328 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1329 gui_print("TWRP injection complete.\n");
1330 }
1331
1332 operation_end(op_status);
1333 return 0;
1334}
1335
1336int GUIAction::checkbackupname(std::string arg)
1337{
1338 int op_status = 0;
1339
1340 operation_start("CheckBackupName");
1341 if (simulate) {
1342 simulate_progress_bar();
1343 } else {
1344 op_status = PartitionManager.Check_Backup_Name(true);
1345 if (op_status != 0)
1346 op_status = 1;
1347 }
1348
1349 operation_end(op_status);
1350 return 0;
1351}
1352
1353int GUIAction::decrypt(std::string arg)
1354{
1355 int op_status = 0;
1356
1357 operation_start("Decrypt");
1358 if (simulate) {
1359 simulate_progress_bar();
1360 } else {
1361 string Password;
1362 DataManager::GetValue("tw_crypto_password", Password);
1363 op_status = PartitionManager.Decrypt_Device(Password);
1364 if (op_status != 0)
1365 op_status = 1;
1366 else {
1367 int load_theme = 1;
1368
1369 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1370
1371 if (load_theme) {
1372 int has_datamedia;
1373
1374 // Check for a custom theme and load it if exists
1375 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1376 if (has_datamedia != 0) {
1377 struct stat st;
1378 int check = 0;
1379 std::string theme_path;
1380
1381 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1382 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1383 } else {
1384 LOGINFO("Got default contexts and file mode for storage files.\n");
1385 }
1386
1387 theme_path = DataManager::GetSettingsStoragePath();
1388 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1389 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1390 check = 1;
1391 }
1392
1393 theme_path += "/TWRP/theme/ui.zip";
1394 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1395 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1396 {
1397 // Loading the custom theme failed - try loading the stock theme
1398 LOGINFO("Attempting to reload stock theme...\n");
1399 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1400 {
1401 LOGERR("Failed to load base packages.\n");
1402 }
1403 }
1404 }
1405 }
1406 }
1407 }
1408 }
1409
1410 operation_end(op_status);
1411 return 0;
1412}
1413
thatcc8ddca2015-01-03 01:59:36 +01001414int GUIAction::adbsideload(std::string arg)
1415{
1416 operation_start("Sideload");
1417 if (simulate) {
1418 simulate_progress_bar();
1419 operation_end(0);
1420 } else {
thatc6085482015-01-09 22:12:43 +01001421 gui_print("Starting ADB sideload feature...\n");
1422 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1423
1424 // wait for the adb connection
1425 int ret = apply_from_adb("/", &sideload_child_pid);
1426 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1427
1428 if (ret != 0) {
1429 if (ret == -2)
1430 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1431 ret = 1; // failure
1432 } else {
1433 int wipe_cache = 0;
1434 int wipe_dalvik = 0;
1435 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1436
1437 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1438 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1439 PartitionManager.Wipe_By_Path("/cache");
1440 if (wipe_dalvik)
1441 PartitionManager.Wipe_Dalvik_Cache();
1442 } else {
1443 ret = 1; // failure
1444 }
thatcc8ddca2015-01-03 01:59:36 +01001445 }
thatc6085482015-01-09 22:12:43 +01001446 if (sideload_child_pid) {
1447 LOGINFO("Signaling child sideload process to exit.\n");
1448 struct stat st;
1449 // Calling stat() on this magic filename signals the minadbd
1450 // subprocess to shut down.
1451 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1452 int status;
1453 LOGINFO("Waiting for child sideload process to exit.\n");
1454 waitpid(sideload_child_pid, &status, 0);
1455 }
1456
1457 TWFunc::Toggle_MTP(mtp_was_enabled);
1458 reinject_after_flash();
1459 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001460 }
that3f7b1ac2014-12-30 11:30:13 +01001461 return 0;
1462}
1463
1464int GUIAction::adbsideloadcancel(std::string arg)
1465{
that3f7b1ac2014-12-30 11:30:13 +01001466 struct stat st;
1467 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1468 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001469 LOGINFO("Signaling child sideload process to exit.\n");
1470 // Calling stat() on this magic filename signals the minadbd
1471 // subprocess to shut down.
1472 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1473 if (!sideload_child_pid) {
1474 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001475 return 0;
1476 }
thatcc8ddca2015-01-03 01:59:36 +01001477 ::sleep(1);
1478 LOGINFO("Killing child sideload process.\n");
1479 kill(sideload_child_pid, SIGTERM);
1480 int status;
1481 LOGINFO("Waiting for child sideload process to exit.\n");
1482 waitpid(sideload_child_pid, &status, 0);
1483 sideload_child_pid = 0;
1484 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001485 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1486 return 0;
1487}
1488
1489int GUIAction::openrecoveryscript(std::string arg)
1490{
1491 operation_start("OpenRecoveryScript");
1492 if (simulate) {
1493 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001494 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001495 } else {
thatc6085482015-01-09 22:12:43 +01001496 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1497 // that we converted to ORS commands during boot in recovery.cpp.
1498 // Run those first.
1499 int reboot = 0;
1500 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1501 gui_print("Processing AOSP recovery commands...\n");
1502 if (OpenRecoveryScript::run_script_file() == 0) {
1503 reboot = 1;
1504 }
that3f7b1ac2014-12-30 11:30:13 +01001505 }
thatc6085482015-01-09 22:12:43 +01001506 // Check for the ORS file in /cache and attempt to run those commands.
1507 if (OpenRecoveryScript::check_for_script_file()) {
1508 gui_print("Processing OpenRecoveryScript file...\n");
1509 if (OpenRecoveryScript::run_script_file() == 0) {
1510 reboot = 1;
1511 }
1512 }
1513 if (reboot) {
1514 usleep(2000000); // Sleep for 2 seconds before rebooting
1515 TWFunc::tw_reboot(rb_system);
1516 } else {
1517 DataManager::SetValue("tw_page_done", 1);
1518 }
1519 operation_end(1);
1520 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001521 }
1522 return 0;
1523}
1524
1525int GUIAction::installsu(std::string arg)
1526{
1527 int op_status = 0;
1528
1529 operation_start("Install SuperSU");
1530 if (simulate) {
1531 simulate_progress_bar();
1532 } else {
1533 if (!TWFunc::Install_SuperSU())
1534 op_status = 1;
1535 }
1536
1537 operation_end(op_status);
1538 return 0;
1539}
1540
1541int GUIAction::fixsu(std::string arg)
1542{
1543 int op_status = 0;
1544
1545 operation_start("Fixing Superuser Permissions");
1546 if (simulate) {
1547 simulate_progress_bar();
1548 } else {
1549 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1550 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1551 }
1552
1553 operation_end(op_status);
1554 return 0;
1555}
1556
1557int GUIAction::decrypt_backup(std::string arg)
1558{
1559 int op_status = 0;
1560
1561 operation_start("Try Restore Decrypt");
1562 if (simulate) {
1563 simulate_progress_bar();
1564 } else {
1565 string Restore_Path, Filename, Password;
1566 DataManager::GetValue("tw_restore", Restore_Path);
1567 Restore_Path += "/";
1568 DataManager::GetValue("tw_restore_password", Password);
1569 TWFunc::SetPerformanceMode(true);
1570 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1571 op_status = 0; // success
1572 else
1573 op_status = 1; // fail
1574 TWFunc::SetPerformanceMode(false);
1575 }
1576
1577 operation_end(op_status);
1578 return 0;
1579}
1580
1581int GUIAction::repair(std::string arg)
1582{
1583 int op_status = 0;
1584
1585 operation_start("Repair Partition");
1586 if (simulate) {
1587 simulate_progress_bar();
1588 } else {
1589 string part_path;
1590 DataManager::GetValue("tw_partition_mount_point", part_path);
1591 if (PartitionManager.Repair_By_Path(part_path, true)) {
1592 op_status = 0; // success
1593 } else {
1594 LOGERR("Error repairing file system.\n");
1595 op_status = 1; // fail
1596 }
1597 }
1598
1599 operation_end(op_status);
1600 return 0;
1601}
1602
1603int GUIAction::changefilesystem(std::string arg)
1604{
1605 int op_status = 0;
1606
1607 operation_start("Change File System");
1608 if (simulate) {
1609 simulate_progress_bar();
1610 } else {
1611 string part_path, file_system;
1612 DataManager::GetValue("tw_partition_mount_point", part_path);
1613 DataManager::GetValue("tw_action_new_file_system", file_system);
1614 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1615 op_status = 0; // success
1616 } else {
1617 LOGERR("Error changing file system.\n");
1618 op_status = 1; // fail
1619 }
1620 }
1621 PartitionManager.Update_System_Details();
1622 operation_end(op_status);
1623 return 0;
1624}
1625
1626int GUIAction::startmtp(std::string arg)
1627{
1628 int op_status = 0;
1629
1630 operation_start("Start MTP");
1631 if (PartitionManager.Enable_MTP())
1632 op_status = 0; // success
1633 else
1634 op_status = 1; // fail
1635
1636 operation_end(op_status);
1637 return 0;
1638}
1639
1640int GUIAction::stopmtp(std::string arg)
1641{
1642 int op_status = 0;
1643
1644 operation_start("Stop MTP");
1645 if (PartitionManager.Disable_MTP())
1646 op_status = 0; // success
1647 else
1648 op_status = 1; // fail
1649
1650 operation_end(op_status);
1651 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001652}
1653
1654int GUIAction::getKeyByName(std::string key)
1655{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001656 if (key == "home") return KEY_HOME;
1657 else if (key == "menu") return KEY_MENU;
1658 else if (key == "back") return KEY_BACK;
1659 else if (key == "search") return KEY_SEARCH;
1660 else if (key == "voldown") return KEY_VOLUMEDOWN;
1661 else if (key == "volup") return KEY_VOLUMEUP;
1662 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001663 int ret_val;
1664 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1665 if (!ret_val)
1666 return KEY_POWER;
1667 else
1668 return ret_val;
1669 }
1670
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001671 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001672}