blob: 953439906a35ea6deaa82cd9e83baffee6e70fbc [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050046#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040048extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000049#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040052#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000053#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040054#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060055#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060056#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040057};
58
59#include "rapidxml.hpp"
60#include "objects.hpp"
61
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070062#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050063extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070064#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040065
Dees_Troy51a0e822012-09-05 15:24:24 -040066void curtainClose(void);
67
that3f7b1ac2014-12-30 11:30:13 +010068GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010069std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010070static string zip_queue[10];
71static int zip_queue_index;
72static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010073pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010074
thatc6085482015-01-09 22:12:43 +010075static ActionThread action_thread;
76
77static void *ActionThread_work_wrapper(void *data)
78{
79 action_thread.run(data);
80 return NULL;
81}
82
83ActionThread::ActionThread()
84{
85 m_thread_running = false;
86 pthread_mutex_init(&m_act_lock, NULL);
87}
88
89ActionThread::~ActionThread()
90{
91 pthread_mutex_lock(&m_act_lock);
92 if(m_thread_running) {
93 pthread_mutex_unlock(&m_act_lock);
94 pthread_join(m_thread, NULL);
95 } else {
96 pthread_mutex_unlock(&m_act_lock);
97 }
98 pthread_mutex_destroy(&m_act_lock);
99}
100
that7d3b54f2015-01-09 22:52:51 +0100101void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100102{
103 pthread_mutex_lock(&m_act_lock);
104 if (m_thread_running) {
105 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100106 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
107 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100108 } else {
109 m_thread_running = true;
110 pthread_mutex_unlock(&m_act_lock);
111 ThreadData *d = new ThreadData;
112 d->act = act;
thatc6085482015-01-09 22:12:43 +0100113
114 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
115 }
116}
117
118void ActionThread::run(void *data)
119{
120 ThreadData *d = (ThreadData*)data;
121 GUIAction* act = d->act;
122
123 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100124 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100125 act->doAction(*it);
126
127 pthread_mutex_lock(&m_act_lock);
128 m_thread_running = false;
129 pthread_mutex_unlock(&m_act_lock);
130 delete d;
131}
132
Dees_Troy51a0e822012-09-05 15:24:24 -0400133GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100134 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400135{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 xml_node<>* child;
137 xml_node<>* actions;
138 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141
that3f7b1ac2014-12-30 11:30:13 +0100142 if (mf.empty()) {
thatc6085482015-01-09 22:12:43 +0100143 // These actions will be run in the caller's thread
that3f7b1ac2014-12-30 11:30:13 +0100144 mf["reboot"] = &GUIAction::reboot;
145 mf["home"] = &GUIAction::home;
146 mf["key"] = &GUIAction::key;
147 mf["page"] = &GUIAction::page;
148 mf["reload"] = &GUIAction::reload;
149 mf["readBackup"] = &GUIAction::readBackup;
150 mf["set"] = &GUIAction::set;
151 mf["clear"] = &GUIAction::clear;
152 mf["mount"] = &GUIAction::mount;
153 mf["unmount"] = &GUIAction::unmount;
154 mf["umount"] = &GUIAction::unmount;
155 mf["restoredefaultsettings"] = &GUIAction::restoredefaultsettings;
156 mf["copylog"] = &GUIAction::copylog;
157 mf["compute"] = &GUIAction::compute;
158 mf["addsubtract"] = &GUIAction::compute;
159 mf["setguitimezone"] = &GUIAction::setguitimezone;
160 mf["overlay"] = &GUIAction::overlay;
161 mf["queuezip"] = &GUIAction::queuezip;
162 mf["cancelzip"] = &GUIAction::cancelzip;
163 mf["queueclear"] = &GUIAction::queueclear;
164 mf["sleep"] = &GUIAction::sleep;
165 mf["appenddatetobackupname"] = &GUIAction::appenddatetobackupname;
166 mf["generatebackupname"] = &GUIAction::generatebackupname;
167 mf["checkpartitionlist"] = &GUIAction::checkpartitionlist;
168 mf["getpartitiondetails"] = &GUIAction::getpartitiondetails;
169 mf["screenshot"] = &GUIAction::screenshot;
170 mf["setbrightness"] = &GUIAction::setbrightness;
that3f7b1ac2014-12-30 11:30:13 +0100171 mf["fileexists"] = &GUIAction::fileexists;
thatc6085482015-01-09 22:12:43 +0100172 mf["killterminal"] = &GUIAction::killterminal;
173 mf["checkbackupname"] = &GUIAction::checkbackupname;
174 mf["adbsideloadcancel"] = &GUIAction::adbsideloadcancel;
175 mf["fixsu"] = &GUIAction::fixsu;
176 mf["startmtp"] = &GUIAction::startmtp;
177 mf["stopmtp"] = &GUIAction::stopmtp;
178
179 // remember actions that run in the caller thread
180 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
181 setActionsRunningInCallerThread.insert(it->first);
182
183 // These actions will run in a separate thread
that3f7b1ac2014-12-30 11:30:13 +0100184 mf["flash"] = &GUIAction::flash;
185 mf["wipe"] = &GUIAction::wipe;
186 mf["refreshsizes"] = &GUIAction::refreshsizes;
187 mf["nandroid"] = &GUIAction::nandroid;
188 mf["fixpermissions"] = &GUIAction::fixpermissions;
189 mf["dd"] = &GUIAction::dd;
190 mf["partitionsd"] = &GUIAction::partitionsd;
191 mf["installhtcdumlock"] = &GUIAction::installhtcdumlock;
192 mf["htcdumlockrestoreboot"] = &GUIAction::htcdumlockrestoreboot;
193 mf["htcdumlockreflashrecovery"] = &GUIAction::htcdumlockreflashrecovery;
194 mf["cmd"] = &GUIAction::cmd;
195 mf["terminalcommand"] = &GUIAction::terminalcommand;
that3f7b1ac2014-12-30 11:30:13 +0100196 mf["reinjecttwrp"] = &GUIAction::reinjecttwrp;
that3f7b1ac2014-12-30 11:30:13 +0100197 mf["decrypt"] = &GUIAction::decrypt;
198 mf["adbsideload"] = &GUIAction::adbsideload;
that3f7b1ac2014-12-30 11:30:13 +0100199 mf["openrecoveryscript"] = &GUIAction::openrecoveryscript;
200 mf["installsu"] = &GUIAction::installsu;
that3f7b1ac2014-12-30 11:30:13 +0100201 mf["decrypt_backup"] = &GUIAction::decrypt_backup;
202 mf["repair"] = &GUIAction::repair;
203 mf["changefilesystem"] = &GUIAction::changefilesystem;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600204 mf["flashimage"] = &GUIAction::flashimage;
that3f7b1ac2014-12-30 11:30:13 +0100205 }
206
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 // First, get the action
208 actions = node->first_node("actions");
209 if (actions) child = actions->first_node("action");
210 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 while (child)
215 {
216 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 attr = child->first_attribute("function");
219 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500220
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 action.mFunction = attr->value();
222 action.mArg = child->value();
223 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 child = child->next_sibling("action");
226 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 // Now, let's get either the key or region
229 child = node->first_node("touch");
230 if (child)
231 {
232 attr = child->first_attribute("key");
233 if (attr)
234 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100235 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
236 for(size_t i = 0; i < keys.size(); ++i)
237 {
238 const int key = getKeyByName(keys[i]);
239 mKeys[key] = false;
240 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 }
242 else
243 {
244 attr = child->first_attribute("x");
245 if (!attr) return;
246 mActionX = atol(attr->value());
247 attr = child->first_attribute("y");
248 if (!attr) return;
249 mActionY = atol(attr->value());
250 attr = child->first_attribute("w");
251 if (!attr) return;
252 mActionW = atol(attr->value());
253 attr = child->first_attribute("h");
254 if (!attr) return;
255 mActionH = atol(attr->value());
256 }
257 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400258}
259
260int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
261{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 if (state == TOUCH_RELEASE)
263 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266}
267
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100268int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400269{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100270 if (mKeys.empty())
271 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100273 std::map<int, bool>::iterator itr = mKeys.find(key);
274 if(itr == mKeys.end())
275 return 0;
276
277 bool prevState = itr->second;
278 itr->second = down;
279
280 // If there is only one key for this action, wait for key up so it
281 // doesn't trigger with multi-key actions.
282 // Else, check if all buttons are pressed, then consume their release events
283 // so they don't trigger one-button actions and reset mKeys pressed status
284 if(mKeys.size() == 1) {
285 if(!down && prevState)
286 doActions();
287 } else if(down) {
288 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
289 if(!itr->second)
290 return 0;
291 }
292
293 // Passed, all req buttons are pressed, reset them and consume release events
294 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
295 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
296 kb->ConsumeKeyRelease(itr->first);
297 itr->second = false;
298 }
299
300 doActions();
301 }
302
Dees_Troy51a0e822012-09-05 15:24:24 -0400303 return 0;
304}
305
Vojtech Bocek07220562014-02-08 02:05:33 +0100306int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400307{
Vojtech Bocek07220562014-02-08 02:05:33 +0100308 GUIObject::NotifyVarChange(varName, value);
309
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200311 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100312 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400314
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200315 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400316}
317
318void GUIAction::simulate_progress_bar(void)
319{
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 for (int i = 0; i < 5; i++)
322 {
323 usleep(500000);
324 DataManager::SetValue("ui_progress", i * 20);
325 }
326}
327
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600328int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400329{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200330 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400331
332 DataManager::SetValue("ui_progress", 0);
333
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 if (filename.empty())
335 {
336 LOGERR("No file specified.\n");
337 return -1;
338 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400341 return -1;
342
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 if (simulate) {
344 simulate_progress_bar();
345 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400346 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
348 // Now, check if we need to ensure TWRP remains installed...
349 struct stat st;
350 if (stat("/sbin/installTwrp", &st) == 0)
351 {
352 DataManager::SetValue("tw_operation", "Configuring TWRP");
353 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200355 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400358 }
359 }
360 }
361
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200362 // Done
363 DataManager::SetValue("ui_progress", 100);
364 DataManager::SetValue("ui_progress", 0);
365 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366}
367
thatc6085482015-01-09 22:12:43 +0100368bool GUIAction::needsToRunInSeparateThread(const GUIAction::Action& action)
369{
370 return setActionsRunningInCallerThread.find(action.mFunction) == setActionsRunningInCallerThread.end();
371}
372
Dees_Troy51a0e822012-09-05 15:24:24 -0400373int GUIAction::doActions()
374{
that3f7b1ac2014-12-30 11:30:13 +0100375 if (mActions.size() < 1)
376 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377
that7d3b54f2015-01-09 22:52:51 +0100378 bool needThread = false;
that3f7b1ac2014-12-30 11:30:13 +0100379 std::vector<Action>::iterator it;
380 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100381 {
382 if (needsToRunInSeparateThread(*it))
383 {
that7d3b54f2015-01-09 22:52:51 +0100384 needThread = true;
thatc6085482015-01-09 22:12:43 +0100385 break;
386 }
that7d3b54f2015-01-09 22:52:51 +0100387 }
388 if (needThread)
389 {
390 action_thread.threadActions(this);
391 }
392 else
393 {
394 for (it = mActions.begin(); it != mActions.end(); ++it)
395 doAction(*it);
thatc6085482015-01-09 22:12:43 +0100396 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200398 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400399}
400
that3f7b1ac2014-12-30 11:30:13 +0100401int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400402{
that3f7b1ac2014-12-30 11:30:13 +0100403 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400404
that3f7b1ac2014-12-30 11:30:13 +0100405 std::string function = gui_parse_text(action.mFunction);
406 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400407
that3f7b1ac2014-12-30 11:30:13 +0100408 // find function and execute it
409 mapFunc::const_iterator funcitr = mf.find(function);
410 if (funcitr != mf.end())
411 return (this->*funcitr->second)(arg);
412
413 LOGERR("Unknown action '%s'\n", function.c_str());
414 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415}
416
417void GUIAction::operation_start(const string operation_name)
418{
that3f7b1ac2014-12-30 11:30:13 +0100419 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000420 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 DataManager::SetValue(TW_ACTION_BUSY, 1);
422 DataManager::SetValue("ui_progress", 0);
423 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400424 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600425 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400426}
427
that3f7b1ac2014-12-30 11:30:13 +0100428void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400429{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000430 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 DataManager::SetValue("ui_progress", 100);
433 if (simulate) {
434 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
435 if (simulate_fail != 0)
436 DataManager::SetValue("tw_operation_status", 1);
437 else
438 DataManager::SetValue("tw_operation_status", 0);
439 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500440 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500442 }
443 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500445 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 }
447 DataManager::SetValue("tw_operation_state", 1);
448 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700449#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500450 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700451#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000452 time(&Stop);
453 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600454 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100455 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400456}
457
that3f7b1ac2014-12-30 11:30:13 +0100458int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400459{
that3f7b1ac2014-12-30 11:30:13 +0100460 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
that3f7b1ac2014-12-30 11:30:13 +0100462 sync();
463 DataManager::SetValue("tw_gui_done", 1);
464 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400465
that3f7b1ac2014-12-30 11:30:13 +0100466 return 0;
467}
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
that3f7b1ac2014-12-30 11:30:13 +0100469int GUIAction::home(std::string arg)
470{
471 PageManager::SelectPackage("TWRP");
472 gui_changePage("main");
473 return 0;
474}
Dees_Troy51a0e822012-09-05 15:24:24 -0400475
that3f7b1ac2014-12-30 11:30:13 +0100476int GUIAction::key(std::string arg)
477{
478 const int key = getKeyByName(arg);
479 PageManager::NotifyKey(key, true);
480 PageManager::NotifyKey(key, false);
481 return 0;
482}
483
484int GUIAction::page(std::string arg)
485{
486 std::string page_name = gui_parse_text(arg);
487 return gui_changePage(page_name);
488}
489
490int GUIAction::reload(std::string arg)
491{
492 int check = 0, ret_val = 0;
493 std::string theme_path;
494
495 operation_start("Reload Theme");
496 theme_path = DataManager::GetSettingsStoragePath();
497 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
498 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
499 check = 1;
500 }
501
502 theme_path += "/TWRP/theme/ui.zip";
503 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600504 {
that3f7b1ac2014-12-30 11:30:13 +0100505 // Loading the custom theme failed - try loading the stock theme
506 LOGINFO("Attempting to reload stock theme...\n");
507 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 {
that3f7b1ac2014-12-30 11:30:13 +0100509 LOGERR("Failed to load base packages.\n");
510 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 }
that3f7b1ac2014-12-30 11:30:13 +0100513 operation_end(ret_val);
514 return 0;
515}
Dees_Troy51a0e822012-09-05 15:24:24 -0400516
that3f7b1ac2014-12-30 11:30:13 +0100517int GUIAction::readBackup(std::string arg)
518{
519 string Restore_Name;
520 DataManager::GetValue("tw_restore", Restore_Name);
521 PartitionManager.Set_Restore_Files(Restore_Name);
522 return 0;
523}
524
525int GUIAction::set(std::string arg)
526{
527 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200528 {
that3f7b1ac2014-12-30 11:30:13 +0100529 string varName = arg.substr(0, arg.find('='));
530 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400531
that3f7b1ac2014-12-30 11:30:13 +0100532 DataManager::GetValue(value, value);
533 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200534 }
that3f7b1ac2014-12-30 11:30:13 +0100535 else
536 DataManager::SetValue(arg, "1");
537 return 0;
538}
Dees_Troy51a0e822012-09-05 15:24:24 -0400539
that3f7b1ac2014-12-30 11:30:13 +0100540int GUIAction::clear(std::string arg)
541{
542 DataManager::SetValue(arg, "0");
543 return 0;
544}
Dees_Troy51a0e822012-09-05 15:24:24 -0400545
that3f7b1ac2014-12-30 11:30:13 +0100546int GUIAction::mount(std::string arg)
547{
548 if (arg == "usb") {
549 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100551 PartitionManager.usb_storage_enable();
552 else
553 gui_print("Simulating actions...\n");
554 } else if (!simulate) {
555 PartitionManager.Mount_By_Path(arg, true);
556 PartitionManager.Add_MTP_Storage(arg);
557 } else
558 gui_print("Simulating actions...\n");
559 return 0;
560}
561
562int GUIAction::unmount(std::string arg)
563{
564 if (arg == "usb") {
565 if (!simulate)
566 PartitionManager.usb_storage_disable();
567 else
568 gui_print("Simulating actions...\n");
569 DataManager::SetValue(TW_ACTION_BUSY, 0);
570 } else if (!simulate) {
571 PartitionManager.UnMount_By_Path(arg, true);
572 } else
573 gui_print("Simulating actions...\n");
574 return 0;
575}
576
577int GUIAction::restoredefaultsettings(std::string arg)
578{
579 operation_start("Restore Defaults");
580 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
581 gui_print("Simulating actions...\n");
582 else {
583 DataManager::ResetDefaults();
584 PartitionManager.Update_System_Details();
585 PartitionManager.Mount_Current_Storage(true);
586 }
587 operation_end(0);
588 return 0;
589}
590
591int GUIAction::copylog(std::string arg)
592{
593 operation_start("Copy Log");
594 if (!simulate)
595 {
596 string dst;
597 PartitionManager.Mount_Current_Storage(true);
598 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
599 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
600 tw_set_default_metadata(dst.c_str());
601 sync();
602 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
603 } else
604 simulate_progress_bar();
605 operation_end(0);
606 return 0;
607}
608
609
610int GUIAction::compute(std::string arg)
611{
612 if (arg.find("+") != string::npos)
613 {
614 string varName = arg.substr(0, arg.find('+'));
615 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
616 int amount_to_add = atoi(string_to_add.c_str());
617 int value;
618
619 DataManager::GetValue(varName, value);
620 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 return 0;
622 }
that3f7b1ac2014-12-30 11:30:13 +0100623 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 {
that3f7b1ac2014-12-30 11:30:13 +0100625 string varName = arg.substr(0, arg.find('-'));
626 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
627 int amount_to_subtract = atoi(string_to_subtract.c_str());
628 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629
that3f7b1ac2014-12-30 11:30:13 +0100630 DataManager::GetValue(varName, value);
631 value -= amount_to_subtract;
632 if (value <= 0)
633 value = 0;
634 DataManager::SetValue(varName, value);
635 return 0;
636 }
637 if (arg.find("*") != string::npos)
638 {
639 string varName = arg.substr(0, arg.find('*'));
640 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
641 int multiply_by = atoi(multiply_by_str.c_str());
642 int value;
643
644 DataManager::GetValue(varName, value);
645 DataManager::SetValue(varName, value*multiply_by);
646 return 0;
647 }
648 if (arg.find("/") != string::npos)
649 {
650 string varName = arg.substr(0, arg.find('/'));
651 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
652 int divide_by = atoi(divide_by_str.c_str());
653 int value;
654
655 if(divide_by != 0)
656 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400657 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100658 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 }
660 return 0;
661 }
that3f7b1ac2014-12-30 11:30:13 +0100662 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
663 return -1;
664}
Dees_Troy51a0e822012-09-05 15:24:24 -0400665
that3f7b1ac2014-12-30 11:30:13 +0100666int GUIAction::setguitimezone(std::string arg)
667{
668 string SelectedZone;
669 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
670 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
671 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
672
673 int dst;
674 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
675
676 string offset;
677 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
678
679 string NewTimeZone = Zone;
680 if (offset != "0")
681 NewTimeZone += ":" + offset;
682
683 if (dst != 0)
684 NewTimeZone += DSTZone;
685
686 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
687 DataManager::update_tz_environment_variables();
688 return 0;
689}
690
691int GUIAction::overlay(std::string arg)
692{
693 return gui_changeOverlay(arg);
694}
695
696int GUIAction::queuezip(std::string arg)
697{
698 if (zip_queue_index >= 10) {
699 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 return 0;
701 }
that3f7b1ac2014-12-30 11:30:13 +0100702 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
703 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
704 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100706 }
707 return 0;
708}
709
710int GUIAction::cancelzip(std::string arg)
711{
712 if (zip_queue_index <= 0) {
713 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100715 } else {
716 zip_queue_index--;
717 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 }
that3f7b1ac2014-12-30 11:30:13 +0100719 return 0;
720}
Dees_Troy51a0e822012-09-05 15:24:24 -0400721
that3f7b1ac2014-12-30 11:30:13 +0100722int GUIAction::queueclear(std::string arg)
723{
724 zip_queue_index = 0;
725 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
726 return 0;
727}
Dees_Troy51a0e822012-09-05 15:24:24 -0400728
that3f7b1ac2014-12-30 11:30:13 +0100729int GUIAction::sleep(std::string arg)
730{
731 operation_start("Sleep");
732 usleep(atoi(arg.c_str()));
733 operation_end(0);
734 return 0;
735}
Dees Troyb21cc642013-09-10 17:36:41 +0000736
that3f7b1ac2014-12-30 11:30:13 +0100737int GUIAction::appenddatetobackupname(std::string arg)
738{
739 operation_start("AppendDateToBackupName");
740 string Backup_Name;
741 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
742 Backup_Name += TWFunc::Get_Current_Date();
743 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
744 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
745 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
746 operation_end(0);
747 return 0;
748}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500749
that3f7b1ac2014-12-30 11:30:13 +0100750int GUIAction::generatebackupname(std::string arg)
751{
752 operation_start("GenerateBackupName");
753 TWFunc::Auto_Generate_Backup_Name();
754 operation_end(0);
755 return 0;
756}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500757
that3f7b1ac2014-12-30 11:30:13 +0100758int GUIAction::checkpartitionlist(std::string arg)
759{
760 string Wipe_List, wipe_path;
761 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500762
that3f7b1ac2014-12-30 11:30:13 +0100763 DataManager::GetValue("tw_wipe_list", Wipe_List);
764 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
765 if (!Wipe_List.empty()) {
766 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
767 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
768 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
769 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
770 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
771 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 } else {
that3f7b1ac2014-12-30 11:30:13 +0100773 count++;
774 }
775 start_pos = end_pos + 1;
776 end_pos = Wipe_List.find(";", start_pos);
777 }
778 DataManager::SetValue("tw_check_partition_list", count);
779 } else {
780 DataManager::SetValue("tw_check_partition_list", 0);
781 }
782 return 0;
783}
Dees_Troy51a0e822012-09-05 15:24:24 -0400784
that3f7b1ac2014-12-30 11:30:13 +0100785int GUIAction::getpartitiondetails(std::string arg)
786{
787 string Wipe_List, wipe_path;
788 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400789
that3f7b1ac2014-12-30 11:30:13 +0100790 DataManager::GetValue("tw_wipe_list", Wipe_List);
791 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
792 if (!Wipe_List.empty()) {
793 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
794 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
795 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
796 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
797 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
798 // Do nothing
799 } else {
800 DataManager::SetValue("tw_partition_path", wipe_path);
801 break;
802 }
803 start_pos = end_pos + 1;
804 end_pos = Wipe_List.find(";", start_pos);
805 }
806 if (!wipe_path.empty()) {
807 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
808 if (Part) {
809 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500810
that3f7b1ac2014-12-30 11:30:13 +0100811 DataManager::SetValue("tw_partition_name", Part->Display_Name);
812 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
813 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
814 DataManager::SetValue("tw_partition_size", Part->Size / mb);
815 DataManager::SetValue("tw_partition_used", Part->Used / mb);
816 DataManager::SetValue("tw_partition_free", Part->Free / mb);
817 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
818 DataManager::SetValue("tw_partition_removable", Part->Removable);
819 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
820
821 if (Part->Can_Repair())
822 DataManager::SetValue("tw_partition_can_repair", 1);
823 else
824 DataManager::SetValue("tw_partition_can_repair", 0);
825 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
826 DataManager::SetValue("tw_partition_vfat", 1);
827 else
828 DataManager::SetValue("tw_partition_vfat", 0);
829 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
830 DataManager::SetValue("tw_partition_exfat", 1);
831 else
832 DataManager::SetValue("tw_partition_exfat", 0);
833 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
834 DataManager::SetValue("tw_partition_f2fs", 1);
835 else
836 DataManager::SetValue("tw_partition_f2fs", 0);
837 if (TWFunc::Path_Exists("/sbin/mke2fs"))
838 DataManager::SetValue("tw_partition_ext", 1);
839 else
840 DataManager::SetValue("tw_partition_ext", 0);
841 return 0;
842 } else {
843 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
844 }
845 }
846 }
847 DataManager::SetValue("tw_partition_name", "");
848 DataManager::SetValue("tw_partition_file_system", "");
849 return 0;
850}
851
852int GUIAction::screenshot(std::string arg)
853{
854 time_t tm;
855 char path[256];
856 int path_len;
857 uid_t uid = -1;
858 gid_t gid = -1;
859
860 struct passwd *pwd = getpwnam("media_rw");
861 if(pwd) {
862 uid = pwd->pw_uid;
863 gid = pwd->pw_gid;
864 }
865
866 const std::string storage = DataManager::GetCurrentStoragePath();
867 if(PartitionManager.Is_Mounted_By_Path(storage)) {
868 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
869 } else {
870 strcpy(path, "/tmp/");
871 }
872
873 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
874 return 0;
875
876 tm = time(NULL);
877 path_len = strlen(path);
878
879 // Screenshot_2014-01-01-18-21-38.png
880 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
881
882 int res = gr_save_screenshot(path);
883 if(res == 0) {
884 chmod(path, 0666);
885 chown(path, uid, gid);
886
887 gui_print("Screenshot was saved to %s\n", path);
888
889 // blink to notify that the screenshow was taken
890 gr_color(255, 255, 255, 255);
891 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
892 gr_flip();
893 gui_forceRender();
894 } else {
895 LOGERR("Failed to take a screenshot!\n");
896 }
897 return 0;
898}
899
900int GUIAction::setbrightness(std::string arg)
901{
902 return TWFunc::Set_Brightness(arg);
903}
904
905int GUIAction::fileexists(std::string arg)
906{
907 struct stat st;
908 string newpath = arg + "/.";
909
910 operation_start("FileExists");
911 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
912 operation_end(0);
913 else
914 operation_end(1);
915 return 0;
916}
917
thatcc8ddca2015-01-03 01:59:36 +0100918void GUIAction::reinject_after_flash()
919{
920 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
921 operation_start("ReinjectTWRP");
922 gui_print("Injecting TWRP into boot image...\n");
923 if (simulate) {
924 simulate_progress_bar();
925 } else {
926 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
927 if (Boot == NULL || Boot->Current_File_System != "emmc")
928 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
929 else {
930 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
931 TWFunc::Exec_Cmd(injectcmd);
932 }
933 gui_print("TWRP injection complete.\n");
934 }
935 }
936}
937
that3f7b1ac2014-12-30 11:30:13 +0100938int GUIAction::flash(std::string arg)
939{
940 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600941 // We're going to jump to this page first, like a loading page
942 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100943 for (i=0; i<zip_queue_index; i++) {
944 operation_start("Flashing");
945 DataManager::SetValue("tw_filename", zip_queue[i]);
946 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
947
948 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600949 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100950 TWFunc::SetPerformanceMode(false);
951 if (ret_val != 0) {
952 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +0100953 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600954 break;
that3f7b1ac2014-12-30 11:30:13 +0100955 }
956 }
957 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +0100958
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600959 if (wipe_cache) {
960 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +0100961 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600962 }
that3f7b1ac2014-12-30 11:30:13 +0100963
thatcc8ddca2015-01-03 01:59:36 +0100964 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100965 PartitionManager.Update_System_Details();
966 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600967 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100968 return 0;
969}
970
971int GUIAction::wipe(std::string arg)
972{
973 operation_start("Format");
974 DataManager::SetValue("tw_partition", arg);
975
976 int ret_val = false;
977
978 if (simulate) {
979 simulate_progress_bar();
980 } else {
981 if (arg == "data")
982 ret_val = PartitionManager.Factory_Reset();
983 else if (arg == "battery")
984 ret_val = PartitionManager.Wipe_Battery_Stats();
985 else if (arg == "rotate")
986 ret_val = PartitionManager.Wipe_Rotate_Data();
987 else if (arg == "dalvik")
988 ret_val = PartitionManager.Wipe_Dalvik_Cache();
989 else if (arg == "DATAMEDIA") {
990 ret_val = PartitionManager.Format_Data();
991 } else if (arg == "INTERNAL") {
992 int has_datamedia, dual_storage;
993
994 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
995 if (has_datamedia) {
996 ret_val = PartitionManager.Wipe_Media_From_Data();
997 } else {
998 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
999 }
1000 } else if (arg == "EXTERNAL") {
1001 string External_Path;
1002
1003 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1004 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1005 } else if (arg == "ANDROIDSECURE") {
1006 ret_val = PartitionManager.Wipe_Android_Secure();
1007 } else if (arg == "LIST") {
1008 string Wipe_List, wipe_path;
1009 bool skip = false;
1010 ret_val = true;
1011 TWPartition* wipe_part = NULL;
1012
1013 DataManager::GetValue("tw_wipe_list", Wipe_List);
1014 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1015 if (!Wipe_List.empty()) {
1016 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1017 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1018 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1019 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1020 if (wipe_path == "/and-sec") {
1021 if (!PartitionManager.Wipe_Android_Secure()) {
1022 LOGERR("Unable to wipe android secure\n");
1023 ret_val = false;
1024 break;
1025 } else {
1026 skip = true;
1027 }
1028 } else if (wipe_path == "DALVIK") {
1029 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1030 LOGERR("Failed to wipe dalvik\n");
1031 ret_val = false;
1032 break;
1033 } else {
1034 skip = true;
1035 }
1036 } else if (wipe_path == "INTERNAL") {
1037 if (!PartitionManager.Wipe_Media_From_Data()) {
1038 ret_val = false;
1039 break;
1040 } else {
1041 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001042 }
1043 }
that3f7b1ac2014-12-30 11:30:13 +01001044 if (!skip) {
1045 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1046 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1047 ret_val = false;
1048 break;
1049 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1050 arg = wipe_path;
1051 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001052 } else {
that3f7b1ac2014-12-30 11:30:13 +01001053 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001054 }
that3f7b1ac2014-12-30 11:30:13 +01001055 start_pos = end_pos + 1;
1056 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001057 }
1058 }
that3f7b1ac2014-12-30 11:30:13 +01001059 } else
1060 ret_val = PartitionManager.Wipe_By_Path(arg);
1061#ifdef TW_OEM_BUILD
1062 if (arg == DataManager::GetSettingsStoragePath()) {
1063 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1064 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001065
that3f7b1ac2014-12-30 11:30:13 +01001066 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1067 LOGINFO("Making TWRP folder and saving settings.\n");
1068 Storage_Path += "/TWRP";
1069 mkdir(Storage_Path.c_str(), 0777);
1070 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 } else {
that3f7b1ac2014-12-30 11:30:13 +01001072 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1073 }
1074 }
1075#endif
1076 }
1077 PartitionManager.Update_System_Details();
1078 if (ret_val)
1079 ret_val = 0; // 0 is success
1080 else
1081 ret_val = 1; // 1 is failure
1082 operation_end(ret_val);
1083 return 0;
1084}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001085
that3f7b1ac2014-12-30 11:30:13 +01001086int GUIAction::refreshsizes(std::string arg)
1087{
1088 operation_start("Refreshing Sizes");
1089 if (simulate) {
1090 simulate_progress_bar();
1091 } else
1092 PartitionManager.Update_System_Details();
1093 operation_end(0);
1094 return 0;
1095}
1096
1097int GUIAction::nandroid(std::string arg)
1098{
1099 operation_start("Nandroid");
1100 int ret = 0;
1101
1102 if (simulate) {
1103 DataManager::SetValue("tw_partition", "Simulation");
1104 simulate_progress_bar();
1105 } else {
1106 if (arg == "backup") {
1107 string Backup_Name;
1108 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1109 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1110 ret = PartitionManager.Run_Backup();
1111 }
1112 else {
1113 operation_end(1);
1114 return -1;
1115
1116 }
1117 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1118 } else if (arg == "restore") {
1119 string Restore_Name;
1120 DataManager::GetValue("tw_restore", Restore_Name);
1121 ret = PartitionManager.Run_Restore(Restore_Name);
1122 } else {
1123 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001124 return -1;
1125 }
1126 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001127 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001128 if (ret == false)
1129 ret = 1; // 1 for failure
1130 else
1131 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001132 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001133 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001134}
Dees_Troy51a0e822012-09-05 15:24:24 -04001135
that3f7b1ac2014-12-30 11:30:13 +01001136int GUIAction::fixpermissions(std::string arg)
1137{
1138 operation_start("Fix Permissions");
1139 LOGINFO("fix permissions started!\n");
1140 if (simulate) {
1141 simulate_progress_bar();
1142 } else {
1143 int op_status = PartitionManager.Fix_Permissions();
1144 if (op_status != 0)
1145 op_status = 1; // failure
1146 operation_end(op_status);
1147 }
1148 return 0;
1149}
1150
1151int GUIAction::dd(std::string arg)
1152{
1153 operation_start("imaging");
1154
1155 if (simulate) {
1156 simulate_progress_bar();
1157 } else {
1158 string cmd = "dd " + arg;
1159 TWFunc::Exec_Cmd(cmd);
1160 }
1161 operation_end(0);
1162 return 0;
1163}
1164
1165int GUIAction::partitionsd(std::string arg)
1166{
1167 operation_start("Partition SD Card");
1168 int ret_val = 0;
1169
1170 if (simulate) {
1171 simulate_progress_bar();
1172 } else {
1173 int allow_partition;
1174 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1175 if (allow_partition == 0) {
1176 gui_print("This device does not have a real SD Card!\nAborting!\n");
1177 } else {
1178 if (!PartitionManager.Partition_SDCard())
1179 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001180 }
that3f7b1ac2014-12-30 11:30:13 +01001181 }
1182 operation_end(ret_val);
1183 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001184
that3f7b1ac2014-12-30 11:30:13 +01001185}
Dees_Troy51a0e822012-09-05 15:24:24 -04001186
that3f7b1ac2014-12-30 11:30:13 +01001187int GUIAction::installhtcdumlock(std::string arg)
1188{
1189 operation_start("Install HTC Dumlock");
1190 if (simulate) {
1191 simulate_progress_bar();
1192 } else
1193 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001194
that3f7b1ac2014-12-30 11:30:13 +01001195 operation_end(0);
1196 return 0;
1197}
Dees_Troy51a0e822012-09-05 15:24:24 -04001198
that3f7b1ac2014-12-30 11:30:13 +01001199int GUIAction::htcdumlockrestoreboot(std::string arg)
1200{
1201 operation_start("HTC Dumlock Restore Boot");
1202 if (simulate) {
1203 simulate_progress_bar();
1204 } else
1205 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001206
that3f7b1ac2014-12-30 11:30:13 +01001207 operation_end(0);
1208 return 0;
1209}
Dees_Troy51a0e822012-09-05 15:24:24 -04001210
that3f7b1ac2014-12-30 11:30:13 +01001211int GUIAction::htcdumlockreflashrecovery(std::string arg)
1212{
1213 operation_start("HTC Dumlock Reflash Recovery");
1214 if (simulate) {
1215 simulate_progress_bar();
1216 } else
1217 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001218
that3f7b1ac2014-12-30 11:30:13 +01001219 operation_end(0);
1220 return 0;
1221}
Dees_Troy51a0e822012-09-05 15:24:24 -04001222
that3f7b1ac2014-12-30 11:30:13 +01001223int GUIAction::cmd(std::string arg)
1224{
1225 int op_status = 0;
1226
1227 operation_start("Command");
1228 LOGINFO("Running command: '%s'\n", arg.c_str());
1229 if (simulate) {
1230 simulate_progress_bar();
1231 } else {
1232 op_status = TWFunc::Exec_Cmd(arg);
1233 if (op_status != 0)
1234 op_status = 1;
1235 }
1236
1237 operation_end(op_status);
1238 return 0;
1239}
1240
1241int GUIAction::terminalcommand(std::string arg)
1242{
1243 int op_status = 0;
1244 string cmdpath, command;
1245
1246 DataManager::GetValue("tw_terminal_location", cmdpath);
1247 operation_start("CommandOutput");
1248 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1249 if (simulate) {
1250 simulate_progress_bar();
1251 operation_end(op_status);
1252 } else {
1253 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1254 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001255 DataManager::SetValue("tw_terminal_state", 1);
1256 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001257 FILE* fp;
1258 char line[512];
1259
1260 fp = popen(command.c_str(), "r");
1261 if (fp == NULL) {
1262 LOGERR("Error opening command to run.\n");
1263 } else {
1264 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1265 struct timeval timeout;
1266 fd_set fdset;
1267
1268 while(keep_going)
1269 {
1270 FD_ZERO(&fdset);
1271 FD_SET(fd, &fdset);
1272 timeout.tv_sec = 0;
1273 timeout.tv_usec = 400000;
1274 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1275 if (has_data == 0) {
1276 // Timeout reached
1277 DataManager::GetValue("tw_terminal_state", check);
1278 if (check == 0) {
1279 keep_going = 0;
1280 }
1281 } else if (has_data < 0) {
1282 // End of execution
1283 keep_going = 0;
1284 } else {
1285 // Try to read output
1286 memset(line, 0, sizeof(line));
1287 bytes_read = read(fd, line, sizeof(line));
1288 if (bytes_read > 0)
1289 gui_print("%s", line); // Display output
1290 else
1291 keep_going = 0; // Done executing
1292 }
1293 }
1294 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001295 }
thatc6085482015-01-09 22:12:43 +01001296 DataManager::SetValue("tw_operation_status", 0);
1297 DataManager::SetValue("tw_operation_state", 1);
1298 DataManager::SetValue("tw_terminal_state", 0);
1299 DataManager::SetValue("tw_background_thread_running", 0);
1300 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001301 }
1302 return 0;
1303}
1304
1305int GUIAction::killterminal(std::string arg)
1306{
1307 int op_status = 0;
1308
1309 LOGINFO("Sending kill command...\n");
1310 operation_start("KillCommand");
1311 DataManager::SetValue("tw_operation_status", 0);
1312 DataManager::SetValue("tw_operation_state", 1);
1313 DataManager::SetValue("tw_terminal_state", 0);
1314 DataManager::SetValue("tw_background_thread_running", 0);
1315 DataManager::SetValue(TW_ACTION_BUSY, 0);
1316 return 0;
1317}
1318
1319int GUIAction::reinjecttwrp(std::string arg)
1320{
1321 int op_status = 0;
1322 operation_start("ReinjectTWRP");
1323 gui_print("Injecting TWRP into boot image...\n");
1324 if (simulate) {
1325 simulate_progress_bar();
1326 } else {
1327 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1328 gui_print("TWRP injection complete.\n");
1329 }
1330
1331 operation_end(op_status);
1332 return 0;
1333}
1334
1335int GUIAction::checkbackupname(std::string arg)
1336{
1337 int op_status = 0;
1338
1339 operation_start("CheckBackupName");
1340 if (simulate) {
1341 simulate_progress_bar();
1342 } else {
1343 op_status = PartitionManager.Check_Backup_Name(true);
1344 if (op_status != 0)
1345 op_status = 1;
1346 }
1347
1348 operation_end(op_status);
1349 return 0;
1350}
1351
1352int GUIAction::decrypt(std::string arg)
1353{
1354 int op_status = 0;
1355
1356 operation_start("Decrypt");
1357 if (simulate) {
1358 simulate_progress_bar();
1359 } else {
1360 string Password;
1361 DataManager::GetValue("tw_crypto_password", Password);
1362 op_status = PartitionManager.Decrypt_Device(Password);
1363 if (op_status != 0)
1364 op_status = 1;
1365 else {
1366 int load_theme = 1;
1367
1368 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1369
1370 if (load_theme) {
1371 int has_datamedia;
1372
1373 // Check for a custom theme and load it if exists
1374 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1375 if (has_datamedia != 0) {
1376 struct stat st;
1377 int check = 0;
1378 std::string theme_path;
1379
1380 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1381 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1382 } else {
1383 LOGINFO("Got default contexts and file mode for storage files.\n");
1384 }
1385
1386 theme_path = DataManager::GetSettingsStoragePath();
1387 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1388 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1389 check = 1;
1390 }
1391
1392 theme_path += "/TWRP/theme/ui.zip";
1393 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1394 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1395 {
1396 // Loading the custom theme failed - try loading the stock theme
1397 LOGINFO("Attempting to reload stock theme...\n");
1398 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1399 {
1400 LOGERR("Failed to load base packages.\n");
1401 }
1402 }
1403 }
1404 }
1405 }
1406 }
1407 }
1408
1409 operation_end(op_status);
1410 return 0;
1411}
1412
thatcc8ddca2015-01-03 01:59:36 +01001413int GUIAction::adbsideload(std::string arg)
1414{
1415 operation_start("Sideload");
1416 if (simulate) {
1417 simulate_progress_bar();
1418 operation_end(0);
1419 } else {
thatc6085482015-01-09 22:12:43 +01001420 gui_print("Starting ADB sideload feature...\n");
1421 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1422
1423 // wait for the adb connection
1424 int ret = apply_from_adb("/", &sideload_child_pid);
1425 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1426
1427 if (ret != 0) {
1428 if (ret == -2)
1429 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1430 ret = 1; // failure
1431 } else {
1432 int wipe_cache = 0;
1433 int wipe_dalvik = 0;
1434 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1435
1436 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1437 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1438 PartitionManager.Wipe_By_Path("/cache");
1439 if (wipe_dalvik)
1440 PartitionManager.Wipe_Dalvik_Cache();
1441 } else {
1442 ret = 1; // failure
1443 }
thatcc8ddca2015-01-03 01:59:36 +01001444 }
thatc6085482015-01-09 22:12:43 +01001445 if (sideload_child_pid) {
1446 LOGINFO("Signaling child sideload process to exit.\n");
1447 struct stat st;
1448 // Calling stat() on this magic filename signals the minadbd
1449 // subprocess to shut down.
1450 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1451 int status;
1452 LOGINFO("Waiting for child sideload process to exit.\n");
1453 waitpid(sideload_child_pid, &status, 0);
1454 }
1455
1456 TWFunc::Toggle_MTP(mtp_was_enabled);
1457 reinject_after_flash();
1458 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001459 }
that3f7b1ac2014-12-30 11:30:13 +01001460 return 0;
1461}
1462
1463int GUIAction::adbsideloadcancel(std::string arg)
1464{
that3f7b1ac2014-12-30 11:30:13 +01001465 struct stat st;
1466 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1467 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001468 LOGINFO("Signaling child sideload process to exit.\n");
1469 // Calling stat() on this magic filename signals the minadbd
1470 // subprocess to shut down.
1471 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1472 if (!sideload_child_pid) {
1473 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001474 return 0;
1475 }
thatcc8ddca2015-01-03 01:59:36 +01001476 ::sleep(1);
1477 LOGINFO("Killing child sideload process.\n");
1478 kill(sideload_child_pid, SIGTERM);
1479 int status;
1480 LOGINFO("Waiting for child sideload process to exit.\n");
1481 waitpid(sideload_child_pid, &status, 0);
1482 sideload_child_pid = 0;
1483 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001484 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1485 return 0;
1486}
1487
1488int GUIAction::openrecoveryscript(std::string arg)
1489{
1490 operation_start("OpenRecoveryScript");
1491 if (simulate) {
1492 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001493 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001494 } else {
thatc6085482015-01-09 22:12:43 +01001495 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1496 // that we converted to ORS commands during boot in recovery.cpp.
1497 // Run those first.
1498 int reboot = 0;
1499 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1500 gui_print("Processing AOSP recovery commands...\n");
1501 if (OpenRecoveryScript::run_script_file() == 0) {
1502 reboot = 1;
1503 }
that3f7b1ac2014-12-30 11:30:13 +01001504 }
thatc6085482015-01-09 22:12:43 +01001505 // Check for the ORS file in /cache and attempt to run those commands.
1506 if (OpenRecoveryScript::check_for_script_file()) {
1507 gui_print("Processing OpenRecoveryScript file...\n");
1508 if (OpenRecoveryScript::run_script_file() == 0) {
1509 reboot = 1;
1510 }
1511 }
1512 if (reboot) {
1513 usleep(2000000); // Sleep for 2 seconds before rebooting
1514 TWFunc::tw_reboot(rb_system);
1515 } else {
1516 DataManager::SetValue("tw_page_done", 1);
1517 }
1518 operation_end(1);
1519 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001520 }
1521 return 0;
1522}
1523
1524int GUIAction::installsu(std::string arg)
1525{
1526 int op_status = 0;
1527
1528 operation_start("Install SuperSU");
1529 if (simulate) {
1530 simulate_progress_bar();
1531 } else {
1532 if (!TWFunc::Install_SuperSU())
1533 op_status = 1;
1534 }
1535
1536 operation_end(op_status);
1537 return 0;
1538}
1539
1540int GUIAction::fixsu(std::string arg)
1541{
1542 int op_status = 0;
1543
1544 operation_start("Fixing Superuser Permissions");
1545 if (simulate) {
1546 simulate_progress_bar();
1547 } else {
1548 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1549 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1550 }
1551
1552 operation_end(op_status);
1553 return 0;
1554}
1555
1556int GUIAction::decrypt_backup(std::string arg)
1557{
1558 int op_status = 0;
1559
1560 operation_start("Try Restore Decrypt");
1561 if (simulate) {
1562 simulate_progress_bar();
1563 } else {
1564 string Restore_Path, Filename, Password;
1565 DataManager::GetValue("tw_restore", Restore_Path);
1566 Restore_Path += "/";
1567 DataManager::GetValue("tw_restore_password", Password);
1568 TWFunc::SetPerformanceMode(true);
1569 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1570 op_status = 0; // success
1571 else
1572 op_status = 1; // fail
1573 TWFunc::SetPerformanceMode(false);
1574 }
1575
1576 operation_end(op_status);
1577 return 0;
1578}
1579
1580int GUIAction::repair(std::string arg)
1581{
1582 int op_status = 0;
1583
1584 operation_start("Repair Partition");
1585 if (simulate) {
1586 simulate_progress_bar();
1587 } else {
1588 string part_path;
1589 DataManager::GetValue("tw_partition_mount_point", part_path);
1590 if (PartitionManager.Repair_By_Path(part_path, true)) {
1591 op_status = 0; // success
1592 } else {
1593 LOGERR("Error repairing file system.\n");
1594 op_status = 1; // fail
1595 }
1596 }
1597
1598 operation_end(op_status);
1599 return 0;
1600}
1601
1602int GUIAction::changefilesystem(std::string arg)
1603{
1604 int op_status = 0;
1605
1606 operation_start("Change File System");
1607 if (simulate) {
1608 simulate_progress_bar();
1609 } else {
1610 string part_path, file_system;
1611 DataManager::GetValue("tw_partition_mount_point", part_path);
1612 DataManager::GetValue("tw_action_new_file_system", file_system);
1613 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1614 op_status = 0; // success
1615 } else {
1616 LOGERR("Error changing file system.\n");
1617 op_status = 1; // fail
1618 }
1619 }
1620 PartitionManager.Update_System_Details();
1621 operation_end(op_status);
1622 return 0;
1623}
1624
1625int GUIAction::startmtp(std::string arg)
1626{
1627 int op_status = 0;
1628
1629 operation_start("Start MTP");
1630 if (PartitionManager.Enable_MTP())
1631 op_status = 0; // success
1632 else
1633 op_status = 1; // fail
1634
1635 operation_end(op_status);
1636 return 0;
1637}
1638
1639int GUIAction::stopmtp(std::string arg)
1640{
1641 int op_status = 0;
1642
1643 operation_start("Stop MTP");
1644 if (PartitionManager.Disable_MTP())
1645 op_status = 0; // success
1646 else
1647 op_status = 1; // fail
1648
1649 operation_end(op_status);
1650 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001651}
1652
Ethan Yonker96af84a2015-01-05 14:58:36 -06001653int GUIAction::flashimage(std::string arg)
1654{
1655 int op_status = 0;
1656
1657 operation_start("Flash Image");
1658 string path, filename, full_filename;
1659 DataManager::GetValue("tw_zip_location", path);
1660 DataManager::GetValue("tw_file", filename);
1661 full_filename = path + "/" + filename;
1662 if (PartitionManager.Flash_Image(full_filename))
1663 op_status = 0; // success
1664 else
1665 op_status = 1; // fail
1666
1667 operation_end(op_status);
1668 return 0;
1669}
1670
Dees_Troy51a0e822012-09-05 15:24:24 -04001671int GUIAction::getKeyByName(std::string key)
1672{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001673 if (key == "home") return KEY_HOME;
1674 else if (key == "menu") return KEY_MENU;
1675 else if (key == "back") return KEY_BACK;
1676 else if (key == "search") return KEY_SEARCH;
1677 else if (key == "voldown") return KEY_VOLUMEDOWN;
1678 else if (key == "volup") return KEY_VOLUMEUP;
1679 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001680 int ret_val;
1681 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1682 if (!ret_val)
1683 return KEY_POWER;
1684 else
1685 return ret_val;
1686 }
1687
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001688 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001689}