blob: 7e432226eaccc693659020a487d67abe9b977a67 [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"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070044#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070046#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040051#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040053#include "../minadbd/adb.h"
54
Dees_Troy32c8eb82012-09-11 15:28:06 -040055int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040056void 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 -040057int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040058int gui_start();
59};
60
61#include "rapidxml.hpp"
62#include "objects.hpp"
63
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070064#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050065extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070066#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040067
Dees_Troy51a0e822012-09-05 15:24:24 -040068void curtainClose(void);
69
70GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010071 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040072{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 xml_node<>* child;
74 xml_node<>* actions;
75 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 // First, get the action
80 actions = node->first_node("actions");
81 if (actions) child = actions->first_node("action");
82 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040085
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 while (child)
87 {
88 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 attr = child->first_attribute("function");
91 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050092
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020093 action.mFunction = attr->value();
94 action.mArg = child->value();
95 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 child = child->next_sibling("action");
98 }
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 // Now, let's get either the key or region
101 child = node->first_node("touch");
102 if (child)
103 {
104 attr = child->first_attribute("key");
105 if (attr)
106 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100107 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
108 for(size_t i = 0; i < keys.size(); ++i)
109 {
110 const int key = getKeyByName(keys[i]);
111 mKeys[key] = false;
112 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 }
114 else
115 {
116 attr = child->first_attribute("x");
117 if (!attr) return;
118 mActionX = atol(attr->value());
119 attr = child->first_attribute("y");
120 if (!attr) return;
121 mActionY = atol(attr->value());
122 attr = child->first_attribute("w");
123 if (!attr) return;
124 mActionW = atol(attr->value());
125 attr = child->first_attribute("h");
126 if (!attr) return;
127 mActionH = atol(attr->value());
128 }
129 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400130}
131
132int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
133{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 if (state == TOUCH_RELEASE)
135 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400138}
139
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100140int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400141{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100142 if (mKeys.empty())
143 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100145 std::map<int, bool>::iterator itr = mKeys.find(key);
146 if(itr == mKeys.end())
147 return 0;
148
149 bool prevState = itr->second;
150 itr->second = down;
151
152 // If there is only one key for this action, wait for key up so it
153 // doesn't trigger with multi-key actions.
154 // Else, check if all buttons are pressed, then consume their release events
155 // so they don't trigger one-button actions and reset mKeys pressed status
156 if(mKeys.size() == 1) {
157 if(!down && prevState)
158 doActions();
159 } else if(down) {
160 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
161 if(!itr->second)
162 return 0;
163 }
164
165 // Passed, all req buttons are pressed, reset them and consume release events
166 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
167 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
168 kb->ConsumeKeyRelease(itr->first);
169 itr->second = false;
170 }
171
172 doActions();
173 }
174
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 return 0;
176}
177
Vojtech Bocek07220562014-02-08 02:05:33 +0100178int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400179{
Vojtech Bocek07220562014-02-08 02:05:33 +0100180 GUIObject::NotifyVarChange(varName, value);
181
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100182 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100184 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400188}
189
190void GUIAction::simulate_progress_bar(void)
191{
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 for (int i = 0; i < 5; i++)
194 {
195 usleep(500000);
196 DataManager::SetValue("ui_progress", i * 20);
197 }
198}
199
Dees_Troy657c3092012-09-10 20:32:10 -0400200int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400201{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200202 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400203
204 DataManager::SetValue("ui_progress", 0);
205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 if (filename.empty())
207 {
208 LOGERR("No file specified.\n");
209 return -1;
210 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 // We're going to jump to this page first, like a loading page
213 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400214
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 int fd = -1;
216 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400219 return -1;
220
221 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200222 {
223 LOGERR("Unable to open zip file.\n");
224 return -1;
225 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 if (twrp != NULL)
230 {
231 unlink("/tmp/twrp.zip");
232 fd = creat("/tmp/twrp.zip", 0666);
233 }
234 if (fd >= 0 && twrp != NULL &&
235 mzExtractZipEntryToFile(&zip, twrp, fd) &&
236 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
237 {
238 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400239 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 gui_changePage("main");
241 }
242 else
243 {
244 // In this case, we just use the default page
245 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400246 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 }
248 if (fd >= 0)
249 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
251 if (simulate) {
252 simulate_progress_bar();
253 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400254 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
256 // Now, check if we need to ensure TWRP remains installed...
257 struct stat st;
258 if (stat("/sbin/installTwrp", &st) == 0)
259 {
260 DataManager::SetValue("tw_operation", "Configuring TWRP");
261 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000262 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200263 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400264 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400266 }
267 }
268 }
269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 // Done
271 DataManager::SetValue("ui_progress", 100);
272 DataManager::SetValue("ui_progress", 0);
273 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400274}
275
276int GUIAction::doActions()
277{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200278 if (mActions.size() < 1) return -1;
279 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400280 return doAction(mActions.at(0), 0);
281
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 // For multi-action, we always use a thread
283 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000284 pthread_attr_t tattr;
285
286 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000288 return -1;
289 }
290 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000291 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000292 return -1;
293 }
294 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000295 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000296 return -1;
297 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500298 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000299 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000300 return -1;
301 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500302 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000303 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200304 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000305 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000306 thread_start(this);
307 } else {
308 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000310 }
311 }
312 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000314 return -1;
315 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400316
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400318}
319
320void* GUIAction::thread_start(void *cookie)
321{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200322 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400323
324 DataManager::SetValue(TW_ACTION_BUSY, 1);
325
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200326 if (ourThis->mActions.size() > 1)
327 {
328 std::vector<Action>::iterator iter;
329 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
330 ourThis->doAction(*iter, 1);
331 }
332 else
333 {
334 ourThis->doAction(ourThis->mActions.at(0), 1);
335 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400336 int check = 0;
337 DataManager::GetValue("tw_background_thread_running", check);
338 if (check == 0)
339 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
343void GUIAction::operation_start(const string operation_name)
344{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000345 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 DataManager::SetValue(TW_ACTION_BUSY, 1);
347 DataManager::SetValue("ui_progress", 0);
348 DataManager::SetValue("tw_operation", operation_name);
349 DataManager::SetValue("tw_operation_status", 0);
350 DataManager::SetValue("tw_operation_state", 0);
351}
352
353void GUIAction::operation_end(const int operation_status, const int simulate)
354{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000355 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 DataManager::SetValue("ui_progress", 100);
358 if (simulate) {
359 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
360 if (simulate_fail != 0)
361 DataManager::SetValue("tw_operation_status", 1);
362 else
363 DataManager::SetValue("tw_operation_status", 0);
364 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500365 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400366 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500367 }
368 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400369 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500370 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400371 }
372 DataManager::SetValue("tw_operation_state", 1);
373 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700374#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500375 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700376#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000377 time(&Stop);
378 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600379 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400380}
381
382int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
383{
384 static string zip_queue[10];
385 static int zip_queue_index;
386 static pthread_t terminal_command;
387 int simulate;
388
389 std::string arg = gui_parse_text(action.mArg);
390
391 std::string function = gui_parse_text(action.mFunction);
392
393 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
394
Dees_Troy6ef66352013-02-21 08:26:57 -0600395 if (function == "reboot")
396 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400398
Dees_Troy6ef66352013-02-21 08:26:57 -0600399 sync();
400 DataManager::SetValue("tw_gui_done", 1);
401 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400402
Dees_Troy6ef66352013-02-21 08:26:57 -0600403 return 0;
404 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 if (function == "home")
406 {
407 PageManager::SelectPackage("TWRP");
408 gui_changePage("main");
409 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 }
411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 if (function == "key")
413 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100414 const int key = getKeyByName(arg);
415 PageManager::NotifyKey(key, true);
416 PageManager::NotifyKey(key, false);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 return 0;
418 }
419
420 if (function == "page") {
421 std::string page_name = gui_parse_text(arg);
422 return gui_changePage(page_name);
423 }
424
425 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 int check = 0, ret_val = 0;
427 std::string theme_path;
428
429 operation_start("Reload Theme");
430 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000432 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400433 check = 1;
434 }
435
436 theme_path += "/TWRP/theme/ui.zip";
437 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
438 {
439 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000440 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
442 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000443 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 ret_val = 1;
445 }
446 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200447 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000448 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 }
450
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200451 if (function == "readBackup")
452 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 string Restore_Name;
454 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200456 return 0;
457 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200459 if (function == "set")
460 {
461 if (arg.find('=') != string::npos)
462 {
463 string varName = arg.substr(0, arg.find('='));
464 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400465
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200466 DataManager::GetValue(value, value);
467 DataManager::SetValue(varName, value);
468 }
469 else
470 DataManager::SetValue(arg, "1");
471 return 0;
472 }
473 if (function == "clear")
474 {
475 DataManager::SetValue(arg, "0");
476 return 0;
477 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400478
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600479 if (function == "mount") {
480 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200481 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400483 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000485 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600486 } else if (!simulate) {
487 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200488 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000489 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200490 return 0;
491 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400492
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600493 if (function == "umount" || function == "unmount") {
494 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200495 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400496 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600500 } else if (!simulate) {
501 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200502 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000503 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200504 return 0;
505 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500506
Dees_Troy51a0e822012-09-05 15:24:24 -0400507 if (function == "restoredefaultsettings")
508 {
509 operation_start("Restore Defaults");
510 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 else {
513 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400514 PartitionManager.Update_System_Details();
515 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 }
517 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000518 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500520
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 if (function == "copylog")
522 {
523 operation_start("Copy Log");
524 if (!simulate)
525 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500526 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400527 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500528 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
529 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 } else
533 simulate_progress_bar();
534 operation_end(0, simulate);
535 return 0;
536 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500537
Dees_Troy51a0e822012-09-05 15:24:24 -0400538 if (function == "compute" || function == "addsubtract")
539 {
540 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200541 {
542 string varName = arg.substr(0, arg.find('+'));
543 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400544 int amount_to_add = atoi(string_to_add.c_str());
545 int value;
546
547 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200548 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400551 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200552 {
553 string varName = arg.substr(0, arg.find('-'));
554 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400555 int amount_to_subtract = atoi(string_to_subtract.c_str());
556 int value;
557
558 DataManager::GetValue(varName, value);
559 value -= amount_to_subtract;
560 if (value <= 0)
561 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200564 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200565 if (arg.find("*") != string::npos)
566 {
567 string varName = arg.substr(0, arg.find('*'));
568 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
569 int multiply_by = atoi(multiply_by_str.c_str());
570 int value;
571
572 DataManager::GetValue(varName, value);
573 DataManager::SetValue(varName, value*multiply_by);
574 return 0;
575 }
576 if (arg.find("/") != string::npos)
577 {
578 string varName = arg.substr(0, arg.find('/'));
579 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
580 int divide_by = atoi(divide_by_str.c_str());
581 int value;
582
583 if(divide_by != 0)
584 {
585 DataManager::GetValue(varName, value);
586 DataManager::SetValue(varName, value/divide_by);
587 }
588 return 0;
589 }
590 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
591 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500593
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 if (function == "setguitimezone")
595 {
596 string SelectedZone;
597 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
598 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
599 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500600
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 int dst;
602 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500603
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 string offset;
605 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500606
Dees_Troy51a0e822012-09-05 15:24:24 -0400607 string NewTimeZone = Zone;
608 if (offset != "0")
609 NewTimeZone += ":" + offset;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500610
Dees_Troy51a0e822012-09-05 15:24:24 -0400611 if (dst != 0)
612 NewTimeZone += DSTZone;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500613
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400615 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 return 0;
617 }
618
619 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000620 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 return 0;
622 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500623
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200625 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400626
627 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200628 {
629 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400631 return 0;
632 }
633 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
634 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
635 zip_queue_index++;
636 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
637 }
638 return 0;
639 }
640
641 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200642 {
643 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000644 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400645 return 0;
646 } else {
647 zip_queue_index--;
648 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
649 }
650 return 0;
651 }
652
653 if (function == "queueclear")
654 {
655 zip_queue_index = 0;
656 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
657 return 0;
658 }
659
660 if (function == "sleep")
661 {
662 operation_start("Sleep");
663 usleep(atoi(arg.c_str()));
664 operation_end(0, simulate);
665 return 0;
666 }
667
Dees Troyb21cc642013-09-10 17:36:41 +0000668 if (function == "appenddatetobackupname")
669 {
670 operation_start("AppendDateToBackupName");
671 string Backup_Name;
672 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
673 Backup_Name += TWFunc::Get_Current_Date();
674 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
675 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
676 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
677 operation_end(0, simulate);
678 return 0;
679 }
680
681 if (function == "generatebackupname")
682 {
683 operation_start("GenerateBackupName");
684 TWFunc::Auto_Generate_Backup_Name();
685 operation_end(0, simulate);
686 return 0;
687 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500688 if (function == "checkpartitionlist") {
689 string Wipe_List, wipe_path;
690 int count = 0;
691
692 DataManager::GetValue("tw_wipe_list", Wipe_List);
693 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
694 if (!Wipe_List.empty()) {
695 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
696 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
697 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
698 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
699 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
700 // Do nothing
701 } else {
702 count++;
703 }
704 start_pos = end_pos + 1;
705 end_pos = Wipe_List.find(";", start_pos);
706 }
707 DataManager::SetValue("tw_check_partition_list", count);
708 } else {
709 DataManager::SetValue("tw_check_partition_list", 0);
710 }
711 return 0;
712 }
713 if (function == "getpartitiondetails") {
714 string Wipe_List, wipe_path;
715 int count = 0;
716
717 DataManager::GetValue("tw_wipe_list", Wipe_List);
718 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
719 if (!Wipe_List.empty()) {
720 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
721 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
722 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
723 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
724 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
725 // Do nothing
726 } else {
727 DataManager::SetValue("tw_partition_path", wipe_path);
728 break;
729 }
730 start_pos = end_pos + 1;
731 end_pos = Wipe_List.find(";", start_pos);
732 }
733 if (!wipe_path.empty()) {
734 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
735 if (Part) {
736 unsigned long long mb = 1048576;
737
738 DataManager::SetValue("tw_partition_name", Part->Display_Name);
739 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
740 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
741 DataManager::SetValue("tw_partition_size", Part->Size / mb);
742 DataManager::SetValue("tw_partition_used", Part->Used / mb);
743 DataManager::SetValue("tw_partition_free", Part->Free / mb);
744 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
745 DataManager::SetValue("tw_partition_removable", Part->Removable);
746 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
747
748 if (Part->Can_Repair())
749 DataManager::SetValue("tw_partition_can_repair", 1);
750 else
751 DataManager::SetValue("tw_partition_can_repair", 0);
752 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
753 DataManager::SetValue("tw_partition_vfat", 1);
754 else
755 DataManager::SetValue("tw_partition_vfat", 0);
756 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
757 DataManager::SetValue("tw_partition_exfat", 1);
758 else
759 DataManager::SetValue("tw_partition_exfat", 0);
760 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
761 DataManager::SetValue("tw_partition_f2fs", 1);
762 else
763 DataManager::SetValue("tw_partition_f2fs", 0);
764 if (TWFunc::Path_Exists("/sbin/mke2fs"))
765 DataManager::SetValue("tw_partition_ext", 1);
766 else
767 DataManager::SetValue("tw_partition_ext", 0);
768 return 0;
769 } else {
770 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
771 }
772 }
773 }
774 DataManager::SetValue("tw_partition_name", "");
775 DataManager::SetValue("tw_partition_file_system", "");
776 return 0;
777 }
Dees Troyb21cc642013-09-10 17:36:41 +0000778
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100779 if (function == "screenshot")
780 {
781 time_t tm;
782 char path[256];
783 int path_len;
784 uid_t uid = -1;
785 gid_t gid = -1;
786
787 struct passwd *pwd = getpwnam("media_rw");
788 if(pwd) {
789 uid = pwd->pw_uid;
790 gid = pwd->pw_gid;
791 }
792
793 const std::string storage = DataManager::GetCurrentStoragePath();
794 if(PartitionManager.Is_Mounted_By_Path(storage)) {
795 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
796 } else {
797 strcpy(path, "/tmp/");
798 }
799
800 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
801 return 0;
802
803 tm = time(NULL);
804 path_len = strlen(path);
805
806 // Screenshot_2014-01-01-18-21-38.png
807 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
808
809 int res = gr_save_screenshot(path);
810 if(res == 0) {
811 chmod(path, 0666);
812 chown(path, uid, gid);
813
814 gui_print("Screenshot was saved to %s\n", path);
815
816 // blink to notify that the screenshow was taken
817 gr_color(255, 255, 255, 255);
818 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
819 gr_flip();
820 gui_forceRender();
821 } else {
822 LOGERR("Failed to take a screenshot!\n");
823 }
824 return 0;
825 }
826
xNUTxe85f02d2014-07-18 01:30:58 +0200827 if (function == "setbrightness")
828 {
829 return TWFunc::Set_Brightness(arg);
830 }
831
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200832 if (isThreaded)
833 {
834 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400835 {
836 struct stat st;
837 string newpath = arg + "/.";
838
839 operation_start("FileExists");
840 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
841 operation_end(0, simulate);
842 else
843 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000844 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400845 }
846
847 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200848 {
Dees_Troy657c3092012-09-10 20:32:10 -0400849 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400850
851 for (i=0; i<zip_queue_index; i++) {
852 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200853 DataManager::SetValue("tw_filename", zip_queue[i]);
854 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400855
Dees_Troy657c3092012-09-10 20:32:10 -0400856 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000858 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400859 i = 10; // Error flashing zip - exit queue
860 ret_val = 1;
861 }
862 }
863 zip_queue_index = 0;
864 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
865
Dees_Troy657c3092012-09-10 20:32:10 -0400866 if (wipe_cache)
867 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200868
Dees_Troy51a0e822012-09-05 15:24:24 -0400869 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
870 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000871 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400872 if (simulate) {
873 simulate_progress_bar();
874 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400875 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
876 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200877 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400878 else {
879 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 +0200880 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400881 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400883 }
884 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400885 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400886 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200887 return 0;
888 }
889 if (function == "wipe")
890 {
891 operation_start("Format");
892 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400893
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400895
896 if (simulate) {
897 simulate_progress_bar();
898 } else {
899 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400900 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400901 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400902 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400903 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400904 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400905 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400906 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400908 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400909 } else if (arg == "INTERNAL") {
910 int has_datamedia, dual_storage;
911
912 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
913 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400914 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400915 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400916 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400917 }
918 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400920
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
922 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400923 } else if (arg == "ANDROIDSECURE") {
924 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500925 } else if (arg == "LIST") {
926 string Wipe_List, wipe_path;
927 bool skip = false;
928 ret_val = true;
929 TWPartition* wipe_part = NULL;
930
931 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000932 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500933 if (!Wipe_List.empty()) {
934 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
935 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
936 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000937 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500938 if (wipe_path == "/and-sec") {
939 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 ret_val = false;
942 break;
943 } else {
944 skip = true;
945 }
946 } else if (wipe_path == "DALVIK") {
947 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000948 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500949 ret_val = false;
950 break;
951 } else {
952 skip = true;
953 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000954 } else if (wipe_path == "INTERNAL") {
955 if (!PartitionManager.Wipe_Media_From_Data()) {
956 ret_val = false;
957 break;
958 } else {
959 skip = true;
960 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500961 }
962 if (!skip) {
963 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000964 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500965 ret_val = false;
966 break;
967 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
968 arg = wipe_path;
969 }
970 } else {
971 skip = false;
972 }
973 start_pos = end_pos + 1;
974 end_pos = Wipe_List.find(";", start_pos);
975 }
976 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400977 } else
978 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500979#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400980 if (arg == DataManager::GetSettingsStoragePath()) {
981 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
982 string Storage_Path = DataManager::GetSettingsStoragePath();
983
984 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000985 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400986 Storage_Path += "/TWRP";
987 mkdir(Storage_Path.c_str(), 0777);
988 DataManager::Flush();
989 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000990 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400991 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500993#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400994 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400995 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400996 if (ret_val)
997 ret_val = 0; // 0 is success
998 else
999 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001000 operation_end(ret_val, simulate);
1001 return 0;
1002 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001003 if (function == "refreshsizes")
1004 {
1005 operation_start("Refreshing Sizes");
1006 if (simulate) {
1007 simulate_progress_bar();
1008 } else
Dees_Troy5bf43922012-09-07 16:07:55 -04001009 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +00001011 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001012 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001013 if (function == "nandroid")
1014 {
1015 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -04001016 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001017
1018 if (simulate) {
1019 DataManager::SetValue("tw_partition", "Simulation");
1020 simulate_progress_bar();
1021 } else {
1022 if (arg == "backup") {
1023 string Backup_Name;
1024 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +00001025 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 -04001026 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001027 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001028 else {
1029 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001030 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001031
Dees_Troy43d8b002012-09-17 16:00:01 -04001032 }
Dees Troyb21cc642013-09-10 17:36:41 +00001033 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -04001034 } else if (arg == "restore") {
1035 string Restore_Name;
1036 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001037 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -04001038 } else {
1039 operation_end(1, simulate);
1040 return -1;
1041 }
1042 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001043 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001044 if (ret == false)
1045 ret = 1; // 1 for failure
1046 else
1047 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001048 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +00001049 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001050 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001051 if (function == "fixpermissions")
1052 {
1053 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001054 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001055 if (simulate) {
1056 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -04001057 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -04001058 int op_status = PartitionManager.Fix_Permissions();
1059 if (op_status != 0)
1060 op_status = 1; // failure
1061 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -04001062 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001063 return 0;
1064 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 if (function == "dd")
1066 {
1067 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -04001068
1069 if (simulate) {
1070 simulate_progress_bar();
1071 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001072 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +02001073 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -04001074 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001075 operation_end(0, simulate);
1076 return 0;
1077 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001078 if (function == "partitionsd")
1079 {
1080 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001081 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082
1083 if (simulate) {
1084 simulate_progress_bar();
1085 } else {
1086 int allow_partition;
1087 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1088 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001090 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001091 if (!PartitionManager.Partition_SDCard())
1092 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -04001093 }
1094 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001095 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001096 return 0;
1097 }
1098 if (function == "installhtcdumlock")
1099 {
1100 operation_start("Install HTC Dumlock");
1101 if (simulate) {
1102 simulate_progress_bar();
1103 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001104 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001105
1106 operation_end(0, simulate);
1107 return 0;
1108 }
1109 if (function == "htcdumlockrestoreboot")
1110 {
1111 operation_start("HTC Dumlock Restore Boot");
1112 if (simulate) {
1113 simulate_progress_bar();
1114 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001115 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001116
1117 operation_end(0, simulate);
1118 return 0;
1119 }
1120 if (function == "htcdumlockreflashrecovery")
1121 {
1122 operation_start("HTC Dumlock Reflash Recovery");
1123 if (simulate) {
1124 simulate_progress_bar();
1125 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001126 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001127
1128 operation_end(0, simulate);
1129 return 0;
1130 }
1131 if (function == "cmd")
1132 {
1133 int op_status = 0;
1134
1135 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001136 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001137 if (simulate) {
1138 simulate_progress_bar();
1139 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001140 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -04001141 if (op_status != 0)
1142 op_status = 1;
1143 }
1144
1145 operation_end(op_status, simulate);
1146 return 0;
1147 }
1148 if (function == "terminalcommand")
1149 {
1150 int op_status = 0;
1151 string cmdpath, command;
1152
1153 DataManager::GetValue("tw_terminal_location", cmdpath);
1154 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001155 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001156 if (simulate) {
1157 simulate_progress_bar();
1158 operation_end(op_status, simulate);
1159 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001160 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001162 DataManager::SetValue("tw_terminal_command_thread", command);
1163 DataManager::SetValue("tw_terminal_state", 1);
1164 DataManager::SetValue("tw_background_thread_running", 1);
1165 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1166 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001167 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001168 DataManager::SetValue("tw_terminal_state", 0);
1169 DataManager::SetValue("tw_background_thread_running", 0);
1170 operation_end(1, simulate);
1171 }
1172 }
1173 return 0;
1174 }
1175 if (function == "killterminal")
1176 {
1177 int op_status = 0;
1178
Dees_Troy2673cec2013-04-02 20:22:16 +00001179 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001180 operation_start("KillCommand");
1181 DataManager::SetValue("tw_operation_status", 0);
1182 DataManager::SetValue("tw_operation_state", 1);
1183 DataManager::SetValue("tw_terminal_state", 0);
1184 DataManager::SetValue("tw_background_thread_running", 0);
1185 DataManager::SetValue(TW_ACTION_BUSY, 0);
1186 return 0;
1187 }
1188 if (function == "reinjecttwrp")
1189 {
1190 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001191 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001192 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001193 if (simulate) {
1194 simulate_progress_bar();
1195 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001196 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001197 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001198 }
1199
1200 operation_end(op_status, simulate);
1201 return 0;
1202 }
1203 if (function == "checkbackupname")
1204 {
1205 int op_status = 0;
1206
1207 operation_start("CheckBackupName");
1208 if (simulate) {
1209 simulate_progress_bar();
1210 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001211 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001212 if (op_status != 0)
1213 op_status = 1;
1214 }
1215
1216 operation_end(op_status, simulate);
1217 return 0;
1218 }
1219 if (function == "decrypt")
1220 {
1221 int op_status = 0;
1222
1223 operation_start("Decrypt");
1224 if (simulate) {
1225 simulate_progress_bar();
1226 } else {
1227 string Password;
1228 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001229 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001230 if (op_status != 0)
1231 op_status = 1;
1232 else {
1233 int load_theme = 1;
1234
1235 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001236
1237 if (load_theme) {
1238 int has_datamedia;
1239
1240 // Check for a custom theme and load it if exists
1241 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1242 if (has_datamedia != 0) {
1243 struct stat st;
1244 int check = 0;
1245 std::string theme_path;
1246
1247 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001248 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001250 check = 1;
1251 }
1252
1253 theme_path += "/TWRP/theme/ui.zip";
1254 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1255 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1256 {
1257 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001259 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1260 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001261 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001262 }
1263 }
1264 }
1265 }
1266 }
1267 }
1268 }
1269
1270 operation_end(op_status, simulate);
1271 return 0;
1272 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001273 if (function == "adbsideload")
1274 {
1275 int ret = 0;
1276
1277 operation_start("Sideload");
1278 if (simulate) {
1279 simulate_progress_bar();
1280 } else {
1281 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001282 int wipe_dalvik = 0;
Vojtech Bocek05534202013-09-11 08:11:56 +02001283 string Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001284
Ethan Yonker45312e52014-02-26 09:23:53 -06001285 if (!PartitionManager.Mount_Current_Storage(false)) {
1286 gui_print("Using RAM for sideload storage.\n");
1287 Sideload_File = "/tmp/sideload.zip";
1288 } else {
1289 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
Dees_Troycfb63ae2012-09-19 14:30:17 -04001290 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001291 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001292 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001293 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001295 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001296 ret = apply_from_adb(Sideload_File.c_str());
1297 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 -05001298 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001299 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001301 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1302 PartitionManager.Wipe_By_Path("/cache");
1303 if (wipe_dalvik)
1304 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001305 } else {
1306 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001307 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001308 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1309 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001311 if (simulate) {
1312 simulate_progress_bar();
1313 } else {
1314 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1315 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001316 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001317 else {
1318 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 +02001319 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001320 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001322 }
1323 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001324 }
1325 operation_end(ret, simulate);
1326 return 0;
1327 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001328 if (function == "adbsideloadcancel")
1329 {
1330 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001332 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001333 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001334 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 property_get("tw_child_pid", child_prop, "error");
1336 if (strcmp(child_prop, "error") == 0) {
1337 LOGERR("Unable to get child ID from prop\n");
1338 return 0;
1339 }
1340 child_pid = atoi(child_prop);
1341 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001342 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001343 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001344 return 0;
1345 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001346 if (function == "openrecoveryscript") {
1347 operation_start("OpenRecoveryScript");
1348 if (simulate) {
1349 simulate_progress_bar();
1350 } else {
1351 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1352 // that we converted to ORS commands during boot in recovery.cpp.
1353 // Run those first.
1354 int reboot = 0;
1355 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001356 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001357 if (OpenRecoveryScript::run_script_file() == 0) {
1358 reboot = 1;
1359 }
1360 }
1361 // Check for the ORS file in /cache and attempt to run those commands.
1362 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001364 if (OpenRecoveryScript::run_script_file() == 0) {
1365 reboot = 1;
1366 }
1367 }
1368 if (reboot) {
1369 usleep(2000000); // Sleep for 2 seconds before rebooting
1370 TWFunc::tw_reboot(rb_system);
1371 } else {
1372 DataManager::SetValue("tw_page_done", 1);
1373 }
1374 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001375 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001376 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001377 if (function == "installsu")
1378 {
1379 int op_status = 0;
1380
1381 operation_start("Install SuperSU");
1382 if (simulate) {
1383 simulate_progress_bar();
1384 } else {
1385 if (!TWFunc::Install_SuperSU())
1386 op_status = 1;
1387 }
1388
1389 operation_end(op_status, simulate);
1390 return 0;
1391 }
1392 if (function == "fixsu")
1393 {
1394 int op_status = 0;
1395
1396 operation_start("Fixing Superuser Permissions");
1397 if (simulate) {
1398 simulate_progress_bar();
1399 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001400 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1401 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001402 }
1403
1404 operation_end(op_status, simulate);
1405 return 0;
1406 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001407 if (function == "decrypt_backup")
1408 {
1409 int op_status = 0;
1410
1411 operation_start("Try Restore Decrypt");
1412 if (simulate) {
1413 simulate_progress_bar();
1414 } else {
1415 string Restore_Path, Filename, Password;
1416 DataManager::GetValue("tw_restore", Restore_Path);
1417 Restore_Path += "/";
1418 DataManager::GetValue("tw_restore_password", Password);
1419 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1420 op_status = 0; // success
1421 else
1422 op_status = 1; // fail
1423 }
1424
1425 operation_end(op_status, simulate);
1426 return 0;
1427 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001428 if (function == "repair")
1429 {
1430 int op_status = 0;
1431
1432 operation_start("Repair Partition");
1433 if (simulate) {
1434 simulate_progress_bar();
1435 } else {
1436 string part_path;
1437 DataManager::GetValue("tw_partition_mount_point", part_path);
1438 if (PartitionManager.Repair_By_Path(part_path, true)) {
1439 op_status = 0; // success
1440 } else {
1441 LOGERR("Error repairing file system.\n");
1442 op_status = 1; // fail
1443 }
1444 }
1445
1446 operation_end(op_status, simulate);
1447 return 0;
1448 }
1449 if (function == "changefilesystem")
1450 {
1451 int op_status = 0;
1452
1453 operation_start("Change File System");
1454 if (simulate) {
1455 simulate_progress_bar();
1456 } else {
1457 string part_path, file_system;
1458 DataManager::GetValue("tw_partition_mount_point", part_path);
1459 DataManager::GetValue("tw_action_new_file_system", file_system);
1460 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1461 op_status = 0; // success
1462 } else {
1463 LOGERR("Error changing file system.\n");
1464 op_status = 1; // fail
1465 }
1466 }
jrior001aad03112014-07-05 10:31:21 -04001467 PartitionManager.Update_System_Details();
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001468 operation_end(op_status, simulate);
1469 return 0;
1470 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001471 if (function == "startmtp")
1472 {
1473 int op_status = 0;
1474
1475 operation_start("Start MTP");
1476 if (PartitionManager.Enable_MTP())
1477 op_status = 0; // success
1478 else
1479 op_status = 1; // fail
1480
1481 operation_end(op_status, simulate);
1482 return 0;
1483 }
1484 if (function == "stopmtp")
1485 {
1486 int op_status = 0;
1487
1488 operation_start("Stop MTP");
1489 if (PartitionManager.Disable_MTP())
1490 op_status = 0; // success
1491 else
1492 op_status = 1; // fail
1493
1494 operation_end(op_status, simulate);
1495 return 0;
1496 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001497 }
1498 else
1499 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001500 pthread_t t;
1501 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001502 return 0;
1503 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001504 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001505 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001506}
1507
1508int GUIAction::getKeyByName(std::string key)
1509{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001510 if (key == "home") return KEY_HOME;
1511 else if (key == "menu") return KEY_MENU;
1512 else if (key == "back") return KEY_BACK;
1513 else if (key == "search") return KEY_SEARCH;
1514 else if (key == "voldown") return KEY_VOLUMEDOWN;
1515 else if (key == "volup") return KEY_VOLUMEUP;
1516 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001517 int ret_val;
1518 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1519 if (!ret_val)
1520 return KEY_POWER;
1521 else
1522 return ret_val;
1523 }
1524
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001525 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001526}
1527
1528void* GUIAction::command_thread(void *cookie)
1529{
1530 string command;
1531 FILE* fp;
1532 char line[512];
1533
1534 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001535 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001536 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001538 } else {
1539 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1540 struct timeval timeout;
1541 fd_set fdset;
1542
1543 while(keep_going)
1544 {
1545 FD_ZERO(&fdset);
1546 FD_SET(fd, &fdset);
1547 timeout.tv_sec = 0;
1548 timeout.tv_usec = 400000;
1549 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1550 if (has_data == 0) {
1551 // Timeout reached
1552 DataManager::GetValue("tw_terminal_state", check);
1553 if (check == 0) {
1554 keep_going = 0;
1555 }
1556 } else if (has_data < 0) {
1557 // End of execution
1558 keep_going = 0;
1559 } else {
1560 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001561 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001562 bytes_read = read(fd, line, sizeof(line));
1563 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001565 else
1566 keep_going = 0; // Done executing
1567 }
1568 }
1569 fclose(fp);
1570 }
1571 DataManager::SetValue("tw_operation_status", 0);
1572 DataManager::SetValue("tw_operation_state", 1);
1573 DataManager::SetValue("tw_terminal_state", 0);
1574 DataManager::SetValue("tw_background_thread_running", 0);
1575 DataManager::SetValue(TW_ACTION_BUSY, 0);
1576 return NULL;
1577}