blob: a341e4a620609f8366e57ae54cf84b3dc6042571 [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{
364 return setActionsRunningInCallerThread.find(action.mFunction) == setActionsRunningInCallerThread.end();
365}
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 {
388 for (it = mActions.begin(); it != mActions.end(); ++it)
389 doAction(*it);
thatc6085482015-01-09 22:12:43 +0100390 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400391
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400393}
394
that3f7b1ac2014-12-30 11:30:13 +0100395int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400396{
that3f7b1ac2014-12-30 11:30:13 +0100397 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400398
that3f7b1ac2014-12-30 11:30:13 +0100399 std::string function = gui_parse_text(action.mFunction);
400 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400401
that3f7b1ac2014-12-30 11:30:13 +0100402 // find function and execute it
403 mapFunc::const_iterator funcitr = mf.find(function);
404 if (funcitr != mf.end())
405 return (this->*funcitr->second)(arg);
406
407 LOGERR("Unknown action '%s'\n", function.c_str());
408 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409}
410
411void GUIAction::operation_start(const string operation_name)
412{
that3f7b1ac2014-12-30 11:30:13 +0100413 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000414 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 DataManager::SetValue(TW_ACTION_BUSY, 1);
416 DataManager::SetValue("ui_progress", 0);
417 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600419 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400420}
421
that3f7b1ac2014-12-30 11:30:13 +0100422void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400423{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000424 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 DataManager::SetValue("ui_progress", 100);
427 if (simulate) {
428 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
429 if (simulate_fail != 0)
430 DataManager::SetValue("tw_operation_status", 1);
431 else
432 DataManager::SetValue("tw_operation_status", 0);
433 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500434 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400435 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500436 }
437 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500439 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 }
441 DataManager::SetValue("tw_operation_state", 1);
442 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500443 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000444 time(&Stop);
445 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600446 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100447 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400448}
449
that3f7b1ac2014-12-30 11:30:13 +0100450int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400451{
that3f7b1ac2014-12-30 11:30:13 +0100452 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400453
that3f7b1ac2014-12-30 11:30:13 +0100454 sync();
455 DataManager::SetValue("tw_gui_done", 1);
456 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
that3f7b1ac2014-12-30 11:30:13 +0100458 return 0;
459}
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
that3f7b1ac2014-12-30 11:30:13 +0100461int GUIAction::home(std::string arg)
462{
463 PageManager::SelectPackage("TWRP");
464 gui_changePage("main");
465 return 0;
466}
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
that3f7b1ac2014-12-30 11:30:13 +0100468int GUIAction::key(std::string arg)
469{
470 const int key = getKeyByName(arg);
471 PageManager::NotifyKey(key, true);
472 PageManager::NotifyKey(key, false);
473 return 0;
474}
475
476int GUIAction::page(std::string arg)
477{
478 std::string page_name = gui_parse_text(arg);
479 return gui_changePage(page_name);
480}
481
482int GUIAction::reload(std::string arg)
483{
484 int check = 0, ret_val = 0;
485 std::string theme_path;
486
487 operation_start("Reload Theme");
488 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 operation_end(ret_val);
506 return 0;
507}
Dees_Troy51a0e822012-09-05 15:24:24 -0400508
that3f7b1ac2014-12-30 11:30:13 +0100509int GUIAction::readBackup(std::string arg)
510{
511 string Restore_Name;
512 DataManager::GetValue("tw_restore", Restore_Name);
513 PartitionManager.Set_Restore_Files(Restore_Name);
514 return 0;
515}
516
517int GUIAction::set(std::string arg)
518{
519 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200520 {
that3f7b1ac2014-12-30 11:30:13 +0100521 string varName = arg.substr(0, arg.find('='));
522 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400523
that3f7b1ac2014-12-30 11:30:13 +0100524 DataManager::GetValue(value, value);
525 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 }
that3f7b1ac2014-12-30 11:30:13 +0100527 else
528 DataManager::SetValue(arg, "1");
529 return 0;
530}
Dees_Troy51a0e822012-09-05 15:24:24 -0400531
that3f7b1ac2014-12-30 11:30:13 +0100532int GUIAction::clear(std::string arg)
533{
534 DataManager::SetValue(arg, "0");
535 return 0;
536}
Dees_Troy51a0e822012-09-05 15:24:24 -0400537
that3f7b1ac2014-12-30 11:30:13 +0100538int GUIAction::mount(std::string arg)
539{
540 if (arg == "usb") {
541 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100543 PartitionManager.usb_storage_enable();
544 else
545 gui_print("Simulating actions...\n");
546 } else if (!simulate) {
547 PartitionManager.Mount_By_Path(arg, true);
548 PartitionManager.Add_MTP_Storage(arg);
549 } else
550 gui_print("Simulating actions...\n");
551 return 0;
552}
553
554int GUIAction::unmount(std::string arg)
555{
556 if (arg == "usb") {
557 if (!simulate)
558 PartitionManager.usb_storage_disable();
559 else
560 gui_print("Simulating actions...\n");
561 DataManager::SetValue(TW_ACTION_BUSY, 0);
562 } else if (!simulate) {
563 PartitionManager.UnMount_By_Path(arg, true);
564 } else
565 gui_print("Simulating actions...\n");
566 return 0;
567}
568
569int GUIAction::restoredefaultsettings(std::string arg)
570{
571 operation_start("Restore Defaults");
572 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
573 gui_print("Simulating actions...\n");
574 else {
575 DataManager::ResetDefaults();
576 PartitionManager.Update_System_Details();
577 PartitionManager.Mount_Current_Storage(true);
578 }
579 operation_end(0);
580 return 0;
581}
582
583int GUIAction::copylog(std::string arg)
584{
585 operation_start("Copy Log");
586 if (!simulate)
587 {
588 string dst;
589 PartitionManager.Mount_Current_Storage(true);
590 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
591 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
592 tw_set_default_metadata(dst.c_str());
593 sync();
594 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
595 } else
596 simulate_progress_bar();
597 operation_end(0);
598 return 0;
599}
600
601
602int GUIAction::compute(std::string arg)
603{
604 if (arg.find("+") != string::npos)
605 {
606 string varName = arg.substr(0, arg.find('+'));
607 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
608 int amount_to_add = atoi(string_to_add.c_str());
609 int value;
610
611 DataManager::GetValue(varName, value);
612 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 return 0;
614 }
that3f7b1ac2014-12-30 11:30:13 +0100615 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 {
that3f7b1ac2014-12-30 11:30:13 +0100617 string varName = arg.substr(0, arg.find('-'));
618 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
619 int amount_to_subtract = atoi(string_to_subtract.c_str());
620 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400621
that3f7b1ac2014-12-30 11:30:13 +0100622 DataManager::GetValue(varName, value);
623 value -= amount_to_subtract;
624 if (value <= 0)
625 value = 0;
626 DataManager::SetValue(varName, value);
627 return 0;
628 }
629 if (arg.find("*") != string::npos)
630 {
631 string varName = arg.substr(0, arg.find('*'));
632 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
633 int multiply_by = atoi(multiply_by_str.c_str());
634 int value;
635
636 DataManager::GetValue(varName, value);
637 DataManager::SetValue(varName, value*multiply_by);
638 return 0;
639 }
640 if (arg.find("/") != string::npos)
641 {
642 string varName = arg.substr(0, arg.find('/'));
643 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
644 int divide_by = atoi(divide_by_str.c_str());
645 int value;
646
647 if(divide_by != 0)
648 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400649 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100650 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 }
652 return 0;
653 }
that3f7b1ac2014-12-30 11:30:13 +0100654 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
655 return -1;
656}
Dees_Troy51a0e822012-09-05 15:24:24 -0400657
that3f7b1ac2014-12-30 11:30:13 +0100658int GUIAction::setguitimezone(std::string arg)
659{
660 string SelectedZone;
661 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
662 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
663 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
664
665 int dst;
666 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
667
668 string offset;
669 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
670
671 string NewTimeZone = Zone;
672 if (offset != "0")
673 NewTimeZone += ":" + offset;
674
675 if (dst != 0)
676 NewTimeZone += DSTZone;
677
678 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
679 DataManager::update_tz_environment_variables();
680 return 0;
681}
682
683int GUIAction::overlay(std::string arg)
684{
685 return gui_changeOverlay(arg);
686}
687
688int GUIAction::queuezip(std::string arg)
689{
690 if (zip_queue_index >= 10) {
691 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 return 0;
693 }
that3f7b1ac2014-12-30 11:30:13 +0100694 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
695 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
696 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100698 }
699 return 0;
700}
701
702int GUIAction::cancelzip(std::string arg)
703{
704 if (zip_queue_index <= 0) {
705 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400706 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100707 } else {
708 zip_queue_index--;
709 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 }
that3f7b1ac2014-12-30 11:30:13 +0100711 return 0;
712}
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
that3f7b1ac2014-12-30 11:30:13 +0100714int GUIAction::queueclear(std::string arg)
715{
716 zip_queue_index = 0;
717 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
718 return 0;
719}
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
that3f7b1ac2014-12-30 11:30:13 +0100721int GUIAction::sleep(std::string arg)
722{
723 operation_start("Sleep");
724 usleep(atoi(arg.c_str()));
725 operation_end(0);
726 return 0;
727}
Dees Troyb21cc642013-09-10 17:36:41 +0000728
that3f7b1ac2014-12-30 11:30:13 +0100729int GUIAction::appenddatetobackupname(std::string arg)
730{
731 operation_start("AppendDateToBackupName");
732 string Backup_Name;
733 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
734 Backup_Name += TWFunc::Get_Current_Date();
735 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
736 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
737 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
738 operation_end(0);
739 return 0;
740}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500741
that3f7b1ac2014-12-30 11:30:13 +0100742int GUIAction::generatebackupname(std::string arg)
743{
744 operation_start("GenerateBackupName");
745 TWFunc::Auto_Generate_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::checkpartitionlist(std::string arg)
751{
752 string Wipe_List, wipe_path;
753 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500754
that3f7b1ac2014-12-30 11:30:13 +0100755 DataManager::GetValue("tw_wipe_list", Wipe_List);
756 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
757 if (!Wipe_List.empty()) {
758 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
759 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
760 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
761 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
762 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
763 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 } else {
that3f7b1ac2014-12-30 11:30:13 +0100765 count++;
766 }
767 start_pos = end_pos + 1;
768 end_pos = Wipe_List.find(";", start_pos);
769 }
770 DataManager::SetValue("tw_check_partition_list", count);
771 } else {
772 DataManager::SetValue("tw_check_partition_list", 0);
773 }
774 return 0;
775}
Dees_Troy51a0e822012-09-05 15:24:24 -0400776
that3f7b1ac2014-12-30 11:30:13 +0100777int GUIAction::getpartitiondetails(std::string arg)
778{
779 string Wipe_List, wipe_path;
780 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400781
that3f7b1ac2014-12-30 11:30:13 +0100782 DataManager::GetValue("tw_wipe_list", Wipe_List);
783 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
784 if (!Wipe_List.empty()) {
785 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
786 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
787 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
788 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
789 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
790 // Do nothing
791 } else {
792 DataManager::SetValue("tw_partition_path", wipe_path);
793 break;
794 }
795 start_pos = end_pos + 1;
796 end_pos = Wipe_List.find(";", start_pos);
797 }
798 if (!wipe_path.empty()) {
799 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
800 if (Part) {
801 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500802
that3f7b1ac2014-12-30 11:30:13 +0100803 DataManager::SetValue("tw_partition_name", Part->Display_Name);
804 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
805 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
806 DataManager::SetValue("tw_partition_size", Part->Size / mb);
807 DataManager::SetValue("tw_partition_used", Part->Used / mb);
808 DataManager::SetValue("tw_partition_free", Part->Free / mb);
809 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
810 DataManager::SetValue("tw_partition_removable", Part->Removable);
811 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
812
813 if (Part->Can_Repair())
814 DataManager::SetValue("tw_partition_can_repair", 1);
815 else
816 DataManager::SetValue("tw_partition_can_repair", 0);
817 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
818 DataManager::SetValue("tw_partition_vfat", 1);
819 else
820 DataManager::SetValue("tw_partition_vfat", 0);
821 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
822 DataManager::SetValue("tw_partition_exfat", 1);
823 else
824 DataManager::SetValue("tw_partition_exfat", 0);
825 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
826 DataManager::SetValue("tw_partition_f2fs", 1);
827 else
828 DataManager::SetValue("tw_partition_f2fs", 0);
829 if (TWFunc::Path_Exists("/sbin/mke2fs"))
830 DataManager::SetValue("tw_partition_ext", 1);
831 else
832 DataManager::SetValue("tw_partition_ext", 0);
833 return 0;
834 } else {
835 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
836 }
837 }
838 }
839 DataManager::SetValue("tw_partition_name", "");
840 DataManager::SetValue("tw_partition_file_system", "");
841 return 0;
842}
843
844int GUIAction::screenshot(std::string arg)
845{
846 time_t tm;
847 char path[256];
848 int path_len;
849 uid_t uid = -1;
850 gid_t gid = -1;
851
852 struct passwd *pwd = getpwnam("media_rw");
853 if(pwd) {
854 uid = pwd->pw_uid;
855 gid = pwd->pw_gid;
856 }
857
858 const std::string storage = DataManager::GetCurrentStoragePath();
859 if(PartitionManager.Is_Mounted_By_Path(storage)) {
860 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
861 } else {
862 strcpy(path, "/tmp/");
863 }
864
865 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
866 return 0;
867
868 tm = time(NULL);
869 path_len = strlen(path);
870
871 // Screenshot_2014-01-01-18-21-38.png
872 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
873
874 int res = gr_save_screenshot(path);
875 if(res == 0) {
876 chmod(path, 0666);
877 chown(path, uid, gid);
878
879 gui_print("Screenshot was saved to %s\n", path);
880
881 // blink to notify that the screenshow was taken
882 gr_color(255, 255, 255, 255);
883 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
884 gr_flip();
885 gui_forceRender();
886 } else {
887 LOGERR("Failed to take a screenshot!\n");
888 }
889 return 0;
890}
891
892int GUIAction::setbrightness(std::string arg)
893{
894 return TWFunc::Set_Brightness(arg);
895}
896
897int GUIAction::fileexists(std::string arg)
898{
899 struct stat st;
900 string newpath = arg + "/.";
901
902 operation_start("FileExists");
903 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
904 operation_end(0);
905 else
906 operation_end(1);
907 return 0;
908}
909
thatcc8ddca2015-01-03 01:59:36 +0100910void GUIAction::reinject_after_flash()
911{
912 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
913 operation_start("ReinjectTWRP");
914 gui_print("Injecting TWRP into boot image...\n");
915 if (simulate) {
916 simulate_progress_bar();
917 } else {
918 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
919 if (Boot == NULL || Boot->Current_File_System != "emmc")
920 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
921 else {
922 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
923 TWFunc::Exec_Cmd(injectcmd);
924 }
925 gui_print("TWRP injection complete.\n");
926 }
927 }
928}
929
that3f7b1ac2014-12-30 11:30:13 +0100930int GUIAction::flash(std::string arg)
931{
932 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600933 // We're going to jump to this page first, like a loading page
934 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100935 for (i=0; i<zip_queue_index; i++) {
936 operation_start("Flashing");
937 DataManager::SetValue("tw_filename", zip_queue[i]);
938 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
939
940 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600941 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100942 TWFunc::SetPerformanceMode(false);
943 if (ret_val != 0) {
944 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +0100945 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600946 break;
that3f7b1ac2014-12-30 11:30:13 +0100947 }
948 }
949 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +0100950
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600951 if (wipe_cache) {
952 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +0100953 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600954 }
that3f7b1ac2014-12-30 11:30:13 +0100955
thatcc8ddca2015-01-03 01:59:36 +0100956 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100957 PartitionManager.Update_System_Details();
958 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600959 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100960 return 0;
961}
962
963int GUIAction::wipe(std::string arg)
964{
965 operation_start("Format");
966 DataManager::SetValue("tw_partition", arg);
967
968 int ret_val = false;
969
970 if (simulate) {
971 simulate_progress_bar();
972 } else {
973 if (arg == "data")
974 ret_val = PartitionManager.Factory_Reset();
975 else if (arg == "battery")
976 ret_val = PartitionManager.Wipe_Battery_Stats();
977 else if (arg == "rotate")
978 ret_val = PartitionManager.Wipe_Rotate_Data();
979 else if (arg == "dalvik")
980 ret_val = PartitionManager.Wipe_Dalvik_Cache();
981 else if (arg == "DATAMEDIA") {
982 ret_val = PartitionManager.Format_Data();
983 } else if (arg == "INTERNAL") {
984 int has_datamedia, dual_storage;
985
986 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
987 if (has_datamedia) {
988 ret_val = PartitionManager.Wipe_Media_From_Data();
989 } else {
990 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
991 }
992 } else if (arg == "EXTERNAL") {
993 string External_Path;
994
995 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
996 ret_val = PartitionManager.Wipe_By_Path(External_Path);
997 } else if (arg == "ANDROIDSECURE") {
998 ret_val = PartitionManager.Wipe_Android_Secure();
999 } else if (arg == "LIST") {
1000 string Wipe_List, wipe_path;
1001 bool skip = false;
1002 ret_val = true;
1003 TWPartition* wipe_part = NULL;
1004
1005 DataManager::GetValue("tw_wipe_list", Wipe_List);
1006 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1007 if (!Wipe_List.empty()) {
1008 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1009 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1010 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1011 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1012 if (wipe_path == "/and-sec") {
1013 if (!PartitionManager.Wipe_Android_Secure()) {
1014 LOGERR("Unable to wipe android secure\n");
1015 ret_val = false;
1016 break;
1017 } else {
1018 skip = true;
1019 }
1020 } else if (wipe_path == "DALVIK") {
1021 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1022 LOGERR("Failed to wipe dalvik\n");
1023 ret_val = false;
1024 break;
1025 } else {
1026 skip = true;
1027 }
1028 } else if (wipe_path == "INTERNAL") {
1029 if (!PartitionManager.Wipe_Media_From_Data()) {
1030 ret_val = false;
1031 break;
1032 } else {
1033 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001034 }
1035 }
that3f7b1ac2014-12-30 11:30:13 +01001036 if (!skip) {
1037 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1038 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1039 ret_val = false;
1040 break;
1041 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1042 arg = wipe_path;
1043 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001044 } else {
that3f7b1ac2014-12-30 11:30:13 +01001045 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001046 }
that3f7b1ac2014-12-30 11:30:13 +01001047 start_pos = end_pos + 1;
1048 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001049 }
1050 }
that3f7b1ac2014-12-30 11:30:13 +01001051 } else
1052 ret_val = PartitionManager.Wipe_By_Path(arg);
1053#ifdef TW_OEM_BUILD
1054 if (arg == DataManager::GetSettingsStoragePath()) {
1055 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1056 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001057
that3f7b1ac2014-12-30 11:30:13 +01001058 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1059 LOGINFO("Making TWRP folder and saving settings.\n");
1060 Storage_Path += "/TWRP";
1061 mkdir(Storage_Path.c_str(), 0777);
1062 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001063 } else {
that3f7b1ac2014-12-30 11:30:13 +01001064 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1065 }
1066 }
1067#endif
1068 }
1069 PartitionManager.Update_System_Details();
1070 if (ret_val)
1071 ret_val = 0; // 0 is success
1072 else
1073 ret_val = 1; // 1 is failure
1074 operation_end(ret_val);
1075 return 0;
1076}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001077
that3f7b1ac2014-12-30 11:30:13 +01001078int GUIAction::refreshsizes(std::string arg)
1079{
1080 operation_start("Refreshing Sizes");
1081 if (simulate) {
1082 simulate_progress_bar();
1083 } else
1084 PartitionManager.Update_System_Details();
1085 operation_end(0);
1086 return 0;
1087}
1088
1089int GUIAction::nandroid(std::string arg)
1090{
1091 operation_start("Nandroid");
1092 int ret = 0;
1093
1094 if (simulate) {
1095 DataManager::SetValue("tw_partition", "Simulation");
1096 simulate_progress_bar();
1097 } else {
1098 if (arg == "backup") {
1099 string Backup_Name;
1100 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1101 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1102 ret = PartitionManager.Run_Backup();
1103 }
1104 else {
1105 operation_end(1);
1106 return -1;
1107
1108 }
1109 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1110 } else if (arg == "restore") {
1111 string Restore_Name;
1112 DataManager::GetValue("tw_restore", Restore_Name);
1113 ret = PartitionManager.Run_Restore(Restore_Name);
1114 } else {
1115 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001116 return -1;
1117 }
1118 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001119 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001120 if (ret == false)
1121 ret = 1; // 1 for failure
1122 else
1123 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001124 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001125 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001126}
Dees_Troy51a0e822012-09-05 15:24:24 -04001127
that3f7b1ac2014-12-30 11:30:13 +01001128int GUIAction::fixpermissions(std::string arg)
1129{
1130 operation_start("Fix Permissions");
1131 LOGINFO("fix permissions started!\n");
1132 if (simulate) {
1133 simulate_progress_bar();
1134 } else {
1135 int op_status = PartitionManager.Fix_Permissions();
1136 if (op_status != 0)
1137 op_status = 1; // failure
1138 operation_end(op_status);
1139 }
1140 return 0;
1141}
1142
1143int GUIAction::dd(std::string arg)
1144{
1145 operation_start("imaging");
1146
1147 if (simulate) {
1148 simulate_progress_bar();
1149 } else {
1150 string cmd = "dd " + arg;
1151 TWFunc::Exec_Cmd(cmd);
1152 }
1153 operation_end(0);
1154 return 0;
1155}
1156
1157int GUIAction::partitionsd(std::string arg)
1158{
1159 operation_start("Partition SD Card");
1160 int ret_val = 0;
1161
1162 if (simulate) {
1163 simulate_progress_bar();
1164 } else {
1165 int allow_partition;
1166 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1167 if (allow_partition == 0) {
1168 gui_print("This device does not have a real SD Card!\nAborting!\n");
1169 } else {
1170 if (!PartitionManager.Partition_SDCard())
1171 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001172 }
that3f7b1ac2014-12-30 11:30:13 +01001173 }
1174 operation_end(ret_val);
1175 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001176
that3f7b1ac2014-12-30 11:30:13 +01001177}
Dees_Troy51a0e822012-09-05 15:24:24 -04001178
that3f7b1ac2014-12-30 11:30:13 +01001179int GUIAction::installhtcdumlock(std::string arg)
1180{
1181 operation_start("Install HTC Dumlock");
1182 if (simulate) {
1183 simulate_progress_bar();
1184 } else
1185 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001186
that3f7b1ac2014-12-30 11:30:13 +01001187 operation_end(0);
1188 return 0;
1189}
Dees_Troy51a0e822012-09-05 15:24:24 -04001190
that3f7b1ac2014-12-30 11:30:13 +01001191int GUIAction::htcdumlockrestoreboot(std::string arg)
1192{
1193 operation_start("HTC Dumlock Restore Boot");
1194 if (simulate) {
1195 simulate_progress_bar();
1196 } else
1197 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001198
that3f7b1ac2014-12-30 11:30:13 +01001199 operation_end(0);
1200 return 0;
1201}
Dees_Troy51a0e822012-09-05 15:24:24 -04001202
that3f7b1ac2014-12-30 11:30:13 +01001203int GUIAction::htcdumlockreflashrecovery(std::string arg)
1204{
1205 operation_start("HTC Dumlock Reflash Recovery");
1206 if (simulate) {
1207 simulate_progress_bar();
1208 } else
1209 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001210
that3f7b1ac2014-12-30 11:30:13 +01001211 operation_end(0);
1212 return 0;
1213}
Dees_Troy51a0e822012-09-05 15:24:24 -04001214
that3f7b1ac2014-12-30 11:30:13 +01001215int GUIAction::cmd(std::string arg)
1216{
1217 int op_status = 0;
1218
1219 operation_start("Command");
1220 LOGINFO("Running command: '%s'\n", arg.c_str());
1221 if (simulate) {
1222 simulate_progress_bar();
1223 } else {
1224 op_status = TWFunc::Exec_Cmd(arg);
1225 if (op_status != 0)
1226 op_status = 1;
1227 }
1228
1229 operation_end(op_status);
1230 return 0;
1231}
1232
1233int GUIAction::terminalcommand(std::string arg)
1234{
1235 int op_status = 0;
1236 string cmdpath, command;
1237
1238 DataManager::GetValue("tw_terminal_location", cmdpath);
1239 operation_start("CommandOutput");
1240 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1241 if (simulate) {
1242 simulate_progress_bar();
1243 operation_end(op_status);
1244 } else {
1245 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1246 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001247 DataManager::SetValue("tw_terminal_state", 1);
1248 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001249 FILE* fp;
1250 char line[512];
1251
1252 fp = popen(command.c_str(), "r");
1253 if (fp == NULL) {
1254 LOGERR("Error opening command to run.\n");
1255 } else {
1256 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1257 struct timeval timeout;
1258 fd_set fdset;
1259
1260 while(keep_going)
1261 {
1262 FD_ZERO(&fdset);
1263 FD_SET(fd, &fdset);
1264 timeout.tv_sec = 0;
1265 timeout.tv_usec = 400000;
1266 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1267 if (has_data == 0) {
1268 // Timeout reached
1269 DataManager::GetValue("tw_terminal_state", check);
1270 if (check == 0) {
1271 keep_going = 0;
1272 }
1273 } else if (has_data < 0) {
1274 // End of execution
1275 keep_going = 0;
1276 } else {
1277 // Try to read output
1278 memset(line, 0, sizeof(line));
1279 bytes_read = read(fd, line, sizeof(line));
1280 if (bytes_read > 0)
1281 gui_print("%s", line); // Display output
1282 else
1283 keep_going = 0; // Done executing
1284 }
1285 }
1286 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001287 }
thatc6085482015-01-09 22:12:43 +01001288 DataManager::SetValue("tw_operation_status", 0);
1289 DataManager::SetValue("tw_operation_state", 1);
1290 DataManager::SetValue("tw_terminal_state", 0);
1291 DataManager::SetValue("tw_background_thread_running", 0);
1292 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001293 }
1294 return 0;
1295}
1296
1297int GUIAction::killterminal(std::string arg)
1298{
1299 int op_status = 0;
1300
1301 LOGINFO("Sending kill command...\n");
1302 operation_start("KillCommand");
1303 DataManager::SetValue("tw_operation_status", 0);
1304 DataManager::SetValue("tw_operation_state", 1);
1305 DataManager::SetValue("tw_terminal_state", 0);
1306 DataManager::SetValue("tw_background_thread_running", 0);
1307 DataManager::SetValue(TW_ACTION_BUSY, 0);
1308 return 0;
1309}
1310
1311int GUIAction::reinjecttwrp(std::string arg)
1312{
1313 int op_status = 0;
1314 operation_start("ReinjectTWRP");
1315 gui_print("Injecting TWRP into boot image...\n");
1316 if (simulate) {
1317 simulate_progress_bar();
1318 } else {
1319 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1320 gui_print("TWRP injection complete.\n");
1321 }
1322
1323 operation_end(op_status);
1324 return 0;
1325}
1326
1327int GUIAction::checkbackupname(std::string arg)
1328{
1329 int op_status = 0;
1330
1331 operation_start("CheckBackupName");
1332 if (simulate) {
1333 simulate_progress_bar();
1334 } else {
1335 op_status = PartitionManager.Check_Backup_Name(true);
1336 if (op_status != 0)
1337 op_status = 1;
1338 }
1339
1340 operation_end(op_status);
1341 return 0;
1342}
1343
1344int GUIAction::decrypt(std::string arg)
1345{
1346 int op_status = 0;
1347
1348 operation_start("Decrypt");
1349 if (simulate) {
1350 simulate_progress_bar();
1351 } else {
1352 string Password;
1353 DataManager::GetValue("tw_crypto_password", Password);
1354 op_status = PartitionManager.Decrypt_Device(Password);
1355 if (op_status != 0)
1356 op_status = 1;
1357 else {
1358 int load_theme = 1;
1359
1360 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1361
1362 if (load_theme) {
1363 int has_datamedia;
1364
1365 // Check for a custom theme and load it if exists
1366 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1367 if (has_datamedia != 0) {
1368 struct stat st;
1369 int check = 0;
1370 std::string theme_path;
1371
1372 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1373 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1374 } else {
1375 LOGINFO("Got default contexts and file mode for storage files.\n");
1376 }
1377
1378 theme_path = DataManager::GetSettingsStoragePath();
1379 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1380 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1381 check = 1;
1382 }
1383
1384 theme_path += "/TWRP/theme/ui.zip";
1385 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1386 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1387 {
1388 // Loading the custom theme failed - try loading the stock theme
1389 LOGINFO("Attempting to reload stock theme...\n");
1390 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1391 {
1392 LOGERR("Failed to load base packages.\n");
1393 }
1394 }
1395 }
1396 }
1397 }
1398 }
1399 }
1400
1401 operation_end(op_status);
1402 return 0;
1403}
1404
thatcc8ddca2015-01-03 01:59:36 +01001405int GUIAction::adbsideload(std::string arg)
1406{
1407 operation_start("Sideload");
1408 if (simulate) {
1409 simulate_progress_bar();
1410 operation_end(0);
1411 } else {
thatc6085482015-01-09 22:12:43 +01001412 gui_print("Starting ADB sideload feature...\n");
1413 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1414
1415 // wait for the adb connection
1416 int ret = apply_from_adb("/", &sideload_child_pid);
1417 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1418
1419 if (ret != 0) {
1420 if (ret == -2)
1421 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1422 ret = 1; // failure
1423 } else {
1424 int wipe_cache = 0;
1425 int wipe_dalvik = 0;
1426 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1427
1428 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1429 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1430 PartitionManager.Wipe_By_Path("/cache");
1431 if (wipe_dalvik)
1432 PartitionManager.Wipe_Dalvik_Cache();
1433 } else {
1434 ret = 1; // failure
1435 }
thatcc8ddca2015-01-03 01:59:36 +01001436 }
thatc6085482015-01-09 22:12:43 +01001437 if (sideload_child_pid) {
1438 LOGINFO("Signaling child sideload process to exit.\n");
1439 struct stat st;
1440 // Calling stat() on this magic filename signals the minadbd
1441 // subprocess to shut down.
1442 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1443 int status;
1444 LOGINFO("Waiting for child sideload process to exit.\n");
1445 waitpid(sideload_child_pid, &status, 0);
1446 }
1447
1448 TWFunc::Toggle_MTP(mtp_was_enabled);
1449 reinject_after_flash();
1450 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001451 }
that3f7b1ac2014-12-30 11:30:13 +01001452 return 0;
1453}
1454
1455int GUIAction::adbsideloadcancel(std::string arg)
1456{
that3f7b1ac2014-12-30 11:30:13 +01001457 struct stat st;
1458 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1459 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001460 LOGINFO("Signaling child sideload process to exit.\n");
1461 // Calling stat() on this magic filename signals the minadbd
1462 // subprocess to shut down.
1463 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1464 if (!sideload_child_pid) {
1465 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001466 return 0;
1467 }
thatcc8ddca2015-01-03 01:59:36 +01001468 ::sleep(1);
1469 LOGINFO("Killing child sideload process.\n");
1470 kill(sideload_child_pid, SIGTERM);
1471 int status;
1472 LOGINFO("Waiting for child sideload process to exit.\n");
1473 waitpid(sideload_child_pid, &status, 0);
1474 sideload_child_pid = 0;
1475 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001476 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1477 return 0;
1478}
1479
1480int GUIAction::openrecoveryscript(std::string arg)
1481{
1482 operation_start("OpenRecoveryScript");
1483 if (simulate) {
1484 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001485 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001486 } else {
thatc6085482015-01-09 22:12:43 +01001487 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1488 // that we converted to ORS commands during boot in recovery.cpp.
1489 // Run those first.
1490 int reboot = 0;
1491 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1492 gui_print("Processing AOSP recovery commands...\n");
1493 if (OpenRecoveryScript::run_script_file() == 0) {
1494 reboot = 1;
1495 }
that3f7b1ac2014-12-30 11:30:13 +01001496 }
thatc6085482015-01-09 22:12:43 +01001497 // Check for the ORS file in /cache and attempt to run those commands.
1498 if (OpenRecoveryScript::check_for_script_file()) {
1499 gui_print("Processing OpenRecoveryScript file...\n");
1500 if (OpenRecoveryScript::run_script_file() == 0) {
1501 reboot = 1;
1502 }
1503 }
1504 if (reboot) {
1505 usleep(2000000); // Sleep for 2 seconds before rebooting
1506 TWFunc::tw_reboot(rb_system);
1507 } else {
1508 DataManager::SetValue("tw_page_done", 1);
1509 }
1510 operation_end(1);
1511 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001512 }
1513 return 0;
1514}
1515
1516int GUIAction::installsu(std::string arg)
1517{
1518 int op_status = 0;
1519
1520 operation_start("Install SuperSU");
1521 if (simulate) {
1522 simulate_progress_bar();
1523 } else {
1524 if (!TWFunc::Install_SuperSU())
1525 op_status = 1;
1526 }
1527
1528 operation_end(op_status);
1529 return 0;
1530}
1531
1532int GUIAction::fixsu(std::string arg)
1533{
1534 int op_status = 0;
1535
1536 operation_start("Fixing Superuser Permissions");
1537 if (simulate) {
1538 simulate_progress_bar();
1539 } else {
1540 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1541 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1542 }
1543
1544 operation_end(op_status);
1545 return 0;
1546}
1547
1548int GUIAction::decrypt_backup(std::string arg)
1549{
1550 int op_status = 0;
1551
1552 operation_start("Try Restore Decrypt");
1553 if (simulate) {
1554 simulate_progress_bar();
1555 } else {
1556 string Restore_Path, Filename, Password;
1557 DataManager::GetValue("tw_restore", Restore_Path);
1558 Restore_Path += "/";
1559 DataManager::GetValue("tw_restore_password", Password);
1560 TWFunc::SetPerformanceMode(true);
1561 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1562 op_status = 0; // success
1563 else
1564 op_status = 1; // fail
1565 TWFunc::SetPerformanceMode(false);
1566 }
1567
1568 operation_end(op_status);
1569 return 0;
1570}
1571
1572int GUIAction::repair(std::string arg)
1573{
1574 int op_status = 0;
1575
1576 operation_start("Repair Partition");
1577 if (simulate) {
1578 simulate_progress_bar();
1579 } else {
1580 string part_path;
1581 DataManager::GetValue("tw_partition_mount_point", part_path);
1582 if (PartitionManager.Repair_By_Path(part_path, true)) {
1583 op_status = 0; // success
1584 } else {
1585 LOGERR("Error repairing file system.\n");
1586 op_status = 1; // fail
1587 }
1588 }
1589
1590 operation_end(op_status);
1591 return 0;
1592}
1593
1594int GUIAction::changefilesystem(std::string arg)
1595{
1596 int op_status = 0;
1597
1598 operation_start("Change File System");
1599 if (simulate) {
1600 simulate_progress_bar();
1601 } else {
1602 string part_path, file_system;
1603 DataManager::GetValue("tw_partition_mount_point", part_path);
1604 DataManager::GetValue("tw_action_new_file_system", file_system);
1605 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1606 op_status = 0; // success
1607 } else {
1608 LOGERR("Error changing file system.\n");
1609 op_status = 1; // fail
1610 }
1611 }
1612 PartitionManager.Update_System_Details();
1613 operation_end(op_status);
1614 return 0;
1615}
1616
1617int GUIAction::startmtp(std::string arg)
1618{
1619 int op_status = 0;
1620
1621 operation_start("Start MTP");
1622 if (PartitionManager.Enable_MTP())
1623 op_status = 0; // success
1624 else
1625 op_status = 1; // fail
1626
1627 operation_end(op_status);
1628 return 0;
1629}
1630
1631int GUIAction::stopmtp(std::string arg)
1632{
1633 int op_status = 0;
1634
1635 operation_start("Stop MTP");
1636 if (PartitionManager.Disable_MTP())
1637 op_status = 0; // success
1638 else
1639 op_status = 1; // fail
1640
1641 operation_end(op_status);
1642 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001643}
1644
Ethan Yonker96af84a2015-01-05 14:58:36 -06001645int GUIAction::flashimage(std::string arg)
1646{
1647 int op_status = 0;
1648
1649 operation_start("Flash Image");
1650 string path, filename, full_filename;
1651 DataManager::GetValue("tw_zip_location", path);
1652 DataManager::GetValue("tw_file", filename);
1653 full_filename = path + "/" + filename;
1654 if (PartitionManager.Flash_Image(full_filename))
1655 op_status = 0; // success
1656 else
1657 op_status = 1; // fail
1658
1659 operation_end(op_status);
1660 return 0;
1661}
1662
Dees_Troy51a0e822012-09-05 15:24:24 -04001663int GUIAction::getKeyByName(std::string key)
1664{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001665 if (key == "home") return KEY_HOME;
1666 else if (key == "menu") return KEY_MENU;
1667 else if (key == "back") return KEY_BACK;
1668 else if (key == "search") return KEY_SEARCH;
1669 else if (key == "voldown") return KEY_VOLUMEDOWN;
1670 else if (key == "volup") return KEY_VOLUMEUP;
1671 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001672 int ret_val;
1673 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1674 if (!ret_val)
1675 return KEY_POWER;
1676 else
1677 return ret_val;
1678 }
1679
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001680 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001681}