blob: 8315ab2e9e6122496050de3f8571c4f16b77f464 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040052#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060053#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060054#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040055};
56
57#include "rapidxml.hpp"
58#include "objects.hpp"
59
Dees_Troy51a0e822012-09-05 15:24:24 -040060void curtainClose(void);
61
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
66static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010067pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010068
thatc6085482015-01-09 22:12:43 +010069static ActionThread action_thread;
70
71static void *ActionThread_work_wrapper(void *data)
72{
73 action_thread.run(data);
74 return NULL;
75}
76
77ActionThread::ActionThread()
78{
79 m_thread_running = false;
80 pthread_mutex_init(&m_act_lock, NULL);
81}
82
83ActionThread::~ActionThread()
84{
85 pthread_mutex_lock(&m_act_lock);
86 if(m_thread_running) {
87 pthread_mutex_unlock(&m_act_lock);
88 pthread_join(m_thread, NULL);
89 } else {
90 pthread_mutex_unlock(&m_act_lock);
91 }
92 pthread_mutex_destroy(&m_act_lock);
93}
94
that7d3b54f2015-01-09 22:52:51 +010095void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +010096{
97 pthread_mutex_lock(&m_act_lock);
98 if (m_thread_running) {
99 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100100 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
101 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100102 } else {
103 m_thread_running = true;
104 pthread_mutex_unlock(&m_act_lock);
105 ThreadData *d = new ThreadData;
106 d->act = act;
thatc6085482015-01-09 22:12:43 +0100107
108 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
109 }
110}
111
112void ActionThread::run(void *data)
113{
114 ThreadData *d = (ThreadData*)data;
115 GUIAction* act = d->act;
116
117 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100118 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100119 act->doAction(*it);
120
121 pthread_mutex_lock(&m_act_lock);
122 m_thread_running = false;
123 pthread_mutex_unlock(&m_act_lock);
124 delete d;
125}
126
Dees_Troy51a0e822012-09-05 15:24:24 -0400127GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100128 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400129{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 xml_node<>* child;
131 xml_node<>* actions;
132 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
that3f7b1ac2014-12-30 11:30:13 +0100136 if (mf.empty()) {
thatc6085482015-01-09 22:12:43 +0100137 // These actions will be run in the caller's thread
that3f7b1ac2014-12-30 11:30:13 +0100138 mf["reboot"] = &GUIAction::reboot;
139 mf["home"] = &GUIAction::home;
140 mf["key"] = &GUIAction::key;
141 mf["page"] = &GUIAction::page;
142 mf["reload"] = &GUIAction::reload;
143 mf["readBackup"] = &GUIAction::readBackup;
144 mf["set"] = &GUIAction::set;
145 mf["clear"] = &GUIAction::clear;
146 mf["mount"] = &GUIAction::mount;
147 mf["unmount"] = &GUIAction::unmount;
148 mf["umount"] = &GUIAction::unmount;
149 mf["restoredefaultsettings"] = &GUIAction::restoredefaultsettings;
150 mf["copylog"] = &GUIAction::copylog;
151 mf["compute"] = &GUIAction::compute;
152 mf["addsubtract"] = &GUIAction::compute;
153 mf["setguitimezone"] = &GUIAction::setguitimezone;
154 mf["overlay"] = &GUIAction::overlay;
155 mf["queuezip"] = &GUIAction::queuezip;
156 mf["cancelzip"] = &GUIAction::cancelzip;
157 mf["queueclear"] = &GUIAction::queueclear;
158 mf["sleep"] = &GUIAction::sleep;
159 mf["appenddatetobackupname"] = &GUIAction::appenddatetobackupname;
160 mf["generatebackupname"] = &GUIAction::generatebackupname;
161 mf["checkpartitionlist"] = &GUIAction::checkpartitionlist;
162 mf["getpartitiondetails"] = &GUIAction::getpartitiondetails;
163 mf["screenshot"] = &GUIAction::screenshot;
164 mf["setbrightness"] = &GUIAction::setbrightness;
that3f7b1ac2014-12-30 11:30:13 +0100165 mf["fileexists"] = &GUIAction::fileexists;
thatc6085482015-01-09 22:12:43 +0100166 mf["killterminal"] = &GUIAction::killterminal;
167 mf["checkbackupname"] = &GUIAction::checkbackupname;
168 mf["adbsideloadcancel"] = &GUIAction::adbsideloadcancel;
169 mf["fixsu"] = &GUIAction::fixsu;
170 mf["startmtp"] = &GUIAction::startmtp;
171 mf["stopmtp"] = &GUIAction::stopmtp;
172
173 // remember actions that run in the caller thread
174 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
175 setActionsRunningInCallerThread.insert(it->first);
176
177 // These actions will run in a separate thread
that3f7b1ac2014-12-30 11:30:13 +0100178 mf["flash"] = &GUIAction::flash;
179 mf["wipe"] = &GUIAction::wipe;
180 mf["refreshsizes"] = &GUIAction::refreshsizes;
181 mf["nandroid"] = &GUIAction::nandroid;
182 mf["fixpermissions"] = &GUIAction::fixpermissions;
183 mf["dd"] = &GUIAction::dd;
184 mf["partitionsd"] = &GUIAction::partitionsd;
185 mf["installhtcdumlock"] = &GUIAction::installhtcdumlock;
186 mf["htcdumlockrestoreboot"] = &GUIAction::htcdumlockrestoreboot;
187 mf["htcdumlockreflashrecovery"] = &GUIAction::htcdumlockreflashrecovery;
188 mf["cmd"] = &GUIAction::cmd;
189 mf["terminalcommand"] = &GUIAction::terminalcommand;
that3f7b1ac2014-12-30 11:30:13 +0100190 mf["reinjecttwrp"] = &GUIAction::reinjecttwrp;
that3f7b1ac2014-12-30 11:30:13 +0100191 mf["decrypt"] = &GUIAction::decrypt;
192 mf["adbsideload"] = &GUIAction::adbsideload;
that3f7b1ac2014-12-30 11:30:13 +0100193 mf["openrecoveryscript"] = &GUIAction::openrecoveryscript;
194 mf["installsu"] = &GUIAction::installsu;
that3f7b1ac2014-12-30 11:30:13 +0100195 mf["decrypt_backup"] = &GUIAction::decrypt_backup;
196 mf["repair"] = &GUIAction::repair;
197 mf["changefilesystem"] = &GUIAction::changefilesystem;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600198 mf["flashimage"] = &GUIAction::flashimage;
that3f7b1ac2014-12-30 11:30:13 +0100199 }
200
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 // First, get the action
202 actions = node->first_node("actions");
203 if (actions) child = actions->first_node("action");
204 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 while (child)
209 {
210 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 attr = child->first_attribute("function");
213 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500214
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 action.mFunction = attr->value();
216 action.mArg = child->value();
217 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400218
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 child = child->next_sibling("action");
220 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400221
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200222 // Now, let's get either the key or region
223 child = node->first_node("touch");
224 if (child)
225 {
226 attr = child->first_attribute("key");
227 if (attr)
228 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100229 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
230 for(size_t i = 0; i < keys.size(); ++i)
231 {
232 const int key = getKeyByName(keys[i]);
233 mKeys[key] = false;
234 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 }
236 else
237 {
238 attr = child->first_attribute("x");
239 if (!attr) return;
240 mActionX = atol(attr->value());
241 attr = child->first_attribute("y");
242 if (!attr) return;
243 mActionY = atol(attr->value());
244 attr = child->first_attribute("w");
245 if (!attr) return;
246 mActionW = atol(attr->value());
247 attr = child->first_attribute("h");
248 if (!attr) return;
249 mActionH = atol(attr->value());
250 }
251 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400252}
253
254int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
255{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (state == TOUCH_RELEASE)
257 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400260}
261
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100262int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400263{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100264 if (mKeys.empty())
265 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100267 std::map<int, bool>::iterator itr = mKeys.find(key);
268 if(itr == mKeys.end())
269 return 0;
270
271 bool prevState = itr->second;
272 itr->second = down;
273
274 // If there is only one key for this action, wait for key up so it
275 // doesn't trigger with multi-key actions.
276 // Else, check if all buttons are pressed, then consume their release events
277 // so they don't trigger one-button actions and reset mKeys pressed status
278 if(mKeys.size() == 1) {
279 if(!down && prevState)
280 doActions();
281 } else if(down) {
282 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
283 if(!itr->second)
284 return 0;
285 }
286
287 // Passed, all req buttons are pressed, reset them and consume release events
288 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
289 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
290 kb->ConsumeKeyRelease(itr->first);
291 itr->second = false;
292 }
293
294 doActions();
295 }
296
Dees_Troy51a0e822012-09-05 15:24:24 -0400297 return 0;
298}
299
Vojtech Bocek07220562014-02-08 02:05:33 +0100300int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400301{
Vojtech Bocek07220562014-02-08 02:05:33 +0100302 GUIObject::NotifyVarChange(varName, value);
303
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100304 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100306 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200307 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400308
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400310}
311
312void GUIAction::simulate_progress_bar(void)
313{
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400315 for (int i = 0; i < 5; i++)
316 {
317 usleep(500000);
318 DataManager::SetValue("ui_progress", i * 20);
319 }
320}
321
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600322int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400323{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200324 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400325
326 DataManager::SetValue("ui_progress", 0);
327
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200328 if (filename.empty())
329 {
330 LOGERR("No file specified.\n");
331 return -1;
332 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400333
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400335 return -1;
336
Dees_Troy51a0e822012-09-05 15:24:24 -0400337 if (simulate) {
338 simulate_progress_bar();
339 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400340 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
342 // Now, check if we need to ensure TWRP remains installed...
343 struct stat st;
344 if (stat("/sbin/installTwrp", &st) == 0)
345 {
346 DataManager::SetValue("tw_operation", "Configuring TWRP");
347 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000348 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200349 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400350 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000351 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400352 }
353 }
354 }
355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 // Done
357 DataManager::SetValue("ui_progress", 100);
358 DataManager::SetValue("ui_progress", 0);
359 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400360}
361
thatc6085482015-01-09 22:12:43 +0100362bool GUIAction::needsToRunInSeparateThread(const GUIAction::Action& action)
363{
Vojtech Bocek85cfb7d2015-01-12 18:25:59 +0100364 return setActionsRunningInCallerThread.find(gui_parse_text(action.mFunction)) == setActionsRunningInCallerThread.end();
thatc6085482015-01-09 22:12:43 +0100365}
366
Dees_Troy51a0e822012-09-05 15:24:24 -0400367int GUIAction::doActions()
368{
that3f7b1ac2014-12-30 11:30:13 +0100369 if (mActions.size() < 1)
370 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
that7d3b54f2015-01-09 22:52:51 +0100372 bool needThread = false;
that3f7b1ac2014-12-30 11:30:13 +0100373 std::vector<Action>::iterator it;
374 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100375 {
376 if (needsToRunInSeparateThread(*it))
377 {
that7d3b54f2015-01-09 22:52:51 +0100378 needThread = true;
thatc6085482015-01-09 22:12:43 +0100379 break;
380 }
that7d3b54f2015-01-09 22:52:51 +0100381 }
382 if (needThread)
383 {
384 action_thread.threadActions(this);
385 }
386 else
387 {
Vojtech Boceke979abd2015-01-12 18:29:12 +0100388 const size_t cnt = mActions.size();
389 for (size_t i = 0; i < cnt; ++i)
390 doAction(mActions[i]);
thatc6085482015-01-09 22:12:43 +0100391 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400392
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400394}
395
that3f7b1ac2014-12-30 11:30:13 +0100396int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400397{
that3f7b1ac2014-12-30 11:30:13 +0100398 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400399
that3f7b1ac2014-12-30 11:30:13 +0100400 std::string function = gui_parse_text(action.mFunction);
401 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400402
that3f7b1ac2014-12-30 11:30:13 +0100403 // find function and execute it
404 mapFunc::const_iterator funcitr = mf.find(function);
405 if (funcitr != mf.end())
406 return (this->*funcitr->second)(arg);
407
408 LOGERR("Unknown action '%s'\n", function.c_str());
409 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410}
411
412void GUIAction::operation_start(const string operation_name)
413{
that3f7b1ac2014-12-30 11:30:13 +0100414 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000415 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 DataManager::SetValue(TW_ACTION_BUSY, 1);
417 DataManager::SetValue("ui_progress", 0);
418 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600420 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
that3f7b1ac2014-12-30 11:30:13 +0100423void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400424{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000425 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 DataManager::SetValue("ui_progress", 100);
428 if (simulate) {
429 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
430 if (simulate_fail != 0)
431 DataManager::SetValue("tw_operation_status", 1);
432 else
433 DataManager::SetValue("tw_operation_status", 0);
434 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500435 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400436 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500437 }
438 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500440 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 }
442 DataManager::SetValue("tw_operation_state", 1);
443 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500444 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000445 time(&Stop);
446 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600447 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100448 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449}
450
that3f7b1ac2014-12-30 11:30:13 +0100451int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400452{
that3f7b1ac2014-12-30 11:30:13 +0100453 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400454
that3f7b1ac2014-12-30 11:30:13 +0100455 sync();
456 DataManager::SetValue("tw_gui_done", 1);
457 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
that3f7b1ac2014-12-30 11:30:13 +0100459 return 0;
460}
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
that3f7b1ac2014-12-30 11:30:13 +0100462int GUIAction::home(std::string arg)
463{
464 PageManager::SelectPackage("TWRP");
465 gui_changePage("main");
466 return 0;
467}
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
that3f7b1ac2014-12-30 11:30:13 +0100469int GUIAction::key(std::string arg)
470{
471 const int key = getKeyByName(arg);
472 PageManager::NotifyKey(key, true);
473 PageManager::NotifyKey(key, false);
474 return 0;
475}
476
477int GUIAction::page(std::string arg)
478{
479 std::string page_name = gui_parse_text(arg);
480 return gui_changePage(page_name);
481}
482
483int GUIAction::reload(std::string arg)
484{
485 int check = 0, ret_val = 0;
486 std::string theme_path;
487
that3f7b1ac2014-12-30 11:30:13 +0100488 theme_path = DataManager::GetSettingsStoragePath();
489 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
490 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
491 check = 1;
492 }
493
494 theme_path += "/TWRP/theme/ui.zip";
495 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600496 {
that3f7b1ac2014-12-30 11:30:13 +0100497 // Loading the custom theme failed - try loading the stock theme
498 LOGINFO("Attempting to reload stock theme...\n");
499 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 {
that3f7b1ac2014-12-30 11:30:13 +0100501 LOGERR("Failed to load base packages.\n");
502 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 }
that3f7b1ac2014-12-30 11:30:13 +0100505 return 0;
506}
Dees_Troy51a0e822012-09-05 15:24:24 -0400507
that3f7b1ac2014-12-30 11:30:13 +0100508int GUIAction::readBackup(std::string arg)
509{
510 string Restore_Name;
511 DataManager::GetValue("tw_restore", Restore_Name);
512 PartitionManager.Set_Restore_Files(Restore_Name);
513 return 0;
514}
515
516int GUIAction::set(std::string arg)
517{
518 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 {
that3f7b1ac2014-12-30 11:30:13 +0100520 string varName = arg.substr(0, arg.find('='));
521 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522
that3f7b1ac2014-12-30 11:30:13 +0100523 DataManager::GetValue(value, value);
524 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200525 }
that3f7b1ac2014-12-30 11:30:13 +0100526 else
527 DataManager::SetValue(arg, "1");
528 return 0;
529}
Dees_Troy51a0e822012-09-05 15:24:24 -0400530
that3f7b1ac2014-12-30 11:30:13 +0100531int GUIAction::clear(std::string arg)
532{
533 DataManager::SetValue(arg, "0");
534 return 0;
535}
Dees_Troy51a0e822012-09-05 15:24:24 -0400536
that3f7b1ac2014-12-30 11:30:13 +0100537int GUIAction::mount(std::string arg)
538{
539 if (arg == "usb") {
540 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100542 PartitionManager.usb_storage_enable();
543 else
544 gui_print("Simulating actions...\n");
545 } else if (!simulate) {
546 PartitionManager.Mount_By_Path(arg, true);
547 PartitionManager.Add_MTP_Storage(arg);
548 } else
549 gui_print("Simulating actions...\n");
550 return 0;
551}
552
553int GUIAction::unmount(std::string arg)
554{
555 if (arg == "usb") {
556 if (!simulate)
557 PartitionManager.usb_storage_disable();
558 else
559 gui_print("Simulating actions...\n");
560 DataManager::SetValue(TW_ACTION_BUSY, 0);
561 } else if (!simulate) {
562 PartitionManager.UnMount_By_Path(arg, true);
563 } else
564 gui_print("Simulating actions...\n");
565 return 0;
566}
567
568int GUIAction::restoredefaultsettings(std::string arg)
569{
570 operation_start("Restore Defaults");
571 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
572 gui_print("Simulating actions...\n");
573 else {
574 DataManager::ResetDefaults();
575 PartitionManager.Update_System_Details();
576 PartitionManager.Mount_Current_Storage(true);
577 }
578 operation_end(0);
579 return 0;
580}
581
582int GUIAction::copylog(std::string arg)
583{
584 operation_start("Copy Log");
585 if (!simulate)
586 {
587 string dst;
588 PartitionManager.Mount_Current_Storage(true);
589 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
590 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
591 tw_set_default_metadata(dst.c_str());
592 sync();
593 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
594 } else
595 simulate_progress_bar();
596 operation_end(0);
597 return 0;
598}
599
600
601int GUIAction::compute(std::string arg)
602{
603 if (arg.find("+") != string::npos)
604 {
605 string varName = arg.substr(0, arg.find('+'));
606 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
607 int amount_to_add = atoi(string_to_add.c_str());
608 int value;
609
610 DataManager::GetValue(varName, value);
611 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400612 return 0;
613 }
that3f7b1ac2014-12-30 11:30:13 +0100614 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400615 {
that3f7b1ac2014-12-30 11:30:13 +0100616 string varName = arg.substr(0, arg.find('-'));
617 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
618 int amount_to_subtract = atoi(string_to_subtract.c_str());
619 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400620
that3f7b1ac2014-12-30 11:30:13 +0100621 DataManager::GetValue(varName, value);
622 value -= amount_to_subtract;
623 if (value <= 0)
624 value = 0;
625 DataManager::SetValue(varName, value);
626 return 0;
627 }
628 if (arg.find("*") != string::npos)
629 {
630 string varName = arg.substr(0, arg.find('*'));
631 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
632 int multiply_by = atoi(multiply_by_str.c_str());
633 int value;
634
635 DataManager::GetValue(varName, value);
636 DataManager::SetValue(varName, value*multiply_by);
637 return 0;
638 }
639 if (arg.find("/") != string::npos)
640 {
641 string varName = arg.substr(0, arg.find('/'));
642 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
643 int divide_by = atoi(divide_by_str.c_str());
644 int value;
645
646 if(divide_by != 0)
647 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400648 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100649 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400650 }
651 return 0;
652 }
that3f7b1ac2014-12-30 11:30:13 +0100653 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
654 return -1;
655}
Dees_Troy51a0e822012-09-05 15:24:24 -0400656
that3f7b1ac2014-12-30 11:30:13 +0100657int GUIAction::setguitimezone(std::string arg)
658{
659 string SelectedZone;
660 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
661 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
662 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
663
664 int dst;
665 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
666
667 string offset;
668 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
669
670 string NewTimeZone = Zone;
671 if (offset != "0")
672 NewTimeZone += ":" + offset;
673
674 if (dst != 0)
675 NewTimeZone += DSTZone;
676
677 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
678 DataManager::update_tz_environment_variables();
679 return 0;
680}
681
682int GUIAction::overlay(std::string arg)
683{
684 return gui_changeOverlay(arg);
685}
686
687int GUIAction::queuezip(std::string arg)
688{
689 if (zip_queue_index >= 10) {
690 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400691 return 0;
692 }
that3f7b1ac2014-12-30 11:30:13 +0100693 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
694 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
695 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100697 }
698 return 0;
699}
700
701int GUIAction::cancelzip(std::string arg)
702{
703 if (zip_queue_index <= 0) {
704 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100706 } else {
707 zip_queue_index--;
708 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400709 }
that3f7b1ac2014-12-30 11:30:13 +0100710 return 0;
711}
Dees_Troy51a0e822012-09-05 15:24:24 -0400712
that3f7b1ac2014-12-30 11:30:13 +0100713int GUIAction::queueclear(std::string arg)
714{
715 zip_queue_index = 0;
716 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
717 return 0;
718}
Dees_Troy51a0e822012-09-05 15:24:24 -0400719
that3f7b1ac2014-12-30 11:30:13 +0100720int GUIAction::sleep(std::string arg)
721{
722 operation_start("Sleep");
723 usleep(atoi(arg.c_str()));
724 operation_end(0);
725 return 0;
726}
Dees Troyb21cc642013-09-10 17:36:41 +0000727
that3f7b1ac2014-12-30 11:30:13 +0100728int GUIAction::appenddatetobackupname(std::string arg)
729{
730 operation_start("AppendDateToBackupName");
731 string Backup_Name;
732 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
733 Backup_Name += TWFunc::Get_Current_Date();
734 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
735 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
736 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
737 operation_end(0);
738 return 0;
739}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500740
that3f7b1ac2014-12-30 11:30:13 +0100741int GUIAction::generatebackupname(std::string arg)
742{
743 operation_start("GenerateBackupName");
744 TWFunc::Auto_Generate_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::checkpartitionlist(std::string arg)
750{
751 string Wipe_List, wipe_path;
752 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500753
that3f7b1ac2014-12-30 11:30:13 +0100754 DataManager::GetValue("tw_wipe_list", Wipe_List);
755 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
756 if (!Wipe_List.empty()) {
757 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
758 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
759 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
760 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
761 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
762 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 } else {
that3f7b1ac2014-12-30 11:30:13 +0100764 count++;
765 }
766 start_pos = end_pos + 1;
767 end_pos = Wipe_List.find(";", start_pos);
768 }
769 DataManager::SetValue("tw_check_partition_list", count);
770 } else {
771 DataManager::SetValue("tw_check_partition_list", 0);
772 }
773 return 0;
774}
Dees_Troy51a0e822012-09-05 15:24:24 -0400775
that3f7b1ac2014-12-30 11:30:13 +0100776int GUIAction::getpartitiondetails(std::string arg)
777{
778 string Wipe_List, wipe_path;
779 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400780
that3f7b1ac2014-12-30 11:30:13 +0100781 DataManager::GetValue("tw_wipe_list", Wipe_List);
782 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
783 if (!Wipe_List.empty()) {
784 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
785 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
786 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
787 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
788 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
789 // Do nothing
790 } else {
791 DataManager::SetValue("tw_partition_path", wipe_path);
792 break;
793 }
794 start_pos = end_pos + 1;
795 end_pos = Wipe_List.find(";", start_pos);
796 }
797 if (!wipe_path.empty()) {
798 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
799 if (Part) {
800 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500801
that3f7b1ac2014-12-30 11:30:13 +0100802 DataManager::SetValue("tw_partition_name", Part->Display_Name);
803 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
804 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
805 DataManager::SetValue("tw_partition_size", Part->Size / mb);
806 DataManager::SetValue("tw_partition_used", Part->Used / mb);
807 DataManager::SetValue("tw_partition_free", Part->Free / mb);
808 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
809 DataManager::SetValue("tw_partition_removable", Part->Removable);
810 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
811
812 if (Part->Can_Repair())
813 DataManager::SetValue("tw_partition_can_repair", 1);
814 else
815 DataManager::SetValue("tw_partition_can_repair", 0);
816 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
817 DataManager::SetValue("tw_partition_vfat", 1);
818 else
819 DataManager::SetValue("tw_partition_vfat", 0);
820 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
821 DataManager::SetValue("tw_partition_exfat", 1);
822 else
823 DataManager::SetValue("tw_partition_exfat", 0);
824 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
825 DataManager::SetValue("tw_partition_f2fs", 1);
826 else
827 DataManager::SetValue("tw_partition_f2fs", 0);
828 if (TWFunc::Path_Exists("/sbin/mke2fs"))
829 DataManager::SetValue("tw_partition_ext", 1);
830 else
831 DataManager::SetValue("tw_partition_ext", 0);
832 return 0;
833 } else {
834 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
835 }
836 }
837 }
838 DataManager::SetValue("tw_partition_name", "");
839 DataManager::SetValue("tw_partition_file_system", "");
840 return 0;
841}
842
843int GUIAction::screenshot(std::string arg)
844{
845 time_t tm;
846 char path[256];
847 int path_len;
848 uid_t uid = -1;
849 gid_t gid = -1;
850
851 struct passwd *pwd = getpwnam("media_rw");
852 if(pwd) {
853 uid = pwd->pw_uid;
854 gid = pwd->pw_gid;
855 }
856
857 const std::string storage = DataManager::GetCurrentStoragePath();
858 if(PartitionManager.Is_Mounted_By_Path(storage)) {
859 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
860 } else {
861 strcpy(path, "/tmp/");
862 }
863
864 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
865 return 0;
866
867 tm = time(NULL);
868 path_len = strlen(path);
869
870 // Screenshot_2014-01-01-18-21-38.png
871 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
872
873 int res = gr_save_screenshot(path);
874 if(res == 0) {
875 chmod(path, 0666);
876 chown(path, uid, gid);
877
878 gui_print("Screenshot was saved to %s\n", path);
879
880 // blink to notify that the screenshow was taken
881 gr_color(255, 255, 255, 255);
882 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
883 gr_flip();
884 gui_forceRender();
885 } else {
886 LOGERR("Failed to take a screenshot!\n");
887 }
888 return 0;
889}
890
891int GUIAction::setbrightness(std::string arg)
892{
893 return TWFunc::Set_Brightness(arg);
894}
895
896int GUIAction::fileexists(std::string arg)
897{
898 struct stat st;
899 string newpath = arg + "/.";
900
901 operation_start("FileExists");
902 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
903 operation_end(0);
904 else
905 operation_end(1);
906 return 0;
907}
908
thatcc8ddca2015-01-03 01:59:36 +0100909void GUIAction::reinject_after_flash()
910{
911 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
912 operation_start("ReinjectTWRP");
913 gui_print("Injecting TWRP into boot image...\n");
914 if (simulate) {
915 simulate_progress_bar();
916 } else {
917 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
918 if (Boot == NULL || Boot->Current_File_System != "emmc")
919 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
920 else {
921 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
922 TWFunc::Exec_Cmd(injectcmd);
923 }
924 gui_print("TWRP injection complete.\n");
925 }
926 }
927}
928
that3f7b1ac2014-12-30 11:30:13 +0100929int GUIAction::flash(std::string arg)
930{
931 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600932 // We're going to jump to this page first, like a loading page
933 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100934 for (i=0; i<zip_queue_index; i++) {
935 operation_start("Flashing");
936 DataManager::SetValue("tw_filename", zip_queue[i]);
937 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
938
939 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600940 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100941 TWFunc::SetPerformanceMode(false);
942 if (ret_val != 0) {
943 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +0100944 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600945 break;
that3f7b1ac2014-12-30 11:30:13 +0100946 }
947 }
948 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +0100949
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600950 if (wipe_cache) {
951 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +0100952 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600953 }
that3f7b1ac2014-12-30 11:30:13 +0100954
thatcc8ddca2015-01-03 01:59:36 +0100955 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100956 PartitionManager.Update_System_Details();
957 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600958 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100959 return 0;
960}
961
962int GUIAction::wipe(std::string arg)
963{
964 operation_start("Format");
965 DataManager::SetValue("tw_partition", arg);
966
967 int ret_val = false;
968
969 if (simulate) {
970 simulate_progress_bar();
971 } else {
972 if (arg == "data")
973 ret_val = PartitionManager.Factory_Reset();
974 else if (arg == "battery")
975 ret_val = PartitionManager.Wipe_Battery_Stats();
976 else if (arg == "rotate")
977 ret_val = PartitionManager.Wipe_Rotate_Data();
978 else if (arg == "dalvik")
979 ret_val = PartitionManager.Wipe_Dalvik_Cache();
980 else if (arg == "DATAMEDIA") {
981 ret_val = PartitionManager.Format_Data();
982 } else if (arg == "INTERNAL") {
983 int has_datamedia, dual_storage;
984
985 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
986 if (has_datamedia) {
987 ret_val = PartitionManager.Wipe_Media_From_Data();
988 } else {
989 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
990 }
991 } else if (arg == "EXTERNAL") {
992 string External_Path;
993
994 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
995 ret_val = PartitionManager.Wipe_By_Path(External_Path);
996 } else if (arg == "ANDROIDSECURE") {
997 ret_val = PartitionManager.Wipe_Android_Secure();
998 } else if (arg == "LIST") {
999 string Wipe_List, wipe_path;
1000 bool skip = false;
1001 ret_val = true;
1002 TWPartition* wipe_part = NULL;
1003
1004 DataManager::GetValue("tw_wipe_list", Wipe_List);
1005 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1006 if (!Wipe_List.empty()) {
1007 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1008 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1009 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1010 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1011 if (wipe_path == "/and-sec") {
1012 if (!PartitionManager.Wipe_Android_Secure()) {
1013 LOGERR("Unable to wipe android secure\n");
1014 ret_val = false;
1015 break;
1016 } else {
1017 skip = true;
1018 }
1019 } else if (wipe_path == "DALVIK") {
1020 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1021 LOGERR("Failed to wipe dalvik\n");
1022 ret_val = false;
1023 break;
1024 } else {
1025 skip = true;
1026 }
1027 } else if (wipe_path == "INTERNAL") {
1028 if (!PartitionManager.Wipe_Media_From_Data()) {
1029 ret_val = false;
1030 break;
1031 } else {
1032 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001033 }
1034 }
that3f7b1ac2014-12-30 11:30:13 +01001035 if (!skip) {
1036 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1037 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1038 ret_val = false;
1039 break;
1040 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1041 arg = wipe_path;
1042 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001043 } else {
that3f7b1ac2014-12-30 11:30:13 +01001044 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001045 }
that3f7b1ac2014-12-30 11:30:13 +01001046 start_pos = end_pos + 1;
1047 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001048 }
1049 }
that3f7b1ac2014-12-30 11:30:13 +01001050 } else
1051 ret_val = PartitionManager.Wipe_By_Path(arg);
1052#ifdef TW_OEM_BUILD
1053 if (arg == DataManager::GetSettingsStoragePath()) {
1054 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1055 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001056
that3f7b1ac2014-12-30 11:30:13 +01001057 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1058 LOGINFO("Making TWRP folder and saving settings.\n");
1059 Storage_Path += "/TWRP";
1060 mkdir(Storage_Path.c_str(), 0777);
1061 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001062 } else {
that3f7b1ac2014-12-30 11:30:13 +01001063 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1064 }
1065 }
1066#endif
1067 }
1068 PartitionManager.Update_System_Details();
1069 if (ret_val)
1070 ret_val = 0; // 0 is success
1071 else
1072 ret_val = 1; // 1 is failure
1073 operation_end(ret_val);
1074 return 0;
1075}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001076
that3f7b1ac2014-12-30 11:30:13 +01001077int GUIAction::refreshsizes(std::string arg)
1078{
1079 operation_start("Refreshing Sizes");
1080 if (simulate) {
1081 simulate_progress_bar();
1082 } else
1083 PartitionManager.Update_System_Details();
1084 operation_end(0);
1085 return 0;
1086}
1087
1088int GUIAction::nandroid(std::string arg)
1089{
1090 operation_start("Nandroid");
1091 int ret = 0;
1092
1093 if (simulate) {
1094 DataManager::SetValue("tw_partition", "Simulation");
1095 simulate_progress_bar();
1096 } else {
1097 if (arg == "backup") {
1098 string Backup_Name;
1099 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1100 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1101 ret = PartitionManager.Run_Backup();
1102 }
1103 else {
1104 operation_end(1);
1105 return -1;
1106
1107 }
1108 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1109 } else if (arg == "restore") {
1110 string Restore_Name;
1111 DataManager::GetValue("tw_restore", Restore_Name);
1112 ret = PartitionManager.Run_Restore(Restore_Name);
1113 } else {
1114 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 return -1;
1116 }
1117 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001118 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 if (ret == false)
1120 ret = 1; // 1 for failure
1121 else
1122 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001123 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001124 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001125}
Dees_Troy51a0e822012-09-05 15:24:24 -04001126
that3f7b1ac2014-12-30 11:30:13 +01001127int GUIAction::fixpermissions(std::string arg)
1128{
1129 operation_start("Fix Permissions");
1130 LOGINFO("fix permissions started!\n");
1131 if (simulate) {
1132 simulate_progress_bar();
1133 } else {
1134 int op_status = PartitionManager.Fix_Permissions();
1135 if (op_status != 0)
1136 op_status = 1; // failure
1137 operation_end(op_status);
1138 }
1139 return 0;
1140}
1141
1142int GUIAction::dd(std::string arg)
1143{
1144 operation_start("imaging");
1145
1146 if (simulate) {
1147 simulate_progress_bar();
1148 } else {
1149 string cmd = "dd " + arg;
1150 TWFunc::Exec_Cmd(cmd);
1151 }
1152 operation_end(0);
1153 return 0;
1154}
1155
1156int GUIAction::partitionsd(std::string arg)
1157{
1158 operation_start("Partition SD Card");
1159 int ret_val = 0;
1160
1161 if (simulate) {
1162 simulate_progress_bar();
1163 } else {
1164 int allow_partition;
1165 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1166 if (allow_partition == 0) {
1167 gui_print("This device does not have a real SD Card!\nAborting!\n");
1168 } else {
1169 if (!PartitionManager.Partition_SDCard())
1170 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001171 }
that3f7b1ac2014-12-30 11:30:13 +01001172 }
1173 operation_end(ret_val);
1174 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001175
that3f7b1ac2014-12-30 11:30:13 +01001176}
Dees_Troy51a0e822012-09-05 15:24:24 -04001177
that3f7b1ac2014-12-30 11:30:13 +01001178int GUIAction::installhtcdumlock(std::string arg)
1179{
1180 operation_start("Install HTC Dumlock");
1181 if (simulate) {
1182 simulate_progress_bar();
1183 } else
1184 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001185
that3f7b1ac2014-12-30 11:30:13 +01001186 operation_end(0);
1187 return 0;
1188}
Dees_Troy51a0e822012-09-05 15:24:24 -04001189
that3f7b1ac2014-12-30 11:30:13 +01001190int GUIAction::htcdumlockrestoreboot(std::string arg)
1191{
1192 operation_start("HTC Dumlock Restore Boot");
1193 if (simulate) {
1194 simulate_progress_bar();
1195 } else
1196 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001197
that3f7b1ac2014-12-30 11:30:13 +01001198 operation_end(0);
1199 return 0;
1200}
Dees_Troy51a0e822012-09-05 15:24:24 -04001201
that3f7b1ac2014-12-30 11:30:13 +01001202int GUIAction::htcdumlockreflashrecovery(std::string arg)
1203{
1204 operation_start("HTC Dumlock Reflash Recovery");
1205 if (simulate) {
1206 simulate_progress_bar();
1207 } else
1208 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001209
that3f7b1ac2014-12-30 11:30:13 +01001210 operation_end(0);
1211 return 0;
1212}
Dees_Troy51a0e822012-09-05 15:24:24 -04001213
that3f7b1ac2014-12-30 11:30:13 +01001214int GUIAction::cmd(std::string arg)
1215{
1216 int op_status = 0;
1217
1218 operation_start("Command");
1219 LOGINFO("Running command: '%s'\n", arg.c_str());
1220 if (simulate) {
1221 simulate_progress_bar();
1222 } else {
1223 op_status = TWFunc::Exec_Cmd(arg);
1224 if (op_status != 0)
1225 op_status = 1;
1226 }
1227
1228 operation_end(op_status);
1229 return 0;
1230}
1231
1232int GUIAction::terminalcommand(std::string arg)
1233{
1234 int op_status = 0;
1235 string cmdpath, command;
1236
1237 DataManager::GetValue("tw_terminal_location", cmdpath);
1238 operation_start("CommandOutput");
1239 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1240 if (simulate) {
1241 simulate_progress_bar();
1242 operation_end(op_status);
1243 } else {
1244 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1245 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001246 DataManager::SetValue("tw_terminal_state", 1);
1247 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001248 FILE* fp;
1249 char line[512];
1250
1251 fp = popen(command.c_str(), "r");
1252 if (fp == NULL) {
1253 LOGERR("Error opening command to run.\n");
1254 } else {
1255 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1256 struct timeval timeout;
1257 fd_set fdset;
1258
1259 while(keep_going)
1260 {
1261 FD_ZERO(&fdset);
1262 FD_SET(fd, &fdset);
1263 timeout.tv_sec = 0;
1264 timeout.tv_usec = 400000;
1265 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1266 if (has_data == 0) {
1267 // Timeout reached
1268 DataManager::GetValue("tw_terminal_state", check);
1269 if (check == 0) {
1270 keep_going = 0;
1271 }
1272 } else if (has_data < 0) {
1273 // End of execution
1274 keep_going = 0;
1275 } else {
1276 // Try to read output
1277 memset(line, 0, sizeof(line));
1278 bytes_read = read(fd, line, sizeof(line));
1279 if (bytes_read > 0)
1280 gui_print("%s", line); // Display output
1281 else
1282 keep_going = 0; // Done executing
1283 }
1284 }
1285 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001286 }
thatc6085482015-01-09 22:12:43 +01001287 DataManager::SetValue("tw_operation_status", 0);
1288 DataManager::SetValue("tw_operation_state", 1);
1289 DataManager::SetValue("tw_terminal_state", 0);
1290 DataManager::SetValue("tw_background_thread_running", 0);
1291 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001292 }
1293 return 0;
1294}
1295
1296int GUIAction::killterminal(std::string arg)
1297{
1298 int op_status = 0;
1299
1300 LOGINFO("Sending kill command...\n");
1301 operation_start("KillCommand");
1302 DataManager::SetValue("tw_operation_status", 0);
1303 DataManager::SetValue("tw_operation_state", 1);
1304 DataManager::SetValue("tw_terminal_state", 0);
1305 DataManager::SetValue("tw_background_thread_running", 0);
1306 DataManager::SetValue(TW_ACTION_BUSY, 0);
1307 return 0;
1308}
1309
1310int GUIAction::reinjecttwrp(std::string arg)
1311{
1312 int op_status = 0;
1313 operation_start("ReinjectTWRP");
1314 gui_print("Injecting TWRP into boot image...\n");
1315 if (simulate) {
1316 simulate_progress_bar();
1317 } else {
1318 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1319 gui_print("TWRP injection complete.\n");
1320 }
1321
1322 operation_end(op_status);
1323 return 0;
1324}
1325
1326int GUIAction::checkbackupname(std::string arg)
1327{
1328 int op_status = 0;
1329
1330 operation_start("CheckBackupName");
1331 if (simulate) {
1332 simulate_progress_bar();
1333 } else {
1334 op_status = PartitionManager.Check_Backup_Name(true);
1335 if (op_status != 0)
1336 op_status = 1;
1337 }
1338
1339 operation_end(op_status);
1340 return 0;
1341}
1342
1343int GUIAction::decrypt(std::string arg)
1344{
1345 int op_status = 0;
1346
1347 operation_start("Decrypt");
1348 if (simulate) {
1349 simulate_progress_bar();
1350 } else {
1351 string Password;
1352 DataManager::GetValue("tw_crypto_password", Password);
1353 op_status = PartitionManager.Decrypt_Device(Password);
1354 if (op_status != 0)
1355 op_status = 1;
1356 else {
that3f7b1ac2014-12-30 11:30:13 +01001357
1358 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1359
Ethan Yonkercf50da52015-01-12 21:59:07 -06001360 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001361
Ethan Yonkercf50da52015-01-12 21:59:07 -06001362 // Check for a custom theme and load it if exists
1363 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1364 if (has_datamedia != 0) {
1365 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1366 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1367 } else {
1368 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001369 }
1370 }
1371 }
1372 }
1373
1374 operation_end(op_status);
1375 return 0;
1376}
1377
thatcc8ddca2015-01-03 01:59:36 +01001378int GUIAction::adbsideload(std::string arg)
1379{
1380 operation_start("Sideload");
1381 if (simulate) {
1382 simulate_progress_bar();
1383 operation_end(0);
1384 } else {
thatc6085482015-01-09 22:12:43 +01001385 gui_print("Starting ADB sideload feature...\n");
1386 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1387
1388 // wait for the adb connection
1389 int ret = apply_from_adb("/", &sideload_child_pid);
1390 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1391
1392 if (ret != 0) {
1393 if (ret == -2)
1394 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1395 ret = 1; // failure
1396 } else {
1397 int wipe_cache = 0;
1398 int wipe_dalvik = 0;
1399 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1400
1401 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1402 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1403 PartitionManager.Wipe_By_Path("/cache");
1404 if (wipe_dalvik)
1405 PartitionManager.Wipe_Dalvik_Cache();
1406 } else {
1407 ret = 1; // failure
1408 }
thatcc8ddca2015-01-03 01:59:36 +01001409 }
thatc6085482015-01-09 22:12:43 +01001410 if (sideload_child_pid) {
1411 LOGINFO("Signaling child sideload process to exit.\n");
1412 struct stat st;
1413 // Calling stat() on this magic filename signals the minadbd
1414 // subprocess to shut down.
1415 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1416 int status;
1417 LOGINFO("Waiting for child sideload process to exit.\n");
1418 waitpid(sideload_child_pid, &status, 0);
1419 }
1420
1421 TWFunc::Toggle_MTP(mtp_was_enabled);
1422 reinject_after_flash();
1423 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001424 }
that3f7b1ac2014-12-30 11:30:13 +01001425 return 0;
1426}
1427
1428int GUIAction::adbsideloadcancel(std::string arg)
1429{
that3f7b1ac2014-12-30 11:30:13 +01001430 struct stat st;
1431 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1432 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001433 LOGINFO("Signaling child sideload process to exit.\n");
1434 // Calling stat() on this magic filename signals the minadbd
1435 // subprocess to shut down.
1436 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1437 if (!sideload_child_pid) {
1438 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001439 return 0;
1440 }
thatcc8ddca2015-01-03 01:59:36 +01001441 ::sleep(1);
1442 LOGINFO("Killing child sideload process.\n");
1443 kill(sideload_child_pid, SIGTERM);
1444 int status;
1445 LOGINFO("Waiting for child sideload process to exit.\n");
1446 waitpid(sideload_child_pid, &status, 0);
1447 sideload_child_pid = 0;
1448 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001449 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1450 return 0;
1451}
1452
1453int GUIAction::openrecoveryscript(std::string arg)
1454{
1455 operation_start("OpenRecoveryScript");
1456 if (simulate) {
1457 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001458 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001459 } else {
thatc6085482015-01-09 22:12:43 +01001460 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1461 // that we converted to ORS commands during boot in recovery.cpp.
1462 // Run those first.
1463 int reboot = 0;
1464 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1465 gui_print("Processing AOSP recovery commands...\n");
1466 if (OpenRecoveryScript::run_script_file() == 0) {
1467 reboot = 1;
1468 }
that3f7b1ac2014-12-30 11:30:13 +01001469 }
thatc6085482015-01-09 22:12:43 +01001470 // Check for the ORS file in /cache and attempt to run those commands.
1471 if (OpenRecoveryScript::check_for_script_file()) {
1472 gui_print("Processing OpenRecoveryScript file...\n");
1473 if (OpenRecoveryScript::run_script_file() == 0) {
1474 reboot = 1;
1475 }
1476 }
1477 if (reboot) {
1478 usleep(2000000); // Sleep for 2 seconds before rebooting
1479 TWFunc::tw_reboot(rb_system);
1480 } else {
1481 DataManager::SetValue("tw_page_done", 1);
1482 }
1483 operation_end(1);
1484 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001485 }
1486 return 0;
1487}
1488
1489int GUIAction::installsu(std::string arg)
1490{
1491 int op_status = 0;
1492
1493 operation_start("Install SuperSU");
1494 if (simulate) {
1495 simulate_progress_bar();
1496 } else {
1497 if (!TWFunc::Install_SuperSU())
1498 op_status = 1;
1499 }
1500
1501 operation_end(op_status);
1502 return 0;
1503}
1504
1505int GUIAction::fixsu(std::string arg)
1506{
1507 int op_status = 0;
1508
1509 operation_start("Fixing Superuser Permissions");
1510 if (simulate) {
1511 simulate_progress_bar();
1512 } else {
1513 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1514 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1515 }
1516
1517 operation_end(op_status);
1518 return 0;
1519}
1520
1521int GUIAction::decrypt_backup(std::string arg)
1522{
1523 int op_status = 0;
1524
1525 operation_start("Try Restore Decrypt");
1526 if (simulate) {
1527 simulate_progress_bar();
1528 } else {
1529 string Restore_Path, Filename, Password;
1530 DataManager::GetValue("tw_restore", Restore_Path);
1531 Restore_Path += "/";
1532 DataManager::GetValue("tw_restore_password", Password);
1533 TWFunc::SetPerformanceMode(true);
1534 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1535 op_status = 0; // success
1536 else
1537 op_status = 1; // fail
1538 TWFunc::SetPerformanceMode(false);
1539 }
1540
1541 operation_end(op_status);
1542 return 0;
1543}
1544
1545int GUIAction::repair(std::string arg)
1546{
1547 int op_status = 0;
1548
1549 operation_start("Repair Partition");
1550 if (simulate) {
1551 simulate_progress_bar();
1552 } else {
1553 string part_path;
1554 DataManager::GetValue("tw_partition_mount_point", part_path);
1555 if (PartitionManager.Repair_By_Path(part_path, true)) {
1556 op_status = 0; // success
1557 } else {
1558 LOGERR("Error repairing file system.\n");
1559 op_status = 1; // fail
1560 }
1561 }
1562
1563 operation_end(op_status);
1564 return 0;
1565}
1566
1567int GUIAction::changefilesystem(std::string arg)
1568{
1569 int op_status = 0;
1570
1571 operation_start("Change File System");
1572 if (simulate) {
1573 simulate_progress_bar();
1574 } else {
1575 string part_path, file_system;
1576 DataManager::GetValue("tw_partition_mount_point", part_path);
1577 DataManager::GetValue("tw_action_new_file_system", file_system);
1578 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1579 op_status = 0; // success
1580 } else {
1581 LOGERR("Error changing file system.\n");
1582 op_status = 1; // fail
1583 }
1584 }
1585 PartitionManager.Update_System_Details();
1586 operation_end(op_status);
1587 return 0;
1588}
1589
1590int GUIAction::startmtp(std::string arg)
1591{
1592 int op_status = 0;
1593
1594 operation_start("Start MTP");
1595 if (PartitionManager.Enable_MTP())
1596 op_status = 0; // success
1597 else
1598 op_status = 1; // fail
1599
1600 operation_end(op_status);
1601 return 0;
1602}
1603
1604int GUIAction::stopmtp(std::string arg)
1605{
1606 int op_status = 0;
1607
1608 operation_start("Stop MTP");
1609 if (PartitionManager.Disable_MTP())
1610 op_status = 0; // success
1611 else
1612 op_status = 1; // fail
1613
1614 operation_end(op_status);
1615 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001616}
1617
Ethan Yonker96af84a2015-01-05 14:58:36 -06001618int GUIAction::flashimage(std::string arg)
1619{
1620 int op_status = 0;
1621
1622 operation_start("Flash Image");
1623 string path, filename, full_filename;
1624 DataManager::GetValue("tw_zip_location", path);
1625 DataManager::GetValue("tw_file", filename);
1626 full_filename = path + "/" + filename;
1627 if (PartitionManager.Flash_Image(full_filename))
1628 op_status = 0; // success
1629 else
1630 op_status = 1; // fail
1631
1632 operation_end(op_status);
1633 return 0;
1634}
1635
Dees_Troy51a0e822012-09-05 15:24:24 -04001636int GUIAction::getKeyByName(std::string key)
1637{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001638 if (key == "home") return KEY_HOME;
1639 else if (key == "menu") return KEY_MENU;
1640 else if (key == "back") return KEY_BACK;
1641 else if (key == "search") return KEY_SEARCH;
1642 else if (key == "voldown") return KEY_VOLUMEDOWN;
1643 else if (key == "volup") return KEY_VOLUMEUP;
1644 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001645 int ret_val;
1646 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1647 if (!ret_val)
1648 return KEY_POWER;
1649 else
1650 return ret_val;
1651 }
1652
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001653 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001654}