blob: 4796ff33533dd23797f8e1e73db9adbf280b717b [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"
Ethan Yonker24813422014-11-07 17:19:07 -060054#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060055#include "../set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056};
57
58#include "rapidxml.hpp"
59#include "objects.hpp"
60
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070061#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050062extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070063#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040064
Dees_Troy51a0e822012-09-05 15:24:24 -040065void curtainClose(void);
66
that3f7b1ac2014-12-30 11:30:13 +010067GUIAction::mapFunc GUIAction::mf;
68static string zip_queue[10];
69static int zip_queue_index;
70static pthread_t terminal_command;
71
Dees_Troy51a0e822012-09-05 15:24:24 -040072GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010073 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040074{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 xml_node<>* child;
76 xml_node<>* actions;
77 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
that3f7b1ac2014-12-30 11:30:13 +010081 if (mf.empty()) {
82 mf["reboot"] = &GUIAction::reboot;
83 mf["home"] = &GUIAction::home;
84 mf["key"] = &GUIAction::key;
85 mf["page"] = &GUIAction::page;
86 mf["reload"] = &GUIAction::reload;
87 mf["readBackup"] = &GUIAction::readBackup;
88 mf["set"] = &GUIAction::set;
89 mf["clear"] = &GUIAction::clear;
90 mf["mount"] = &GUIAction::mount;
91 mf["unmount"] = &GUIAction::unmount;
92 mf["umount"] = &GUIAction::unmount;
93 mf["restoredefaultsettings"] = &GUIAction::restoredefaultsettings;
94 mf["copylog"] = &GUIAction::copylog;
95 mf["compute"] = &GUIAction::compute;
96 mf["addsubtract"] = &GUIAction::compute;
97 mf["setguitimezone"] = &GUIAction::setguitimezone;
98 mf["overlay"] = &GUIAction::overlay;
99 mf["queuezip"] = &GUIAction::queuezip;
100 mf["cancelzip"] = &GUIAction::cancelzip;
101 mf["queueclear"] = &GUIAction::queueclear;
102 mf["sleep"] = &GUIAction::sleep;
103 mf["appenddatetobackupname"] = &GUIAction::appenddatetobackupname;
104 mf["generatebackupname"] = &GUIAction::generatebackupname;
105 mf["checkpartitionlist"] = &GUIAction::checkpartitionlist;
106 mf["getpartitiondetails"] = &GUIAction::getpartitiondetails;
107 mf["screenshot"] = &GUIAction::screenshot;
108 mf["setbrightness"] = &GUIAction::setbrightness;
109
110 // threaded actions
111 mf["fileexists"] = &GUIAction::fileexists;
112 mf["flash"] = &GUIAction::flash;
113 mf["wipe"] = &GUIAction::wipe;
114 mf["refreshsizes"] = &GUIAction::refreshsizes;
115 mf["nandroid"] = &GUIAction::nandroid;
116 mf["fixpermissions"] = &GUIAction::fixpermissions;
117 mf["dd"] = &GUIAction::dd;
118 mf["partitionsd"] = &GUIAction::partitionsd;
119 mf["installhtcdumlock"] = &GUIAction::installhtcdumlock;
120 mf["htcdumlockrestoreboot"] = &GUIAction::htcdumlockrestoreboot;
121 mf["htcdumlockreflashrecovery"] = &GUIAction::htcdumlockreflashrecovery;
122 mf["cmd"] = &GUIAction::cmd;
123 mf["terminalcommand"] = &GUIAction::terminalcommand;
124 mf["killterminal"] = &GUIAction::killterminal;
125 mf["reinjecttwrp"] = &GUIAction::reinjecttwrp;
126 mf["checkbackupname"] = &GUIAction::checkbackupname;
127 mf["decrypt"] = &GUIAction::decrypt;
128 mf["adbsideload"] = &GUIAction::adbsideload;
129 mf["adbsideloadcancel"] = &GUIAction::adbsideloadcancel;
130 mf["openrecoveryscript"] = &GUIAction::openrecoveryscript;
131 mf["installsu"] = &GUIAction::installsu;
132 mf["fixsu"] = &GUIAction::fixsu;
133 mf["decrypt_backup"] = &GUIAction::decrypt_backup;
134 mf["repair"] = &GUIAction::repair;
135 mf["changefilesystem"] = &GUIAction::changefilesystem;
136 mf["startmtp"] = &GUIAction::startmtp;
137 mf["stopmtp"] = &GUIAction::stopmtp;
138 }
139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 // First, get the action
141 actions = node->first_node("actions");
142 if (actions) child = actions->first_node("action");
143 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 while (child)
148 {
149 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 attr = child->first_attribute("function");
152 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 action.mFunction = attr->value();
155 action.mArg = child->value();
156 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 child = child->next_sibling("action");
159 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400160
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 // Now, let's get either the key or region
162 child = node->first_node("touch");
163 if (child)
164 {
165 attr = child->first_attribute("key");
166 if (attr)
167 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100168 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
169 for(size_t i = 0; i < keys.size(); ++i)
170 {
171 const int key = getKeyByName(keys[i]);
172 mKeys[key] = false;
173 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 }
175 else
176 {
177 attr = child->first_attribute("x");
178 if (!attr) return;
179 mActionX = atol(attr->value());
180 attr = child->first_attribute("y");
181 if (!attr) return;
182 mActionY = atol(attr->value());
183 attr = child->first_attribute("w");
184 if (!attr) return;
185 mActionW = atol(attr->value());
186 attr = child->first_attribute("h");
187 if (!attr) return;
188 mActionH = atol(attr->value());
189 }
190 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400191}
192
193int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
194{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200195 if (state == TOUCH_RELEASE)
196 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400197
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200198 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400199}
200
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100201int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400202{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100203 if (mKeys.empty())
204 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100206 std::map<int, bool>::iterator itr = mKeys.find(key);
207 if(itr == mKeys.end())
208 return 0;
209
210 bool prevState = itr->second;
211 itr->second = down;
212
213 // If there is only one key for this action, wait for key up so it
214 // doesn't trigger with multi-key actions.
215 // Else, check if all buttons are pressed, then consume their release events
216 // so they don't trigger one-button actions and reset mKeys pressed status
217 if(mKeys.size() == 1) {
218 if(!down && prevState)
219 doActions();
220 } else if(down) {
221 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
222 if(!itr->second)
223 return 0;
224 }
225
226 // Passed, all req buttons are pressed, reset them and consume release events
227 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
228 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
229 kb->ConsumeKeyRelease(itr->first);
230 itr->second = false;
231 }
232
233 doActions();
234 }
235
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 return 0;
237}
238
Vojtech Bocek07220562014-02-08 02:05:33 +0100239int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400240{
Vojtech Bocek07220562014-02-08 02:05:33 +0100241 GUIObject::NotifyVarChange(varName, value);
242
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100243 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100245 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249}
250
251void GUIAction::simulate_progress_bar(void)
252{
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400254 for (int i = 0; i < 5; i++)
255 {
256 usleep(500000);
257 DataManager::SetValue("ui_progress", i * 20);
258 }
259}
260
that3f7b1ac2014-12-30 11:30:13 +0100261int GUIAction::flash_zip(std::string filename, std::string pageName, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400262{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
265 DataManager::SetValue("ui_progress", 0);
266
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 if (filename.empty())
268 {
269 LOGERR("No file specified.\n");
270 return -1;
271 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400272
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 // We're going to jump to this page first, like a loading page
274 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400275
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200276 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400277 return -1;
278
that3f7b1ac2014-12-30 11:30:13 +0100279 // TODO: why again?
Ethan Yonker57e35872014-11-07 14:52:22 -0600280 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400281
Dees_Troy51a0e822012-09-05 15:24:24 -0400282 if (simulate) {
283 simulate_progress_bar();
284 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400285 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400286
287 // Now, check if we need to ensure TWRP remains installed...
288 struct stat st;
289 if (stat("/sbin/installTwrp", &st) == 0)
290 {
291 DataManager::SetValue("tw_operation", "Configuring TWRP");
292 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000293 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200294 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400295 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000296 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400297 }
298 }
299 }
300
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200301 // Done
302 DataManager::SetValue("ui_progress", 100);
303 DataManager::SetValue("ui_progress", 0);
304 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400305}
306
307int GUIAction::doActions()
308{
that3f7b1ac2014-12-30 11:30:13 +0100309 if (mActions.size() < 1)
310 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
that3f7b1ac2014-12-30 11:30:13 +0100312 std::vector<Action>::iterator it;
313 for (it = mActions.begin(); it != mActions.end(); ++it)
314 doAction(*it);
Dees_Troy51a0e822012-09-05 15:24:24 -0400315
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
that3f7b1ac2014-12-30 11:30:13 +0100319int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400320{
that3f7b1ac2014-12-30 11:30:13 +0100321 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400322
that3f7b1ac2014-12-30 11:30:13 +0100323 std::string function = gui_parse_text(action.mFunction);
324 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400325
that3f7b1ac2014-12-30 11:30:13 +0100326 // find function and execute it
327 mapFunc::const_iterator funcitr = mf.find(function);
328 if (funcitr != mf.end())
329 return (this->*funcitr->second)(arg);
330
331 LOGERR("Unknown action '%s'\n", function.c_str());
332 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
335void GUIAction::operation_start(const string operation_name)
336{
that3f7b1ac2014-12-30 11:30:13 +0100337 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000338 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 DataManager::SetValue(TW_ACTION_BUSY, 1);
340 DataManager::SetValue("ui_progress", 0);
341 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400342 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600343 DataManager::SetValue("tw_operation_status", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400344}
345
that3f7b1ac2014-12-30 11:30:13 +0100346void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400347{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000348 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350 DataManager::SetValue("ui_progress", 100);
351 if (simulate) {
352 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
353 if (simulate_fail != 0)
354 DataManager::SetValue("tw_operation_status", 1);
355 else
356 DataManager::SetValue("tw_operation_status", 0);
357 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500358 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500360 }
361 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400362 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500363 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 }
365 DataManager::SetValue("tw_operation_state", 1);
366 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700367#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500368 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700369#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000370 time(&Stop);
371 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600372 DataManager::Vibrate("tw_action_vibrate");
that3f7b1ac2014-12-30 11:30:13 +0100373 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400374}
375
that3f7b1ac2014-12-30 11:30:13 +0100376int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400377{
that3f7b1ac2014-12-30 11:30:13 +0100378 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
that3f7b1ac2014-12-30 11:30:13 +0100380 sync();
381 DataManager::SetValue("tw_gui_done", 1);
382 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400383
that3f7b1ac2014-12-30 11:30:13 +0100384 return 0;
385}
Dees_Troy51a0e822012-09-05 15:24:24 -0400386
that3f7b1ac2014-12-30 11:30:13 +0100387int GUIAction::home(std::string arg)
388{
389 PageManager::SelectPackage("TWRP");
390 gui_changePage("main");
391 return 0;
392}
Dees_Troy51a0e822012-09-05 15:24:24 -0400393
that3f7b1ac2014-12-30 11:30:13 +0100394int GUIAction::key(std::string arg)
395{
396 const int key = getKeyByName(arg);
397 PageManager::NotifyKey(key, true);
398 PageManager::NotifyKey(key, false);
399 return 0;
400}
401
402int GUIAction::page(std::string arg)
403{
404 std::string page_name = gui_parse_text(arg);
405 return gui_changePage(page_name);
406}
407
408int GUIAction::reload(std::string arg)
409{
410 int check = 0, ret_val = 0;
411 std::string theme_path;
412
413 operation_start("Reload Theme");
414 theme_path = DataManager::GetSettingsStoragePath();
415 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
416 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
417 check = 1;
418 }
419
420 theme_path += "/TWRP/theme/ui.zip";
421 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
Dees_Troy6ef66352013-02-21 08:26:57 -0600422 {
that3f7b1ac2014-12-30 11:30:13 +0100423 // Loading the custom theme failed - try loading the stock theme
424 LOGINFO("Attempting to reload stock theme...\n");
425 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 {
that3f7b1ac2014-12-30 11:30:13 +0100427 LOGERR("Failed to load base packages.\n");
428 ret_val = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400429 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400430 }
that3f7b1ac2014-12-30 11:30:13 +0100431 operation_end(ret_val);
432 return 0;
433}
Dees_Troy51a0e822012-09-05 15:24:24 -0400434
that3f7b1ac2014-12-30 11:30:13 +0100435int GUIAction::readBackup(std::string arg)
436{
437 string Restore_Name;
438 DataManager::GetValue("tw_restore", Restore_Name);
439 PartitionManager.Set_Restore_Files(Restore_Name);
440 return 0;
441}
442
443int GUIAction::set(std::string arg)
444{
445 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200446 {
that3f7b1ac2014-12-30 11:30:13 +0100447 string varName = arg.substr(0, arg.find('='));
448 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
that3f7b1ac2014-12-30 11:30:13 +0100450 DataManager::GetValue(value, value);
451 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 }
that3f7b1ac2014-12-30 11:30:13 +0100453 else
454 DataManager::SetValue(arg, "1");
455 return 0;
456}
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
that3f7b1ac2014-12-30 11:30:13 +0100458int GUIAction::clear(std::string arg)
459{
460 DataManager::SetValue(arg, "0");
461 return 0;
462}
Dees_Troy51a0e822012-09-05 15:24:24 -0400463
that3f7b1ac2014-12-30 11:30:13 +0100464int GUIAction::mount(std::string arg)
465{
466 if (arg == "usb") {
467 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100469 PartitionManager.usb_storage_enable();
470 else
471 gui_print("Simulating actions...\n");
472 } else if (!simulate) {
473 PartitionManager.Mount_By_Path(arg, true);
474 PartitionManager.Add_MTP_Storage(arg);
475 } else
476 gui_print("Simulating actions...\n");
477 return 0;
478}
479
480int GUIAction::unmount(std::string arg)
481{
482 if (arg == "usb") {
483 if (!simulate)
484 PartitionManager.usb_storage_disable();
485 else
486 gui_print("Simulating actions...\n");
487 DataManager::SetValue(TW_ACTION_BUSY, 0);
488 } else if (!simulate) {
489 PartitionManager.UnMount_By_Path(arg, true);
490 } else
491 gui_print("Simulating actions...\n");
492 return 0;
493}
494
495int GUIAction::restoredefaultsettings(std::string arg)
496{
497 operation_start("Restore Defaults");
498 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
499 gui_print("Simulating actions...\n");
500 else {
501 DataManager::ResetDefaults();
502 PartitionManager.Update_System_Details();
503 PartitionManager.Mount_Current_Storage(true);
504 }
505 operation_end(0);
506 return 0;
507}
508
509int GUIAction::copylog(std::string arg)
510{
511 operation_start("Copy Log");
512 if (!simulate)
513 {
514 string dst;
515 PartitionManager.Mount_Current_Storage(true);
516 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
517 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
518 tw_set_default_metadata(dst.c_str());
519 sync();
520 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
521 } else
522 simulate_progress_bar();
523 operation_end(0);
524 return 0;
525}
526
527
528int GUIAction::compute(std::string arg)
529{
530 if (arg.find("+") != string::npos)
531 {
532 string varName = arg.substr(0, arg.find('+'));
533 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
534 int amount_to_add = atoi(string_to_add.c_str());
535 int value;
536
537 DataManager::GetValue(varName, value);
538 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 return 0;
540 }
that3f7b1ac2014-12-30 11:30:13 +0100541 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 {
that3f7b1ac2014-12-30 11:30:13 +0100543 string varName = arg.substr(0, arg.find('-'));
544 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
545 int amount_to_subtract = atoi(string_to_subtract.c_str());
546 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400547
that3f7b1ac2014-12-30 11:30:13 +0100548 DataManager::GetValue(varName, value);
549 value -= amount_to_subtract;
550 if (value <= 0)
551 value = 0;
552 DataManager::SetValue(varName, value);
553 return 0;
554 }
555 if (arg.find("*") != string::npos)
556 {
557 string varName = arg.substr(0, arg.find('*'));
558 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
559 int multiply_by = atoi(multiply_by_str.c_str());
560 int value;
561
562 DataManager::GetValue(varName, value);
563 DataManager::SetValue(varName, value*multiply_by);
564 return 0;
565 }
566 if (arg.find("/") != string::npos)
567 {
568 string varName = arg.substr(0, arg.find('/'));
569 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
570 int divide_by = atoi(divide_by_str.c_str());
571 int value;
572
573 if(divide_by != 0)
574 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400575 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100576 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 }
578 return 0;
579 }
that3f7b1ac2014-12-30 11:30:13 +0100580 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
581 return -1;
582}
Dees_Troy51a0e822012-09-05 15:24:24 -0400583
that3f7b1ac2014-12-30 11:30:13 +0100584int GUIAction::setguitimezone(std::string arg)
585{
586 string SelectedZone;
587 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
588 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
589 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
590
591 int dst;
592 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
593
594 string offset;
595 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
596
597 string NewTimeZone = Zone;
598 if (offset != "0")
599 NewTimeZone += ":" + offset;
600
601 if (dst != 0)
602 NewTimeZone += DSTZone;
603
604 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
605 DataManager::update_tz_environment_variables();
606 return 0;
607}
608
609int GUIAction::overlay(std::string arg)
610{
611 return gui_changeOverlay(arg);
612}
613
614int GUIAction::queuezip(std::string arg)
615{
616 if (zip_queue_index >= 10) {
617 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400618 return 0;
619 }
that3f7b1ac2014-12-30 11:30:13 +0100620 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
621 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
622 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100624 }
625 return 0;
626}
627
628int GUIAction::cancelzip(std::string arg)
629{
630 if (zip_queue_index <= 0) {
631 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100633 } else {
634 zip_queue_index--;
635 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400636 }
that3f7b1ac2014-12-30 11:30:13 +0100637 return 0;
638}
Dees_Troy51a0e822012-09-05 15:24:24 -0400639
that3f7b1ac2014-12-30 11:30:13 +0100640int GUIAction::queueclear(std::string arg)
641{
642 zip_queue_index = 0;
643 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
644 return 0;
645}
Dees_Troy51a0e822012-09-05 15:24:24 -0400646
that3f7b1ac2014-12-30 11:30:13 +0100647int GUIAction::sleep(std::string arg)
648{
649 operation_start("Sleep");
650 usleep(atoi(arg.c_str()));
651 operation_end(0);
652 return 0;
653}
Dees Troyb21cc642013-09-10 17:36:41 +0000654
that3f7b1ac2014-12-30 11:30:13 +0100655int GUIAction::appenddatetobackupname(std::string arg)
656{
657 operation_start("AppendDateToBackupName");
658 string Backup_Name;
659 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
660 Backup_Name += TWFunc::Get_Current_Date();
661 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
662 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
663 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
664 operation_end(0);
665 return 0;
666}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500667
that3f7b1ac2014-12-30 11:30:13 +0100668int GUIAction::generatebackupname(std::string arg)
669{
670 operation_start("GenerateBackupName");
671 TWFunc::Auto_Generate_Backup_Name();
672 operation_end(0);
673 return 0;
674}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500675
that3f7b1ac2014-12-30 11:30:13 +0100676int GUIAction::checkpartitionlist(std::string arg)
677{
678 string Wipe_List, wipe_path;
679 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500680
that3f7b1ac2014-12-30 11:30:13 +0100681 DataManager::GetValue("tw_wipe_list", Wipe_List);
682 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
683 if (!Wipe_List.empty()) {
684 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
685 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
686 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
687 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
688 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
689 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 } else {
that3f7b1ac2014-12-30 11:30:13 +0100691 count++;
692 }
693 start_pos = end_pos + 1;
694 end_pos = Wipe_List.find(";", start_pos);
695 }
696 DataManager::SetValue("tw_check_partition_list", count);
697 } else {
698 DataManager::SetValue("tw_check_partition_list", 0);
699 }
700 return 0;
701}
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
that3f7b1ac2014-12-30 11:30:13 +0100703int GUIAction::getpartitiondetails(std::string arg)
704{
705 string Wipe_List, wipe_path;
706 int count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400707
that3f7b1ac2014-12-30 11:30:13 +0100708 DataManager::GetValue("tw_wipe_list", Wipe_List);
709 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
710 if (!Wipe_List.empty()) {
711 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
712 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
713 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
714 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
715 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
716 // Do nothing
717 } else {
718 DataManager::SetValue("tw_partition_path", wipe_path);
719 break;
720 }
721 start_pos = end_pos + 1;
722 end_pos = Wipe_List.find(";", start_pos);
723 }
724 if (!wipe_path.empty()) {
725 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
726 if (Part) {
727 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500728
that3f7b1ac2014-12-30 11:30:13 +0100729 DataManager::SetValue("tw_partition_name", Part->Display_Name);
730 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
731 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
732 DataManager::SetValue("tw_partition_size", Part->Size / mb);
733 DataManager::SetValue("tw_partition_used", Part->Used / mb);
734 DataManager::SetValue("tw_partition_free", Part->Free / mb);
735 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
736 DataManager::SetValue("tw_partition_removable", Part->Removable);
737 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
738
739 if (Part->Can_Repair())
740 DataManager::SetValue("tw_partition_can_repair", 1);
741 else
742 DataManager::SetValue("tw_partition_can_repair", 0);
743 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
744 DataManager::SetValue("tw_partition_vfat", 1);
745 else
746 DataManager::SetValue("tw_partition_vfat", 0);
747 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
748 DataManager::SetValue("tw_partition_exfat", 1);
749 else
750 DataManager::SetValue("tw_partition_exfat", 0);
751 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
752 DataManager::SetValue("tw_partition_f2fs", 1);
753 else
754 DataManager::SetValue("tw_partition_f2fs", 0);
755 if (TWFunc::Path_Exists("/sbin/mke2fs"))
756 DataManager::SetValue("tw_partition_ext", 1);
757 else
758 DataManager::SetValue("tw_partition_ext", 0);
759 return 0;
760 } else {
761 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
762 }
763 }
764 }
765 DataManager::SetValue("tw_partition_name", "");
766 DataManager::SetValue("tw_partition_file_system", "");
767 return 0;
768}
769
770int GUIAction::screenshot(std::string arg)
771{
772 time_t tm;
773 char path[256];
774 int path_len;
775 uid_t uid = -1;
776 gid_t gid = -1;
777
778 struct passwd *pwd = getpwnam("media_rw");
779 if(pwd) {
780 uid = pwd->pw_uid;
781 gid = pwd->pw_gid;
782 }
783
784 const std::string storage = DataManager::GetCurrentStoragePath();
785 if(PartitionManager.Is_Mounted_By_Path(storage)) {
786 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
787 } else {
788 strcpy(path, "/tmp/");
789 }
790
791 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
792 return 0;
793
794 tm = time(NULL);
795 path_len = strlen(path);
796
797 // Screenshot_2014-01-01-18-21-38.png
798 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
799
800 int res = gr_save_screenshot(path);
801 if(res == 0) {
802 chmod(path, 0666);
803 chown(path, uid, gid);
804
805 gui_print("Screenshot was saved to %s\n", path);
806
807 // blink to notify that the screenshow was taken
808 gr_color(255, 255, 255, 255);
809 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
810 gr_flip();
811 gui_forceRender();
812 } else {
813 LOGERR("Failed to take a screenshot!\n");
814 }
815 return 0;
816}
817
818int GUIAction::setbrightness(std::string arg)
819{
820 return TWFunc::Set_Brightness(arg);
821}
822
823int GUIAction::fileexists(std::string arg)
824{
825 struct stat st;
826 string newpath = arg + "/.";
827
828 operation_start("FileExists");
829 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
830 operation_end(0);
831 else
832 operation_end(1);
833 return 0;
834}
835
836int GUIAction::flash(std::string arg)
837{
838 int i, ret_val = 0, wipe_cache = 0;
839 for (i=0; i<zip_queue_index; i++) {
840 operation_start("Flashing");
841 DataManager::SetValue("tw_filename", zip_queue[i]);
842 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
843
844 TWFunc::SetPerformanceMode(true);
845 ret_val = flash_zip(zip_queue[i], arg, &wipe_cache);
846 TWFunc::SetPerformanceMode(false);
847 if (ret_val != 0) {
848 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
849 i = 10; // Error flashing zip - exit queue
850 ret_val = 1;
851 }
852 }
853 zip_queue_index = 0;
854 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
855
856 if (wipe_cache)
857 PartitionManager.Wipe_By_Path("/cache");
858
859 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
860 operation_start("ReinjectTWRP");
861 gui_print("Injecting TWRP into boot image...\n");
862 if (simulate) {
863 simulate_progress_bar();
864 } else {
865 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
866 if (Boot == NULL || Boot->Current_File_System != "emmc")
867 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
868 else {
869 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
870 TWFunc::Exec_Cmd(injectcmd);
871 }
872 gui_print("TWRP injection complete.\n");
873 }
874 }
875 PartitionManager.Update_System_Details();
876 operation_end(ret_val);
877 return 0;
878}
879
880int GUIAction::wipe(std::string arg)
881{
882 operation_start("Format");
883 DataManager::SetValue("tw_partition", arg);
884
885 int ret_val = false;
886
887 if (simulate) {
888 simulate_progress_bar();
889 } else {
890 if (arg == "data")
891 ret_val = PartitionManager.Factory_Reset();
892 else if (arg == "battery")
893 ret_val = PartitionManager.Wipe_Battery_Stats();
894 else if (arg == "rotate")
895 ret_val = PartitionManager.Wipe_Rotate_Data();
896 else if (arg == "dalvik")
897 ret_val = PartitionManager.Wipe_Dalvik_Cache();
898 else if (arg == "DATAMEDIA") {
899 ret_val = PartitionManager.Format_Data();
900 } else if (arg == "INTERNAL") {
901 int has_datamedia, dual_storage;
902
903 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
904 if (has_datamedia) {
905 ret_val = PartitionManager.Wipe_Media_From_Data();
906 } else {
907 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
908 }
909 } else if (arg == "EXTERNAL") {
910 string External_Path;
911
912 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
913 ret_val = PartitionManager.Wipe_By_Path(External_Path);
914 } else if (arg == "ANDROIDSECURE") {
915 ret_val = PartitionManager.Wipe_Android_Secure();
916 } else if (arg == "LIST") {
917 string Wipe_List, wipe_path;
918 bool skip = false;
919 ret_val = true;
920 TWPartition* wipe_part = NULL;
921
922 DataManager::GetValue("tw_wipe_list", Wipe_List);
923 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
924 if (!Wipe_List.empty()) {
925 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
926 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
927 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
928 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
929 if (wipe_path == "/and-sec") {
930 if (!PartitionManager.Wipe_Android_Secure()) {
931 LOGERR("Unable to wipe android secure\n");
932 ret_val = false;
933 break;
934 } else {
935 skip = true;
936 }
937 } else if (wipe_path == "DALVIK") {
938 if (!PartitionManager.Wipe_Dalvik_Cache()) {
939 LOGERR("Failed to wipe dalvik\n");
940 ret_val = false;
941 break;
942 } else {
943 skip = true;
944 }
945 } else if (wipe_path == "INTERNAL") {
946 if (!PartitionManager.Wipe_Media_From_Data()) {
947 ret_val = false;
948 break;
949 } else {
950 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500951 }
952 }
that3f7b1ac2014-12-30 11:30:13 +0100953 if (!skip) {
954 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
955 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
956 ret_val = false;
957 break;
958 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
959 arg = wipe_path;
960 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400961 } else {
that3f7b1ac2014-12-30 11:30:13 +0100962 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400963 }
that3f7b1ac2014-12-30 11:30:13 +0100964 start_pos = end_pos + 1;
965 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400966 }
967 }
that3f7b1ac2014-12-30 11:30:13 +0100968 } else
969 ret_val = PartitionManager.Wipe_By_Path(arg);
970#ifdef TW_OEM_BUILD
971 if (arg == DataManager::GetSettingsStoragePath()) {
972 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
973 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -0400974
that3f7b1ac2014-12-30 11:30:13 +0100975 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
976 LOGINFO("Making TWRP folder and saving settings.\n");
977 Storage_Path += "/TWRP";
978 mkdir(Storage_Path.c_str(), 0777);
979 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -0400980 } else {
that3f7b1ac2014-12-30 11:30:13 +0100981 LOGERR("Unable to recreate TWRP folder and save settings.\n");
982 }
983 }
984#endif
985 }
986 PartitionManager.Update_System_Details();
987 if (ret_val)
988 ret_val = 0; // 0 is success
989 else
990 ret_val = 1; // 1 is failure
991 operation_end(ret_val);
992 return 0;
993}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000994
that3f7b1ac2014-12-30 11:30:13 +0100995int GUIAction::refreshsizes(std::string arg)
996{
997 operation_start("Refreshing Sizes");
998 if (simulate) {
999 simulate_progress_bar();
1000 } else
1001 PartitionManager.Update_System_Details();
1002 operation_end(0);
1003 return 0;
1004}
1005
1006int GUIAction::nandroid(std::string arg)
1007{
1008 operation_start("Nandroid");
1009 int ret = 0;
1010
1011 if (simulate) {
1012 DataManager::SetValue("tw_partition", "Simulation");
1013 simulate_progress_bar();
1014 } else {
1015 if (arg == "backup") {
1016 string Backup_Name;
1017 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1018 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
1019 ret = PartitionManager.Run_Backup();
1020 }
1021 else {
1022 operation_end(1);
1023 return -1;
1024
1025 }
1026 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
1027 } else if (arg == "restore") {
1028 string Restore_Name;
1029 DataManager::GetValue("tw_restore", Restore_Name);
1030 ret = PartitionManager.Run_Restore(Restore_Name);
1031 } else {
1032 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 return -1;
1034 }
1035 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001036 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001037 if (ret == false)
1038 ret = 1; // 1 for failure
1039 else
1040 ret = 0; // 0 for success
that3f7b1ac2014-12-30 11:30:13 +01001041 operation_end(ret);
Dees_Troy83bd4832013-05-04 12:39:56 +00001042 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001043}
Dees_Troy51a0e822012-09-05 15:24:24 -04001044
that3f7b1ac2014-12-30 11:30:13 +01001045int GUIAction::fixpermissions(std::string arg)
1046{
1047 operation_start("Fix Permissions");
1048 LOGINFO("fix permissions started!\n");
1049 if (simulate) {
1050 simulate_progress_bar();
1051 } else {
1052 int op_status = PartitionManager.Fix_Permissions();
1053 if (op_status != 0)
1054 op_status = 1; // failure
1055 operation_end(op_status);
1056 }
1057 return 0;
1058}
1059
1060int GUIAction::dd(std::string arg)
1061{
1062 operation_start("imaging");
1063
1064 if (simulate) {
1065 simulate_progress_bar();
1066 } else {
1067 string cmd = "dd " + arg;
1068 TWFunc::Exec_Cmd(cmd);
1069 }
1070 operation_end(0);
1071 return 0;
1072}
1073
1074int GUIAction::partitionsd(std::string arg)
1075{
1076 operation_start("Partition SD Card");
1077 int ret_val = 0;
1078
1079 if (simulate) {
1080 simulate_progress_bar();
1081 } else {
1082 int allow_partition;
1083 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1084 if (allow_partition == 0) {
1085 gui_print("This device does not have a real SD Card!\nAborting!\n");
1086 } else {
1087 if (!PartitionManager.Partition_SDCard())
1088 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001089 }
that3f7b1ac2014-12-30 11:30:13 +01001090 }
1091 operation_end(ret_val);
1092 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093
that3f7b1ac2014-12-30 11:30:13 +01001094}
Dees_Troy51a0e822012-09-05 15:24:24 -04001095
that3f7b1ac2014-12-30 11:30:13 +01001096int GUIAction::installhtcdumlock(std::string arg)
1097{
1098 operation_start("Install HTC Dumlock");
1099 if (simulate) {
1100 simulate_progress_bar();
1101 } else
1102 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001103
that3f7b1ac2014-12-30 11:30:13 +01001104 operation_end(0);
1105 return 0;
1106}
Dees_Troy51a0e822012-09-05 15:24:24 -04001107
that3f7b1ac2014-12-30 11:30:13 +01001108int GUIAction::htcdumlockrestoreboot(std::string arg)
1109{
1110 operation_start("HTC Dumlock Restore Boot");
1111 if (simulate) {
1112 simulate_progress_bar();
1113 } else
1114 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001115
that3f7b1ac2014-12-30 11:30:13 +01001116 operation_end(0);
1117 return 0;
1118}
Dees_Troy51a0e822012-09-05 15:24:24 -04001119
that3f7b1ac2014-12-30 11:30:13 +01001120int GUIAction::htcdumlockreflashrecovery(std::string arg)
1121{
1122 operation_start("HTC Dumlock Reflash Recovery");
1123 if (simulate) {
1124 simulate_progress_bar();
1125 } else
1126 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001127
that3f7b1ac2014-12-30 11:30:13 +01001128 operation_end(0);
1129 return 0;
1130}
Dees_Troy51a0e822012-09-05 15:24:24 -04001131
that3f7b1ac2014-12-30 11:30:13 +01001132int GUIAction::cmd(std::string arg)
1133{
1134 int op_status = 0;
1135
1136 operation_start("Command");
1137 LOGINFO("Running command: '%s'\n", arg.c_str());
1138 if (simulate) {
1139 simulate_progress_bar();
1140 } else {
1141 op_status = TWFunc::Exec_Cmd(arg);
1142 if (op_status != 0)
1143 op_status = 1;
1144 }
1145
1146 operation_end(op_status);
1147 return 0;
1148}
1149
1150int GUIAction::terminalcommand(std::string arg)
1151{
1152 int op_status = 0;
1153 string cmdpath, command;
1154
1155 DataManager::GetValue("tw_terminal_location", cmdpath);
1156 operation_start("CommandOutput");
1157 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1158 if (simulate) {
1159 simulate_progress_bar();
1160 operation_end(op_status);
1161 } else {
1162 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1163 LOGINFO("Actual command is: '%s'\n", command.c_str());
1164 DataManager::SetValue("tw_terminal_command_thread", command);
1165 DataManager::SetValue("tw_terminal_state", 1);
1166 DataManager::SetValue("tw_background_thread_running", 1);
1167 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1168 if (op_status != 0) {
1169 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001170 DataManager::SetValue("tw_terminal_state", 0);
1171 DataManager::SetValue("tw_background_thread_running", 0);
that3f7b1ac2014-12-30 11:30:13 +01001172 operation_end(1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001173 }
that3f7b1ac2014-12-30 11:30:13 +01001174 }
1175 return 0;
1176}
1177
1178int GUIAction::killterminal(std::string arg)
1179{
1180 int op_status = 0;
1181
1182 LOGINFO("Sending kill command...\n");
1183 operation_start("KillCommand");
1184 DataManager::SetValue("tw_operation_status", 0);
1185 DataManager::SetValue("tw_operation_state", 1);
1186 DataManager::SetValue("tw_terminal_state", 0);
1187 DataManager::SetValue("tw_background_thread_running", 0);
1188 DataManager::SetValue(TW_ACTION_BUSY, 0);
1189 return 0;
1190}
1191
1192int GUIAction::reinjecttwrp(std::string arg)
1193{
1194 int op_status = 0;
1195 operation_start("ReinjectTWRP");
1196 gui_print("Injecting TWRP into boot image...\n");
1197 if (simulate) {
1198 simulate_progress_bar();
1199 } else {
1200 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1201 gui_print("TWRP injection complete.\n");
1202 }
1203
1204 operation_end(op_status);
1205 return 0;
1206}
1207
1208int GUIAction::checkbackupname(std::string arg)
1209{
1210 int op_status = 0;
1211
1212 operation_start("CheckBackupName");
1213 if (simulate) {
1214 simulate_progress_bar();
1215 } else {
1216 op_status = PartitionManager.Check_Backup_Name(true);
1217 if (op_status != 0)
1218 op_status = 1;
1219 }
1220
1221 operation_end(op_status);
1222 return 0;
1223}
1224
1225int GUIAction::decrypt(std::string arg)
1226{
1227 int op_status = 0;
1228
1229 operation_start("Decrypt");
1230 if (simulate) {
1231 simulate_progress_bar();
1232 } else {
1233 string Password;
1234 DataManager::GetValue("tw_crypto_password", Password);
1235 op_status = PartitionManager.Decrypt_Device(Password);
1236 if (op_status != 0)
1237 op_status = 1;
1238 else {
1239 int load_theme = 1;
1240
1241 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1242
1243 if (load_theme) {
1244 int has_datamedia;
1245
1246 // Check for a custom theme and load it if exists
1247 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1248 if (has_datamedia != 0) {
1249 struct stat st;
1250 int check = 0;
1251 std::string theme_path;
1252
1253 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1254 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1255 } else {
1256 LOGINFO("Got default contexts and file mode for storage files.\n");
1257 }
1258
1259 theme_path = DataManager::GetSettingsStoragePath();
1260 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
1261 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1262 check = 1;
1263 }
1264
1265 theme_path += "/TWRP/theme/ui.zip";
1266 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1267 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1268 {
1269 // Loading the custom theme failed - try loading the stock theme
1270 LOGINFO("Attempting to reload stock theme...\n");
1271 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1272 {
1273 LOGERR("Failed to load base packages.\n");
1274 }
1275 }
1276 }
1277 }
1278 }
1279 }
1280 }
1281
1282 operation_end(op_status);
1283 return 0;
1284}
1285
1286int GUIAction::adbsideload(std::string arg)
1287{
1288 int ret = 0;
1289
1290 operation_start("Sideload");
1291 if (simulate) {
1292 simulate_progress_bar();
1293 } else {
1294 int wipe_cache = 0;
1295 int wipe_dalvik = 0;
1296
1297 gui_print("Starting ADB sideload feature...\n");
1298 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1299 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1300 ret = apply_from_adb("/");
1301 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1302 char file_prop[PROPERTY_VALUE_MAX];
1303 property_get("tw_sideload_file", file_prop, "error");
1304 if (ret != 0) {
1305 ret = 1; // failure
1306 if (ret == -2)
1307 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
1308 } else {
1309 if (TWinstall_zip(file_prop, &wipe_cache) == 0) {
1310 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1311 PartitionManager.Wipe_By_Path("/cache");
1312 if (wipe_dalvik)
1313 PartitionManager.Wipe_Dalvik_Cache();
1314 } else {
1315 ret = 1; // failure
1316 }
1317 set_usb_driver(false);
1318 maybe_restart_adbd();
1319 }
1320 TWFunc::Toggle_MTP(mtp_was_enabled);
1321 if (strcmp(file_prop, "error") != 0) {
1322 struct stat st;
1323 stat("/sideload/exit", &st);
1324 int child_pid, status;
1325 char child_prop[PROPERTY_VALUE_MAX];
1326 property_get("tw_child_pid", child_prop, "error");
1327 if (strcmp(child_prop, "error") == 0) {
1328 LOGERR("Unable to get child ID from prop\n");
1329 } else {
1330 child_pid = atoi(child_prop);
1331 LOGINFO("Waiting for child sideload process to exit.\n");
1332 waitpid(child_pid, &status, 0);
1333 }
1334 }
1335 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001336 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001337 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001338 if (simulate) {
1339 simulate_progress_bar();
1340 } else {
that3f7b1ac2014-12-30 11:30:13 +01001341 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1342 if (Boot == NULL || Boot->Current_File_System != "emmc")
1343 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1344 else {
1345 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1346 TWFunc::Exec_Cmd(injectcmd);
1347 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001348 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001349 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001350 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001351 }
that3f7b1ac2014-12-30 11:30:13 +01001352 operation_end(ret);
1353 return 0;
1354}
1355
1356int GUIAction::adbsideloadcancel(std::string arg)
1357{
1358 int child_pid;
1359 char child_prop[PROPERTY_VALUE_MAX];
1360 struct stat st;
1361 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1362 gui_print("Cancelling ADB sideload...\n");
1363 stat("/sideload/exit", &st);
1364 ::sleep(1);
1365 property_get("tw_child_pid", child_prop, "error");
1366 if (strcmp(child_prop, "error") == 0) {
1367 LOGERR("Unable to get child ID from prop\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001368 return 0;
1369 }
that3f7b1ac2014-12-30 11:30:13 +01001370 child_pid = atoi(child_prop);
1371 kill(child_pid, SIGTERM);
1372 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1373 return 0;
1374}
1375
1376int GUIAction::openrecoveryscript(std::string arg)
1377{
1378 operation_start("OpenRecoveryScript");
1379 if (simulate) {
1380 simulate_progress_bar();
1381 } else {
1382 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1383 // that we converted to ORS commands during boot in recovery.cpp.
1384 // Run those first.
1385 int reboot = 0;
1386 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1387 gui_print("Processing AOSP recovery commands...\n");
1388 if (OpenRecoveryScript::run_script_file() == 0) {
1389 reboot = 1;
1390 }
1391 }
1392 // Check for the ORS file in /cache and attempt to run those commands.
1393 if (OpenRecoveryScript::check_for_script_file()) {
1394 gui_print("Processing OpenRecoveryScript file...\n");
1395 if (OpenRecoveryScript::run_script_file() == 0) {
1396 reboot = 1;
1397 }
1398 }
1399 if (reboot) {
1400 usleep(2000000); // Sleep for 2 seconds before rebooting
1401 TWFunc::tw_reboot(rb_system);
1402 } else {
1403 DataManager::SetValue("tw_page_done", 1);
1404 }
1405 }
1406 return 0;
1407}
1408
1409int GUIAction::installsu(std::string arg)
1410{
1411 int op_status = 0;
1412
1413 operation_start("Install SuperSU");
1414 if (simulate) {
1415 simulate_progress_bar();
1416 } else {
1417 if (!TWFunc::Install_SuperSU())
1418 op_status = 1;
1419 }
1420
1421 operation_end(op_status);
1422 return 0;
1423}
1424
1425int GUIAction::fixsu(std::string arg)
1426{
1427 int op_status = 0;
1428
1429 operation_start("Fixing Superuser Permissions");
1430 if (simulate) {
1431 simulate_progress_bar();
1432 } else {
1433 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1434 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1435 }
1436
1437 operation_end(op_status);
1438 return 0;
1439}
1440
1441int GUIAction::decrypt_backup(std::string arg)
1442{
1443 int op_status = 0;
1444
1445 operation_start("Try Restore Decrypt");
1446 if (simulate) {
1447 simulate_progress_bar();
1448 } else {
1449 string Restore_Path, Filename, Password;
1450 DataManager::GetValue("tw_restore", Restore_Path);
1451 Restore_Path += "/";
1452 DataManager::GetValue("tw_restore_password", Password);
1453 TWFunc::SetPerformanceMode(true);
1454 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1455 op_status = 0; // success
1456 else
1457 op_status = 1; // fail
1458 TWFunc::SetPerformanceMode(false);
1459 }
1460
1461 operation_end(op_status);
1462 return 0;
1463}
1464
1465int GUIAction::repair(std::string arg)
1466{
1467 int op_status = 0;
1468
1469 operation_start("Repair Partition");
1470 if (simulate) {
1471 simulate_progress_bar();
1472 } else {
1473 string part_path;
1474 DataManager::GetValue("tw_partition_mount_point", part_path);
1475 if (PartitionManager.Repair_By_Path(part_path, true)) {
1476 op_status = 0; // success
1477 } else {
1478 LOGERR("Error repairing file system.\n");
1479 op_status = 1; // fail
1480 }
1481 }
1482
1483 operation_end(op_status);
1484 return 0;
1485}
1486
1487int GUIAction::changefilesystem(std::string arg)
1488{
1489 int op_status = 0;
1490
1491 operation_start("Change File System");
1492 if (simulate) {
1493 simulate_progress_bar();
1494 } else {
1495 string part_path, file_system;
1496 DataManager::GetValue("tw_partition_mount_point", part_path);
1497 DataManager::GetValue("tw_action_new_file_system", file_system);
1498 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1499 op_status = 0; // success
1500 } else {
1501 LOGERR("Error changing file system.\n");
1502 op_status = 1; // fail
1503 }
1504 }
1505 PartitionManager.Update_System_Details();
1506 operation_end(op_status);
1507 return 0;
1508}
1509
1510int GUIAction::startmtp(std::string arg)
1511{
1512 int op_status = 0;
1513
1514 operation_start("Start MTP");
1515 if (PartitionManager.Enable_MTP())
1516 op_status = 0; // success
1517 else
1518 op_status = 1; // fail
1519
1520 operation_end(op_status);
1521 return 0;
1522}
1523
1524int GUIAction::stopmtp(std::string arg)
1525{
1526 int op_status = 0;
1527
1528 operation_start("Stop MTP");
1529 if (PartitionManager.Disable_MTP())
1530 op_status = 0; // success
1531 else
1532 op_status = 1; // fail
1533
1534 operation_end(op_status);
1535 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001536}
1537
1538int GUIAction::getKeyByName(std::string key)
1539{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001540 if (key == "home") return KEY_HOME;
1541 else if (key == "menu") return KEY_MENU;
1542 else if (key == "back") return KEY_BACK;
1543 else if (key == "search") return KEY_SEARCH;
1544 else if (key == "voldown") return KEY_VOLUMEDOWN;
1545 else if (key == "volup") return KEY_VOLUMEUP;
1546 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001547 int ret_val;
1548 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1549 if (!ret_val)
1550 return KEY_POWER;
1551 else
1552 return ret_val;
1553 }
1554
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001555 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001556}
1557
1558void* GUIAction::command_thread(void *cookie)
1559{
1560 string command;
1561 FILE* fp;
1562 char line[512];
1563
1564 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001565 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001566 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001567 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001568 } else {
1569 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1570 struct timeval timeout;
1571 fd_set fdset;
1572
1573 while(keep_going)
1574 {
1575 FD_ZERO(&fdset);
1576 FD_SET(fd, &fdset);
1577 timeout.tv_sec = 0;
1578 timeout.tv_usec = 400000;
1579 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1580 if (has_data == 0) {
1581 // Timeout reached
1582 DataManager::GetValue("tw_terminal_state", check);
1583 if (check == 0) {
1584 keep_going = 0;
1585 }
1586 } else if (has_data < 0) {
1587 // End of execution
1588 keep_going = 0;
1589 } else {
1590 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001591 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001592 bytes_read = read(fd, line, sizeof(line));
1593 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001595 else
1596 keep_going = 0; // Done executing
1597 }
1598 }
1599 fclose(fp);
1600 }
1601 DataManager::SetValue("tw_operation_status", 0);
1602 DataManager::SetValue("tw_operation_state", 1);
1603 DataManager::SetValue("tw_terminal_state", 0);
1604 DataManager::SetValue("tw_background_thread_running", 0);
1605 DataManager::SetValue(TW_ACTION_BUSY, 0);
1606 return NULL;
1607}