blob: d54ea0a033251ab3b60f17ff264c110bbe734db8 [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
101void ActionThread::threadActions(GUIAction *act, size_t start_index)
102{
103 pthread_mutex_lock(&m_act_lock);
104 if (m_thread_running) {
105 pthread_mutex_unlock(&m_act_lock);
106 LOGERR("Another threaded action is already running -- not running actions '%s' and following\n", act->mActions[start_index].mFunction.c_str());
107 } else {
108 m_thread_running = true;
109 pthread_mutex_unlock(&m_act_lock);
110 ThreadData *d = new ThreadData;
111 d->act = act;
112 d->start_index = start_index;
113
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;
124 for (it = act->mActions.begin() + d->start_index; it != act->mActions.end(); ++it)
125 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
that3f7b1ac2014-12-30 11:30:13 +0100383 std::vector<Action>::iterator it;
384 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100385 {
386 if (needsToRunInSeparateThread(*it))
387 {
388 // run all remaining actions in a separate thread
389 action_thread.threadActions(this, it - mActions.begin());
390 // ...and we're done here
391 break;
392 }
393
that3f7b1ac2014-12-30 11:30:13 +0100394 doAction(*it);
thatc6085482015-01-09 22:12:43 +0100395 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400396
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400398}
399
that3f7b1ac2014-12-30 11:30:13 +0100400int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400401{
that3f7b1ac2014-12-30 11:30:13 +0100402 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400403
that3f7b1ac2014-12-30 11:30:13 +0100404 std::string function = gui_parse_text(action.mFunction);
405 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400406
that3f7b1ac2014-12-30 11:30:13 +0100407 // find function and execute it
408 mapFunc::const_iterator funcitr = mf.find(function);
409 if (funcitr != mf.end())
410 return (this->*funcitr->second)(arg);
411
412 LOGERR("Unknown action '%s'\n", function.c_str());
413 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414}
415
416void GUIAction::operation_start(const string operation_name)
417{
that3f7b1ac2014-12-30 11:30:13 +0100418 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000419 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 DataManager::SetValue(TW_ACTION_BUSY, 1);
421 DataManager::SetValue("ui_progress", 0);
422 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600424 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400425}
426
that3f7b1ac2014-12-30 11:30:13 +0100427void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400428{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000429 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431 DataManager::SetValue("ui_progress", 100);
432 if (simulate) {
433 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
434 if (simulate_fail != 0)
435 DataManager::SetValue("tw_operation_status", 1);
436 else
437 DataManager::SetValue("tw_operation_status", 0);
438 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500439 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500441 }
442 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500444 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 }
446 DataManager::SetValue("tw_operation_state", 1);
447 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700448#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500449 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700450#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000451 time(&Stop);
452 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600453 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100454 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400455}
456
that3f7b1ac2014-12-30 11:30:13 +0100457int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400458{
that3f7b1ac2014-12-30 11:30:13 +0100459 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
that3f7b1ac2014-12-30 11:30:13 +0100461 sync();
462 DataManager::SetValue("tw_gui_done", 1);
463 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
that3f7b1ac2014-12-30 11:30:13 +0100465 return 0;
466}
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
that3f7b1ac2014-12-30 11:30:13 +0100468int GUIAction::home(std::string arg)
469{
470 PageManager::SelectPackage("TWRP");
471 gui_changePage("main");
472 return 0;
473}
Dees_Troy51a0e822012-09-05 15:24:24 -0400474
that3f7b1ac2014-12-30 11:30:13 +0100475int GUIAction::key(std::string arg)
476{
477 const int key = getKeyByName(arg);
478 PageManager::NotifyKey(key, true);
479 PageManager::NotifyKey(key, false);
480 return 0;
481}
482
483int GUIAction::page(std::string arg)
484{
485 std::string page_name = gui_parse_text(arg);
486 return gui_changePage(page_name);
487}
488
489int GUIAction::reload(std::string arg)
490{
491 int check = 0, ret_val = 0;
492 std::string theme_path;
493
494 operation_start("Reload Theme");
495 theme_path = DataManager::GetSettingsStoragePath();
496 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
497 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
498 check = 1;
499 }
500
501 theme_path += "/TWRP/theme/ui.zip";
502 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600503 {
that3f7b1ac2014-12-30 11:30:13 +0100504 // Loading the custom theme failed - try loading the stock theme
505 LOGINFO("Attempting to reload stock theme...\n");
506 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400507 {
that3f7b1ac2014-12-30 11:30:13 +0100508 LOGERR("Failed to load base packages.\n");
509 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 }
that3f7b1ac2014-12-30 11:30:13 +0100512 operation_end(ret_val);
513 return 0;
514}
Dees_Troy51a0e822012-09-05 15:24:24 -0400515
that3f7b1ac2014-12-30 11:30:13 +0100516int GUIAction::readBackup(std::string arg)
517{
518 string Restore_Name;
519 DataManager::GetValue("tw_restore", Restore_Name);
520 PartitionManager.Set_Restore_Files(Restore_Name);
521 return 0;
522}
523
524int GUIAction::set(std::string arg)
525{
526 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200527 {
that3f7b1ac2014-12-30 11:30:13 +0100528 string varName = arg.substr(0, arg.find('='));
529 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530
that3f7b1ac2014-12-30 11:30:13 +0100531 DataManager::GetValue(value, value);
532 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533 }
that3f7b1ac2014-12-30 11:30:13 +0100534 else
535 DataManager::SetValue(arg, "1");
536 return 0;
537}
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
that3f7b1ac2014-12-30 11:30:13 +0100539int GUIAction::clear(std::string arg)
540{
541 DataManager::SetValue(arg, "0");
542 return 0;
543}
Dees_Troy51a0e822012-09-05 15:24:24 -0400544
that3f7b1ac2014-12-30 11:30:13 +0100545int GUIAction::mount(std::string arg)
546{
547 if (arg == "usb") {
548 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100550 PartitionManager.usb_storage_enable();
551 else
552 gui_print("Simulating actions...\n");
553 } else if (!simulate) {
554 PartitionManager.Mount_By_Path(arg, true);
555 PartitionManager.Add_MTP_Storage(arg);
556 } else
557 gui_print("Simulating actions...\n");
558 return 0;
559}
560
561int GUIAction::unmount(std::string arg)
562{
563 if (arg == "usb") {
564 if (!simulate)
565 PartitionManager.usb_storage_disable();
566 else
567 gui_print("Simulating actions...\n");
568 DataManager::SetValue(TW_ACTION_BUSY, 0);
569 } else if (!simulate) {
570 PartitionManager.UnMount_By_Path(arg, true);
571 } else
572 gui_print("Simulating actions...\n");
573 return 0;
574}
575
576int GUIAction::restoredefaultsettings(std::string arg)
577{
578 operation_start("Restore Defaults");
579 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
580 gui_print("Simulating actions...\n");
581 else {
582 DataManager::ResetDefaults();
583 PartitionManager.Update_System_Details();
584 PartitionManager.Mount_Current_Storage(true);
585 }
586 operation_end(0);
587 return 0;
588}
589
590int GUIAction::copylog(std::string arg)
591{
592 operation_start("Copy Log");
593 if (!simulate)
594 {
595 string dst;
596 PartitionManager.Mount_Current_Storage(true);
597 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
598 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
599 tw_set_default_metadata(dst.c_str());
600 sync();
601 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
602 } else
603 simulate_progress_bar();
604 operation_end(0);
605 return 0;
606}
607
608
609int GUIAction::compute(std::string arg)
610{
611 if (arg.find("+") != string::npos)
612 {
613 string varName = arg.substr(0, arg.find('+'));
614 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
615 int amount_to_add = atoi(string_to_add.c_str());
616 int value;
617
618 DataManager::GetValue(varName, value);
619 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 return 0;
621 }
that3f7b1ac2014-12-30 11:30:13 +0100622 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 {
that3f7b1ac2014-12-30 11:30:13 +0100624 string varName = arg.substr(0, arg.find('-'));
625 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
626 int amount_to_subtract = atoi(string_to_subtract.c_str());
627 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400628
that3f7b1ac2014-12-30 11:30:13 +0100629 DataManager::GetValue(varName, value);
630 value -= amount_to_subtract;
631 if (value <= 0)
632 value = 0;
633 DataManager::SetValue(varName, value);
634 return 0;
635 }
636 if (arg.find("*") != string::npos)
637 {
638 string varName = arg.substr(0, arg.find('*'));
639 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
640 int multiply_by = atoi(multiply_by_str.c_str());
641 int value;
642
643 DataManager::GetValue(varName, value);
644 DataManager::SetValue(varName, value*multiply_by);
645 return 0;
646 }
647 if (arg.find("/") != string::npos)
648 {
649 string varName = arg.substr(0, arg.find('/'));
650 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
651 int divide_by = atoi(divide_by_str.c_str());
652 int value;
653
654 if(divide_by != 0)
655 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400656 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100657 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400658 }
659 return 0;
660 }
that3f7b1ac2014-12-30 11:30:13 +0100661 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
662 return -1;
663}
Dees_Troy51a0e822012-09-05 15:24:24 -0400664
that3f7b1ac2014-12-30 11:30:13 +0100665int GUIAction::setguitimezone(std::string arg)
666{
667 string SelectedZone;
668 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
669 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
670 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
671
672 int dst;
673 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
674
675 string offset;
676 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
677
678 string NewTimeZone = Zone;
679 if (offset != "0")
680 NewTimeZone += ":" + offset;
681
682 if (dst != 0)
683 NewTimeZone += DSTZone;
684
685 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
686 DataManager::update_tz_environment_variables();
687 return 0;
688}
689
690int GUIAction::overlay(std::string arg)
691{
692 return gui_changeOverlay(arg);
693}
694
695int GUIAction::queuezip(std::string arg)
696{
697 if (zip_queue_index >= 10) {
698 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 return 0;
700 }
that3f7b1ac2014-12-30 11:30:13 +0100701 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
702 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
703 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100705 }
706 return 0;
707}
708
709int GUIAction::cancelzip(std::string arg)
710{
711 if (zip_queue_index <= 0) {
712 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100714 } else {
715 zip_queue_index--;
716 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 }
that3f7b1ac2014-12-30 11:30:13 +0100718 return 0;
719}
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
that3f7b1ac2014-12-30 11:30:13 +0100721int GUIAction::queueclear(std::string arg)
722{
723 zip_queue_index = 0;
724 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
725 return 0;
726}
Dees_Troy51a0e822012-09-05 15:24:24 -0400727
that3f7b1ac2014-12-30 11:30:13 +0100728int GUIAction::sleep(std::string arg)
729{
730 operation_start("Sleep");
731 usleep(atoi(arg.c_str()));
732 operation_end(0);
733 return 0;
734}
Dees Troyb21cc642013-09-10 17:36:41 +0000735
that3f7b1ac2014-12-30 11:30:13 +0100736int GUIAction::appenddatetobackupname(std::string arg)
737{
738 operation_start("AppendDateToBackupName");
739 string Backup_Name;
740 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
741 Backup_Name += TWFunc::Get_Current_Date();
742 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
743 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
744 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
745 operation_end(0);
746 return 0;
747}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500748
that3f7b1ac2014-12-30 11:30:13 +0100749int GUIAction::generatebackupname(std::string arg)
750{
751 operation_start("GenerateBackupName");
752 TWFunc::Auto_Generate_Backup_Name();
753 operation_end(0);
754 return 0;
755}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500756
that3f7b1ac2014-12-30 11:30:13 +0100757int GUIAction::checkpartitionlist(std::string arg)
758{
759 string Wipe_List, wipe_path;
760 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500761
that3f7b1ac2014-12-30 11:30:13 +0100762 DataManager::GetValue("tw_wipe_list", Wipe_List);
763 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
764 if (!Wipe_List.empty()) {
765 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
766 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
767 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
768 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
769 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
770 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400771 } else {
that3f7b1ac2014-12-30 11:30:13 +0100772 count++;
773 }
774 start_pos = end_pos + 1;
775 end_pos = Wipe_List.find(";", start_pos);
776 }
777 DataManager::SetValue("tw_check_partition_list", count);
778 } else {
779 DataManager::SetValue("tw_check_partition_list", 0);
780 }
781 return 0;
782}
Dees_Troy51a0e822012-09-05 15:24:24 -0400783
that3f7b1ac2014-12-30 11:30:13 +0100784int GUIAction::getpartitiondetails(std::string arg)
785{
786 string Wipe_List, wipe_path;
787 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400788
that3f7b1ac2014-12-30 11:30:13 +0100789 DataManager::GetValue("tw_wipe_list", Wipe_List);
790 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
791 if (!Wipe_List.empty()) {
792 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
793 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
794 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
795 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
796 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
797 // Do nothing
798 } else {
799 DataManager::SetValue("tw_partition_path", wipe_path);
800 break;
801 }
802 start_pos = end_pos + 1;
803 end_pos = Wipe_List.find(";", start_pos);
804 }
805 if (!wipe_path.empty()) {
806 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
807 if (Part) {
808 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500809
that3f7b1ac2014-12-30 11:30:13 +0100810 DataManager::SetValue("tw_partition_name", Part->Display_Name);
811 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
812 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
813 DataManager::SetValue("tw_partition_size", Part->Size / mb);
814 DataManager::SetValue("tw_partition_used", Part->Used / mb);
815 DataManager::SetValue("tw_partition_free", Part->Free / mb);
816 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
817 DataManager::SetValue("tw_partition_removable", Part->Removable);
818 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
819
820 if (Part->Can_Repair())
821 DataManager::SetValue("tw_partition_can_repair", 1);
822 else
823 DataManager::SetValue("tw_partition_can_repair", 0);
824 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
825 DataManager::SetValue("tw_partition_vfat", 1);
826 else
827 DataManager::SetValue("tw_partition_vfat", 0);
828 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
829 DataManager::SetValue("tw_partition_exfat", 1);
830 else
831 DataManager::SetValue("tw_partition_exfat", 0);
832 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
833 DataManager::SetValue("tw_partition_f2fs", 1);
834 else
835 DataManager::SetValue("tw_partition_f2fs", 0);
836 if (TWFunc::Path_Exists("/sbin/mke2fs"))
837 DataManager::SetValue("tw_partition_ext", 1);
838 else
839 DataManager::SetValue("tw_partition_ext", 0);
840 return 0;
841 } else {
842 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
843 }
844 }
845 }
846 DataManager::SetValue("tw_partition_name", "");
847 DataManager::SetValue("tw_partition_file_system", "");
848 return 0;
849}
850
851int GUIAction::screenshot(std::string arg)
852{
853 time_t tm;
854 char path[256];
855 int path_len;
856 uid_t uid = -1;
857 gid_t gid = -1;
858
859 struct passwd *pwd = getpwnam("media_rw");
860 if(pwd) {
861 uid = pwd->pw_uid;
862 gid = pwd->pw_gid;
863 }
864
865 const std::string storage = DataManager::GetCurrentStoragePath();
866 if(PartitionManager.Is_Mounted_By_Path(storage)) {
867 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
868 } else {
869 strcpy(path, "/tmp/");
870 }
871
872 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
873 return 0;
874
875 tm = time(NULL);
876 path_len = strlen(path);
877
878 // Screenshot_2014-01-01-18-21-38.png
879 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
880
881 int res = gr_save_screenshot(path);
882 if(res == 0) {
883 chmod(path, 0666);
884 chown(path, uid, gid);
885
886 gui_print("Screenshot was saved to %s\n", path);
887
888 // blink to notify that the screenshow was taken
889 gr_color(255, 255, 255, 255);
890 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
891 gr_flip();
892 gui_forceRender();
893 } else {
894 LOGERR("Failed to take a screenshot!\n");
895 }
896 return 0;
897}
898
899int GUIAction::setbrightness(std::string arg)
900{
901 return TWFunc::Set_Brightness(arg);
902}
903
904int GUIAction::fileexists(std::string arg)
905{
906 struct stat st;
907 string newpath = arg + "/.";
908
909 operation_start("FileExists");
910 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
911 operation_end(0);
912 else
913 operation_end(1);
914 return 0;
915}
916
thatcc8ddca2015-01-03 01:59:36 +0100917void GUIAction::reinject_after_flash()
918{
919 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
920 operation_start("ReinjectTWRP");
921 gui_print("Injecting TWRP into boot image...\n");
922 if (simulate) {
923 simulate_progress_bar();
924 } else {
925 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
926 if (Boot == NULL || Boot->Current_File_System != "emmc")
927 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
928 else {
929 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
930 TWFunc::Exec_Cmd(injectcmd);
931 }
932 gui_print("TWRP injection complete.\n");
933 }
934 }
935}
936
that3f7b1ac2014-12-30 11:30:13 +0100937int GUIAction::flash(std::string arg)
938{
939 int i, ret_val = 0, wipe_cache = 0;
940 for (i=0; i<zip_queue_index; i++) {
941 operation_start("Flashing");
942 DataManager::SetValue("tw_filename", zip_queue[i]);
943 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
944
945 TWFunc::SetPerformanceMode(true);
946 ret_val = flash_zip(zip_queue[i], arg, &wipe_cache);
947 TWFunc::SetPerformanceMode(false);
948 if (ret_val != 0) {
949 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
950 i = 10; // Error flashing zip - exit queue
951 ret_val = 1;
952 }
953 }
954 zip_queue_index = 0;
955 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
956
957 if (wipe_cache)
958 PartitionManager.Wipe_By_Path("/cache");
959
thatcc8ddca2015-01-03 01:59:36 +0100960 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100961 PartitionManager.Update_System_Details();
962 operation_end(ret_val);
963 return 0;
964}
965
966int GUIAction::wipe(std::string arg)
967{
968 operation_start("Format");
969 DataManager::SetValue("tw_partition", arg);
970
971 int ret_val = false;
972
973 if (simulate) {
974 simulate_progress_bar();
975 } else {
976 if (arg == "data")
977 ret_val = PartitionManager.Factory_Reset();
978 else if (arg == "battery")
979 ret_val = PartitionManager.Wipe_Battery_Stats();
980 else if (arg == "rotate")
981 ret_val = PartitionManager.Wipe_Rotate_Data();
982 else if (arg == "dalvik")
983 ret_val = PartitionManager.Wipe_Dalvik_Cache();
984 else if (arg == "DATAMEDIA") {
985 ret_val = PartitionManager.Format_Data();
986 } else if (arg == "INTERNAL") {
987 int has_datamedia, dual_storage;
988
989 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
990 if (has_datamedia) {
991 ret_val = PartitionManager.Wipe_Media_From_Data();
992 } else {
993 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
994 }
995 } else if (arg == "EXTERNAL") {
996 string External_Path;
997
998 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
999 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1000 } else if (arg == "ANDROIDSECURE") {
1001 ret_val = PartitionManager.Wipe_Android_Secure();
1002 } else if (arg == "LIST") {
1003 string Wipe_List, wipe_path;
1004 bool skip = false;
1005 ret_val = true;
1006 TWPartition* wipe_part = NULL;
1007
1008 DataManager::GetValue("tw_wipe_list", Wipe_List);
1009 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1010 if (!Wipe_List.empty()) {
1011 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1012 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1013 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1014 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1015 if (wipe_path == "/and-sec") {
1016 if (!PartitionManager.Wipe_Android_Secure()) {
1017 LOGERR("Unable to wipe android secure\n");
1018 ret_val = false;
1019 break;
1020 } else {
1021 skip = true;
1022 }
1023 } else if (wipe_path == "DALVIK") {
1024 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1025 LOGERR("Failed to wipe dalvik\n");
1026 ret_val = false;
1027 break;
1028 } else {
1029 skip = true;
1030 }
1031 } else if (wipe_path == "INTERNAL") {
1032 if (!PartitionManager.Wipe_Media_From_Data()) {
1033 ret_val = false;
1034 break;
1035 } else {
1036 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001037 }
1038 }
that3f7b1ac2014-12-30 11:30:13 +01001039 if (!skip) {
1040 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1041 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1042 ret_val = false;
1043 break;
1044 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1045 arg = wipe_path;
1046 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 } else {
that3f7b1ac2014-12-30 11:30:13 +01001048 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001049 }
that3f7b1ac2014-12-30 11:30:13 +01001050 start_pos = end_pos + 1;
1051 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001052 }
1053 }
that3f7b1ac2014-12-30 11:30:13 +01001054 } else
1055 ret_val = PartitionManager.Wipe_By_Path(arg);
1056#ifdef TW_OEM_BUILD
1057 if (arg == DataManager::GetSettingsStoragePath()) {
1058 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1059 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001060
that3f7b1ac2014-12-30 11:30:13 +01001061 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1062 LOGINFO("Making TWRP folder and saving settings.\n");
1063 Storage_Path += "/TWRP";
1064 mkdir(Storage_Path.c_str(), 0777);
1065 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001066 } else {
that3f7b1ac2014-12-30 11:30:13 +01001067 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1068 }
1069 }
1070#endif
1071 }
1072 PartitionManager.Update_System_Details();
1073 if (ret_val)
1074 ret_val = 0; // 0 is success
1075 else
1076 ret_val = 1; // 1 is failure
1077 operation_end(ret_val);
1078 return 0;
1079}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001080
that3f7b1ac2014-12-30 11:30:13 +01001081int GUIAction::refreshsizes(std::string arg)
1082{
1083 operation_start("Refreshing Sizes");
1084 if (simulate) {
1085 simulate_progress_bar();
1086 } else
1087 PartitionManager.Update_System_Details();
1088 operation_end(0);
1089 return 0;
1090}
1091
1092int GUIAction::nandroid(std::string arg)
1093{
1094 operation_start("Nandroid");
1095 int ret = 0;
1096
1097 if (simulate) {
1098 DataManager::SetValue("tw_partition", "Simulation");
1099 simulate_progress_bar();
1100 } else {
1101 if (arg == "backup") {
1102 string Backup_Name;
1103 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1104 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1105 ret = PartitionManager.Run_Backup();
1106 }
1107 else {
1108 operation_end(1);
1109 return -1;
1110
1111 }
1112 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1113 } else if (arg == "restore") {
1114 string Restore_Name;
1115 DataManager::GetValue("tw_restore", Restore_Name);
1116 ret = PartitionManager.Run_Restore(Restore_Name);
1117 } else {
1118 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001119 return -1;
1120 }
1121 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001122 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001123 if (ret == false)
1124 ret = 1; // 1 for failure
1125 else
1126 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001127 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001128 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001129}
Dees_Troy51a0e822012-09-05 15:24:24 -04001130
that3f7b1ac2014-12-30 11:30:13 +01001131int GUIAction::fixpermissions(std::string arg)
1132{
1133 operation_start("Fix Permissions");
1134 LOGINFO("fix permissions started!\n");
1135 if (simulate) {
1136 simulate_progress_bar();
1137 } else {
1138 int op_status = PartitionManager.Fix_Permissions();
1139 if (op_status != 0)
1140 op_status = 1; // failure
1141 operation_end(op_status);
1142 }
1143 return 0;
1144}
1145
1146int GUIAction::dd(std::string arg)
1147{
1148 operation_start("imaging");
1149
1150 if (simulate) {
1151 simulate_progress_bar();
1152 } else {
1153 string cmd = "dd " + arg;
1154 TWFunc::Exec_Cmd(cmd);
1155 }
1156 operation_end(0);
1157 return 0;
1158}
1159
1160int GUIAction::partitionsd(std::string arg)
1161{
1162 operation_start("Partition SD Card");
1163 int ret_val = 0;
1164
1165 if (simulate) {
1166 simulate_progress_bar();
1167 } else {
1168 int allow_partition;
1169 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1170 if (allow_partition == 0) {
1171 gui_print("This device does not have a real SD Card!\nAborting!\n");
1172 } else {
1173 if (!PartitionManager.Partition_SDCard())
1174 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001175 }
that3f7b1ac2014-12-30 11:30:13 +01001176 }
1177 operation_end(ret_val);
1178 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001179
that3f7b1ac2014-12-30 11:30:13 +01001180}
Dees_Troy51a0e822012-09-05 15:24:24 -04001181
that3f7b1ac2014-12-30 11:30:13 +01001182int GUIAction::installhtcdumlock(std::string arg)
1183{
1184 operation_start("Install HTC Dumlock");
1185 if (simulate) {
1186 simulate_progress_bar();
1187 } else
1188 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001189
that3f7b1ac2014-12-30 11:30:13 +01001190 operation_end(0);
1191 return 0;
1192}
Dees_Troy51a0e822012-09-05 15:24:24 -04001193
that3f7b1ac2014-12-30 11:30:13 +01001194int GUIAction::htcdumlockrestoreboot(std::string arg)
1195{
1196 operation_start("HTC Dumlock Restore Boot");
1197 if (simulate) {
1198 simulate_progress_bar();
1199 } else
1200 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001201
that3f7b1ac2014-12-30 11:30:13 +01001202 operation_end(0);
1203 return 0;
1204}
Dees_Troy51a0e822012-09-05 15:24:24 -04001205
that3f7b1ac2014-12-30 11:30:13 +01001206int GUIAction::htcdumlockreflashrecovery(std::string arg)
1207{
1208 operation_start("HTC Dumlock Reflash Recovery");
1209 if (simulate) {
1210 simulate_progress_bar();
1211 } else
1212 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001213
that3f7b1ac2014-12-30 11:30:13 +01001214 operation_end(0);
1215 return 0;
1216}
Dees_Troy51a0e822012-09-05 15:24:24 -04001217
that3f7b1ac2014-12-30 11:30:13 +01001218int GUIAction::cmd(std::string arg)
1219{
1220 int op_status = 0;
1221
1222 operation_start("Command");
1223 LOGINFO("Running command: '%s'\n", arg.c_str());
1224 if (simulate) {
1225 simulate_progress_bar();
1226 } else {
1227 op_status = TWFunc::Exec_Cmd(arg);
1228 if (op_status != 0)
1229 op_status = 1;
1230 }
1231
1232 operation_end(op_status);
1233 return 0;
1234}
1235
1236int GUIAction::terminalcommand(std::string arg)
1237{
1238 int op_status = 0;
1239 string cmdpath, command;
1240
1241 DataManager::GetValue("tw_terminal_location", cmdpath);
1242 operation_start("CommandOutput");
1243 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1244 if (simulate) {
1245 simulate_progress_bar();
1246 operation_end(op_status);
1247 } else {
1248 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1249 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001250 DataManager::SetValue("tw_terminal_state", 1);
1251 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001252 FILE* fp;
1253 char line[512];
1254
1255 fp = popen(command.c_str(), "r");
1256 if (fp == NULL) {
1257 LOGERR("Error opening command to run.\n");
1258 } else {
1259 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1260 struct timeval timeout;
1261 fd_set fdset;
1262
1263 while(keep_going)
1264 {
1265 FD_ZERO(&fdset);
1266 FD_SET(fd, &fdset);
1267 timeout.tv_sec = 0;
1268 timeout.tv_usec = 400000;
1269 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1270 if (has_data == 0) {
1271 // Timeout reached
1272 DataManager::GetValue("tw_terminal_state", check);
1273 if (check == 0) {
1274 keep_going = 0;
1275 }
1276 } else if (has_data < 0) {
1277 // End of execution
1278 keep_going = 0;
1279 } else {
1280 // Try to read output
1281 memset(line, 0, sizeof(line));
1282 bytes_read = read(fd, line, sizeof(line));
1283 if (bytes_read > 0)
1284 gui_print("%s", line); // Display output
1285 else
1286 keep_going = 0; // Done executing
1287 }
1288 }
1289 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001290 }
thatc6085482015-01-09 22:12:43 +01001291 DataManager::SetValue("tw_operation_status", 0);
1292 DataManager::SetValue("tw_operation_state", 1);
1293 DataManager::SetValue("tw_terminal_state", 0);
1294 DataManager::SetValue("tw_background_thread_running", 0);
1295 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001296 }
1297 return 0;
1298}
1299
1300int GUIAction::killterminal(std::string arg)
1301{
1302 int op_status = 0;
1303
1304 LOGINFO("Sending kill command...\n");
1305 operation_start("KillCommand");
1306 DataManager::SetValue("tw_operation_status", 0);
1307 DataManager::SetValue("tw_operation_state", 1);
1308 DataManager::SetValue("tw_terminal_state", 0);
1309 DataManager::SetValue("tw_background_thread_running", 0);
1310 DataManager::SetValue(TW_ACTION_BUSY, 0);
1311 return 0;
1312}
1313
1314int GUIAction::reinjecttwrp(std::string arg)
1315{
1316 int op_status = 0;
1317 operation_start("ReinjectTWRP");
1318 gui_print("Injecting TWRP into boot image...\n");
1319 if (simulate) {
1320 simulate_progress_bar();
1321 } else {
1322 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1323 gui_print("TWRP injection complete.\n");
1324 }
1325
1326 operation_end(op_status);
1327 return 0;
1328}
1329
1330int GUIAction::checkbackupname(std::string arg)
1331{
1332 int op_status = 0;
1333
1334 operation_start("CheckBackupName");
1335 if (simulate) {
1336 simulate_progress_bar();
1337 } else {
1338 op_status = PartitionManager.Check_Backup_Name(true);
1339 if (op_status != 0)
1340 op_status = 1;
1341 }
1342
1343 operation_end(op_status);
1344 return 0;
1345}
1346
1347int GUIAction::decrypt(std::string arg)
1348{
1349 int op_status = 0;
1350
1351 operation_start("Decrypt");
1352 if (simulate) {
1353 simulate_progress_bar();
1354 } else {
1355 string Password;
1356 DataManager::GetValue("tw_crypto_password", Password);
1357 op_status = PartitionManager.Decrypt_Device(Password);
1358 if (op_status != 0)
1359 op_status = 1;
1360 else {
1361 int load_theme = 1;
1362
1363 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1364
1365 if (load_theme) {
1366 int has_datamedia;
1367
1368 // Check for a custom theme and load it if exists
1369 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1370 if (has_datamedia != 0) {
1371 struct stat st;
1372 int check = 0;
1373 std::string theme_path;
1374
1375 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1376 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1377 } else {
1378 LOGINFO("Got default contexts and file mode for storage files.\n");
1379 }
1380
1381 theme_path = DataManager::GetSettingsStoragePath();
1382 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1383 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1384 check = 1;
1385 }
1386
1387 theme_path += "/TWRP/theme/ui.zip";
1388 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1389 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1390 {
1391 // Loading the custom theme failed - try loading the stock theme
1392 LOGINFO("Attempting to reload stock theme...\n");
1393 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1394 {
1395 LOGERR("Failed to load base packages.\n");
1396 }
1397 }
1398 }
1399 }
1400 }
1401 }
1402 }
1403
1404 operation_end(op_status);
1405 return 0;
1406}
1407
thatcc8ddca2015-01-03 01:59:36 +01001408int GUIAction::adbsideload(std::string arg)
1409{
1410 operation_start("Sideload");
1411 if (simulate) {
1412 simulate_progress_bar();
1413 operation_end(0);
1414 } else {
thatc6085482015-01-09 22:12:43 +01001415 gui_print("Starting ADB sideload feature...\n");
1416 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1417
1418 // wait for the adb connection
1419 int ret = apply_from_adb("/", &sideload_child_pid);
1420 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1421
1422 if (ret != 0) {
1423 if (ret == -2)
1424 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1425 ret = 1; // failure
1426 } else {
1427 int wipe_cache = 0;
1428 int wipe_dalvik = 0;
1429 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1430
1431 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1432 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1433 PartitionManager.Wipe_By_Path("/cache");
1434 if (wipe_dalvik)
1435 PartitionManager.Wipe_Dalvik_Cache();
1436 } else {
1437 ret = 1; // failure
1438 }
thatcc8ddca2015-01-03 01:59:36 +01001439 }
thatc6085482015-01-09 22:12:43 +01001440 if (sideload_child_pid) {
1441 LOGINFO("Signaling child sideload process to exit.\n");
1442 struct stat st;
1443 // Calling stat() on this magic filename signals the minadbd
1444 // subprocess to shut down.
1445 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1446 int status;
1447 LOGINFO("Waiting for child sideload process to exit.\n");
1448 waitpid(sideload_child_pid, &status, 0);
1449 }
1450
1451 TWFunc::Toggle_MTP(mtp_was_enabled);
1452 reinject_after_flash();
1453 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001454 }
that3f7b1ac2014-12-30 11:30:13 +01001455 return 0;
1456}
1457
1458int GUIAction::adbsideloadcancel(std::string arg)
1459{
that3f7b1ac2014-12-30 11:30:13 +01001460 struct stat st;
1461 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1462 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001463 LOGINFO("Signaling child sideload process to exit.\n");
1464 // Calling stat() on this magic filename signals the minadbd
1465 // subprocess to shut down.
1466 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1467 if (!sideload_child_pid) {
1468 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001469 return 0;
1470 }
thatcc8ddca2015-01-03 01:59:36 +01001471 ::sleep(1);
1472 LOGINFO("Killing child sideload process.\n");
1473 kill(sideload_child_pid, SIGTERM);
1474 int status;
1475 LOGINFO("Waiting for child sideload process to exit.\n");
1476 waitpid(sideload_child_pid, &status, 0);
1477 sideload_child_pid = 0;
1478 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001479 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1480 return 0;
1481}
1482
1483int GUIAction::openrecoveryscript(std::string arg)
1484{
1485 operation_start("OpenRecoveryScript");
1486 if (simulate) {
1487 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001488 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001489 } else {
thatc6085482015-01-09 22:12:43 +01001490 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1491 // that we converted to ORS commands during boot in recovery.cpp.
1492 // Run those first.
1493 int reboot = 0;
1494 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1495 gui_print("Processing AOSP recovery commands...\n");
1496 if (OpenRecoveryScript::run_script_file() == 0) {
1497 reboot = 1;
1498 }
that3f7b1ac2014-12-30 11:30:13 +01001499 }
thatc6085482015-01-09 22:12:43 +01001500 // Check for the ORS file in /cache and attempt to run those commands.
1501 if (OpenRecoveryScript::check_for_script_file()) {
1502 gui_print("Processing OpenRecoveryScript file...\n");
1503 if (OpenRecoveryScript::run_script_file() == 0) {
1504 reboot = 1;
1505 }
1506 }
1507 if (reboot) {
1508 usleep(2000000); // Sleep for 2 seconds before rebooting
1509 TWFunc::tw_reboot(rb_system);
1510 } else {
1511 DataManager::SetValue("tw_page_done", 1);
1512 }
1513 operation_end(1);
1514 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001515 }
1516 return 0;
1517}
1518
1519int GUIAction::installsu(std::string arg)
1520{
1521 int op_status = 0;
1522
1523 operation_start("Install SuperSU");
1524 if (simulate) {
1525 simulate_progress_bar();
1526 } else {
1527 if (!TWFunc::Install_SuperSU())
1528 op_status = 1;
1529 }
1530
1531 operation_end(op_status);
1532 return 0;
1533}
1534
1535int GUIAction::fixsu(std::string arg)
1536{
1537 int op_status = 0;
1538
1539 operation_start("Fixing Superuser Permissions");
1540 if (simulate) {
1541 simulate_progress_bar();
1542 } else {
1543 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1544 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1545 }
1546
1547 operation_end(op_status);
1548 return 0;
1549}
1550
1551int GUIAction::decrypt_backup(std::string arg)
1552{
1553 int op_status = 0;
1554
1555 operation_start("Try Restore Decrypt");
1556 if (simulate) {
1557 simulate_progress_bar();
1558 } else {
1559 string Restore_Path, Filename, Password;
1560 DataManager::GetValue("tw_restore", Restore_Path);
1561 Restore_Path += "/";
1562 DataManager::GetValue("tw_restore_password", Password);
1563 TWFunc::SetPerformanceMode(true);
1564 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1565 op_status = 0; // success
1566 else
1567 op_status = 1; // fail
1568 TWFunc::SetPerformanceMode(false);
1569 }
1570
1571 operation_end(op_status);
1572 return 0;
1573}
1574
1575int GUIAction::repair(std::string arg)
1576{
1577 int op_status = 0;
1578
1579 operation_start("Repair Partition");
1580 if (simulate) {
1581 simulate_progress_bar();
1582 } else {
1583 string part_path;
1584 DataManager::GetValue("tw_partition_mount_point", part_path);
1585 if (PartitionManager.Repair_By_Path(part_path, true)) {
1586 op_status = 0; // success
1587 } else {
1588 LOGERR("Error repairing file system.\n");
1589 op_status = 1; // fail
1590 }
1591 }
1592
1593 operation_end(op_status);
1594 return 0;
1595}
1596
1597int GUIAction::changefilesystem(std::string arg)
1598{
1599 int op_status = 0;
1600
1601 operation_start("Change File System");
1602 if (simulate) {
1603 simulate_progress_bar();
1604 } else {
1605 string part_path, file_system;
1606 DataManager::GetValue("tw_partition_mount_point", part_path);
1607 DataManager::GetValue("tw_action_new_file_system", file_system);
1608 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1609 op_status = 0; // success
1610 } else {
1611 LOGERR("Error changing file system.\n");
1612 op_status = 1; // fail
1613 }
1614 }
1615 PartitionManager.Update_System_Details();
1616 operation_end(op_status);
1617 return 0;
1618}
1619
1620int GUIAction::startmtp(std::string arg)
1621{
1622 int op_status = 0;
1623
1624 operation_start("Start MTP");
1625 if (PartitionManager.Enable_MTP())
1626 op_status = 0; // success
1627 else
1628 op_status = 1; // fail
1629
1630 operation_end(op_status);
1631 return 0;
1632}
1633
1634int GUIAction::stopmtp(std::string arg)
1635{
1636 int op_status = 0;
1637
1638 operation_start("Stop MTP");
1639 if (PartitionManager.Disable_MTP())
1640 op_status = 0; // success
1641 else
1642 op_status = 1; // fail
1643
1644 operation_end(op_status);
1645 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001646}
1647
1648int GUIAction::getKeyByName(std::string key)
1649{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001650 if (key == "home") return KEY_HOME;
1651 else if (key == "menu") return KEY_MENU;
1652 else if (key == "back") return KEY_BACK;
1653 else if (key == "search") return KEY_SEARCH;
1654 else if (key == "voldown") return KEY_VOLUMEDOWN;
1655 else if (key == "volup") return KEY_VOLUMEUP;
1656 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001657 int ret_val;
1658 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1659 if (!ret_val)
1660 return KEY_POWER;
1661 else
1662 return ret_val;
1663 }
1664
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001665 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001666}