blob: 96f6209576e2cc1d12507173380e4c5c3eb50acb [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
that3f7b1ac2014-12-30 11:30:13 +0100328int GUIAction::flash_zip(std::string filename, std::string pageName, 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 // We're going to jump to this page first, like a loading page
341 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400342
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200343 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400344 return -1;
345
that3f7b1ac2014-12-30 11:30:13 +0100346 // TODO: why again?
Ethan Yonker57e35872014-11-07 14:52:22 -0600347 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400348
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 if (simulate) {
350 simulate_progress_bar();
351 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400352 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400353
354 // Now, check if we need to ensure TWRP remains installed...
355 struct stat st;
356 if (stat("/sbin/installTwrp", &st) == 0)
357 {
358 DataManager::SetValue("tw_operation", "Configuring TWRP");
359 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000360 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200361 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400362 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000363 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 }
365 }
366 }
367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 // Done
369 DataManager::SetValue("ui_progress", 100);
370 DataManager::SetValue("ui_progress", 0);
371 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
thatc6085482015-01-09 22:12:43 +0100374bool GUIAction::needsToRunInSeparateThread(const GUIAction::Action& action)
375{
376 return setActionsRunningInCallerThread.find(action.mFunction) == setActionsRunningInCallerThread.end();
377}
378
Dees_Troy51a0e822012-09-05 15:24:24 -0400379int GUIAction::doActions()
380{
that3f7b1ac2014-12-30 11:30:13 +0100381 if (mActions.size() < 1)
382 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383
that7d3b54f2015-01-09 22:52:51 +0100384 bool needThread = false;
that3f7b1ac2014-12-30 11:30:13 +0100385 std::vector<Action>::iterator it;
386 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100387 {
388 if (needsToRunInSeparateThread(*it))
389 {
that7d3b54f2015-01-09 22:52:51 +0100390 needThread = true;
thatc6085482015-01-09 22:12:43 +0100391 break;
392 }
that7d3b54f2015-01-09 22:52:51 +0100393 }
394 if (needThread)
395 {
396 action_thread.threadActions(this);
397 }
398 else
399 {
400 for (it = mActions.begin(); it != mActions.end(); ++it)
401 doAction(*it);
thatc6085482015-01-09 22:12:43 +0100402 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400403
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400405}
406
that3f7b1ac2014-12-30 11:30:13 +0100407int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400408{
that3f7b1ac2014-12-30 11:30:13 +0100409 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400410
that3f7b1ac2014-12-30 11:30:13 +0100411 std::string function = gui_parse_text(action.mFunction);
412 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400413
that3f7b1ac2014-12-30 11:30:13 +0100414 // find function and execute it
415 mapFunc::const_iterator funcitr = mf.find(function);
416 if (funcitr != mf.end())
417 return (this->*funcitr->second)(arg);
418
419 LOGERR("Unknown action '%s'\n", function.c_str());
420 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
423void GUIAction::operation_start(const string operation_name)
424{
that3f7b1ac2014-12-30 11:30:13 +0100425 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000426 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 DataManager::SetValue(TW_ACTION_BUSY, 1);
428 DataManager::SetValue("ui_progress", 0);
429 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400430 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600431 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400432}
433
that3f7b1ac2014-12-30 11:30:13 +0100434void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400435{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000436 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 DataManager::SetValue("ui_progress", 100);
439 if (simulate) {
440 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
441 if (simulate_fail != 0)
442 DataManager::SetValue("tw_operation_status", 1);
443 else
444 DataManager::SetValue("tw_operation_status", 0);
445 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500446 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500448 }
449 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500451 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 }
453 DataManager::SetValue("tw_operation_state", 1);
454 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700455#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500456 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700457#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000458 time(&Stop);
459 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600460 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100461 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462}
463
that3f7b1ac2014-12-30 11:30:13 +0100464int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400465{
that3f7b1ac2014-12-30 11:30:13 +0100466 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
that3f7b1ac2014-12-30 11:30:13 +0100468 sync();
469 DataManager::SetValue("tw_gui_done", 1);
470 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
that3f7b1ac2014-12-30 11:30:13 +0100472 return 0;
473}
Dees_Troy51a0e822012-09-05 15:24:24 -0400474
that3f7b1ac2014-12-30 11:30:13 +0100475int GUIAction::home(std::string arg)
476{
477 PageManager::SelectPackage("TWRP");
478 gui_changePage("main");
479 return 0;
480}
Dees_Troy51a0e822012-09-05 15:24:24 -0400481
that3f7b1ac2014-12-30 11:30:13 +0100482int GUIAction::key(std::string arg)
483{
484 const int key = getKeyByName(arg);
485 PageManager::NotifyKey(key, true);
486 PageManager::NotifyKey(key, false);
487 return 0;
488}
489
490int GUIAction::page(std::string arg)
491{
492 std::string page_name = gui_parse_text(arg);
493 return gui_changePage(page_name);
494}
495
496int GUIAction::reload(std::string arg)
497{
498 int check = 0, ret_val = 0;
499 std::string theme_path;
500
501 operation_start("Reload Theme");
502 theme_path = DataManager::GetSettingsStoragePath();
503 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
504 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
505 check = 1;
506 }
507
508 theme_path += "/TWRP/theme/ui.zip";
509 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600510 {
that3f7b1ac2014-12-30 11:30:13 +0100511 // Loading the custom theme failed - try loading the stock theme
512 LOGINFO("Attempting to reload stock theme...\n");
513 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 {
that3f7b1ac2014-12-30 11:30:13 +0100515 LOGERR("Failed to load base packages.\n");
516 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 }
that3f7b1ac2014-12-30 11:30:13 +0100519 operation_end(ret_val);
520 return 0;
521}
Dees_Troy51a0e822012-09-05 15:24:24 -0400522
that3f7b1ac2014-12-30 11:30:13 +0100523int GUIAction::readBackup(std::string arg)
524{
525 string Restore_Name;
526 DataManager::GetValue("tw_restore", Restore_Name);
527 PartitionManager.Set_Restore_Files(Restore_Name);
528 return 0;
529}
530
531int GUIAction::set(std::string arg)
532{
533 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200534 {
that3f7b1ac2014-12-30 11:30:13 +0100535 string varName = arg.substr(0, arg.find('='));
536 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400537
that3f7b1ac2014-12-30 11:30:13 +0100538 DataManager::GetValue(value, value);
539 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 }
that3f7b1ac2014-12-30 11:30:13 +0100541 else
542 DataManager::SetValue(arg, "1");
543 return 0;
544}
Dees_Troy51a0e822012-09-05 15:24:24 -0400545
that3f7b1ac2014-12-30 11:30:13 +0100546int GUIAction::clear(std::string arg)
547{
548 DataManager::SetValue(arg, "0");
549 return 0;
550}
Dees_Troy51a0e822012-09-05 15:24:24 -0400551
that3f7b1ac2014-12-30 11:30:13 +0100552int GUIAction::mount(std::string arg)
553{
554 if (arg == "usb") {
555 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400556 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100557 PartitionManager.usb_storage_enable();
558 else
559 gui_print("Simulating actions...\n");
560 } else if (!simulate) {
561 PartitionManager.Mount_By_Path(arg, true);
562 PartitionManager.Add_MTP_Storage(arg);
563 } else
564 gui_print("Simulating actions...\n");
565 return 0;
566}
567
568int GUIAction::unmount(std::string arg)
569{
570 if (arg == "usb") {
571 if (!simulate)
572 PartitionManager.usb_storage_disable();
573 else
574 gui_print("Simulating actions...\n");
575 DataManager::SetValue(TW_ACTION_BUSY, 0);
576 } else if (!simulate) {
577 PartitionManager.UnMount_By_Path(arg, true);
578 } else
579 gui_print("Simulating actions...\n");
580 return 0;
581}
582
583int GUIAction::restoredefaultsettings(std::string arg)
584{
585 operation_start("Restore Defaults");
586 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
587 gui_print("Simulating actions...\n");
588 else {
589 DataManager::ResetDefaults();
590 PartitionManager.Update_System_Details();
591 PartitionManager.Mount_Current_Storage(true);
592 }
593 operation_end(0);
594 return 0;
595}
596
597int GUIAction::copylog(std::string arg)
598{
599 operation_start("Copy Log");
600 if (!simulate)
601 {
602 string dst;
603 PartitionManager.Mount_Current_Storage(true);
604 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
605 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
606 tw_set_default_metadata(dst.c_str());
607 sync();
608 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
609 } else
610 simulate_progress_bar();
611 operation_end(0);
612 return 0;
613}
614
615
616int GUIAction::compute(std::string arg)
617{
618 if (arg.find("+") != string::npos)
619 {
620 string varName = arg.substr(0, arg.find('+'));
621 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
622 int amount_to_add = atoi(string_to_add.c_str());
623 int value;
624
625 DataManager::GetValue(varName, value);
626 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400627 return 0;
628 }
that3f7b1ac2014-12-30 11:30:13 +0100629 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 {
that3f7b1ac2014-12-30 11:30:13 +0100631 string varName = arg.substr(0, arg.find('-'));
632 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
633 int amount_to_subtract = atoi(string_to_subtract.c_str());
634 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400635
that3f7b1ac2014-12-30 11:30:13 +0100636 DataManager::GetValue(varName, value);
637 value -= amount_to_subtract;
638 if (value <= 0)
639 value = 0;
640 DataManager::SetValue(varName, value);
641 return 0;
642 }
643 if (arg.find("*") != string::npos)
644 {
645 string varName = arg.substr(0, arg.find('*'));
646 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
647 int multiply_by = atoi(multiply_by_str.c_str());
648 int value;
649
650 DataManager::GetValue(varName, value);
651 DataManager::SetValue(varName, value*multiply_by);
652 return 0;
653 }
654 if (arg.find("/") != string::npos)
655 {
656 string varName = arg.substr(0, arg.find('/'));
657 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
658 int divide_by = atoi(divide_by_str.c_str());
659 int value;
660
661 if(divide_by != 0)
662 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400663 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100664 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400665 }
666 return 0;
667 }
that3f7b1ac2014-12-30 11:30:13 +0100668 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
669 return -1;
670}
Dees_Troy51a0e822012-09-05 15:24:24 -0400671
that3f7b1ac2014-12-30 11:30:13 +0100672int GUIAction::setguitimezone(std::string arg)
673{
674 string SelectedZone;
675 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
676 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
677 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
678
679 int dst;
680 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
681
682 string offset;
683 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
684
685 string NewTimeZone = Zone;
686 if (offset != "0")
687 NewTimeZone += ":" + offset;
688
689 if (dst != 0)
690 NewTimeZone += DSTZone;
691
692 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
693 DataManager::update_tz_environment_variables();
694 return 0;
695}
696
697int GUIAction::overlay(std::string arg)
698{
699 return gui_changeOverlay(arg);
700}
701
702int GUIAction::queuezip(std::string arg)
703{
704 if (zip_queue_index >= 10) {
705 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400706 return 0;
707 }
that3f7b1ac2014-12-30 11:30:13 +0100708 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
709 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
710 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100712 }
713 return 0;
714}
715
716int GUIAction::cancelzip(std::string arg)
717{
718 if (zip_queue_index <= 0) {
719 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400720 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100721 } else {
722 zip_queue_index--;
723 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 }
that3f7b1ac2014-12-30 11:30:13 +0100725 return 0;
726}
Dees_Troy51a0e822012-09-05 15:24:24 -0400727
that3f7b1ac2014-12-30 11:30:13 +0100728int GUIAction::queueclear(std::string arg)
729{
730 zip_queue_index = 0;
731 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
732 return 0;
733}
Dees_Troy51a0e822012-09-05 15:24:24 -0400734
that3f7b1ac2014-12-30 11:30:13 +0100735int GUIAction::sleep(std::string arg)
736{
737 operation_start("Sleep");
738 usleep(atoi(arg.c_str()));
739 operation_end(0);
740 return 0;
741}
Dees Troyb21cc642013-09-10 17:36:41 +0000742
that3f7b1ac2014-12-30 11:30:13 +0100743int GUIAction::appenddatetobackupname(std::string arg)
744{
745 operation_start("AppendDateToBackupName");
746 string Backup_Name;
747 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
748 Backup_Name += TWFunc::Get_Current_Date();
749 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
750 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
751 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
752 operation_end(0);
753 return 0;
754}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500755
that3f7b1ac2014-12-30 11:30:13 +0100756int GUIAction::generatebackupname(std::string arg)
757{
758 operation_start("GenerateBackupName");
759 TWFunc::Auto_Generate_Backup_Name();
760 operation_end(0);
761 return 0;
762}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500763
that3f7b1ac2014-12-30 11:30:13 +0100764int GUIAction::checkpartitionlist(std::string arg)
765{
766 string Wipe_List, wipe_path;
767 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500768
that3f7b1ac2014-12-30 11:30:13 +0100769 DataManager::GetValue("tw_wipe_list", Wipe_List);
770 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
771 if (!Wipe_List.empty()) {
772 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
773 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
774 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
775 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
776 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
777 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 } else {
that3f7b1ac2014-12-30 11:30:13 +0100779 count++;
780 }
781 start_pos = end_pos + 1;
782 end_pos = Wipe_List.find(";", start_pos);
783 }
784 DataManager::SetValue("tw_check_partition_list", count);
785 } else {
786 DataManager::SetValue("tw_check_partition_list", 0);
787 }
788 return 0;
789}
Dees_Troy51a0e822012-09-05 15:24:24 -0400790
that3f7b1ac2014-12-30 11:30:13 +0100791int GUIAction::getpartitiondetails(std::string arg)
792{
793 string Wipe_List, wipe_path;
794 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400795
that3f7b1ac2014-12-30 11:30:13 +0100796 DataManager::GetValue("tw_wipe_list", Wipe_List);
797 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
798 if (!Wipe_List.empty()) {
799 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
800 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
801 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
802 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
803 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
804 // Do nothing
805 } else {
806 DataManager::SetValue("tw_partition_path", wipe_path);
807 break;
808 }
809 start_pos = end_pos + 1;
810 end_pos = Wipe_List.find(";", start_pos);
811 }
812 if (!wipe_path.empty()) {
813 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
814 if (Part) {
815 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500816
that3f7b1ac2014-12-30 11:30:13 +0100817 DataManager::SetValue("tw_partition_name", Part->Display_Name);
818 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
819 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
820 DataManager::SetValue("tw_partition_size", Part->Size / mb);
821 DataManager::SetValue("tw_partition_used", Part->Used / mb);
822 DataManager::SetValue("tw_partition_free", Part->Free / mb);
823 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
824 DataManager::SetValue("tw_partition_removable", Part->Removable);
825 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
826
827 if (Part->Can_Repair())
828 DataManager::SetValue("tw_partition_can_repair", 1);
829 else
830 DataManager::SetValue("tw_partition_can_repair", 0);
831 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
832 DataManager::SetValue("tw_partition_vfat", 1);
833 else
834 DataManager::SetValue("tw_partition_vfat", 0);
835 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
836 DataManager::SetValue("tw_partition_exfat", 1);
837 else
838 DataManager::SetValue("tw_partition_exfat", 0);
839 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
840 DataManager::SetValue("tw_partition_f2fs", 1);
841 else
842 DataManager::SetValue("tw_partition_f2fs", 0);
843 if (TWFunc::Path_Exists("/sbin/mke2fs"))
844 DataManager::SetValue("tw_partition_ext", 1);
845 else
846 DataManager::SetValue("tw_partition_ext", 0);
847 return 0;
848 } else {
849 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
850 }
851 }
852 }
853 DataManager::SetValue("tw_partition_name", "");
854 DataManager::SetValue("tw_partition_file_system", "");
855 return 0;
856}
857
858int GUIAction::screenshot(std::string arg)
859{
860 time_t tm;
861 char path[256];
862 int path_len;
863 uid_t uid = -1;
864 gid_t gid = -1;
865
866 struct passwd *pwd = getpwnam("media_rw");
867 if(pwd) {
868 uid = pwd->pw_uid;
869 gid = pwd->pw_gid;
870 }
871
872 const std::string storage = DataManager::GetCurrentStoragePath();
873 if(PartitionManager.Is_Mounted_By_Path(storage)) {
874 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
875 } else {
876 strcpy(path, "/tmp/");
877 }
878
879 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
880 return 0;
881
882 tm = time(NULL);
883 path_len = strlen(path);
884
885 // Screenshot_2014-01-01-18-21-38.png
886 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
887
888 int res = gr_save_screenshot(path);
889 if(res == 0) {
890 chmod(path, 0666);
891 chown(path, uid, gid);
892
893 gui_print("Screenshot was saved to %s\n", path);
894
895 // blink to notify that the screenshow was taken
896 gr_color(255, 255, 255, 255);
897 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
898 gr_flip();
899 gui_forceRender();
900 } else {
901 LOGERR("Failed to take a screenshot!\n");
902 }
903 return 0;
904}
905
906int GUIAction::setbrightness(std::string arg)
907{
908 return TWFunc::Set_Brightness(arg);
909}
910
911int GUIAction::fileexists(std::string arg)
912{
913 struct stat st;
914 string newpath = arg + "/.";
915
916 operation_start("FileExists");
917 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
918 operation_end(0);
919 else
920 operation_end(1);
921 return 0;
922}
923
thatcc8ddca2015-01-03 01:59:36 +0100924void GUIAction::reinject_after_flash()
925{
926 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
927 operation_start("ReinjectTWRP");
928 gui_print("Injecting TWRP into boot image...\n");
929 if (simulate) {
930 simulate_progress_bar();
931 } else {
932 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
933 if (Boot == NULL || Boot->Current_File_System != "emmc")
934 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
935 else {
936 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
937 TWFunc::Exec_Cmd(injectcmd);
938 }
939 gui_print("TWRP injection complete.\n");
940 }
941 }
942}
943
that3f7b1ac2014-12-30 11:30:13 +0100944int GUIAction::flash(std::string arg)
945{
946 int i, ret_val = 0, wipe_cache = 0;
947 for (i=0; i<zip_queue_index; i++) {
948 operation_start("Flashing");
949 DataManager::SetValue("tw_filename", zip_queue[i]);
950 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
951
952 TWFunc::SetPerformanceMode(true);
953 ret_val = flash_zip(zip_queue[i], arg, &wipe_cache);
954 TWFunc::SetPerformanceMode(false);
955 if (ret_val != 0) {
956 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
957 i = 10; // Error flashing zip - exit queue
958 ret_val = 1;
959 }
960 }
961 zip_queue_index = 0;
962 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
963
964 if (wipe_cache)
965 PartitionManager.Wipe_By_Path("/cache");
966
thatcc8ddca2015-01-03 01:59:36 +0100967 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100968 PartitionManager.Update_System_Details();
969 operation_end(ret_val);
970 return 0;
971}
972
973int GUIAction::wipe(std::string arg)
974{
975 operation_start("Format");
976 DataManager::SetValue("tw_partition", arg);
977
978 int ret_val = false;
979
980 if (simulate) {
981 simulate_progress_bar();
982 } else {
983 if (arg == "data")
984 ret_val = PartitionManager.Factory_Reset();
985 else if (arg == "battery")
986 ret_val = PartitionManager.Wipe_Battery_Stats();
987 else if (arg == "rotate")
988 ret_val = PartitionManager.Wipe_Rotate_Data();
989 else if (arg == "dalvik")
990 ret_val = PartitionManager.Wipe_Dalvik_Cache();
991 else if (arg == "DATAMEDIA") {
992 ret_val = PartitionManager.Format_Data();
993 } else if (arg == "INTERNAL") {
994 int has_datamedia, dual_storage;
995
996 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
997 if (has_datamedia) {
998 ret_val = PartitionManager.Wipe_Media_From_Data();
999 } else {
1000 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1001 }
1002 } else if (arg == "EXTERNAL") {
1003 string External_Path;
1004
1005 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1006 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1007 } else if (arg == "ANDROIDSECURE") {
1008 ret_val = PartitionManager.Wipe_Android_Secure();
1009 } else if (arg == "LIST") {
1010 string Wipe_List, wipe_path;
1011 bool skip = false;
1012 ret_val = true;
1013 TWPartition* wipe_part = NULL;
1014
1015 DataManager::GetValue("tw_wipe_list", Wipe_List);
1016 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1017 if (!Wipe_List.empty()) {
1018 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1019 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1020 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1021 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1022 if (wipe_path == "/and-sec") {
1023 if (!PartitionManager.Wipe_Android_Secure()) {
1024 LOGERR("Unable to wipe android secure\n");
1025 ret_val = false;
1026 break;
1027 } else {
1028 skip = true;
1029 }
1030 } else if (wipe_path == "DALVIK") {
1031 if (!PartitionManager.Wipe_Dalvik_Cache()) {
1032 LOGERR("Failed to wipe dalvik\n");
1033 ret_val = false;
1034 break;
1035 } else {
1036 skip = true;
1037 }
1038 } else if (wipe_path == "INTERNAL") {
1039 if (!PartitionManager.Wipe_Media_From_Data()) {
1040 ret_val = false;
1041 break;
1042 } else {
1043 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001044 }
1045 }
that3f7b1ac2014-12-30 11:30:13 +01001046 if (!skip) {
1047 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
1048 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
1049 ret_val = false;
1050 break;
1051 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1052 arg = wipe_path;
1053 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001054 } else {
that3f7b1ac2014-12-30 11:30:13 +01001055 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001056 }
that3f7b1ac2014-12-30 11:30:13 +01001057 start_pos = end_pos + 1;
1058 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001059 }
1060 }
that3f7b1ac2014-12-30 11:30:13 +01001061 } else
1062 ret_val = PartitionManager.Wipe_By_Path(arg);
1063#ifdef TW_OEM_BUILD
1064 if (arg == DataManager::GetSettingsStoragePath()) {
1065 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1066 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001067
that3f7b1ac2014-12-30 11:30:13 +01001068 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1069 LOGINFO("Making TWRP folder and saving settings.\n");
1070 Storage_Path += "/TWRP";
1071 mkdir(Storage_Path.c_str(), 0777);
1072 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001073 } else {
that3f7b1ac2014-12-30 11:30:13 +01001074 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1075 }
1076 }
1077#endif
1078 }
1079 PartitionManager.Update_System_Details();
1080 if (ret_val)
1081 ret_val = 0; // 0 is success
1082 else
1083 ret_val = 1; // 1 is failure
1084 operation_end(ret_val);
1085 return 0;
1086}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001087
that3f7b1ac2014-12-30 11:30:13 +01001088int GUIAction::refreshsizes(std::string arg)
1089{
1090 operation_start("Refreshing Sizes");
1091 if (simulate) {
1092 simulate_progress_bar();
1093 } else
1094 PartitionManager.Update_System_Details();
1095 operation_end(0);
1096 return 0;
1097}
1098
1099int GUIAction::nandroid(std::string arg)
1100{
1101 operation_start("Nandroid");
1102 int ret = 0;
1103
1104 if (simulate) {
1105 DataManager::SetValue("tw_partition", "Simulation");
1106 simulate_progress_bar();
1107 } else {
1108 if (arg == "backup") {
1109 string Backup_Name;
1110 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1111 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1112 ret = PartitionManager.Run_Backup();
1113 }
1114 else {
1115 operation_end(1);
1116 return -1;
1117
1118 }
1119 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1120 } else if (arg == "restore") {
1121 string Restore_Name;
1122 DataManager::GetValue("tw_restore", Restore_Name);
1123 ret = PartitionManager.Run_Restore(Restore_Name);
1124 } else {
1125 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001126 return -1;
1127 }
1128 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001129 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001130 if (ret == false)
1131 ret = 1; // 1 for failure
1132 else
1133 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001134 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001135 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001136}
Dees_Troy51a0e822012-09-05 15:24:24 -04001137
that3f7b1ac2014-12-30 11:30:13 +01001138int GUIAction::fixpermissions(std::string arg)
1139{
1140 operation_start("Fix Permissions");
1141 LOGINFO("fix permissions started!\n");
1142 if (simulate) {
1143 simulate_progress_bar();
1144 } else {
1145 int op_status = PartitionManager.Fix_Permissions();
1146 if (op_status != 0)
1147 op_status = 1; // failure
1148 operation_end(op_status);
1149 }
1150 return 0;
1151}
1152
1153int GUIAction::dd(std::string arg)
1154{
1155 operation_start("imaging");
1156
1157 if (simulate) {
1158 simulate_progress_bar();
1159 } else {
1160 string cmd = "dd " + arg;
1161 TWFunc::Exec_Cmd(cmd);
1162 }
1163 operation_end(0);
1164 return 0;
1165}
1166
1167int GUIAction::partitionsd(std::string arg)
1168{
1169 operation_start("Partition SD Card");
1170 int ret_val = 0;
1171
1172 if (simulate) {
1173 simulate_progress_bar();
1174 } else {
1175 int allow_partition;
1176 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1177 if (allow_partition == 0) {
1178 gui_print("This device does not have a real SD Card!\nAborting!\n");
1179 } else {
1180 if (!PartitionManager.Partition_SDCard())
1181 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001182 }
that3f7b1ac2014-12-30 11:30:13 +01001183 }
1184 operation_end(ret_val);
1185 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001186
that3f7b1ac2014-12-30 11:30:13 +01001187}
Dees_Troy51a0e822012-09-05 15:24:24 -04001188
that3f7b1ac2014-12-30 11:30:13 +01001189int GUIAction::installhtcdumlock(std::string arg)
1190{
1191 operation_start("Install HTC Dumlock");
1192 if (simulate) {
1193 simulate_progress_bar();
1194 } else
1195 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001196
that3f7b1ac2014-12-30 11:30:13 +01001197 operation_end(0);
1198 return 0;
1199}
Dees_Troy51a0e822012-09-05 15:24:24 -04001200
that3f7b1ac2014-12-30 11:30:13 +01001201int GUIAction::htcdumlockrestoreboot(std::string arg)
1202{
1203 operation_start("HTC Dumlock Restore Boot");
1204 if (simulate) {
1205 simulate_progress_bar();
1206 } else
1207 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001208
that3f7b1ac2014-12-30 11:30:13 +01001209 operation_end(0);
1210 return 0;
1211}
Dees_Troy51a0e822012-09-05 15:24:24 -04001212
that3f7b1ac2014-12-30 11:30:13 +01001213int GUIAction::htcdumlockreflashrecovery(std::string arg)
1214{
1215 operation_start("HTC Dumlock Reflash Recovery");
1216 if (simulate) {
1217 simulate_progress_bar();
1218 } else
1219 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001220
that3f7b1ac2014-12-30 11:30:13 +01001221 operation_end(0);
1222 return 0;
1223}
Dees_Troy51a0e822012-09-05 15:24:24 -04001224
that3f7b1ac2014-12-30 11:30:13 +01001225int GUIAction::cmd(std::string arg)
1226{
1227 int op_status = 0;
1228
1229 operation_start("Command");
1230 LOGINFO("Running command: '%s'\n", arg.c_str());
1231 if (simulate) {
1232 simulate_progress_bar();
1233 } else {
1234 op_status = TWFunc::Exec_Cmd(arg);
1235 if (op_status != 0)
1236 op_status = 1;
1237 }
1238
1239 operation_end(op_status);
1240 return 0;
1241}
1242
1243int GUIAction::terminalcommand(std::string arg)
1244{
1245 int op_status = 0;
1246 string cmdpath, command;
1247
1248 DataManager::GetValue("tw_terminal_location", cmdpath);
1249 operation_start("CommandOutput");
1250 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1251 if (simulate) {
1252 simulate_progress_bar();
1253 operation_end(op_status);
1254 } else {
1255 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1256 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001257 DataManager::SetValue("tw_terminal_state", 1);
1258 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001259 FILE* fp;
1260 char line[512];
1261
1262 fp = popen(command.c_str(), "r");
1263 if (fp == NULL) {
1264 LOGERR("Error opening command to run.\n");
1265 } else {
1266 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1267 struct timeval timeout;
1268 fd_set fdset;
1269
1270 while(keep_going)
1271 {
1272 FD_ZERO(&fdset);
1273 FD_SET(fd, &fdset);
1274 timeout.tv_sec = 0;
1275 timeout.tv_usec = 400000;
1276 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1277 if (has_data == 0) {
1278 // Timeout reached
1279 DataManager::GetValue("tw_terminal_state", check);
1280 if (check == 0) {
1281 keep_going = 0;
1282 }
1283 } else if (has_data < 0) {
1284 // End of execution
1285 keep_going = 0;
1286 } else {
1287 // Try to read output
1288 memset(line, 0, sizeof(line));
1289 bytes_read = read(fd, line, sizeof(line));
1290 if (bytes_read > 0)
1291 gui_print("%s", line); // Display output
1292 else
1293 keep_going = 0; // Done executing
1294 }
1295 }
1296 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001297 }
thatc6085482015-01-09 22:12:43 +01001298 DataManager::SetValue("tw_operation_status", 0);
1299 DataManager::SetValue("tw_operation_state", 1);
1300 DataManager::SetValue("tw_terminal_state", 0);
1301 DataManager::SetValue("tw_background_thread_running", 0);
1302 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001303 }
1304 return 0;
1305}
1306
1307int GUIAction::killterminal(std::string arg)
1308{
1309 int op_status = 0;
1310
1311 LOGINFO("Sending kill command...\n");
1312 operation_start("KillCommand");
1313 DataManager::SetValue("tw_operation_status", 0);
1314 DataManager::SetValue("tw_operation_state", 1);
1315 DataManager::SetValue("tw_terminal_state", 0);
1316 DataManager::SetValue("tw_background_thread_running", 0);
1317 DataManager::SetValue(TW_ACTION_BUSY, 0);
1318 return 0;
1319}
1320
1321int GUIAction::reinjecttwrp(std::string arg)
1322{
1323 int op_status = 0;
1324 operation_start("ReinjectTWRP");
1325 gui_print("Injecting TWRP into boot image...\n");
1326 if (simulate) {
1327 simulate_progress_bar();
1328 } else {
1329 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1330 gui_print("TWRP injection complete.\n");
1331 }
1332
1333 operation_end(op_status);
1334 return 0;
1335}
1336
1337int GUIAction::checkbackupname(std::string arg)
1338{
1339 int op_status = 0;
1340
1341 operation_start("CheckBackupName");
1342 if (simulate) {
1343 simulate_progress_bar();
1344 } else {
1345 op_status = PartitionManager.Check_Backup_Name(true);
1346 if (op_status != 0)
1347 op_status = 1;
1348 }
1349
1350 operation_end(op_status);
1351 return 0;
1352}
1353
1354int GUIAction::decrypt(std::string arg)
1355{
1356 int op_status = 0;
1357
1358 operation_start("Decrypt");
1359 if (simulate) {
1360 simulate_progress_bar();
1361 } else {
1362 string Password;
1363 DataManager::GetValue("tw_crypto_password", Password);
1364 op_status = PartitionManager.Decrypt_Device(Password);
1365 if (op_status != 0)
1366 op_status = 1;
1367 else {
1368 int load_theme = 1;
1369
1370 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1371
1372 if (load_theme) {
1373 int has_datamedia;
1374
1375 // Check for a custom theme and load it if exists
1376 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1377 if (has_datamedia != 0) {
1378 struct stat st;
1379 int check = 0;
1380 std::string theme_path;
1381
1382 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1383 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1384 } else {
1385 LOGINFO("Got default contexts and file mode for storage files.\n");
1386 }
1387
1388 theme_path = DataManager::GetSettingsStoragePath();
1389 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1390 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1391 check = 1;
1392 }
1393
1394 theme_path += "/TWRP/theme/ui.zip";
1395 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1396 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1397 {
1398 // Loading the custom theme failed - try loading the stock theme
1399 LOGINFO("Attempting to reload stock theme...\n");
1400 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1401 {
1402 LOGERR("Failed to load base packages.\n");
1403 }
1404 }
1405 }
1406 }
1407 }
1408 }
1409 }
1410
1411 operation_end(op_status);
1412 return 0;
1413}
1414
thatcc8ddca2015-01-03 01:59:36 +01001415int GUIAction::adbsideload(std::string arg)
1416{
1417 operation_start("Sideload");
1418 if (simulate) {
1419 simulate_progress_bar();
1420 operation_end(0);
1421 } else {
thatc6085482015-01-09 22:12:43 +01001422 gui_print("Starting ADB sideload feature...\n");
1423 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1424
1425 // wait for the adb connection
1426 int ret = apply_from_adb("/", &sideload_child_pid);
1427 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1428
1429 if (ret != 0) {
1430 if (ret == -2)
1431 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1432 ret = 1; // failure
1433 } else {
1434 int wipe_cache = 0;
1435 int wipe_dalvik = 0;
1436 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1437
1438 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1439 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1440 PartitionManager.Wipe_By_Path("/cache");
1441 if (wipe_dalvik)
1442 PartitionManager.Wipe_Dalvik_Cache();
1443 } else {
1444 ret = 1; // failure
1445 }
thatcc8ddca2015-01-03 01:59:36 +01001446 }
thatc6085482015-01-09 22:12:43 +01001447 if (sideload_child_pid) {
1448 LOGINFO("Signaling child sideload process to exit.\n");
1449 struct stat st;
1450 // Calling stat() on this magic filename signals the minadbd
1451 // subprocess to shut down.
1452 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1453 int status;
1454 LOGINFO("Waiting for child sideload process to exit.\n");
1455 waitpid(sideload_child_pid, &status, 0);
1456 }
1457
1458 TWFunc::Toggle_MTP(mtp_was_enabled);
1459 reinject_after_flash();
1460 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001461 }
that3f7b1ac2014-12-30 11:30:13 +01001462 return 0;
1463}
1464
1465int GUIAction::adbsideloadcancel(std::string arg)
1466{
that3f7b1ac2014-12-30 11:30:13 +01001467 struct stat st;
1468 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1469 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001470 LOGINFO("Signaling child sideload process to exit.\n");
1471 // Calling stat() on this magic filename signals the minadbd
1472 // subprocess to shut down.
1473 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1474 if (!sideload_child_pid) {
1475 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001476 return 0;
1477 }
thatcc8ddca2015-01-03 01:59:36 +01001478 ::sleep(1);
1479 LOGINFO("Killing child sideload process.\n");
1480 kill(sideload_child_pid, SIGTERM);
1481 int status;
1482 LOGINFO("Waiting for child sideload process to exit.\n");
1483 waitpid(sideload_child_pid, &status, 0);
1484 sideload_child_pid = 0;
1485 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001486 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1487 return 0;
1488}
1489
1490int GUIAction::openrecoveryscript(std::string arg)
1491{
1492 operation_start("OpenRecoveryScript");
1493 if (simulate) {
1494 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001495 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001496 } else {
thatc6085482015-01-09 22:12:43 +01001497 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1498 // that we converted to ORS commands during boot in recovery.cpp.
1499 // Run those first.
1500 int reboot = 0;
1501 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1502 gui_print("Processing AOSP recovery commands...\n");
1503 if (OpenRecoveryScript::run_script_file() == 0) {
1504 reboot = 1;
1505 }
that3f7b1ac2014-12-30 11:30:13 +01001506 }
thatc6085482015-01-09 22:12:43 +01001507 // Check for the ORS file in /cache and attempt to run those commands.
1508 if (OpenRecoveryScript::check_for_script_file()) {
1509 gui_print("Processing OpenRecoveryScript file...\n");
1510 if (OpenRecoveryScript::run_script_file() == 0) {
1511 reboot = 1;
1512 }
1513 }
1514 if (reboot) {
1515 usleep(2000000); // Sleep for 2 seconds before rebooting
1516 TWFunc::tw_reboot(rb_system);
1517 } else {
1518 DataManager::SetValue("tw_page_done", 1);
1519 }
1520 operation_end(1);
1521 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001522 }
1523 return 0;
1524}
1525
1526int GUIAction::installsu(std::string arg)
1527{
1528 int op_status = 0;
1529
1530 operation_start("Install SuperSU");
1531 if (simulate) {
1532 simulate_progress_bar();
1533 } else {
1534 if (!TWFunc::Install_SuperSU())
1535 op_status = 1;
1536 }
1537
1538 operation_end(op_status);
1539 return 0;
1540}
1541
1542int GUIAction::fixsu(std::string arg)
1543{
1544 int op_status = 0;
1545
1546 operation_start("Fixing Superuser Permissions");
1547 if (simulate) {
1548 simulate_progress_bar();
1549 } else {
1550 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1551 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1552 }
1553
1554 operation_end(op_status);
1555 return 0;
1556}
1557
1558int GUIAction::decrypt_backup(std::string arg)
1559{
1560 int op_status = 0;
1561
1562 operation_start("Try Restore Decrypt");
1563 if (simulate) {
1564 simulate_progress_bar();
1565 } else {
1566 string Restore_Path, Filename, Password;
1567 DataManager::GetValue("tw_restore", Restore_Path);
1568 Restore_Path += "/";
1569 DataManager::GetValue("tw_restore_password", Password);
1570 TWFunc::SetPerformanceMode(true);
1571 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1572 op_status = 0; // success
1573 else
1574 op_status = 1; // fail
1575 TWFunc::SetPerformanceMode(false);
1576 }
1577
1578 operation_end(op_status);
1579 return 0;
1580}
1581
1582int GUIAction::repair(std::string arg)
1583{
1584 int op_status = 0;
1585
1586 operation_start("Repair Partition");
1587 if (simulate) {
1588 simulate_progress_bar();
1589 } else {
1590 string part_path;
1591 DataManager::GetValue("tw_partition_mount_point", part_path);
1592 if (PartitionManager.Repair_By_Path(part_path, true)) {
1593 op_status = 0; // success
1594 } else {
1595 LOGERR("Error repairing file system.\n");
1596 op_status = 1; // fail
1597 }
1598 }
1599
1600 operation_end(op_status);
1601 return 0;
1602}
1603
1604int GUIAction::changefilesystem(std::string arg)
1605{
1606 int op_status = 0;
1607
1608 operation_start("Change File System");
1609 if (simulate) {
1610 simulate_progress_bar();
1611 } else {
1612 string part_path, file_system;
1613 DataManager::GetValue("tw_partition_mount_point", part_path);
1614 DataManager::GetValue("tw_action_new_file_system", file_system);
1615 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1616 op_status = 0; // success
1617 } else {
1618 LOGERR("Error changing file system.\n");
1619 op_status = 1; // fail
1620 }
1621 }
1622 PartitionManager.Update_System_Details();
1623 operation_end(op_status);
1624 return 0;
1625}
1626
1627int GUIAction::startmtp(std::string arg)
1628{
1629 int op_status = 0;
1630
1631 operation_start("Start MTP");
1632 if (PartitionManager.Enable_MTP())
1633 op_status = 0; // success
1634 else
1635 op_status = 1; // fail
1636
1637 operation_end(op_status);
1638 return 0;
1639}
1640
1641int GUIAction::stopmtp(std::string arg)
1642{
1643 int op_status = 0;
1644
1645 operation_start("Stop MTP");
1646 if (PartitionManager.Disable_MTP())
1647 op_status = 0; // success
1648 else
1649 op_status = 1; // fail
1650
1651 operation_end(op_status);
1652 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001653}
1654
Ethan Yonker96af84a2015-01-05 14:58:36 -06001655int GUIAction::flashimage(std::string arg)
1656{
1657 int op_status = 0;
1658
1659 operation_start("Flash Image");
1660 string path, filename, full_filename;
1661 DataManager::GetValue("tw_zip_location", path);
1662 DataManager::GetValue("tw_file", filename);
1663 full_filename = path + "/" + filename;
1664 if (PartitionManager.Flash_Image(full_filename))
1665 op_status = 0; // success
1666 else
1667 op_status = 1; // fail
1668
1669 operation_end(op_status);
1670 return 0;
1671}
1672
Dees_Troy51a0e822012-09-05 15:24:24 -04001673int GUIAction::getKeyByName(std::string key)
1674{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001675 if (key == "home") return KEY_HOME;
1676 else if (key == "menu") return KEY_MENU;
1677 else if (key == "back") return KEY_BACK;
1678 else if (key == "search") return KEY_SEARCH;
1679 else if (key == "voldown") return KEY_VOLUMEDOWN;
1680 else if (key == "volup") return KEY_VOLUMEUP;
1681 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001682 int ret_val;
1683 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1684 if (!ret_val)
1685 return KEY_POWER;
1686 else
1687 return ret_val;
1688 }
1689
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001690 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001691}