blob: 5ba749c8137ff69fd070b8a619fdaf9c3d72e459 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010046
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 "../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"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053};
Ethan Yonkerf1179622016-08-25 15:32:21 -050054#include "../set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
that3f7b1ac2014-12-30 11:30:13 +010061GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010062std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010063static string zip_queue[10];
64static int zip_queue_index;
65static pthread_t terminal_command;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010067
that73a52952015-01-28 23:07:34 +010068static void *ActionThread_work_wrapper(void *data);
69
70class ActionThread
71{
72public:
73 ActionThread();
74 ~ActionThread();
75
76 void threadActions(GUIAction *act);
77 void run(void *data);
78private:
79 friend void *ActionThread_work_wrapper(void*);
80 struct ThreadData
81 {
82 ActionThread *this_;
83 GUIAction *act;
84 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
85 };
86
87 pthread_t m_thread;
88 bool m_thread_running;
89 pthread_mutex_t m_act_lock;
90};
91
92static ActionThread action_thread; // for all kinds of longer running actions
93static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010094
95static void *ActionThread_work_wrapper(void *data)
96{
that73a52952015-01-28 23:07:34 +010097 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010098 return NULL;
99}
100
101ActionThread::ActionThread()
102{
103 m_thread_running = false;
104 pthread_mutex_init(&m_act_lock, NULL);
105}
106
107ActionThread::~ActionThread()
108{
109 pthread_mutex_lock(&m_act_lock);
110 if(m_thread_running) {
111 pthread_mutex_unlock(&m_act_lock);
112 pthread_join(m_thread, NULL);
113 } else {
114 pthread_mutex_unlock(&m_act_lock);
115 }
116 pthread_mutex_destroy(&m_act_lock);
117}
118
that7d3b54f2015-01-09 22:52:51 +0100119void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100120{
121 pthread_mutex_lock(&m_act_lock);
122 if (m_thread_running) {
123 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100124 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
125 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100126 } else {
127 m_thread_running = true;
128 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100129 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100130 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
131 }
132}
133
134void ActionThread::run(void *data)
135{
136 ThreadData *d = (ThreadData*)data;
137 GUIAction* act = d->act;
138
139 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100140 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100141 act->doAction(*it);
142
143 pthread_mutex_lock(&m_act_lock);
144 m_thread_running = false;
145 pthread_mutex_unlock(&m_act_lock);
146 delete d;
147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 xml_node<>* child;
153 xml_node<>* actions;
154 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
that3f7b1ac2014-12-30 11:30:13 +0100158 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100159#define ADD_ACTION(n) mf[#n] = &GUIAction::n
160#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100161 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100162 ADD_ACTION(reboot);
163 ADD_ACTION(home);
164 ADD_ACTION(key);
165 ADD_ACTION(page);
166 ADD_ACTION(reload);
167 ADD_ACTION(readBackup);
168 ADD_ACTION(set);
169 ADD_ACTION(clear);
170 ADD_ACTION(mount);
171 ADD_ACTION(unmount);
172 ADD_ACTION_EX("umount", unmount);
173 ADD_ACTION(restoredefaultsettings);
174 ADD_ACTION(copylog);
175 ADD_ACTION(compute);
176 ADD_ACTION_EX("addsubtract", compute);
177 ADD_ACTION(setguitimezone);
178 ADD_ACTION(overlay);
179 ADD_ACTION(queuezip);
180 ADD_ACTION(cancelzip);
181 ADD_ACTION(queueclear);
182 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500183 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600201 ADD_ACTION(checkforapp);
thatc6085482015-01-09 22:12:43 +0100202
203 // remember actions that run in the caller thread
204 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
205 setActionsRunningInCallerThread.insert(it->first);
206
207 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100208 ADD_ACTION(flash);
209 ADD_ACTION(wipe);
210 ADD_ACTION(refreshsizes);
211 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600212 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100213 ADD_ACTION(fixpermissions);
214 ADD_ACTION(dd);
215 ADD_ACTION(partitionsd);
216 ADD_ACTION(installhtcdumlock);
217 ADD_ACTION(htcdumlockrestoreboot);
218 ADD_ACTION(htcdumlockreflashrecovery);
219 ADD_ACTION(cmd);
220 ADD_ACTION(terminalcommand);
221 ADD_ACTION(reinjecttwrp);
222 ADD_ACTION(decrypt);
223 ADD_ACTION(adbsideload);
224 ADD_ACTION(openrecoveryscript);
225 ADD_ACTION(installsu);
226 ADD_ACTION(decrypt_backup);
227 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500228 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100229 ADD_ACTION(changefilesystem);
230 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100231 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600232 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600233 ADD_ACTION(installapp);
that3f7b1ac2014-12-30 11:30:13 +0100234 }
235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600237 actions = FindNode(node, "actions");
238 if (actions) child = FindNode(actions, "action");
239 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 while (child)
244 {
245 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 attr = child->first_attribute("function");
248 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 action.mFunction = attr->value();
251 action.mArg = child->value();
252 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 child = child->next_sibling("action");
255 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600258 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 if (child)
260 {
261 attr = child->first_attribute("key");
262 if (attr)
263 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100264 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
265 for(size_t i = 0; i < keys.size(); ++i)
266 {
267 const int key = getKeyByName(keys[i]);
268 mKeys[key] = false;
269 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 }
271 else
272 {
273 attr = child->first_attribute("x");
274 if (!attr) return;
275 mActionX = atol(attr->value());
276 attr = child->first_attribute("y");
277 if (!attr) return;
278 mActionY = atol(attr->value());
279 attr = child->first_attribute("w");
280 if (!attr) return;
281 mActionW = atol(attr->value());
282 attr = child->first_attribute("h");
283 if (!attr) return;
284 mActionH = atol(attr->value());
285 }
286 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400287}
288
Matt Mower25036b72016-06-24 12:30:18 -0500289int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400290{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 if (state == TOUCH_RELEASE)
292 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295}
296
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100297int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400298{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100299 std::map<int, bool>::iterator itr = mKeys.find(key);
300 if(itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100301 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100302
303 bool prevState = itr->second;
304 itr->second = down;
305
306 // If there is only one key for this action, wait for key up so it
307 // doesn't trigger with multi-key actions.
308 // Else, check if all buttons are pressed, then consume their release events
309 // so they don't trigger one-button actions and reset mKeys pressed status
310 if(mKeys.size() == 1) {
thatd4725ca2016-01-19 00:15:21 +0100311 if(!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312 doActions();
thatd4725ca2016-01-19 00:15:21 +0100313 return 0;
314 }
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100315 } else if(down) {
316 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
317 if(!itr->second)
that8834a0f2016-01-05 23:29:30 +0100318 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100319 }
320
321 // Passed, all req buttons are pressed, reset them and consume release events
322 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
323 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
324 kb->ConsumeKeyRelease(itr->first);
325 itr->second = false;
326 }
327
328 doActions();
thatd4725ca2016-01-19 00:15:21 +0100329 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100330 }
331
thatd4725ca2016-01-19 00:15:21 +0100332 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
Vojtech Bocek07220562014-02-08 02:05:33 +0100335int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400336{
Vojtech Bocek07220562014-02-08 02:05:33 +0100337 GUIObject::NotifyVarChange(varName, value);
338
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100339 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100341 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400343
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
347void GUIAction::simulate_progress_bar(void)
348{
Ethan Yonker74db1572015-10-28 12:44:49 -0500349 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400350 for (int i = 0; i < 5; i++)
351 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500352 if (PartitionManager.stop_backup.get_value()) {
353 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600354 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500355 DataManager::SetValue("ui_progress", 0);
356 PartitionManager.stop_backup.set_value(0);
357 return;
358 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 usleep(500000);
360 DataManager::SetValue("ui_progress", i * 20);
361 }
362}
363
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600364int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400365{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
368 DataManager::SetValue("ui_progress", 0);
369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 if (filename.empty())
371 {
372 LOGERR("No file specified.\n");
373 return -1;
374 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Ethan Yonker9598c072016-01-15 21:54:34 -0600376 if (!TWFunc::Path_Exists(filename)) {
377 if (!PartitionManager.Mount_By_Path(filename, true)) {
378 return -1;
379 }
380 if (!TWFunc::Path_Exists(filename)) {
381 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
382 return -1;
383 }
384 }
Dees_Troy657c3092012-09-10 20:32:10 -0400385
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 if (simulate) {
387 simulate_progress_bar();
388 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400389 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400390
391 // Now, check if we need to ensure TWRP remains installed...
392 struct stat st;
393 if (stat("/sbin/installTwrp", &st) == 0)
394 {
395 DataManager::SetValue("tw_operation", "Configuring TWRP");
396 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500397 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200398 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500400 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 }
402 }
403 }
404
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 // Done
406 DataManager::SetValue("ui_progress", 100);
407 DataManager::SetValue("ui_progress", 0);
408 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409}
410
that73a52952015-01-28 23:07:34 +0100411GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100412{
that73a52952015-01-28 23:07:34 +0100413 string func = gui_parse_text(action.mFunction);
414 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
415 if (needsThread) {
416 if (func == "cancelbackup")
417 return THREAD_CANCEL;
418 else
419 return THREAD_ACTION;
420 }
421 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100422}
423
Dees_Troy51a0e822012-09-05 15:24:24 -0400424int GUIAction::doActions()
425{
that3f7b1ac2014-12-30 11:30:13 +0100426 if (mActions.size() < 1)
427 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400428
that73a52952015-01-28 23:07:34 +0100429 // Determine in which thread to run the actions.
430 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
431 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100432 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100433 for (it = mActions.begin(); it != mActions.end(); ++it) {
434 ThreadType tt = getThreadType(*it);
435 if (tt == THREAD_NONE)
436 continue;
437 if (threadType == THREAD_NONE)
438 threadType = tt;
439 else if (threadType != tt) {
440 LOGERR("Can't mix normal and cancel actions in the same list.\n"
441 "Running the whole batch in the cancel thread.\n");
442 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100443 break;
444 }
that7d3b54f2015-01-09 22:52:51 +0100445 }
that73a52952015-01-28 23:07:34 +0100446
447 // Now run the actions in the desired thread.
448 switch (threadType) {
449 case THREAD_ACTION:
450 action_thread.threadActions(this);
451 break;
452
453 case THREAD_CANCEL:
454 cancel_thread.threadActions(this);
455 break;
456
457 default: {
458 // no iterators here because theme reloading might kill our object
459 const size_t cnt = mActions.size();
460 for (size_t i = 0; i < cnt; ++i)
461 doAction(mActions[i]);
462 }
thatc6085482015-01-09 22:12:43 +0100463 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466}
467
that3f7b1ac2014-12-30 11:30:13 +0100468int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400469{
that3f7b1ac2014-12-30 11:30:13 +0100470 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
that3f7b1ac2014-12-30 11:30:13 +0100472 std::string function = gui_parse_text(action.mFunction);
473 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474
that3f7b1ac2014-12-30 11:30:13 +0100475 // find function and execute it
476 mapFunc::const_iterator funcitr = mf.find(function);
477 if (funcitr != mf.end())
478 return (this->*funcitr->second)(arg);
479
480 LOGERR("Unknown action '%s'\n", function.c_str());
481 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482}
483
484void GUIAction::operation_start(const string operation_name)
485{
that3f7b1ac2014-12-30 11:30:13 +0100486 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000487 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 DataManager::SetValue(TW_ACTION_BUSY, 1);
489 DataManager::SetValue("ui_progress", 0);
490 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600492 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400493}
494
that3f7b1ac2014-12-30 11:30:13 +0100495void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400496{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000497 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 DataManager::SetValue("ui_progress", 100);
500 if (simulate) {
501 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
502 if (simulate_fail != 0)
503 DataManager::SetValue("tw_operation_status", 1);
504 else
505 DataManager::SetValue("tw_operation_status", 0);
506 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 }
510 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500512 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 }
514 DataManager::SetValue("tw_operation_state", 1);
515 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500516 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200517 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000518 time(&Stop);
519 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600520 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100521 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522}
523
that3f7b1ac2014-12-30 11:30:13 +0100524int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400525{
that3f7b1ac2014-12-30 11:30:13 +0100526 sync();
527 DataManager::SetValue("tw_gui_done", 1);
528 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
that3f7b1ac2014-12-30 11:30:13 +0100530 return 0;
531}
Dees_Troy51a0e822012-09-05 15:24:24 -0400532
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500533int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100534{
that3f7b1ac2014-12-30 11:30:13 +0100535 gui_changePage("main");
536 return 0;
537}
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
that3f7b1ac2014-12-30 11:30:13 +0100539int GUIAction::key(std::string arg)
540{
541 const int key = getKeyByName(arg);
542 PageManager::NotifyKey(key, true);
543 PageManager::NotifyKey(key, false);
544 return 0;
545}
546
547int GUIAction::page(std::string arg)
548{
LuK133762326f42015-09-30 19:57:46 +0200549 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100550 std::string page_name = gui_parse_text(arg);
551 return gui_changePage(page_name);
552}
553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500556 PageManager::RequestReload();
557 // The actual reload is handled in pages.cpp in PageManager::RunReload()
558 // The reload will occur on the next Update or Render call and will
559 // be performed in the rendoer thread instead of the action thread
560 // to prevent crashing which could occur when we start deleting
561 // GUI resources in the action thread while we attempt to render
562 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100563 return 0;
564}
Dees_Troy51a0e822012-09-05 15:24:24 -0400565
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500566int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100567{
568 string Restore_Name;
569 DataManager::GetValue("tw_restore", Restore_Name);
570 PartitionManager.Set_Restore_Files(Restore_Name);
571 return 0;
572}
573
574int GUIAction::set(std::string arg)
575{
576 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200577 {
that3f7b1ac2014-12-30 11:30:13 +0100578 string varName = arg.substr(0, arg.find('='));
579 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400580
that3f7b1ac2014-12-30 11:30:13 +0100581 DataManager::GetValue(value, value);
582 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 }
that3f7b1ac2014-12-30 11:30:13 +0100584 else
585 DataManager::SetValue(arg, "1");
586 return 0;
587}
Dees_Troy51a0e822012-09-05 15:24:24 -0400588
that3f7b1ac2014-12-30 11:30:13 +0100589int GUIAction::clear(std::string arg)
590{
591 DataManager::SetValue(arg, "0");
592 return 0;
593}
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
that3f7b1ac2014-12-30 11:30:13 +0100595int GUIAction::mount(std::string arg)
596{
597 if (arg == "usb") {
598 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100600 PartitionManager.usb_storage_enable();
601 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500602 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100603 } else if (!simulate) {
604 PartitionManager.Mount_By_Path(arg, true);
605 PartitionManager.Add_MTP_Storage(arg);
606 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500607 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100608 return 0;
609}
610
611int GUIAction::unmount(std::string arg)
612{
613 if (arg == "usb") {
614 if (!simulate)
615 PartitionManager.usb_storage_disable();
616 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500617 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100618 DataManager::SetValue(TW_ACTION_BUSY, 0);
619 } else if (!simulate) {
620 PartitionManager.UnMount_By_Path(arg, true);
621 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500622 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100623 return 0;
624}
625
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500626int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100627{
628 operation_start("Restore Defaults");
629 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500630 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100631 else {
632 DataManager::ResetDefaults();
633 PartitionManager.Update_System_Details();
634 PartitionManager.Mount_Current_Storage(true);
635 }
636 operation_end(0);
637 return 0;
638}
639
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500640int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100641{
642 operation_start("Copy Log");
643 if (!simulate)
644 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400645 string dst, curr_storage;
646 int copy_kernel_log = 0;
647
648 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100649 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400650 curr_storage = DataManager::GetCurrentStoragePath();
651 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100652 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
653 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400654 if (copy_kernel_log)
655 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100656 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400657 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100658 } else
659 simulate_progress_bar();
660 operation_end(0);
661 return 0;
662}
663
664
665int GUIAction::compute(std::string arg)
666{
667 if (arg.find("+") != string::npos)
668 {
669 string varName = arg.substr(0, arg.find('+'));
670 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
671 int amount_to_add = atoi(string_to_add.c_str());
672 int value;
673
674 DataManager::GetValue(varName, value);
675 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 return 0;
677 }
that3f7b1ac2014-12-30 11:30:13 +0100678 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 {
that3f7b1ac2014-12-30 11:30:13 +0100680 string varName = arg.substr(0, arg.find('-'));
681 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
682 int amount_to_subtract = atoi(string_to_subtract.c_str());
683 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400684
that3f7b1ac2014-12-30 11:30:13 +0100685 DataManager::GetValue(varName, value);
686 value -= amount_to_subtract;
687 if (value <= 0)
688 value = 0;
689 DataManager::SetValue(varName, value);
690 return 0;
691 }
692 if (arg.find("*") != string::npos)
693 {
694 string varName = arg.substr(0, arg.find('*'));
695 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
696 int multiply_by = atoi(multiply_by_str.c_str());
697 int value;
698
699 DataManager::GetValue(varName, value);
700 DataManager::SetValue(varName, value*multiply_by);
701 return 0;
702 }
703 if (arg.find("/") != string::npos)
704 {
705 string varName = arg.substr(0, arg.find('/'));
706 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
707 int divide_by = atoi(divide_by_str.c_str());
708 int value;
709
710 if(divide_by != 0)
711 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100713 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 }
715 return 0;
716 }
that3f7b1ac2014-12-30 11:30:13 +0100717 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
718 return -1;
719}
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500721int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100722{
723 string SelectedZone;
724 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
725 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
726 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
727
728 int dst;
729 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
730
731 string offset;
732 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
733
734 string NewTimeZone = Zone;
735 if (offset != "0")
736 NewTimeZone += ":" + offset;
737
738 if (dst != 0)
739 NewTimeZone += DSTZone;
740
741 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
742 DataManager::update_tz_environment_variables();
743 return 0;
744}
745
746int GUIAction::overlay(std::string arg)
747{
748 return gui_changeOverlay(arg);
749}
750
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500751int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100752{
753 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500754 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400755 return 0;
756 }
that3f7b1ac2014-12-30 11:30:13 +0100757 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
758 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
759 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400760 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100761 }
762 return 0;
763}
764
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500765int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100766{
767 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500768 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100770 } else {
771 zip_queue_index--;
772 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 }
that3f7b1ac2014-12-30 11:30:13 +0100774 return 0;
775}
Dees_Troy51a0e822012-09-05 15:24:24 -0400776
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500777int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100778{
779 zip_queue_index = 0;
780 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
781 return 0;
782}
Dees_Troy51a0e822012-09-05 15:24:24 -0400783
that3f7b1ac2014-12-30 11:30:13 +0100784int GUIAction::sleep(std::string arg)
785{
786 operation_start("Sleep");
787 usleep(atoi(arg.c_str()));
788 operation_end(0);
789 return 0;
790}
Dees Troyb21cc642013-09-10 17:36:41 +0000791
Matt Mower9a2a2052016-05-31 21:31:22 -0500792int GUIAction::sleepcounter(std::string arg)
793{
794 operation_start("SleepCounter");
795 // Ensure user notices countdown in case it needs to be cancelled
796 blankTimer.resetTimerAndUnblank();
797 int total = atoi(arg.c_str());
798 for (int t = total; t > 0; t--) {
799 int progress = (int)(((float)(total-t)/(float)total)*100.0);
800 DataManager::SetValue("ui_progress", progress);
801 ::sleep(1);
802 DataManager::SetValue("tw_sleep", t-1);
803 }
804 DataManager::SetValue("ui_progress", 100);
805 operation_end(0);
806 return 0;
807}
808
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500809int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100810{
811 operation_start("AppendDateToBackupName");
812 string Backup_Name;
813 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
814 Backup_Name += TWFunc::Get_Current_Date();
815 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
816 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
817 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500818 PageManager::NotifyKey(KEY_END, true);
819 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100820 operation_end(0);
821 return 0;
822}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500823
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500824int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100825{
826 operation_start("GenerateBackupName");
827 TWFunc::Auto_Generate_Backup_Name();
828 operation_end(0);
829 return 0;
830}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500831
Ethan Yonker483e9f42016-01-11 22:21:18 -0600832int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100833{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600834 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100835 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500836
Ethan Yonker483e9f42016-01-11 22:21:18 -0600837 if (arg.empty())
838 arg = "tw_wipe_list";
839 DataManager::GetValue(arg, List);
840 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
841 if (!List.empty()) {
842 size_t start_pos = 0, end_pos = List.find(";", start_pos);
843 while (end_pos != string::npos && start_pos < List.size()) {
844 part_path = List.substr(start_pos, end_pos - start_pos);
845 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
846 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100847 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400848 } else {
that3f7b1ac2014-12-30 11:30:13 +0100849 count++;
850 }
851 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100853 }
854 DataManager::SetValue("tw_check_partition_list", count);
855 } else {
856 DataManager::SetValue("tw_check_partition_list", 0);
857 }
858 return 0;
859}
Dees_Troy51a0e822012-09-05 15:24:24 -0400860
Ethan Yonker483e9f42016-01-11 22:21:18 -0600861int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100862{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100864 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400865
Ethan Yonker483e9f42016-01-11 22:21:18 -0600866 if (arg.empty())
867 arg = "tw_wipe_list";
868 DataManager::GetValue(arg, List);
869 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
870 if (!List.empty()) {
871 size_t start_pos = 0, end_pos = List.find(";", start_pos);
872 part_path = List;
873 while (end_pos != string::npos && start_pos < List.size()) {
874 part_path = List.substr(start_pos, end_pos - start_pos);
875 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
876 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100877 // Do nothing
878 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600879 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100880 break;
881 }
882 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100884 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600885 if (!part_path.empty()) {
886 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100887 if (Part) {
888 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500889
that3f7b1ac2014-12-30 11:30:13 +0100890 DataManager::SetValue("tw_partition_name", Part->Display_Name);
891 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
892 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
893 DataManager::SetValue("tw_partition_size", Part->Size / mb);
894 DataManager::SetValue("tw_partition_used", Part->Used / mb);
895 DataManager::SetValue("tw_partition_free", Part->Free / mb);
896 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
897 DataManager::SetValue("tw_partition_removable", Part->Removable);
898 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
899
900 if (Part->Can_Repair())
901 DataManager::SetValue("tw_partition_can_repair", 1);
902 else
903 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500904 if (Part->Can_Resize())
905 DataManager::SetValue("tw_partition_can_resize", 1);
906 else
907 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600908 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100909 DataManager::SetValue("tw_partition_vfat", 1);
910 else
911 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600912 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100913 DataManager::SetValue("tw_partition_exfat", 1);
914 else
915 DataManager::SetValue("tw_partition_exfat", 0);
916 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
917 DataManager::SetValue("tw_partition_f2fs", 1);
918 else
919 DataManager::SetValue("tw_partition_f2fs", 0);
920 if (TWFunc::Path_Exists("/sbin/mke2fs"))
921 DataManager::SetValue("tw_partition_ext", 1);
922 else
923 DataManager::SetValue("tw_partition_ext", 0);
924 return 0;
925 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600926 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100927 }
928 }
929 }
930 DataManager::SetValue("tw_partition_name", "");
931 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600932 // Set this to 0 to prevent trying to partition this device, just in case
933 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100934 return 0;
935}
936
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500937int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100938{
939 time_t tm;
940 char path[256];
941 int path_len;
942 uid_t uid = -1;
943 gid_t gid = -1;
944
945 struct passwd *pwd = getpwnam("media_rw");
946 if(pwd) {
947 uid = pwd->pw_uid;
948 gid = pwd->pw_gid;
949 }
950
951 const std::string storage = DataManager::GetCurrentStoragePath();
952 if(PartitionManager.Is_Mounted_By_Path(storage)) {
953 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
954 } else {
955 strcpy(path, "/tmp/");
956 }
957
Xuefer579e9072016-01-22 13:35:50 +0800958 if(!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100959 return 0;
960
961 tm = time(NULL);
962 path_len = strlen(path);
963
964 // Screenshot_2014-01-01-18-21-38.png
965 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
966
967 int res = gr_save_screenshot(path);
968 if(res == 0) {
969 chmod(path, 0666);
970 chown(path, uid, gid);
971
that677b13f2015-12-26 23:57:31 +0100972 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100973
974 // blink to notify that the screenshow was taken
975 gr_color(255, 255, 255, 255);
976 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
977 gr_flip();
978 gui_forceRender();
979 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500980 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100981 }
982 return 0;
983}
984
985int GUIAction::setbrightness(std::string arg)
986{
987 return TWFunc::Set_Brightness(arg);
988}
989
990int GUIAction::fileexists(std::string arg)
991{
992 struct stat st;
993 string newpath = arg + "/.";
994
995 operation_start("FileExists");
996 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
997 operation_end(0);
998 else
999 operation_end(1);
1000 return 0;
1001}
1002
thatcc8ddca2015-01-03 01:59:36 +01001003void GUIAction::reinject_after_flash()
1004{
1005 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001006 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001007 if (simulate) {
1008 simulate_progress_bar();
1009 } else {
1010 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1011 if (Boot == NULL || Boot->Current_File_System != "emmc")
1012 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1013 else {
1014 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1015 TWFunc::Exec_Cmd(injectcmd);
1016 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001017 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001018 }
1019 }
1020}
1021
that3f7b1ac2014-12-30 11:30:13 +01001022int GUIAction::flash(std::string arg)
1023{
1024 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001025 // We're going to jump to this page first, like a loading page
1026 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001027 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001028 string zip_path = zip_queue[i];
1029 size_t slashpos = zip_path.find_last_of('/');
1030 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001031 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001032 DataManager::SetValue("tw_filename", zip_path);
1033 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001034 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1035
1036 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001037 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001038 TWFunc::SetPerformanceMode(false);
1039 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001040 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001041 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001042 break;
that3f7b1ac2014-12-30 11:30:13 +01001043 }
1044 }
1045 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001046
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001047 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001048 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001049 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001050 }
that3f7b1ac2014-12-30 11:30:13 +01001051
thatcc8ddca2015-01-03 01:59:36 +01001052 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001053 PartitionManager.Update_System_Details();
1054 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001055 // This needs to be after the operation_end call so we change pages before we change variables that we display on the screen
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001056 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001057 return 0;
1058}
1059
1060int GUIAction::wipe(std::string arg)
1061{
1062 operation_start("Format");
1063 DataManager::SetValue("tw_partition", arg);
1064
1065 int ret_val = false;
1066
1067 if (simulate) {
1068 simulate_progress_bar();
1069 } else {
1070 if (arg == "data")
1071 ret_val = PartitionManager.Factory_Reset();
1072 else if (arg == "battery")
1073 ret_val = PartitionManager.Wipe_Battery_Stats();
1074 else if (arg == "rotate")
1075 ret_val = PartitionManager.Wipe_Rotate_Data();
1076 else if (arg == "dalvik")
1077 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1078 else if (arg == "DATAMEDIA") {
1079 ret_val = PartitionManager.Format_Data();
1080 } else if (arg == "INTERNAL") {
1081 int has_datamedia, dual_storage;
1082
1083 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1084 if (has_datamedia) {
1085 ret_val = PartitionManager.Wipe_Media_From_Data();
1086 } else {
1087 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1088 }
1089 } else if (arg == "EXTERNAL") {
1090 string External_Path;
1091
1092 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1093 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1094 } else if (arg == "ANDROIDSECURE") {
1095 ret_val = PartitionManager.Wipe_Android_Secure();
1096 } else if (arg == "LIST") {
1097 string Wipe_List, wipe_path;
1098 bool skip = false;
1099 ret_val = true;
1100 TWPartition* wipe_part = NULL;
1101
1102 DataManager::GetValue("tw_wipe_list", Wipe_List);
1103 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1104 if (!Wipe_List.empty()) {
1105 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1106 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1107 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1108 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1109 if (wipe_path == "/and-sec") {
1110 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001111 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001112 ret_val = false;
1113 break;
1114 } else {
1115 skip = true;
1116 }
1117 } else if (wipe_path == "DALVIK") {
1118 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001119 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001120 ret_val = false;
1121 break;
1122 } else {
1123 skip = true;
1124 }
1125 } else if (wipe_path == "INTERNAL") {
1126 if (!PartitionManager.Wipe_Media_From_Data()) {
1127 ret_val = false;
1128 break;
1129 } else {
1130 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001131 }
1132 }
that3f7b1ac2014-12-30 11:30:13 +01001133 if (!skip) {
1134 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001135 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001136 ret_val = false;
1137 break;
1138 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1139 arg = wipe_path;
1140 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 } else {
that3f7b1ac2014-12-30 11:30:13 +01001142 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 }
that3f7b1ac2014-12-30 11:30:13 +01001144 start_pos = end_pos + 1;
1145 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001146 }
1147 }
that3f7b1ac2014-12-30 11:30:13 +01001148 } else
1149 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001150#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001151 if (arg == DataManager::GetSettingsStoragePath()) {
1152 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1153 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001154
that3f7b1ac2014-12-30 11:30:13 +01001155 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1156 LOGINFO("Making TWRP folder and saving settings.\n");
1157 Storage_Path += "/TWRP";
1158 mkdir(Storage_Path.c_str(), 0777);
1159 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001160 } else {
that3f7b1ac2014-12-30 11:30:13 +01001161 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1162 }
1163 }
1164#endif
1165 }
1166 PartitionManager.Update_System_Details();
1167 if (ret_val)
1168 ret_val = 0; // 0 is success
1169 else
1170 ret_val = 1; // 1 is failure
1171 operation_end(ret_val);
1172 return 0;
1173}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001174
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001175int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001176{
1177 operation_start("Refreshing Sizes");
1178 if (simulate) {
1179 simulate_progress_bar();
1180 } else
1181 PartitionManager.Update_System_Details();
1182 operation_end(0);
1183 return 0;
1184}
1185
1186int GUIAction::nandroid(std::string arg)
1187{
that3f7b1ac2014-12-30 11:30:13 +01001188 if (simulate) {
that73a52952015-01-28 23:07:34 +01001189 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001190 DataManager::SetValue("tw_partition", "Simulation");
1191 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001192 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001193 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001194 operation_start("Nandroid");
1195 int ret = 0;
1196
that3f7b1ac2014-12-30 11:30:13 +01001197 if (arg == "backup") {
1198 string Backup_Name;
1199 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001200 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
1201 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001202 ret = PartitionManager.Run_Backup(false);
1203 } else {
that3f7b1ac2014-12-30 11:30:13 +01001204 operation_end(1);
1205 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001206 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001207 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001208 } else if (arg == "restore") {
1209 string Restore_Name;
1210 DataManager::GetValue("tw_restore", Restore_Name);
1211 ret = PartitionManager.Run_Restore(Restore_Name);
1212 } else {
1213 operation_end(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001214 return -1;
1215 }
1216 DataManager::SetValue("tw_encrypt_backup", 0);
1217 if (!PartitionManager.stop_backup.get_value()) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001218 if (ret == false)
1219 ret = 1; // 1 for failure
1220 else
1221 ret = 0; // 0 for success
bigbiff7abc5fe2015-01-17 16:53:12 -05001222 DataManager::SetValue("tw_cancel_backup", 0);
bigbiff7abc5fe2015-01-17 16:53:12 -05001223 }
1224 else {
1225 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -06001226 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -05001227 ret = 0;
1228 }
that73a52952015-01-28 23:07:34 +01001229 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001230 return ret;
1231 }
1232 return 0;
1233}
1234
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001235int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001236 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001237 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001238 }
1239 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001240 int op_status = PartitionManager.Cancel_Backup();
1241 if (op_status != 0)
1242 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001243 }
1244
1245 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001246}
Dees_Troy51a0e822012-09-05 15:24:24 -04001247
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001248int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001249{
that73a52952015-01-28 23:07:34 +01001250 int op_status = 0;
1251
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001252 operation_start("Fix Contexts");
1253 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001254 if (simulate) {
1255 simulate_progress_bar();
1256 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001257 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001258 if (op_status != 0)
1259 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001260 }
that73a52952015-01-28 23:07:34 +01001261 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001262 return 0;
1263}
1264
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001265int GUIAction::fixpermissions(std::string arg)
1266{
1267 return fixcontexts(arg);
1268}
1269
that3f7b1ac2014-12-30 11:30:13 +01001270int GUIAction::dd(std::string arg)
1271{
1272 operation_start("imaging");
1273
1274 if (simulate) {
1275 simulate_progress_bar();
1276 } else {
1277 string cmd = "dd " + arg;
1278 TWFunc::Exec_Cmd(cmd);
1279 }
1280 operation_end(0);
1281 return 0;
1282}
1283
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001284int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001285{
1286 operation_start("Partition SD Card");
1287 int ret_val = 0;
1288
1289 if (simulate) {
1290 simulate_progress_bar();
1291 } else {
1292 int allow_partition;
1293 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1294 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001296 } else {
1297 if (!PartitionManager.Partition_SDCard())
1298 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001299 }
that3f7b1ac2014-12-30 11:30:13 +01001300 }
1301 operation_end(ret_val);
1302 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001303
that3f7b1ac2014-12-30 11:30:13 +01001304}
Dees_Troy51a0e822012-09-05 15:24:24 -04001305
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001306int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001307{
1308 operation_start("Install HTC Dumlock");
1309 if (simulate) {
1310 simulate_progress_bar();
1311 } else
1312 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001313
that3f7b1ac2014-12-30 11:30:13 +01001314 operation_end(0);
1315 return 0;
1316}
Dees_Troy51a0e822012-09-05 15:24:24 -04001317
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001318int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001319{
1320 operation_start("HTC Dumlock Restore Boot");
1321 if (simulate) {
1322 simulate_progress_bar();
1323 } else
1324 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001325
that3f7b1ac2014-12-30 11:30:13 +01001326 operation_end(0);
1327 return 0;
1328}
Dees_Troy51a0e822012-09-05 15:24:24 -04001329
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001330int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001331{
1332 operation_start("HTC Dumlock Reflash Recovery");
1333 if (simulate) {
1334 simulate_progress_bar();
1335 } else
1336 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001337
that3f7b1ac2014-12-30 11:30:13 +01001338 operation_end(0);
1339 return 0;
1340}
Dees_Troy51a0e822012-09-05 15:24:24 -04001341
that3f7b1ac2014-12-30 11:30:13 +01001342int GUIAction::cmd(std::string arg)
1343{
1344 int op_status = 0;
1345
1346 operation_start("Command");
1347 LOGINFO("Running command: '%s'\n", arg.c_str());
1348 if (simulate) {
1349 simulate_progress_bar();
1350 } else {
1351 op_status = TWFunc::Exec_Cmd(arg);
1352 if (op_status != 0)
1353 op_status = 1;
1354 }
1355
1356 operation_end(op_status);
1357 return 0;
1358}
1359
1360int GUIAction::terminalcommand(std::string arg)
1361{
1362 int op_status = 0;
1363 string cmdpath, command;
1364
1365 DataManager::GetValue("tw_terminal_location", cmdpath);
1366 operation_start("CommandOutput");
1367 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1368 if (simulate) {
1369 simulate_progress_bar();
1370 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001371 } else if (arg == "exit") {
1372 LOGINFO("Exiting terminal\n");
1373 operation_end(op_status);
1374 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001375 } else {
1376 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1377 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001378 DataManager::SetValue("tw_terminal_state", 1);
1379 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001380 FILE* fp;
1381 char line[512];
1382
1383 fp = popen(command.c_str(), "r");
1384 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001385 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001386 } else {
1387 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1388 struct timeval timeout;
1389 fd_set fdset;
1390
1391 while(keep_going)
1392 {
1393 FD_ZERO(&fdset);
1394 FD_SET(fd, &fdset);
1395 timeout.tv_sec = 0;
1396 timeout.tv_usec = 400000;
1397 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1398 if (has_data == 0) {
1399 // Timeout reached
1400 DataManager::GetValue("tw_terminal_state", check);
1401 if (check == 0) {
1402 keep_going = 0;
1403 }
1404 } else if (has_data < 0) {
1405 // End of execution
1406 keep_going = 0;
1407 } else {
1408 // Try to read output
xiaolue738da52015-02-22 20:49:35 +08001409 if(fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001410 gui_print("%s", line); // Display output
1411 else
1412 keep_going = 0; // Done executing
1413 }
1414 }
1415 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001416 }
thatc6085482015-01-09 22:12:43 +01001417 DataManager::SetValue("tw_operation_status", 0);
1418 DataManager::SetValue("tw_operation_state", 1);
1419 DataManager::SetValue("tw_terminal_state", 0);
1420 DataManager::SetValue("tw_background_thread_running", 0);
1421 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001422 }
1423 return 0;
1424}
1425
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001426int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001427{
1428 int op_status = 0;
1429
1430 LOGINFO("Sending kill command...\n");
1431 operation_start("KillCommand");
1432 DataManager::SetValue("tw_operation_status", 0);
1433 DataManager::SetValue("tw_operation_state", 1);
1434 DataManager::SetValue("tw_terminal_state", 0);
1435 DataManager::SetValue("tw_background_thread_running", 0);
1436 DataManager::SetValue(TW_ACTION_BUSY, 0);
1437 return 0;
1438}
1439
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001440int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001441{
1442 int op_status = 0;
1443 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001445 if (simulate) {
1446 simulate_progress_bar();
1447 } else {
1448 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001449 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001450 }
1451
1452 operation_end(op_status);
1453 return 0;
1454}
1455
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001456int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001457{
1458 int op_status = 0;
1459
1460 operation_start("CheckBackupName");
1461 if (simulate) {
1462 simulate_progress_bar();
1463 } else {
1464 op_status = PartitionManager.Check_Backup_Name(true);
1465 if (op_status != 0)
1466 op_status = 1;
1467 }
1468
1469 operation_end(op_status);
1470 return 0;
1471}
1472
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001473int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001474{
1475 int op_status = 0;
1476
1477 operation_start("Decrypt");
1478 if (simulate) {
1479 simulate_progress_bar();
1480 } else {
1481 string Password;
1482 DataManager::GetValue("tw_crypto_password", Password);
1483 op_status = PartitionManager.Decrypt_Device(Password);
1484 if (op_status != 0)
1485 op_status = 1;
1486 else {
that3f7b1ac2014-12-30 11:30:13 +01001487
1488 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1489
Ethan Yonkercf50da52015-01-12 21:59:07 -06001490 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001491
Ethan Yonkercf50da52015-01-12 21:59:07 -06001492 // Check for a custom theme and load it if exists
1493 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1494 if (has_datamedia != 0) {
1495 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001496 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001497 } else {
1498 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001499 }
1500 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001501 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001502 }
1503 }
1504
1505 operation_end(op_status);
1506 return 0;
1507}
1508
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001509int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001510{
1511 operation_start("Sideload");
1512 if (simulate) {
1513 simulate_progress_bar();
1514 operation_end(0);
1515 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001517 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1518
1519 // wait for the adb connection
1520 int ret = apply_from_adb("/", &sideload_child_pid);
1521 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1522
1523 if (ret != 0) {
1524 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001525 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001526 ret = 1; // failure
1527 } else {
1528 int wipe_cache = 0;
1529 int wipe_dalvik = 0;
1530 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1531
1532 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1533 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1534 PartitionManager.Wipe_By_Path("/cache");
1535 if (wipe_dalvik)
1536 PartitionManager.Wipe_Dalvik_Cache();
1537 } else {
1538 ret = 1; // failure
1539 }
thatcc8ddca2015-01-03 01:59:36 +01001540 }
thatc6085482015-01-09 22:12:43 +01001541 if (sideload_child_pid) {
1542 LOGINFO("Signaling child sideload process to exit.\n");
1543 struct stat st;
1544 // Calling stat() on this magic filename signals the minadbd
1545 // subprocess to shut down.
1546 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1547 int status;
1548 LOGINFO("Waiting for child sideload process to exit.\n");
1549 waitpid(sideload_child_pid, &status, 0);
1550 }
Dees Troy28a85d22015-04-28 02:03:16 +00001551 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001552 TWFunc::Toggle_MTP(mtp_was_enabled);
1553 reinject_after_flash();
1554 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001555 }
that3f7b1ac2014-12-30 11:30:13 +01001556 return 0;
1557}
1558
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001559int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001560{
that3f7b1ac2014-12-30 11:30:13 +01001561 struct stat st;
1562 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001563 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001564 LOGINFO("Signaling child sideload process to exit.\n");
1565 // Calling stat() on this magic filename signals the minadbd
1566 // subprocess to shut down.
1567 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1568 if (!sideload_child_pid) {
1569 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001570 return 0;
1571 }
thatcc8ddca2015-01-03 01:59:36 +01001572 ::sleep(1);
1573 LOGINFO("Killing child sideload process.\n");
1574 kill(sideload_child_pid, SIGTERM);
1575 int status;
1576 LOGINFO("Waiting for child sideload process to exit.\n");
1577 waitpid(sideload_child_pid, &status, 0);
1578 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001579 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1580 return 0;
1581}
1582
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001583int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001584{
1585 operation_start("OpenRecoveryScript");
1586 if (simulate) {
1587 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001588 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001589 } else {
that10ae24f2015-12-26 20:53:51 +01001590 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001591 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001592 }
1593 return 0;
1594}
1595
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001596int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001597{
1598 int op_status = 0;
1599
1600 operation_start("Install SuperSU");
1601 if (simulate) {
1602 simulate_progress_bar();
1603 } else {
1604 if (!TWFunc::Install_SuperSU())
1605 op_status = 1;
1606 }
1607
1608 operation_end(op_status);
1609 return 0;
1610}
1611
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001612int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001613{
1614 int op_status = 0;
1615
1616 operation_start("Fixing Superuser Permissions");
1617 if (simulate) {
1618 simulate_progress_bar();
1619 } else {
1620 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1621 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1622 }
1623
1624 operation_end(op_status);
1625 return 0;
1626}
1627
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001628int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001629{
1630 int op_status = 0;
1631
1632 operation_start("Try Restore Decrypt");
1633 if (simulate) {
1634 simulate_progress_bar();
1635 } else {
1636 string Restore_Path, Filename, Password;
1637 DataManager::GetValue("tw_restore", Restore_Path);
1638 Restore_Path += "/";
1639 DataManager::GetValue("tw_restore_password", Password);
1640 TWFunc::SetPerformanceMode(true);
1641 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1642 op_status = 0; // success
1643 else
1644 op_status = 1; // fail
1645 TWFunc::SetPerformanceMode(false);
1646 }
1647
1648 operation_end(op_status);
1649 return 0;
1650}
1651
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001652int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001653{
1654 int op_status = 0;
1655
1656 operation_start("Repair Partition");
1657 if (simulate) {
1658 simulate_progress_bar();
1659 } else {
1660 string part_path;
1661 DataManager::GetValue("tw_partition_mount_point", part_path);
1662 if (PartitionManager.Repair_By_Path(part_path, true)) {
1663 op_status = 0; // success
1664 } else {
that3f7b1ac2014-12-30 11:30:13 +01001665 op_status = 1; // fail
1666 }
1667 }
1668
1669 operation_end(op_status);
1670 return 0;
1671}
1672
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001673int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001674{
1675 int op_status = 0;
1676
1677 operation_start("Resize Partition");
1678 if (simulate) {
1679 simulate_progress_bar();
1680 } else {
1681 string part_path;
1682 DataManager::GetValue("tw_partition_mount_point", part_path);
1683 if (PartitionManager.Resize_By_Path(part_path, true)) {
1684 op_status = 0; // success
1685 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001686 op_status = 1; // fail
1687 }
1688 }
1689
1690 operation_end(op_status);
1691 return 0;
1692}
1693
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001694int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001695{
1696 int op_status = 0;
1697
1698 operation_start("Change File System");
1699 if (simulate) {
1700 simulate_progress_bar();
1701 } else {
1702 string part_path, file_system;
1703 DataManager::GetValue("tw_partition_mount_point", part_path);
1704 DataManager::GetValue("tw_action_new_file_system", file_system);
1705 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1706 op_status = 0; // success
1707 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001708 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001709 op_status = 1; // fail
1710 }
1711 }
1712 PartitionManager.Update_System_Details();
1713 operation_end(op_status);
1714 return 0;
1715}
1716
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001717int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001718{
1719 int op_status = 0;
1720
1721 operation_start("Start MTP");
1722 if (PartitionManager.Enable_MTP())
1723 op_status = 0; // success
1724 else
1725 op_status = 1; // fail
1726
1727 operation_end(op_status);
1728 return 0;
1729}
1730
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001731int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001732{
1733 int op_status = 0;
1734
1735 operation_start("Stop MTP");
1736 if (PartitionManager.Disable_MTP())
1737 op_status = 0; // success
1738 else
1739 op_status = 1; // fail
1740
1741 operation_end(op_status);
1742 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001743}
1744
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001745int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001746{
1747 int op_status = 0;
1748
1749 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001750 string path, filename;
1751 DataManager::GetValue("tw_zip_location", path);
1752 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001753 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001754 op_status = 0; // success
1755 else
1756 op_status = 1; // fail
1757
1758 operation_end(op_status);
1759 return 0;
1760}
1761
that10ae24f2015-12-26 20:53:51 +01001762int GUIAction::twcmd(std::string arg)
1763{
1764 operation_start("TWRP CLI Command");
1765 if (simulate)
1766 simulate_progress_bar();
1767 else
1768 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1769 operation_end(0);
1770 return 0;
1771}
1772
Dees_Troy51a0e822012-09-05 15:24:24 -04001773int GUIAction::getKeyByName(std::string key)
1774{
that8834a0f2016-01-05 23:29:30 +01001775 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001776 else if (key == "menu") return KEY_MENU;
1777 else if (key == "back") return KEY_BACK;
1778 else if (key == "search") return KEY_SEARCH;
1779 else if (key == "voldown") return KEY_VOLUMEDOWN;
1780 else if (key == "volup") return KEY_VOLUMEUP;
1781 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001782 int ret_val;
1783 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1784 if (!ret_val)
1785 return KEY_POWER;
1786 else
1787 return ret_val;
1788 }
1789
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001790 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001791}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001792
1793int GUIAction::checkpartitionlifetimewrites(std::string arg)
1794{
1795 int op_status = 0;
1796 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1797
1798 operation_start("Check Partition Lifetime Writes");
1799 if (sys) {
1800 if (sys->Check_Lifetime_Writes() != 0)
1801 DataManager::SetValue("tw_lifetime_writes", 1);
1802 else
1803 DataManager::SetValue("tw_lifetime_writes", 0);
1804 op_status = 0; // success
1805 } else {
1806 DataManager::SetValue("tw_lifetime_writes", 1);
1807 op_status = 1; // fail
1808 }
1809
1810 operation_end(op_status);
1811 return 0;
1812}
1813
1814int GUIAction::mountsystemtoggle(std::string arg)
1815{
1816 int op_status = 0;
1817 bool remount_system = PartitionManager.Is_Mounted_By_Path("/system");
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001818 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001819
1820 operation_start("Toggle System Mount");
1821 if (!PartitionManager.UnMount_By_Path("/system", true)) {
1822 op_status = 1; // fail
1823 } else {
1824 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/system");
1825 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001826 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001827 DataManager::SetValue("tw_mount_system_ro", 0);
1828 Part->Change_Mount_Read_Only(false);
1829 } else {
1830 DataManager::SetValue("tw_mount_system_ro", 1);
1831 Part->Change_Mount_Read_Only(true);
1832 }
1833 if (remount_system) {
1834 Part->Mount(true);
1835 }
1836 op_status = 0; // success
1837 } else {
1838 op_status = 1; // fail
1839 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001840 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1841 if (Part) {
1842 if (arg == "0") {
1843 Part->Change_Mount_Read_Only(false);
1844 } else {
1845 Part->Change_Mount_Read_Only(true);
1846 }
1847 if (remount_vendor) {
1848 Part->Mount(true);
1849 }
1850 op_status = 0; // success
1851 } else {
1852 op_status = 1; // fail
1853 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001854 }
1855
1856 operation_end(op_status);
1857 return 0;
1858}
Ethan Yonker74db1572015-10-28 12:44:49 -05001859
1860int GUIAction::setlanguage(std::string arg __unused)
1861{
1862 int op_status = 0;
1863
1864 operation_start("Set Language");
1865 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1866 PageManager::RequestReload();
1867 op_status = 0; // success
1868
1869 operation_end(op_status);
1870 return 0;
1871}
Ethan Yonker1b190162016-12-05 15:25:19 -06001872
1873int GUIAction::setbootslot(std::string arg)
1874{
1875 operation_start("Set Boot Slot");
1876 if (!simulate)
1877 {
1878 PartitionManager.Set_Active_Slot(arg);
1879 } else
1880 simulate_progress_bar();
1881 operation_end(0);
1882 return 0;
1883}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001884
1885int GUIAction::checkforapp(std::string arg __unused)
1886{
1887 int op_status = 1;
1888 operation_start("Check for TWRP App");
1889 if (!simulate)
1890 {
1891 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1892 int sdkver = 0;
1893 if (!sdkverstr.empty()) {
1894 sdkver = atoi(sdkverstr.c_str());
1895 }
1896 if (sdkver <= 13) {
1897 if (sdkver == 0)
1898 LOGINFO("Unable to read sdk version from build prop\n");
1899 else
1900 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
1901 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1902 goto exit;
1903 }
1904 if (PartitionManager.Mount_By_Path("/system", false)) {
1905 string base_path = "/system";
1906 if (TWFunc::Path_Exists("/system/system"))
1907 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
1908 string install_path = base_path + "/priv-app";
1909 if (!TWFunc::Path_Exists(install_path))
1910 install_path = base_path + "/app";
1911 install_path += "/twrpapp";
1912 if (TWFunc::Path_Exists(install_path)) {
1913 LOGINFO("App found at '%s'\n", install_path.c_str());
1914 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1915 goto exit;
1916 }
1917 } else if (PartitionManager.Mount_By_Path("/data", false)) {
1918 string parent_path = "/data/app";
1919 DIR *d = opendir("/data/app");
1920 struct dirent *p;
Ethan Yonker4767caf2017-01-11 10:45:04 -06001921 size_t len = strlen("me.twrp.twrpapp-");
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001922 while ((p = readdir(d))) {
1923 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
1924 continue;
1925 if (p->d_type == DT_DIR && strlen(p->d_name) >= len && strncmp(p->d_name, "me.twrp.twrpapp-", len) == 0) {
1926 LOGINFO("App found at %s/%s\n", parent_path.c_str(), p->d_name);
1927 closedir(d);
1928 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
1929 goto exit;
1930 }
1931 }
1932 closedir(d);
1933 }
1934 } else
1935 simulate_progress_bar();
1936 LOGINFO("App not installed\n");
1937 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1938exit:
1939 operation_end(0);
1940 return 0;
1941}
1942
1943int GUIAction::installapp(std::string arg __unused)
1944{
1945 int op_status = 1;
1946 operation_start("Install TWRP App");
1947 if (!simulate)
1948 {
1949 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1950 if (PartitionManager.Mount_By_Path("/data", true)) {
1951 string install_path = "/data/app";
1952 string context = "u:object_r:apk_data_file:s0";
1953 if (!TWFunc::Path_Exists(install_path)) {
1954 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1955 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1956 goto exit;
1957 }
1958 if (chown(install_path.c_str(), 1000, 1000)) {
1959 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1960 goto exit;
1961 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001962 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001963 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1964 goto exit;
1965 }
1966 }
1967 install_path += "/me.twrp.twrpapp-1";
1968 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1969 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1970 goto exit;
1971 }
1972 if (chown(install_path.c_str(), 1000, 1000)) {
1973 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1974 goto exit;
1975 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001976 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001977 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1978 goto exit;
1979 }
1980 install_path += "/base.apk";
1981 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
1982 LOGERR("Error copying apk file\n");
1983 goto exit;
1984 }
1985 if (chown(install_path.c_str(), 1000, 1000)) {
1986 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001989 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001990 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
1993 sync();
1994 sync();
1995 }
1996 } else {
1997 if (PartitionManager.Mount_By_Path("/system", true)) {
1998 string base_path = "/system";
1999 if (TWFunc::Path_Exists("/system/system"))
2000 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2001 string install_path = base_path + "/priv-app";
2002 string context = "u:object_r:system_file:s0";
2003 if (!TWFunc::Path_Exists(install_path))
2004 install_path = base_path + "/app";
2005 if (TWFunc::Path_Exists(install_path)) {
2006 install_path += "/twrpapp";
2007 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2008 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002009 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002010 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2011 goto exit;
2012 }
2013 install_path += "/me.twrp.twrpapp.apk";
2014 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2015 LOGERR("Error copying apk file\n");
2016 goto exit;
2017 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002018 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002019 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2020 goto exit;
2021 }
2022 sync();
2023 sync();
2024 PartitionManager.UnMount_By_Path("/system", true);
2025 op_status = 0;
2026 } else {
2027 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2028 }
2029 }
2030 }
2031 }
2032 } else
2033 simulate_progress_bar();
2034exit:
2035 operation_end(0);
2036 return 0;
2037}