blob: f030635d74ae59110cde5d1d9dc98621f3d9d7eb [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"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Dees_Troy51a0e822012-09-05 15:24:24 -040061void curtainClose(void);
62
that3f7b1ac2014-12-30 11:30:13 +010063GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010064std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010065static string zip_queue[10];
66static int zip_queue_index;
67static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010068pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010069
thatc6085482015-01-09 22:12:43 +010070static ActionThread action_thread;
71
72static void *ActionThread_work_wrapper(void *data)
73{
74 action_thread.run(data);
75 return NULL;
76}
77
78ActionThread::ActionThread()
79{
80 m_thread_running = false;
81 pthread_mutex_init(&m_act_lock, NULL);
82}
83
84ActionThread::~ActionThread()
85{
86 pthread_mutex_lock(&m_act_lock);
87 if(m_thread_running) {
88 pthread_mutex_unlock(&m_act_lock);
89 pthread_join(m_thread, NULL);
90 } else {
91 pthread_mutex_unlock(&m_act_lock);
92 }
93 pthread_mutex_destroy(&m_act_lock);
94}
95
that7d3b54f2015-01-09 22:52:51 +010096void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +010097{
98 pthread_mutex_lock(&m_act_lock);
99 if (m_thread_running) {
100 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100101 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
102 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100103 } else {
104 m_thread_running = true;
105 pthread_mutex_unlock(&m_act_lock);
106 ThreadData *d = new ThreadData;
107 d->act = act;
thatc6085482015-01-09 22:12:43 +0100108
109 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
110 }
111}
112
113void ActionThread::run(void *data)
114{
115 ThreadData *d = (ThreadData*)data;
116 GUIAction* act = d->act;
117
118 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100119 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100120 act->doAction(*it);
121
122 pthread_mutex_lock(&m_act_lock);
123 m_thread_running = false;
124 pthread_mutex_unlock(&m_act_lock);
125 delete d;
126}
127
Dees_Troy51a0e822012-09-05 15:24:24 -0400128GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100129 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400130{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 xml_node<>* child;
132 xml_node<>* actions;
133 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
that3f7b1ac2014-12-30 11:30:13 +0100137 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100138#define ADD_ACTION(n) mf[#n] = &GUIAction::n
139#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100140 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100141 ADD_ACTION(reboot);
142 ADD_ACTION(home);
143 ADD_ACTION(key);
144 ADD_ACTION(page);
145 ADD_ACTION(reload);
146 ADD_ACTION(readBackup);
147 ADD_ACTION(set);
148 ADD_ACTION(clear);
149 ADD_ACTION(mount);
150 ADD_ACTION(unmount);
151 ADD_ACTION_EX("umount", unmount);
152 ADD_ACTION(restoredefaultsettings);
153 ADD_ACTION(copylog);
154 ADD_ACTION(compute);
155 ADD_ACTION_EX("addsubtract", compute);
156 ADD_ACTION(setguitimezone);
157 ADD_ACTION(overlay);
158 ADD_ACTION(queuezip);
159 ADD_ACTION(cancelzip);
160 ADD_ACTION(queueclear);
161 ADD_ACTION(sleep);
162 ADD_ACTION(appenddatetobackupname);
163 ADD_ACTION(generatebackupname);
164 ADD_ACTION(checkpartitionlist);
165 ADD_ACTION(getpartitiondetails);
166 ADD_ACTION(screenshot);
167 ADD_ACTION(setbrightness);
168 ADD_ACTION(fileexists);
169 ADD_ACTION(killterminal);
170 ADD_ACTION(checkbackupname);
171 ADD_ACTION(adbsideloadcancel);
172 ADD_ACTION(fixsu);
173 ADD_ACTION(startmtp);
174 ADD_ACTION(stopmtp);
175 ADD_ACTION(cancelbackup);
thatc6085482015-01-09 22:12:43 +0100176
177 // remember actions that run in the caller thread
178 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
179 setActionsRunningInCallerThread.insert(it->first);
180
181 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100182 ADD_ACTION(flash);
183 ADD_ACTION(wipe);
184 ADD_ACTION(refreshsizes);
185 ADD_ACTION(nandroid);
186 ADD_ACTION(fixpermissions);
187 ADD_ACTION(dd);
188 ADD_ACTION(partitionsd);
189 ADD_ACTION(installhtcdumlock);
190 ADD_ACTION(htcdumlockrestoreboot);
191 ADD_ACTION(htcdumlockreflashrecovery);
192 ADD_ACTION(cmd);
193 ADD_ACTION(terminalcommand);
194 ADD_ACTION(reinjecttwrp);
195 ADD_ACTION(decrypt);
196 ADD_ACTION(adbsideload);
197 ADD_ACTION(openrecoveryscript);
198 ADD_ACTION(installsu);
199 ADD_ACTION(decrypt_backup);
200 ADD_ACTION(repair);
201 ADD_ACTION(changefilesystem);
202 ADD_ACTION(flashimage);
that3f7b1ac2014-12-30 11:30:13 +0100203 }
204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 // First, get the action
206 actions = node->first_node("actions");
207 if (actions) child = actions->first_node("action");
208 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 while (child)
213 {
214 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 attr = child->first_attribute("function");
217 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500218
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 action.mFunction = attr->value();
220 action.mArg = child->value();
221 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400222
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200223 child = child->next_sibling("action");
224 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 // Now, let's get either the key or region
227 child = node->first_node("touch");
228 if (child)
229 {
230 attr = child->first_attribute("key");
231 if (attr)
232 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100233 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
234 for(size_t i = 0; i < keys.size(); ++i)
235 {
236 const int key = getKeyByName(keys[i]);
237 mKeys[key] = false;
238 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 }
240 else
241 {
242 attr = child->first_attribute("x");
243 if (!attr) return;
244 mActionX = atol(attr->value());
245 attr = child->first_attribute("y");
246 if (!attr) return;
247 mActionY = atol(attr->value());
248 attr = child->first_attribute("w");
249 if (!attr) return;
250 mActionW = atol(attr->value());
251 attr = child->first_attribute("h");
252 if (!attr) return;
253 mActionH = atol(attr->value());
254 }
255 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400256}
257
258int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
259{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 if (state == TOUCH_RELEASE)
261 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264}
265
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100266int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400267{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100268 if (mKeys.empty())
269 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400270
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100271 std::map<int, bool>::iterator itr = mKeys.find(key);
272 if(itr == mKeys.end())
273 return 0;
274
275 bool prevState = itr->second;
276 itr->second = down;
277
278 // If there is only one key for this action, wait for key up so it
279 // doesn't trigger with multi-key actions.
280 // Else, check if all buttons are pressed, then consume their release events
281 // so they don't trigger one-button actions and reset mKeys pressed status
282 if(mKeys.size() == 1) {
283 if(!down && prevState)
284 doActions();
285 } else if(down) {
286 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
287 if(!itr->second)
288 return 0;
289 }
290
291 // Passed, all req buttons are pressed, reset them and consume release events
292 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
293 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
294 kb->ConsumeKeyRelease(itr->first);
295 itr->second = false;
296 }
297
298 doActions();
299 }
300
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 return 0;
302}
303
Vojtech Bocek07220562014-02-08 02:05:33 +0100304int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400305{
Vojtech Bocek07220562014-02-08 02:05:33 +0100306 GUIObject::NotifyVarChange(varName, value);
307
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100308 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100310 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200311 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400312
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400314}
315
316void GUIAction::simulate_progress_bar(void)
317{
Dees_Troy2673cec2013-04-02 20:22:16 +0000318 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400319 for (int i = 0; i < 5; i++)
320 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500321 if (PartitionManager.stop_backup.get_value()) {
322 DataManager::SetValue("tw_cancel_backup", 1);
323 gui_print("Backup Canceled.\n");
324 DataManager::SetValue("ui_progress", 0);
325 PartitionManager.stop_backup.set_value(0);
326 return;
327 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400328 usleep(500000);
329 DataManager::SetValue("ui_progress", i * 20);
330 }
331}
332
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600333int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400334{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
337 DataManager::SetValue("ui_progress", 0);
338
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 if (filename.empty())
340 {
341 LOGERR("No file specified.\n");
342 return -1;
343 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400344
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400346 return -1;
347
Dees_Troy51a0e822012-09-05 15:24:24 -0400348 if (simulate) {
349 simulate_progress_bar();
350 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400351 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400352
353 // Now, check if we need to ensure TWRP remains installed...
354 struct stat st;
355 if (stat("/sbin/installTwrp", &st) == 0)
356 {
357 DataManager::SetValue("tw_operation", "Configuring TWRP");
358 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000359 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200360 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400361 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000362 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 }
364 }
365 }
366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 // Done
368 DataManager::SetValue("ui_progress", 100);
369 DataManager::SetValue("ui_progress", 0);
370 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371}
372
thatc6085482015-01-09 22:12:43 +0100373bool GUIAction::needsToRunInSeparateThread(const GUIAction::Action& action)
374{
Vojtech Bocek85cfb7d2015-01-12 18:25:59 +0100375 return setActionsRunningInCallerThread.find(gui_parse_text(action.mFunction)) == setActionsRunningInCallerThread.end();
thatc6085482015-01-09 22:12:43 +0100376}
377
Dees_Troy51a0e822012-09-05 15:24:24 -0400378int GUIAction::doActions()
379{
that3f7b1ac2014-12-30 11:30:13 +0100380 if (mActions.size() < 1)
381 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
that7d3b54f2015-01-09 22:52:51 +0100383 bool needThread = false;
that3f7b1ac2014-12-30 11:30:13 +0100384 std::vector<Action>::iterator it;
385 for (it = mActions.begin(); it != mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100386 {
387 if (needsToRunInSeparateThread(*it))
388 {
that7d3b54f2015-01-09 22:52:51 +0100389 needThread = true;
thatc6085482015-01-09 22:12:43 +0100390 break;
391 }
that7d3b54f2015-01-09 22:52:51 +0100392 }
393 if (needThread)
394 {
395 action_thread.threadActions(this);
396 }
397 else
398 {
Vojtech Boceke979abd2015-01-12 18:29:12 +0100399 const size_t cnt = mActions.size();
400 for (size_t i = 0; i < cnt; ++i)
401 doAction(mActions[i]);
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);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500455 blankTimer.resetTimerAndUnblank();
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000456 time(&Stop);
457 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600458 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100459 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460}
461
that3f7b1ac2014-12-30 11:30:13 +0100462int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400463{
that3f7b1ac2014-12-30 11:30:13 +0100464 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400465
that3f7b1ac2014-12-30 11:30:13 +0100466 sync();
467 DataManager::SetValue("tw_gui_done", 1);
468 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400469
that3f7b1ac2014-12-30 11:30:13 +0100470 return 0;
471}
Dees_Troy51a0e822012-09-05 15:24:24 -0400472
that3f7b1ac2014-12-30 11:30:13 +0100473int GUIAction::home(std::string arg)
474{
475 PageManager::SelectPackage("TWRP");
476 gui_changePage("main");
477 return 0;
478}
Dees_Troy51a0e822012-09-05 15:24:24 -0400479
that3f7b1ac2014-12-30 11:30:13 +0100480int GUIAction::key(std::string arg)
481{
482 const int key = getKeyByName(arg);
483 PageManager::NotifyKey(key, true);
484 PageManager::NotifyKey(key, false);
485 return 0;
486}
487
488int GUIAction::page(std::string arg)
489{
490 std::string page_name = gui_parse_text(arg);
491 return gui_changePage(page_name);
492}
493
494int GUIAction::reload(std::string arg)
495{
496 int check = 0, ret_val = 0;
497 std::string theme_path;
498
that3f7b1ac2014-12-30 11:30:13 +0100499 theme_path = DataManager::GetSettingsStoragePath();
500 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
501 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
502 check = 1;
503 }
504
505 theme_path += "/TWRP/theme/ui.zip";
506 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600507 {
that3f7b1ac2014-12-30 11:30:13 +0100508 // Loading the custom theme failed - try loading the stock theme
509 LOGINFO("Attempting to reload stock theme...\n");
Dees Troy3454ade2015-01-20 19:21:04 +0000510 if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 {
that3f7b1ac2014-12-30 11:30:13 +0100512 LOGERR("Failed to load base packages.\n");
513 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 }
that3f7b1ac2014-12-30 11:30:13 +0100516 return 0;
517}
Dees_Troy51a0e822012-09-05 15:24:24 -0400518
that3f7b1ac2014-12-30 11:30:13 +0100519int GUIAction::readBackup(std::string arg)
520{
521 string Restore_Name;
522 DataManager::GetValue("tw_restore", Restore_Name);
523 PartitionManager.Set_Restore_Files(Restore_Name);
524 return 0;
525}
526
527int GUIAction::set(std::string arg)
528{
529 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200530 {
that3f7b1ac2014-12-30 11:30:13 +0100531 string varName = arg.substr(0, arg.find('='));
532 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400533
that3f7b1ac2014-12-30 11:30:13 +0100534 DataManager::GetValue(value, value);
535 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200536 }
that3f7b1ac2014-12-30 11:30:13 +0100537 else
538 DataManager::SetValue(arg, "1");
539 return 0;
540}
Dees_Troy51a0e822012-09-05 15:24:24 -0400541
that3f7b1ac2014-12-30 11:30:13 +0100542int GUIAction::clear(std::string arg)
543{
544 DataManager::SetValue(arg, "0");
545 return 0;
546}
Dees_Troy51a0e822012-09-05 15:24:24 -0400547
that3f7b1ac2014-12-30 11:30:13 +0100548int GUIAction::mount(std::string arg)
549{
550 if (arg == "usb") {
551 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400552 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100553 PartitionManager.usb_storage_enable();
554 else
555 gui_print("Simulating actions...\n");
556 } else if (!simulate) {
557 PartitionManager.Mount_By_Path(arg, true);
558 PartitionManager.Add_MTP_Storage(arg);
559 } else
560 gui_print("Simulating actions...\n");
561 return 0;
562}
563
564int GUIAction::unmount(std::string arg)
565{
566 if (arg == "usb") {
567 if (!simulate)
568 PartitionManager.usb_storage_disable();
569 else
570 gui_print("Simulating actions...\n");
571 DataManager::SetValue(TW_ACTION_BUSY, 0);
572 } else if (!simulate) {
573 PartitionManager.UnMount_By_Path(arg, true);
574 } else
575 gui_print("Simulating actions...\n");
576 return 0;
577}
578
579int GUIAction::restoredefaultsettings(std::string arg)
580{
581 operation_start("Restore Defaults");
582 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
583 gui_print("Simulating actions...\n");
584 else {
585 DataManager::ResetDefaults();
586 PartitionManager.Update_System_Details();
587 PartitionManager.Mount_Current_Storage(true);
588 }
589 operation_end(0);
590 return 0;
591}
592
593int GUIAction::copylog(std::string arg)
594{
595 operation_start("Copy Log");
596 if (!simulate)
597 {
598 string dst;
599 PartitionManager.Mount_Current_Storage(true);
600 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
601 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
602 tw_set_default_metadata(dst.c_str());
603 sync();
604 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
605 } else
606 simulate_progress_bar();
607 operation_end(0);
608 return 0;
609}
610
611
612int GUIAction::compute(std::string arg)
613{
614 if (arg.find("+") != string::npos)
615 {
616 string varName = arg.substr(0, arg.find('+'));
617 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
618 int amount_to_add = atoi(string_to_add.c_str());
619 int value;
620
621 DataManager::GetValue(varName, value);
622 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 return 0;
624 }
that3f7b1ac2014-12-30 11:30:13 +0100625 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400626 {
that3f7b1ac2014-12-30 11:30:13 +0100627 string varName = arg.substr(0, arg.find('-'));
628 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
629 int amount_to_subtract = atoi(string_to_subtract.c_str());
630 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400631
that3f7b1ac2014-12-30 11:30:13 +0100632 DataManager::GetValue(varName, value);
633 value -= amount_to_subtract;
634 if (value <= 0)
635 value = 0;
636 DataManager::SetValue(varName, value);
637 return 0;
638 }
639 if (arg.find("*") != string::npos)
640 {
641 string varName = arg.substr(0, arg.find('*'));
642 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
643 int multiply_by = atoi(multiply_by_str.c_str());
644 int value;
645
646 DataManager::GetValue(varName, value);
647 DataManager::SetValue(varName, value*multiply_by);
648 return 0;
649 }
650 if (arg.find("/") != string::npos)
651 {
652 string varName = arg.substr(0, arg.find('/'));
653 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
654 int divide_by = atoi(divide_by_str.c_str());
655 int value;
656
657 if(divide_by != 0)
658 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100660 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 }
662 return 0;
663 }
that3f7b1ac2014-12-30 11:30:13 +0100664 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
665 return -1;
666}
Dees_Troy51a0e822012-09-05 15:24:24 -0400667
that3f7b1ac2014-12-30 11:30:13 +0100668int GUIAction::setguitimezone(std::string arg)
669{
670 string SelectedZone;
671 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
672 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
673 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
674
675 int dst;
676 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
677
678 string offset;
679 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
680
681 string NewTimeZone = Zone;
682 if (offset != "0")
683 NewTimeZone += ":" + offset;
684
685 if (dst != 0)
686 NewTimeZone += DSTZone;
687
688 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
689 DataManager::update_tz_environment_variables();
690 return 0;
691}
692
693int GUIAction::overlay(std::string arg)
694{
695 return gui_changeOverlay(arg);
696}
697
698int GUIAction::queuezip(std::string arg)
699{
700 if (zip_queue_index >= 10) {
701 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 return 0;
703 }
that3f7b1ac2014-12-30 11:30:13 +0100704 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
705 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
706 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100708 }
709 return 0;
710}
711
712int GUIAction::cancelzip(std::string arg)
713{
714 if (zip_queue_index <= 0) {
715 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100717 } else {
718 zip_queue_index--;
719 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400720 }
that3f7b1ac2014-12-30 11:30:13 +0100721 return 0;
722}
Dees_Troy51a0e822012-09-05 15:24:24 -0400723
that3f7b1ac2014-12-30 11:30:13 +0100724int GUIAction::queueclear(std::string arg)
725{
726 zip_queue_index = 0;
727 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
728 return 0;
729}
Dees_Troy51a0e822012-09-05 15:24:24 -0400730
that3f7b1ac2014-12-30 11:30:13 +0100731int GUIAction::sleep(std::string arg)
732{
733 operation_start("Sleep");
734 usleep(atoi(arg.c_str()));
735 operation_end(0);
736 return 0;
737}
Dees Troyb21cc642013-09-10 17:36:41 +0000738
that3f7b1ac2014-12-30 11:30:13 +0100739int GUIAction::appenddatetobackupname(std::string arg)
740{
741 operation_start("AppendDateToBackupName");
742 string Backup_Name;
743 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
744 Backup_Name += TWFunc::Get_Current_Date();
745 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
746 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
747 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
748 operation_end(0);
749 return 0;
750}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500751
that3f7b1ac2014-12-30 11:30:13 +0100752int GUIAction::generatebackupname(std::string arg)
753{
754 operation_start("GenerateBackupName");
755 TWFunc::Auto_Generate_Backup_Name();
756 operation_end(0);
757 return 0;
758}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500759
that3f7b1ac2014-12-30 11:30:13 +0100760int GUIAction::checkpartitionlist(std::string arg)
761{
762 string Wipe_List, wipe_path;
763 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500764
that3f7b1ac2014-12-30 11:30:13 +0100765 DataManager::GetValue("tw_wipe_list", Wipe_List);
766 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
767 if (!Wipe_List.empty()) {
768 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
769 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
770 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
771 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
772 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
773 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400774 } else {
that3f7b1ac2014-12-30 11:30:13 +0100775 count++;
776 }
777 start_pos = end_pos + 1;
778 end_pos = Wipe_List.find(";", start_pos);
779 }
780 DataManager::SetValue("tw_check_partition_list", count);
781 } else {
782 DataManager::SetValue("tw_check_partition_list", 0);
783 }
784 return 0;
785}
Dees_Troy51a0e822012-09-05 15:24:24 -0400786
that3f7b1ac2014-12-30 11:30:13 +0100787int GUIAction::getpartitiondetails(std::string arg)
788{
789 string Wipe_List, wipe_path;
790 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400791
that3f7b1ac2014-12-30 11:30:13 +0100792 DataManager::GetValue("tw_wipe_list", Wipe_List);
793 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
794 if (!Wipe_List.empty()) {
795 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
796 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
797 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
798 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
799 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
800 // Do nothing
801 } else {
802 DataManager::SetValue("tw_partition_path", wipe_path);
803 break;
804 }
805 start_pos = end_pos + 1;
806 end_pos = Wipe_List.find(";", start_pos);
807 }
808 if (!wipe_path.empty()) {
809 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
810 if (Part) {
811 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500812
that3f7b1ac2014-12-30 11:30:13 +0100813 DataManager::SetValue("tw_partition_name", Part->Display_Name);
814 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
815 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
816 DataManager::SetValue("tw_partition_size", Part->Size / mb);
817 DataManager::SetValue("tw_partition_used", Part->Used / mb);
818 DataManager::SetValue("tw_partition_free", Part->Free / mb);
819 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
820 DataManager::SetValue("tw_partition_removable", Part->Removable);
821 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
822
823 if (Part->Can_Repair())
824 DataManager::SetValue("tw_partition_can_repair", 1);
825 else
826 DataManager::SetValue("tw_partition_can_repair", 0);
827 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
828 DataManager::SetValue("tw_partition_vfat", 1);
829 else
830 DataManager::SetValue("tw_partition_vfat", 0);
831 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
832 DataManager::SetValue("tw_partition_exfat", 1);
833 else
834 DataManager::SetValue("tw_partition_exfat", 0);
835 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
836 DataManager::SetValue("tw_partition_f2fs", 1);
837 else
838 DataManager::SetValue("tw_partition_f2fs", 0);
839 if (TWFunc::Path_Exists("/sbin/mke2fs"))
840 DataManager::SetValue("tw_partition_ext", 1);
841 else
842 DataManager::SetValue("tw_partition_ext", 0);
843 return 0;
844 } else {
845 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
846 }
847 }
848 }
849 DataManager::SetValue("tw_partition_name", "");
850 DataManager::SetValue("tw_partition_file_system", "");
851 return 0;
852}
853
854int GUIAction::screenshot(std::string arg)
855{
856 time_t tm;
857 char path[256];
858 int path_len;
859 uid_t uid = -1;
860 gid_t gid = -1;
861
862 struct passwd *pwd = getpwnam("media_rw");
863 if(pwd) {
864 uid = pwd->pw_uid;
865 gid = pwd->pw_gid;
866 }
867
868 const std::string storage = DataManager::GetCurrentStoragePath();
869 if(PartitionManager.Is_Mounted_By_Path(storage)) {
870 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
871 } else {
872 strcpy(path, "/tmp/");
873 }
874
875 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
876 return 0;
877
878 tm = time(NULL);
879 path_len = strlen(path);
880
881 // Screenshot_2014-01-01-18-21-38.png
882 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
883
884 int res = gr_save_screenshot(path);
885 if(res == 0) {
886 chmod(path, 0666);
887 chown(path, uid, gid);
888
889 gui_print("Screenshot was saved to %s\n", path);
890
891 // blink to notify that the screenshow was taken
892 gr_color(255, 255, 255, 255);
893 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
894 gr_flip();
895 gui_forceRender();
896 } else {
897 LOGERR("Failed to take a screenshot!\n");
898 }
899 return 0;
900}
901
902int GUIAction::setbrightness(std::string arg)
903{
904 return TWFunc::Set_Brightness(arg);
905}
906
907int GUIAction::fileexists(std::string arg)
908{
909 struct stat st;
910 string newpath = arg + "/.";
911
912 operation_start("FileExists");
913 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
914 operation_end(0);
915 else
916 operation_end(1);
917 return 0;
918}
919
thatcc8ddca2015-01-03 01:59:36 +0100920void GUIAction::reinject_after_flash()
921{
922 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
923 operation_start("ReinjectTWRP");
924 gui_print("Injecting TWRP into boot image...\n");
925 if (simulate) {
926 simulate_progress_bar();
927 } else {
928 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
929 if (Boot == NULL || Boot->Current_File_System != "emmc")
930 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
931 else {
932 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
933 TWFunc::Exec_Cmd(injectcmd);
934 }
935 gui_print("TWRP injection complete.\n");
936 }
937 }
938}
939
that3f7b1ac2014-12-30 11:30:13 +0100940int GUIAction::flash(std::string arg)
941{
942 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600943 // We're going to jump to this page first, like a loading page
944 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +0100945 for (i=0; i<zip_queue_index; i++) {
946 operation_start("Flashing");
947 DataManager::SetValue("tw_filename", zip_queue[i]);
948 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
949
950 TWFunc::SetPerformanceMode(true);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600951 ret_val = flash_zip(zip_queue[i], &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +0100952 TWFunc::SetPerformanceMode(false);
953 if (ret_val != 0) {
954 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
that3f7b1ac2014-12-30 11:30:13 +0100955 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600956 break;
that3f7b1ac2014-12-30 11:30:13 +0100957 }
958 }
959 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +0100960
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600961 if (wipe_cache) {
962 gui_print("One or more zip requested a cache wipe\nWiping cache now.\n");
that3f7b1ac2014-12-30 11:30:13 +0100963 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600964 }
that3f7b1ac2014-12-30 11:30:13 +0100965
thatcc8ddca2015-01-03 01:59:36 +0100966 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +0100967 PartitionManager.Update_System_Details();
968 operation_end(ret_val);
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600969 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100970 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{
that3f7b1ac2014-12-30 11:30:13 +01001101 if (simulate) {
1102 DataManager::SetValue("tw_partition", "Simulation");
1103 simulate_progress_bar();
1104 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001105 operation_start("Nandroid");
1106 int ret = 0;
1107
that3f7b1ac2014-12-30 11:30:13 +01001108 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;
that3f7b1ac2014-12-30 11:30:13 +01001117 }
1118 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1119 } else if (arg == "restore") {
1120 string Restore_Name;
1121 DataManager::GetValue("tw_restore", Restore_Name);
1122 ret = PartitionManager.Run_Restore(Restore_Name);
1123 } else {
1124 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001125 return -1;
1126 }
1127 DataManager::SetValue("tw_encrypt_backup", 0);
1128 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001129 if (ret == false)
1130 ret = 1; // 1 for failure
1131 else
1132 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001133 DataManager::SetValue("tw_cancel_backup", 0);
that3f7b1ac2014-12-30 11:30:13 +01001134 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001135 }
1136 else {
1137 DataManager::SetValue("tw_cancel_backup", 1);
1138 gui_print("Backup Canceled.\n");
1139 ret = 0;
1140 }
1141 return ret;
1142 }
1143 return 0;
1144}
1145
1146int GUIAction::cancelbackup(std::string arg) {
1147 if (simulate) {
1148 simulate_progress_bar();
1149 PartitionManager.stop_backup.set_value(1);
1150 operation_end(0);
1151 }
1152 else {
1153 operation_start("Cancel Backup");
1154 int op_status = PartitionManager.Cancel_Backup();
1155 if (op_status != 0)
1156 op_status = 1; // failure
1157 operation_end(op_status);
1158 }
1159
1160 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001161}
Dees_Troy51a0e822012-09-05 15:24:24 -04001162
that3f7b1ac2014-12-30 11:30:13 +01001163int GUIAction::fixpermissions(std::string arg)
1164{
1165 operation_start("Fix Permissions");
1166 LOGINFO("fix permissions started!\n");
1167 if (simulate) {
1168 simulate_progress_bar();
1169 } else {
1170 int op_status = PartitionManager.Fix_Permissions();
1171 if (op_status != 0)
1172 op_status = 1; // failure
1173 operation_end(op_status);
1174 }
1175 return 0;
1176}
1177
1178int GUIAction::dd(std::string arg)
1179{
1180 operation_start("imaging");
1181
1182 if (simulate) {
1183 simulate_progress_bar();
1184 } else {
1185 string cmd = "dd " + arg;
1186 TWFunc::Exec_Cmd(cmd);
1187 }
1188 operation_end(0);
1189 return 0;
1190}
1191
1192int GUIAction::partitionsd(std::string arg)
1193{
1194 operation_start("Partition SD Card");
1195 int ret_val = 0;
1196
1197 if (simulate) {
1198 simulate_progress_bar();
1199 } else {
1200 int allow_partition;
1201 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1202 if (allow_partition == 0) {
1203 gui_print("This device does not have a real SD Card!\nAborting!\n");
1204 } else {
1205 if (!PartitionManager.Partition_SDCard())
1206 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001207 }
that3f7b1ac2014-12-30 11:30:13 +01001208 }
1209 operation_end(ret_val);
1210 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001211
that3f7b1ac2014-12-30 11:30:13 +01001212}
Dees_Troy51a0e822012-09-05 15:24:24 -04001213
that3f7b1ac2014-12-30 11:30:13 +01001214int GUIAction::installhtcdumlock(std::string arg)
1215{
1216 operation_start("Install HTC Dumlock");
1217 if (simulate) {
1218 simulate_progress_bar();
1219 } else
1220 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001221
that3f7b1ac2014-12-30 11:30:13 +01001222 operation_end(0);
1223 return 0;
1224}
Dees_Troy51a0e822012-09-05 15:24:24 -04001225
that3f7b1ac2014-12-30 11:30:13 +01001226int GUIAction::htcdumlockrestoreboot(std::string arg)
1227{
1228 operation_start("HTC Dumlock Restore Boot");
1229 if (simulate) {
1230 simulate_progress_bar();
1231 } else
1232 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001233
that3f7b1ac2014-12-30 11:30:13 +01001234 operation_end(0);
1235 return 0;
1236}
Dees_Troy51a0e822012-09-05 15:24:24 -04001237
that3f7b1ac2014-12-30 11:30:13 +01001238int GUIAction::htcdumlockreflashrecovery(std::string arg)
1239{
1240 operation_start("HTC Dumlock Reflash Recovery");
1241 if (simulate) {
1242 simulate_progress_bar();
1243 } else
1244 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001245
that3f7b1ac2014-12-30 11:30:13 +01001246 operation_end(0);
1247 return 0;
1248}
Dees_Troy51a0e822012-09-05 15:24:24 -04001249
that3f7b1ac2014-12-30 11:30:13 +01001250int GUIAction::cmd(std::string arg)
1251{
1252 int op_status = 0;
1253
1254 operation_start("Command");
1255 LOGINFO("Running command: '%s'\n", arg.c_str());
1256 if (simulate) {
1257 simulate_progress_bar();
1258 } else {
1259 op_status = TWFunc::Exec_Cmd(arg);
1260 if (op_status != 0)
1261 op_status = 1;
1262 }
1263
1264 operation_end(op_status);
1265 return 0;
1266}
1267
1268int GUIAction::terminalcommand(std::string arg)
1269{
1270 int op_status = 0;
1271 string cmdpath, command;
1272
1273 DataManager::GetValue("tw_terminal_location", cmdpath);
1274 operation_start("CommandOutput");
1275 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1276 if (simulate) {
1277 simulate_progress_bar();
1278 operation_end(op_status);
1279 } else {
1280 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1281 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001282 DataManager::SetValue("tw_terminal_state", 1);
1283 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001284 FILE* fp;
1285 char line[512];
1286
1287 fp = popen(command.c_str(), "r");
1288 if (fp == NULL) {
1289 LOGERR("Error opening command to run.\n");
1290 } else {
1291 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1292 struct timeval timeout;
1293 fd_set fdset;
1294
1295 while(keep_going)
1296 {
1297 FD_ZERO(&fdset);
1298 FD_SET(fd, &fdset);
1299 timeout.tv_sec = 0;
1300 timeout.tv_usec = 400000;
1301 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1302 if (has_data == 0) {
1303 // Timeout reached
1304 DataManager::GetValue("tw_terminal_state", check);
1305 if (check == 0) {
1306 keep_going = 0;
1307 }
1308 } else if (has_data < 0) {
1309 // End of execution
1310 keep_going = 0;
1311 } else {
1312 // Try to read output
1313 memset(line, 0, sizeof(line));
1314 bytes_read = read(fd, line, sizeof(line));
1315 if (bytes_read > 0)
1316 gui_print("%s", line); // Display output
1317 else
1318 keep_going = 0; // Done executing
1319 }
1320 }
1321 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001322 }
thatc6085482015-01-09 22:12:43 +01001323 DataManager::SetValue("tw_operation_status", 0);
1324 DataManager::SetValue("tw_operation_state", 1);
1325 DataManager::SetValue("tw_terminal_state", 0);
1326 DataManager::SetValue("tw_background_thread_running", 0);
1327 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001328 }
1329 return 0;
1330}
1331
1332int GUIAction::killterminal(std::string arg)
1333{
1334 int op_status = 0;
1335
1336 LOGINFO("Sending kill command...\n");
1337 operation_start("KillCommand");
1338 DataManager::SetValue("tw_operation_status", 0);
1339 DataManager::SetValue("tw_operation_state", 1);
1340 DataManager::SetValue("tw_terminal_state", 0);
1341 DataManager::SetValue("tw_background_thread_running", 0);
1342 DataManager::SetValue(TW_ACTION_BUSY, 0);
1343 return 0;
1344}
1345
1346int GUIAction::reinjecttwrp(std::string arg)
1347{
1348 int op_status = 0;
1349 operation_start("ReinjectTWRP");
1350 gui_print("Injecting TWRP into boot image...\n");
1351 if (simulate) {
1352 simulate_progress_bar();
1353 } else {
1354 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1355 gui_print("TWRP injection complete.\n");
1356 }
1357
1358 operation_end(op_status);
1359 return 0;
1360}
1361
1362int GUIAction::checkbackupname(std::string arg)
1363{
1364 int op_status = 0;
1365
1366 operation_start("CheckBackupName");
1367 if (simulate) {
1368 simulate_progress_bar();
1369 } else {
1370 op_status = PartitionManager.Check_Backup_Name(true);
1371 if (op_status != 0)
1372 op_status = 1;
1373 }
1374
1375 operation_end(op_status);
1376 return 0;
1377}
1378
1379int GUIAction::decrypt(std::string arg)
1380{
1381 int op_status = 0;
1382
1383 operation_start("Decrypt");
1384 if (simulate) {
1385 simulate_progress_bar();
1386 } else {
1387 string Password;
1388 DataManager::GetValue("tw_crypto_password", Password);
1389 op_status = PartitionManager.Decrypt_Device(Password);
1390 if (op_status != 0)
1391 op_status = 1;
1392 else {
that3f7b1ac2014-12-30 11:30:13 +01001393
1394 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1395
Ethan Yonkercf50da52015-01-12 21:59:07 -06001396 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001397
Ethan Yonkercf50da52015-01-12 21:59:07 -06001398 // Check for a custom theme and load it if exists
1399 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1400 if (has_datamedia != 0) {
1401 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001402 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001403 } else {
1404 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001405 }
1406 }
1407 }
1408 }
1409
1410 operation_end(op_status);
1411 return 0;
1412}
1413
thatcc8ddca2015-01-03 01:59:36 +01001414int GUIAction::adbsideload(std::string arg)
1415{
1416 operation_start("Sideload");
1417 if (simulate) {
1418 simulate_progress_bar();
1419 operation_end(0);
1420 } else {
thatc6085482015-01-09 22:12:43 +01001421 gui_print("Starting ADB sideload feature...\n");
1422 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1423
1424 // wait for the adb connection
1425 int ret = apply_from_adb("/", &sideload_child_pid);
1426 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1427
1428 if (ret != 0) {
1429 if (ret == -2)
1430 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1431 ret = 1; // failure
1432 } else {
1433 int wipe_cache = 0;
1434 int wipe_dalvik = 0;
1435 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1436
1437 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1438 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1439 PartitionManager.Wipe_By_Path("/cache");
1440 if (wipe_dalvik)
1441 PartitionManager.Wipe_Dalvik_Cache();
1442 } else {
1443 ret = 1; // failure
1444 }
thatcc8ddca2015-01-03 01:59:36 +01001445 }
thatc6085482015-01-09 22:12:43 +01001446 if (sideload_child_pid) {
1447 LOGINFO("Signaling child sideload process to exit.\n");
1448 struct stat st;
1449 // Calling stat() on this magic filename signals the minadbd
1450 // subprocess to shut down.
1451 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1452 int status;
1453 LOGINFO("Waiting for child sideload process to exit.\n");
1454 waitpid(sideload_child_pid, &status, 0);
1455 }
1456
1457 TWFunc::Toggle_MTP(mtp_was_enabled);
1458 reinject_after_flash();
1459 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001460 }
that3f7b1ac2014-12-30 11:30:13 +01001461 return 0;
1462}
1463
1464int GUIAction::adbsideloadcancel(std::string arg)
1465{
that3f7b1ac2014-12-30 11:30:13 +01001466 struct stat st;
1467 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1468 gui_print("Cancelling ADB sideload...\n");
thatcc8ddca2015-01-03 01:59:36 +01001469 LOGINFO("Signaling child sideload process to exit.\n");
1470 // Calling stat() on this magic filename signals the minadbd
1471 // subprocess to shut down.
1472 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1473 if (!sideload_child_pid) {
1474 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001475 return 0;
1476 }
thatcc8ddca2015-01-03 01:59:36 +01001477 ::sleep(1);
1478 LOGINFO("Killing child sideload process.\n");
1479 kill(sideload_child_pid, SIGTERM);
1480 int status;
1481 LOGINFO("Waiting for child sideload process to exit.\n");
1482 waitpid(sideload_child_pid, &status, 0);
1483 sideload_child_pid = 0;
1484 operation_end(1);
that3f7b1ac2014-12-30 11:30:13 +01001485 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1486 return 0;
1487}
1488
1489int GUIAction::openrecoveryscript(std::string arg)
1490{
1491 operation_start("OpenRecoveryScript");
1492 if (simulate) {
1493 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001494 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001495 } else {
thatc6085482015-01-09 22:12:43 +01001496 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1497 // that we converted to ORS commands during boot in recovery.cpp.
1498 // Run those first.
1499 int reboot = 0;
1500 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1501 gui_print("Processing AOSP recovery commands...\n");
1502 if (OpenRecoveryScript::run_script_file() == 0) {
1503 reboot = 1;
1504 }
that3f7b1ac2014-12-30 11:30:13 +01001505 }
thatc6085482015-01-09 22:12:43 +01001506 // Check for the ORS file in /cache and attempt to run those commands.
1507 if (OpenRecoveryScript::check_for_script_file()) {
1508 gui_print("Processing OpenRecoveryScript file...\n");
1509 if (OpenRecoveryScript::run_script_file() == 0) {
1510 reboot = 1;
1511 }
1512 }
1513 if (reboot) {
1514 usleep(2000000); // Sleep for 2 seconds before rebooting
1515 TWFunc::tw_reboot(rb_system);
1516 } else {
1517 DataManager::SetValue("tw_page_done", 1);
1518 }
1519 operation_end(1);
1520 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001521 }
1522 return 0;
1523}
1524
1525int GUIAction::installsu(std::string arg)
1526{
1527 int op_status = 0;
1528
1529 operation_start("Install SuperSU");
1530 if (simulate) {
1531 simulate_progress_bar();
1532 } else {
1533 if (!TWFunc::Install_SuperSU())
1534 op_status = 1;
1535 }
1536
1537 operation_end(op_status);
1538 return 0;
1539}
1540
1541int GUIAction::fixsu(std::string arg)
1542{
1543 int op_status = 0;
1544
1545 operation_start("Fixing Superuser Permissions");
1546 if (simulate) {
1547 simulate_progress_bar();
1548 } else {
1549 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1550 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1551 }
1552
1553 operation_end(op_status);
1554 return 0;
1555}
1556
1557int GUIAction::decrypt_backup(std::string arg)
1558{
1559 int op_status = 0;
1560
1561 operation_start("Try Restore Decrypt");
1562 if (simulate) {
1563 simulate_progress_bar();
1564 } else {
1565 string Restore_Path, Filename, Password;
1566 DataManager::GetValue("tw_restore", Restore_Path);
1567 Restore_Path += "/";
1568 DataManager::GetValue("tw_restore_password", Password);
1569 TWFunc::SetPerformanceMode(true);
1570 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1571 op_status = 0; // success
1572 else
1573 op_status = 1; // fail
1574 TWFunc::SetPerformanceMode(false);
1575 }
1576
1577 operation_end(op_status);
1578 return 0;
1579}
1580
1581int GUIAction::repair(std::string arg)
1582{
1583 int op_status = 0;
1584
1585 operation_start("Repair Partition");
1586 if (simulate) {
1587 simulate_progress_bar();
1588 } else {
1589 string part_path;
1590 DataManager::GetValue("tw_partition_mount_point", part_path);
1591 if (PartitionManager.Repair_By_Path(part_path, true)) {
1592 op_status = 0; // success
1593 } else {
1594 LOGERR("Error repairing file system.\n");
1595 op_status = 1; // fail
1596 }
1597 }
1598
1599 operation_end(op_status);
1600 return 0;
1601}
1602
1603int GUIAction::changefilesystem(std::string arg)
1604{
1605 int op_status = 0;
1606
1607 operation_start("Change File System");
1608 if (simulate) {
1609 simulate_progress_bar();
1610 } else {
1611 string part_path, file_system;
1612 DataManager::GetValue("tw_partition_mount_point", part_path);
1613 DataManager::GetValue("tw_action_new_file_system", file_system);
1614 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1615 op_status = 0; // success
1616 } else {
1617 LOGERR("Error changing file system.\n");
1618 op_status = 1; // fail
1619 }
1620 }
1621 PartitionManager.Update_System_Details();
1622 operation_end(op_status);
1623 return 0;
1624}
1625
1626int GUIAction::startmtp(std::string arg)
1627{
1628 int op_status = 0;
1629
1630 operation_start("Start MTP");
1631 if (PartitionManager.Enable_MTP())
1632 op_status = 0; // success
1633 else
1634 op_status = 1; // fail
1635
1636 operation_end(op_status);
1637 return 0;
1638}
1639
1640int GUIAction::stopmtp(std::string arg)
1641{
1642 int op_status = 0;
1643
1644 operation_start("Stop MTP");
1645 if (PartitionManager.Disable_MTP())
1646 op_status = 0; // success
1647 else
1648 op_status = 1; // fail
1649
1650 operation_end(op_status);
1651 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001652}
1653
Ethan Yonker96af84a2015-01-05 14:58:36 -06001654int GUIAction::flashimage(std::string arg)
1655{
1656 int op_status = 0;
1657
1658 operation_start("Flash Image");
1659 string path, filename, full_filename;
1660 DataManager::GetValue("tw_zip_location", path);
1661 DataManager::GetValue("tw_file", filename);
1662 full_filename = path + "/" + filename;
1663 if (PartitionManager.Flash_Image(full_filename))
1664 op_status = 0; // success
1665 else
1666 op_status = 1; // fail
1667
1668 operation_end(op_status);
1669 return 0;
1670}
1671
Dees_Troy51a0e822012-09-05 15:24:24 -04001672int GUIAction::getKeyByName(std::string key)
1673{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001674 if (key == "home") return KEY_HOME;
1675 else if (key == "menu") return KEY_MENU;
1676 else if (key == "back") return KEY_BACK;
1677 else if (key == "search") return KEY_SEARCH;
1678 else if (key == "voldown") return KEY_VOLUMEDOWN;
1679 else if (key == "volup") return KEY_VOLUMEUP;
1680 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001681 int ret_val;
1682 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1683 if (!ret_val)
1684 return KEY_POWER;
1685 else
1686 return ret_val;
1687 }
1688
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001689 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001690}