blob: d4d944dfb080f7c4322a88ec905607333406dea5 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 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>
Dees_Troy51a0e822012-09-05 15:24:24 -040035
36#include <string>
37#include <sstream>
38#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040040#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070043#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050044#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#endif
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"
53
Dees_Troy32c8eb82012-09-11 15:28:06 -040054int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040055void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040056int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040057int gui_start();
58};
59
60#include "rapidxml.hpp"
61#include "objects.hpp"
62
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070063#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070065#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040066
Dees_Troy51a0e822012-09-05 15:24:24 -040067void curtainClose(void);
68
69GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010070 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 xml_node<>* child;
73 xml_node<>* actions;
74 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 // First, get the action
79 actions = node->first_node("actions");
80 if (actions) child = actions->first_node("action");
81 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 while (child)
86 {
87 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040088
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 attr = child->first_attribute("function");
90 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050091
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 action.mFunction = attr->value();
93 action.mArg = child->value();
94 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 child = child->next_sibling("action");
97 }
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 // Now, let's get either the key or region
100 child = node->first_node("touch");
101 if (child)
102 {
103 attr = child->first_attribute("key");
104 if (attr)
105 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100106 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
107 for(size_t i = 0; i < keys.size(); ++i)
108 {
109 const int key = getKeyByName(keys[i]);
110 mKeys[key] = false;
111 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 }
113 else
114 {
115 attr = child->first_attribute("x");
116 if (!attr) return;
117 mActionX = atol(attr->value());
118 attr = child->first_attribute("y");
119 if (!attr) return;
120 mActionY = atol(attr->value());
121 attr = child->first_attribute("w");
122 if (!attr) return;
123 mActionW = atol(attr->value());
124 attr = child->first_attribute("h");
125 if (!attr) return;
126 mActionH = atol(attr->value());
127 }
128 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400129}
130
131int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
132{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 if (state == TOUCH_RELEASE)
134 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100139int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400140{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100141 if (mKeys.empty())
142 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100144 std::map<int, bool>::iterator itr = mKeys.find(key);
145 if(itr == mKeys.end())
146 return 0;
147
148 bool prevState = itr->second;
149 itr->second = down;
150
151 // If there is only one key for this action, wait for key up so it
152 // doesn't trigger with multi-key actions.
153 // Else, check if all buttons are pressed, then consume their release events
154 // so they don't trigger one-button actions and reset mKeys pressed status
155 if(mKeys.size() == 1) {
156 if(!down && prevState)
157 doActions();
158 } else if(down) {
159 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
160 if(!itr->second)
161 return 0;
162 }
163
164 // Passed, all req buttons are pressed, reset them and consume release events
165 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
166 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
167 kb->ConsumeKeyRelease(itr->first);
168 itr->second = false;
169 }
170
171 doActions();
172 }
173
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 return 0;
175}
176
Vojtech Bocek07220562014-02-08 02:05:33 +0100177int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400178{
Vojtech Bocek07220562014-02-08 02:05:33 +0100179 GUIObject::NotifyVarChange(varName, value);
180
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100181 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100183 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187}
188
189void GUIAction::simulate_progress_bar(void)
190{
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 for (int i = 0; i < 5; i++)
193 {
194 usleep(500000);
195 DataManager::SetValue("ui_progress", i * 20);
196 }
197}
198
Dees_Troy657c3092012-09-10 20:32:10 -0400199int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400200{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
203 DataManager::SetValue("ui_progress", 0);
204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 if (filename.empty())
206 {
207 LOGERR("No file specified.\n");
208 return -1;
209 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211 // We're going to jump to this page first, like a loading page
212 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 int fd = -1;
215 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400218 return -1;
219
220 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 {
222 LOGERR("Unable to open zip file.\n");
223 return -1;
224 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 if (twrp != NULL)
229 {
230 unlink("/tmp/twrp.zip");
231 fd = creat("/tmp/twrp.zip", 0666);
232 }
233 if (fd >= 0 && twrp != NULL &&
234 mzExtractZipEntryToFile(&zip, twrp, fd) &&
235 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
236 {
237 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400238 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 gui_changePage("main");
240 }
241 else
242 {
243 // In this case, we just use the default page
244 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400245 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 }
247 if (fd >= 0)
248 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
250 if (simulate) {
251 simulate_progress_bar();
252 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400253 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
255 // Now, check if we need to ensure TWRP remains installed...
256 struct stat st;
257 if (stat("/sbin/installTwrp", &st) == 0)
258 {
259 DataManager::SetValue("tw_operation", "Configuring TWRP");
260 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000261 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200262 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400263 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400265 }
266 }
267 }
268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 // Done
270 DataManager::SetValue("ui_progress", 100);
271 DataManager::SetValue("ui_progress", 0);
272 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273}
274
275int GUIAction::doActions()
276{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 if (mActions.size() < 1) return -1;
278 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400279 return doAction(mActions.at(0), 0);
280
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 // For multi-action, we always use a thread
282 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 pthread_attr_t tattr;
284
285 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000287 return -1;
288 }
289 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000291 return -1;
292 }
293 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000295 return -1;
296 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500297 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000298 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000299 return -1;
300 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500301 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000302 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000305 thread_start(this);
306 } else {
307 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000308 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000309 }
310 }
311 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000312 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000313 return -1;
314 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400315
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319void* GUIAction::thread_start(void *cookie)
320{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400322
323 DataManager::SetValue(TW_ACTION_BUSY, 1);
324
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 if (ourThis->mActions.size() > 1)
326 {
327 std::vector<Action>::iterator iter;
328 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
329 ourThis->doAction(*iter, 1);
330 }
331 else
332 {
333 ourThis->doAction(ourThis->mActions.at(0), 1);
334 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 int check = 0;
336 DataManager::GetValue("tw_background_thread_running", check);
337 if (check == 0)
338 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200339 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342void GUIAction::operation_start(const string operation_name)
343{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000344 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 DataManager::SetValue(TW_ACTION_BUSY, 1);
346 DataManager::SetValue("ui_progress", 0);
347 DataManager::SetValue("tw_operation", operation_name);
348 DataManager::SetValue("tw_operation_status", 0);
349 DataManager::SetValue("tw_operation_state", 0);
350}
351
352void GUIAction::operation_end(const int operation_status, const int simulate)
353{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000354 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 DataManager::SetValue("ui_progress", 100);
357 if (simulate) {
358 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
359 if (simulate_fail != 0)
360 DataManager::SetValue("tw_operation_status", 1);
361 else
362 DataManager::SetValue("tw_operation_status", 0);
363 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500364 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400365 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500366 }
367 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 }
371 DataManager::SetValue("tw_operation_state", 1);
372 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700373#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500374 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700375#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000376 time(&Stop);
377 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600378 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400379}
380
381int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
382{
383 static string zip_queue[10];
384 static int zip_queue_index;
385 static pthread_t terminal_command;
386 int simulate;
387
388 std::string arg = gui_parse_text(action.mArg);
389
390 std::string function = gui_parse_text(action.mFunction);
391
392 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
393
Dees_Troy6ef66352013-02-21 08:26:57 -0600394 if (function == "reboot")
395 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200396 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
Dees_Troy6ef66352013-02-21 08:26:57 -0600398 sync();
399 DataManager::SetValue("tw_gui_done", 1);
400 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400401
Dees_Troy6ef66352013-02-21 08:26:57 -0600402 return 0;
403 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 if (function == "home")
405 {
406 PageManager::SelectPackage("TWRP");
407 gui_changePage("main");
408 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 }
410
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 if (function == "key")
412 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100413 const int key = getKeyByName(arg);
414 PageManager::NotifyKey(key, true);
415 PageManager::NotifyKey(key, false);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 return 0;
417 }
418
419 if (function == "page") {
420 std::string page_name = gui_parse_text(arg);
421 return gui_changePage(page_name);
422 }
423
424 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 int check = 0, ret_val = 0;
426 std::string theme_path;
427
428 operation_start("Reload Theme");
429 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 check = 1;
433 }
434
435 theme_path += "/TWRP/theme/ui.zip";
436 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
437 {
438 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000439 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
441 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 ret_val = 1;
444 }
445 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200446 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000447 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 }
449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200450 if (function == "readBackup")
451 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 string Restore_Name;
453 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200455 return 0;
456 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 if (function == "set")
459 {
460 if (arg.find('=') != string::npos)
461 {
462 string varName = arg.substr(0, arg.find('='));
463 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 DataManager::GetValue(value, value);
466 DataManager::SetValue(varName, value);
467 }
468 else
469 DataManager::SetValue(arg, "1");
470 return 0;
471 }
472 if (function == "clear")
473 {
474 DataManager::SetValue(arg, "0");
475 return 0;
476 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400477
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600478 if (function == "mount") {
479 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400482 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000484 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600485 } else if (!simulate) {
486 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200489 return 0;
490 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400491
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600492 if (function == "umount" || function == "unmount") {
493 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200494 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400495 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000497 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600499 } else if (!simulate) {
500 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200501 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000502 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200503 return 0;
504 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500505
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 if (function == "restoredefaultsettings")
507 {
508 operation_start("Restore Defaults");
509 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 else {
512 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400513 PartitionManager.Update_System_Details();
514 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 }
516 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000517 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500519
Dees_Troy51a0e822012-09-05 15:24:24 -0400520 if (function == "copylog")
521 {
522 operation_start("Copy Log");
523 if (!simulate)
524 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400526 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500527 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
528 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 } else
532 simulate_progress_bar();
533 operation_end(0, simulate);
534 return 0;
535 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500536
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 if (function == "compute" || function == "addsubtract")
538 {
539 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 {
541 string varName = arg.substr(0, arg.find('+'));
542 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400543 int amount_to_add = atoi(string_to_add.c_str());
544 int value;
545
546 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200547 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400548 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200549 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200551 {
552 string varName = arg.substr(0, arg.find('-'));
553 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400554 int amount_to_subtract = atoi(string_to_subtract.c_str());
555 int value;
556
557 DataManager::GetValue(varName, value);
558 value -= amount_to_subtract;
559 if (value <= 0)
560 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200561 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200563 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200564 if (arg.find("*") != string::npos)
565 {
566 string varName = arg.substr(0, arg.find('*'));
567 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
568 int multiply_by = atoi(multiply_by_str.c_str());
569 int value;
570
571 DataManager::GetValue(varName, value);
572 DataManager::SetValue(varName, value*multiply_by);
573 return 0;
574 }
575 if (arg.find("/") != string::npos)
576 {
577 string varName = arg.substr(0, arg.find('/'));
578 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
579 int divide_by = atoi(divide_by_str.c_str());
580 int value;
581
582 if(divide_by != 0)
583 {
584 DataManager::GetValue(varName, value);
585 DataManager::SetValue(varName, value/divide_by);
586 }
587 return 0;
588 }
589 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
590 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400591 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500592
Dees_Troy51a0e822012-09-05 15:24:24 -0400593 if (function == "setguitimezone")
594 {
595 string SelectedZone;
596 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
597 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
598 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500599
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 int dst;
601 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500602
Dees_Troy51a0e822012-09-05 15:24:24 -0400603 string offset;
604 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500605
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 string NewTimeZone = Zone;
607 if (offset != "0")
608 NewTimeZone += ":" + offset;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500609
Dees_Troy51a0e822012-09-05 15:24:24 -0400610 if (dst != 0)
611 NewTimeZone += DSTZone;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500612
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400614 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400615 return 0;
616 }
617
618 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000619 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 return 0;
621 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500622
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200624 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400625
626 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200627 {
628 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000629 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 return 0;
631 }
632 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
633 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
634 zip_queue_index++;
635 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
636 }
637 return 0;
638 }
639
640 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200641 {
642 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000643 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 return 0;
645 } else {
646 zip_queue_index--;
647 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
648 }
649 return 0;
650 }
651
652 if (function == "queueclear")
653 {
654 zip_queue_index = 0;
655 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
656 return 0;
657 }
658
659 if (function == "sleep")
660 {
661 operation_start("Sleep");
662 usleep(atoi(arg.c_str()));
663 operation_end(0, simulate);
664 return 0;
665 }
666
Dees Troyb21cc642013-09-10 17:36:41 +0000667 if (function == "appenddatetobackupname")
668 {
669 operation_start("AppendDateToBackupName");
670 string Backup_Name;
671 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
672 Backup_Name += TWFunc::Get_Current_Date();
673 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
674 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
675 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
676 operation_end(0, simulate);
677 return 0;
678 }
679
680 if (function == "generatebackupname")
681 {
682 operation_start("GenerateBackupName");
683 TWFunc::Auto_Generate_Backup_Name();
684 operation_end(0, simulate);
685 return 0;
686 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500687 if (function == "checkpartitionlist") {
688 string Wipe_List, wipe_path;
689 int count = 0;
690
691 DataManager::GetValue("tw_wipe_list", Wipe_List);
692 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
693 if (!Wipe_List.empty()) {
694 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
695 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
696 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
697 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
698 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
699 // Do nothing
700 } else {
701 count++;
702 }
703 start_pos = end_pos + 1;
704 end_pos = Wipe_List.find(";", start_pos);
705 }
706 DataManager::SetValue("tw_check_partition_list", count);
707 } else {
708 DataManager::SetValue("tw_check_partition_list", 0);
709 }
710 return 0;
711 }
712 if (function == "getpartitiondetails") {
713 string Wipe_List, wipe_path;
714 int count = 0;
715
716 DataManager::GetValue("tw_wipe_list", Wipe_List);
717 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
718 if (!Wipe_List.empty()) {
719 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
720 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
721 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
722 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
723 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
724 // Do nothing
725 } else {
726 DataManager::SetValue("tw_partition_path", wipe_path);
727 break;
728 }
729 start_pos = end_pos + 1;
730 end_pos = Wipe_List.find(";", start_pos);
731 }
732 if (!wipe_path.empty()) {
733 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
734 if (Part) {
735 unsigned long long mb = 1048576;
736
737 DataManager::SetValue("tw_partition_name", Part->Display_Name);
738 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
739 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
740 DataManager::SetValue("tw_partition_size", Part->Size / mb);
741 DataManager::SetValue("tw_partition_used", Part->Used / mb);
742 DataManager::SetValue("tw_partition_free", Part->Free / mb);
743 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
744 DataManager::SetValue("tw_partition_removable", Part->Removable);
745 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
746
747 if (Part->Can_Repair())
748 DataManager::SetValue("tw_partition_can_repair", 1);
749 else
750 DataManager::SetValue("tw_partition_can_repair", 0);
751 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
752 DataManager::SetValue("tw_partition_vfat", 1);
753 else
754 DataManager::SetValue("tw_partition_vfat", 0);
755 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
756 DataManager::SetValue("tw_partition_exfat", 1);
757 else
758 DataManager::SetValue("tw_partition_exfat", 0);
759 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
760 DataManager::SetValue("tw_partition_f2fs", 1);
761 else
762 DataManager::SetValue("tw_partition_f2fs", 0);
763 if (TWFunc::Path_Exists("/sbin/mke2fs"))
764 DataManager::SetValue("tw_partition_ext", 1);
765 else
766 DataManager::SetValue("tw_partition_ext", 0);
767 return 0;
768 } else {
769 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
770 }
771 }
772 }
773 DataManager::SetValue("tw_partition_name", "");
774 DataManager::SetValue("tw_partition_file_system", "");
775 return 0;
776 }
Dees Troyb21cc642013-09-10 17:36:41 +0000777
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778 if (isThreaded)
779 {
780 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400781 {
782 struct stat st;
783 string newpath = arg + "/.";
784
785 operation_start("FileExists");
786 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
787 operation_end(0, simulate);
788 else
789 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000790 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 }
792
793 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200794 {
Dees_Troy657c3092012-09-10 20:32:10 -0400795 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400796
797 for (i=0; i<zip_queue_index; i++) {
798 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200799 DataManager::SetValue("tw_filename", zip_queue[i]);
800 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400801
Dees_Troy657c3092012-09-10 20:32:10 -0400802 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400803 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 i = 10; // Error flashing zip - exit queue
806 ret_val = 1;
807 }
808 }
809 zip_queue_index = 0;
810 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
811
Dees_Troy657c3092012-09-10 20:32:10 -0400812 if (wipe_cache)
813 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200814
Dees_Troy51a0e822012-09-05 15:24:24 -0400815 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
816 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000817 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400818 if (simulate) {
819 simulate_progress_bar();
820 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400821 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
822 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200823 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400824 else {
825 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200826 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400827 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000828 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400829 }
830 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400831 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200833 return 0;
834 }
835 if (function == "wipe")
836 {
837 operation_start("Format");
838 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400839
Dees_Troy38bd7602012-09-14 13:33:53 -0400840 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400841
842 if (simulate) {
843 simulate_progress_bar();
844 } else {
845 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400846 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400847 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400848 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400849 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400850 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400851 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400852 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400854 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400855 } else if (arg == "INTERNAL") {
856 int has_datamedia, dual_storage;
857
858 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
859 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400860 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400861 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400862 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400863 }
864 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400865 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400866
Dees_Troy38bd7602012-09-14 13:33:53 -0400867 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
868 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400869 } else if (arg == "ANDROIDSECURE") {
870 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500871 } else if (arg == "LIST") {
872 string Wipe_List, wipe_path;
873 bool skip = false;
874 ret_val = true;
875 TWPartition* wipe_part = NULL;
876
877 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000878 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 if (!Wipe_List.empty()) {
880 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
881 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
882 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500884 if (wipe_path == "/and-sec") {
885 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000886 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500887 ret_val = false;
888 break;
889 } else {
890 skip = true;
891 }
892 } else if (wipe_path == "DALVIK") {
893 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500895 ret_val = false;
896 break;
897 } else {
898 skip = true;
899 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000900 } else if (wipe_path == "INTERNAL") {
901 if (!PartitionManager.Wipe_Media_From_Data()) {
902 ret_val = false;
903 break;
904 } else {
905 skip = true;
906 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500907 }
908 if (!skip) {
909 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000910 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 ret_val = false;
912 break;
913 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
914 arg = wipe_path;
915 }
916 } else {
917 skip = false;
918 }
919 start_pos = end_pos + 1;
920 end_pos = Wipe_List.find(";", start_pos);
921 }
922 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400923 } else
924 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500925#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400926 if (arg == DataManager::GetSettingsStoragePath()) {
927 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
928 string Storage_Path = DataManager::GetSettingsStoragePath();
929
930 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000931 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400932 Storage_Path += "/TWRP";
933 mkdir(Storage_Path.c_str(), 0777);
934 DataManager::Flush();
935 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000936 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400937 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400938 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500939#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400940 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400941 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400942 if (ret_val)
943 ret_val = 0; // 0 is success
944 else
945 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200946 operation_end(ret_val, simulate);
947 return 0;
948 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400949 if (function == "refreshsizes")
950 {
951 operation_start("Refreshing Sizes");
952 if (simulate) {
953 simulate_progress_bar();
954 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400956 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000957 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400958 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200959 if (function == "nandroid")
960 {
961 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400962 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400963
964 if (simulate) {
965 DataManager::SetValue("tw_partition", "Simulation");
966 simulate_progress_bar();
967 } else {
968 if (arg == "backup") {
969 string Backup_Name;
970 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000971 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400972 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500973 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400974 else {
975 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400976 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000977
Dees_Troy43d8b002012-09-17 16:00:01 -0400978 }
Dees Troyb21cc642013-09-10 17:36:41 +0000979 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -0400980 } else if (arg == "restore") {
981 string Restore_Name;
982 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400983 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400984 } else {
985 operation_end(1, simulate);
986 return -1;
987 }
988 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000989 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400990 if (ret == false)
991 ret = 1; // 1 for failure
992 else
993 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200994 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000995 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200996 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400997 if (function == "fixpermissions")
998 {
999 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001000 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001001 if (simulate) {
1002 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -04001003 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -04001004 int op_status = PartitionManager.Fix_Permissions();
1005 if (op_status != 0)
1006 op_status = 1; // failure
1007 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -04001008 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001009 return 0;
1010 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001011 if (function == "dd")
1012 {
1013 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -04001014
1015 if (simulate) {
1016 simulate_progress_bar();
1017 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001018 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +02001019 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -04001020 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001021 operation_end(0, simulate);
1022 return 0;
1023 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001024 if (function == "partitionsd")
1025 {
1026 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001027 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001028
1029 if (simulate) {
1030 simulate_progress_bar();
1031 } else {
1032 int allow_partition;
1033 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1034 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001036 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001037 if (!PartitionManager.Partition_SDCard())
1038 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -04001039 }
1040 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001041 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001042 return 0;
1043 }
1044 if (function == "installhtcdumlock")
1045 {
1046 operation_start("Install HTC Dumlock");
1047 if (simulate) {
1048 simulate_progress_bar();
1049 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001051
1052 operation_end(0, simulate);
1053 return 0;
1054 }
1055 if (function == "htcdumlockrestoreboot")
1056 {
1057 operation_start("HTC Dumlock Restore Boot");
1058 if (simulate) {
1059 simulate_progress_bar();
1060 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001062
1063 operation_end(0, simulate);
1064 return 0;
1065 }
1066 if (function == "htcdumlockreflashrecovery")
1067 {
1068 operation_start("HTC Dumlock Reflash Recovery");
1069 if (simulate) {
1070 simulate_progress_bar();
1071 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001072 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001073
1074 operation_end(0, simulate);
1075 return 0;
1076 }
1077 if (function == "cmd")
1078 {
1079 int op_status = 0;
1080
1081 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001083 if (simulate) {
1084 simulate_progress_bar();
1085 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001086 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -04001087 if (op_status != 0)
1088 op_status = 1;
1089 }
1090
1091 operation_end(op_status, simulate);
1092 return 0;
1093 }
1094 if (function == "terminalcommand")
1095 {
1096 int op_status = 0;
1097 string cmdpath, command;
1098
1099 DataManager::GetValue("tw_terminal_location", cmdpath);
1100 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001102 if (simulate) {
1103 simulate_progress_bar();
1104 operation_end(op_status, simulate);
1105 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001106 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001108 DataManager::SetValue("tw_terminal_command_thread", command);
1109 DataManager::SetValue("tw_terminal_state", 1);
1110 DataManager::SetValue("tw_background_thread_running", 1);
1111 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1112 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001114 DataManager::SetValue("tw_terminal_state", 0);
1115 DataManager::SetValue("tw_background_thread_running", 0);
1116 operation_end(1, simulate);
1117 }
1118 }
1119 return 0;
1120 }
1121 if (function == "killterminal")
1122 {
1123 int op_status = 0;
1124
Dees_Troy2673cec2013-04-02 20:22:16 +00001125 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001126 operation_start("KillCommand");
1127 DataManager::SetValue("tw_operation_status", 0);
1128 DataManager::SetValue("tw_operation_state", 1);
1129 DataManager::SetValue("tw_terminal_state", 0);
1130 DataManager::SetValue("tw_background_thread_running", 0);
1131 DataManager::SetValue(TW_ACTION_BUSY, 0);
1132 return 0;
1133 }
1134 if (function == "reinjecttwrp")
1135 {
1136 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001137 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001138 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001139 if (simulate) {
1140 simulate_progress_bar();
1141 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001142 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001144 }
1145
1146 operation_end(op_status, simulate);
1147 return 0;
1148 }
1149 if (function == "checkbackupname")
1150 {
1151 int op_status = 0;
1152
1153 operation_start("CheckBackupName");
1154 if (simulate) {
1155 simulate_progress_bar();
1156 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001157 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001158 if (op_status != 0)
1159 op_status = 1;
1160 }
1161
1162 operation_end(op_status, simulate);
1163 return 0;
1164 }
1165 if (function == "decrypt")
1166 {
1167 int op_status = 0;
1168
1169 operation_start("Decrypt");
1170 if (simulate) {
1171 simulate_progress_bar();
1172 } else {
1173 string Password;
1174 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001175 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001176 if (op_status != 0)
1177 op_status = 1;
1178 else {
1179 int load_theme = 1;
1180
1181 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001182
1183 if (load_theme) {
1184 int has_datamedia;
1185
1186 // Check for a custom theme and load it if exists
1187 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1188 if (has_datamedia != 0) {
1189 struct stat st;
1190 int check = 0;
1191 std::string theme_path;
1192
1193 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001194 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001196 check = 1;
1197 }
1198
1199 theme_path += "/TWRP/theme/ui.zip";
1200 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1201 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1202 {
1203 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001205 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1206 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001208 }
1209 }
1210 }
1211 }
1212 }
1213 }
1214 }
1215
1216 operation_end(op_status, simulate);
1217 return 0;
1218 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001219 if (function == "adbsideload")
1220 {
1221 int ret = 0;
1222
1223 operation_start("Sideload");
1224 if (simulate) {
1225 simulate_progress_bar();
1226 } else {
1227 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001228 int wipe_dalvik = 0;
Vojtech Bocek05534202013-09-11 08:11:56 +02001229 string Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001230
Ethan Yonker45312e52014-02-26 09:23:53 -06001231 if (!PartitionManager.Mount_Current_Storage(false)) {
1232 gui_print("Using RAM for sideload storage.\n");
1233 Sideload_File = "/tmp/sideload.zip";
1234 } else {
1235 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
Dees_Troycfb63ae2012-09-19 14:30:17 -04001236 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001237 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001238 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001239 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001240 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001241 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001242 ret = apply_from_adb(Sideload_File.c_str());
1243 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001244 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001245 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001246 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001247 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1248 PartitionManager.Wipe_By_Path("/cache");
1249 if (wipe_dalvik)
1250 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 } else {
1252 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001253 }
Dees Troy8c0f06b2013-11-26 21:21:29 +00001254 PartitionManager.Update_System_Details();
Dees_Troy06b4fe92012-10-16 11:43:20 -04001255 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1256 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001258 if (simulate) {
1259 simulate_progress_bar();
1260 } else {
1261 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1262 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001263 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001264 else {
1265 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001266 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001267 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001269 }
1270 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001271 }
1272 operation_end(ret, simulate);
1273 return 0;
1274 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001275 if (function == "adbsideloadcancel")
1276 {
1277 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001279 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001280 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001281 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 property_get("tw_child_pid", child_prop, "error");
1283 if (strcmp(child_prop, "error") == 0) {
1284 LOGERR("Unable to get child ID from prop\n");
1285 return 0;
1286 }
1287 child_pid = atoi(child_prop);
1288 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001289 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001290 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001291 return 0;
1292 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001293 if (function == "openrecoveryscript") {
1294 operation_start("OpenRecoveryScript");
1295 if (simulate) {
1296 simulate_progress_bar();
1297 } else {
1298 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1299 // that we converted to ORS commands during boot in recovery.cpp.
1300 // Run those first.
1301 int reboot = 0;
1302 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001303 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001304 if (OpenRecoveryScript::run_script_file() == 0) {
1305 reboot = 1;
1306 }
1307 }
1308 // Check for the ORS file in /cache and attempt to run those commands.
1309 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001311 if (OpenRecoveryScript::run_script_file() == 0) {
1312 reboot = 1;
1313 }
1314 }
1315 if (reboot) {
1316 usleep(2000000); // Sleep for 2 seconds before rebooting
1317 TWFunc::tw_reboot(rb_system);
1318 } else {
1319 DataManager::SetValue("tw_page_done", 1);
1320 }
1321 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001322 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001323 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001324 if (function == "installsu")
1325 {
1326 int op_status = 0;
1327
1328 operation_start("Install SuperSU");
1329 if (simulate) {
1330 simulate_progress_bar();
1331 } else {
1332 if (!TWFunc::Install_SuperSU())
1333 op_status = 1;
1334 }
1335
1336 operation_end(op_status, simulate);
1337 return 0;
1338 }
1339 if (function == "fixsu")
1340 {
1341 int op_status = 0;
1342
1343 operation_start("Fixing Superuser Permissions");
1344 if (simulate) {
1345 simulate_progress_bar();
1346 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001347 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1348 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001349 }
1350
1351 operation_end(op_status, simulate);
1352 return 0;
1353 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001354 if (function == "decrypt_backup")
1355 {
1356 int op_status = 0;
1357
1358 operation_start("Try Restore Decrypt");
1359 if (simulate) {
1360 simulate_progress_bar();
1361 } else {
1362 string Restore_Path, Filename, Password;
1363 DataManager::GetValue("tw_restore", Restore_Path);
1364 Restore_Path += "/";
1365 DataManager::GetValue("tw_restore_password", Password);
1366 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1367 op_status = 0; // success
1368 else
1369 op_status = 1; // fail
1370 }
1371
1372 operation_end(op_status, simulate);
1373 return 0;
1374 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001375 if (function == "repair")
1376 {
1377 int op_status = 0;
1378
1379 operation_start("Repair Partition");
1380 if (simulate) {
1381 simulate_progress_bar();
1382 } else {
1383 string part_path;
1384 DataManager::GetValue("tw_partition_mount_point", part_path);
1385 if (PartitionManager.Repair_By_Path(part_path, true)) {
1386 op_status = 0; // success
1387 } else {
1388 LOGERR("Error repairing file system.\n");
1389 op_status = 1; // fail
1390 }
1391 }
1392
1393 operation_end(op_status, simulate);
1394 return 0;
1395 }
1396 if (function == "changefilesystem")
1397 {
1398 int op_status = 0;
1399
1400 operation_start("Change File System");
1401 if (simulate) {
1402 simulate_progress_bar();
1403 } else {
1404 string part_path, file_system;
1405 DataManager::GetValue("tw_partition_mount_point", part_path);
1406 DataManager::GetValue("tw_action_new_file_system", file_system);
1407 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1408 op_status = 0; // success
1409 } else {
1410 LOGERR("Error changing file system.\n");
1411 op_status = 1; // fail
1412 }
1413 }
1414
1415 operation_end(op_status, simulate);
1416 return 0;
1417 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001418 }
1419 else
1420 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001421 pthread_t t;
1422 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001423 return 0;
1424 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001425 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001426 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001427}
1428
1429int GUIAction::getKeyByName(std::string key)
1430{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001431 if (key == "home") return KEY_HOME;
1432 else if (key == "menu") return KEY_MENU;
1433 else if (key == "back") return KEY_BACK;
1434 else if (key == "search") return KEY_SEARCH;
1435 else if (key == "voldown") return KEY_VOLUMEDOWN;
1436 else if (key == "volup") return KEY_VOLUMEUP;
1437 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001438 int ret_val;
1439 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1440 if (!ret_val)
1441 return KEY_POWER;
1442 else
1443 return ret_val;
1444 }
1445
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001446 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001447}
1448
1449void* GUIAction::command_thread(void *cookie)
1450{
1451 string command;
1452 FILE* fp;
1453 char line[512];
1454
1455 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001456 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001457 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001459 } else {
1460 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1461 struct timeval timeout;
1462 fd_set fdset;
1463
1464 while(keep_going)
1465 {
1466 FD_ZERO(&fdset);
1467 FD_SET(fd, &fdset);
1468 timeout.tv_sec = 0;
1469 timeout.tv_usec = 400000;
1470 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1471 if (has_data == 0) {
1472 // Timeout reached
1473 DataManager::GetValue("tw_terminal_state", check);
1474 if (check == 0) {
1475 keep_going = 0;
1476 }
1477 } else if (has_data < 0) {
1478 // End of execution
1479 keep_going = 0;
1480 } else {
1481 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001482 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001483 bytes_read = read(fd, line, sizeof(line));
1484 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001486 else
1487 keep_going = 0; // Done executing
1488 }
1489 }
1490 fclose(fp);
1491 }
1492 DataManager::SetValue("tw_operation_status", 0);
1493 DataManager::SetValue("tw_operation_state", 1);
1494 DataManager::SetValue("tw_terminal_state", 0);
1495 DataManager::SetValue("tw_background_thread_running", 0);
1496 DataManager::SetValue(TW_ACTION_BUSY, 0);
1497 return NULL;
1498}