Move all AOSP code out of recovery binary

Improves license compatibility between GPL and Apache

Change-Id: I2b165aa575bb6213af6b07936f99610c113443f0
diff --git a/gui/action.cpp b/gui/action.cpp
index 3456324..b35074c 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -38,17 +38,15 @@
 #include "../twrp-functions.hpp"
 #include "../openrecoveryscript.hpp"
 
-#include "../ui.h"
 #include "../adb_install.h"
 #include "blanktimer.hpp"
 
 extern "C" {
-#include "../common.h"
+#include "../twcommon.h"
 #include "../minuitwrp/minui.h"
-#include "../recovery_ui.h"
 #include "../variables.h"
 #include "../twinstall.h"
-
+#include "cutils/properties.h"
 #include "../minadbd/adb.h"
 
 int TWinstall_zip(const char* path, int* wipe_cache);
@@ -60,7 +58,6 @@
 #include "rapidxml.hpp"
 #include "objects.hpp"
 
-extern RecoveryUI* ui;
 extern blanktimer blankTimer;
 
 void curtainClose(void);
@@ -160,7 +157,7 @@
 
 void GUIAction::simulate_progress_bar(void)
 {
-	ui_print("Simulating actions...\n");
+	gui_print("Simulating actions...\n");
 	for (int i = 0; i < 5; i++)
 	{
 		usleep(500000);
@@ -176,7 +173,7 @@
 
     if (filename.empty())
     {
-        LOGE("No file specified.\n");
+        LOGERR("No file specified.\n");
         return -1;
     }
 
@@ -191,7 +188,7 @@
 
 	if (mzOpenZipArchive(filename.c_str(), &zip))
     {
-        LOGE("Unable to open zip file.\n");
+        LOGERR("Unable to open zip file.\n");
         return -1;
     }
 
@@ -231,10 +228,10 @@
 		{
 			DataManager::SetValue("tw_operation", "Configuring TWRP");
 			DataManager::SetValue("tw_partition", "");
-			ui_print("Configuring TWRP...\n");
+			gui_print("Configuring TWRP...\n");
 			if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
 			{
-				ui_print("Unable to configure TWRP with this kernel.\n");
+				gui_print("Unable to configure TWRP with this kernel.\n");
 			}
 		}
 	}
@@ -256,33 +253,33 @@
 	pthread_attr_t tattr;
 
 	if (pthread_attr_init(&tattr)) {
-		LOGE("Unable to pthread_attr_init\n");
+		LOGERR("Unable to pthread_attr_init\n");
 		return -1;
 	}
 	if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
-		LOGE("Error setting pthread_attr_setdetachstate\n");
+		LOGERR("Error setting pthread_attr_setdetachstate\n");
 		return -1;
 	}
 	if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
-		LOGE("Error setting pthread_attr_setscope\n");
+		LOGERR("Error setting pthread_attr_setscope\n");
 		return -1;
 	}
 	/*if (pthread_attr_setstacksize(&tattr, 524288)) {
-		LOGE("Error setting pthread_attr_setstacksize\n");
+		LOGERR("Error setting pthread_attr_setstacksize\n");
 		return -1;
 	}
 	*/
 	int ret = pthread_create(&t, &tattr, thread_start, this);
     if (ret) {
-		LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
+		LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
 		thread_start(this);
 	} else {
 		if (pthread_join(t, NULL)) {
-			LOGE("Error joining threads\n");
+			LOGERR("Error joining threads\n");
 		}
 	}
 	if (pthread_attr_destroy(&tattr)) {
-		LOGE("Failed to pthread_attr_destroy\n");
+		LOGERR("Failed to pthread_attr_destroy\n");
 		return -1;
 	}
 
@@ -392,7 +389,7 @@
 		operation_start("Reload Theme");
 		theme_path = DataManager::GetSettingsStoragePath();
 		if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
-			LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
+			LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
 			check = 1;
 		}
 
@@ -400,10 +397,10 @@
 		if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
 		{
 			// Loading the custom theme failed - try loading the stock theme
-			LOGI("Attempting to reload stock theme...\n");
+			LOGINFO("Attempting to reload stock theme...\n");
 			if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
 			{
-				LOGE("Failed to load base packages.\n");
+				LOGERR("Failed to load base packages.\n");
 				ret_val = 1;
 			}
 		}
@@ -446,7 +443,7 @@
 			if (!simulate)
 				PartitionManager.usb_storage_enable();
 			else
-				ui_print("Simulating actions...\n");
+				gui_print("Simulating actions...\n");
         }
         else if (!simulate)
         {
@@ -458,7 +455,7 @@
 			else
 				PartitionManager.Mount_By_Path(arg, true);
         } else
-			ui_print("Simulating actions...\n");
+			gui_print("Simulating actions...\n");
         return 0;
     }
 
@@ -469,7 +466,7 @@
             if (!simulate)
 				PartitionManager.usb_storage_disable();
 			else
-				ui_print("Simulating actions...\n");
+				gui_print("Simulating actions...\n");
 			DataManager::SetValue(TW_ACTION_BUSY, 0);
         }
         else if (!simulate)
@@ -482,7 +479,7 @@
 			else
 				PartitionManager.UnMount_By_Path(arg, true);
         } else
-			ui_print("Simulating actions...\n");
+			gui_print("Simulating actions...\n");
         return 0;
     }
 	
@@ -490,7 +487,7 @@
 	{
 		operation_start("Restore Defaults");
 		if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
-			ui_print("Simulating actions...\n");
+			gui_print("Simulating actions...\n");
 		else {
 			DataManager::ResetDefaults();
 			PartitionManager.Update_System_Details();
@@ -509,7 +506,7 @@
 			dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
 			TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
 			sync();
-			ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
+			gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
 		} else
 			simulate_progress_bar();
 		operation_end(0, simulate);
@@ -631,7 +628,7 @@
 	if (function == "queuezip")
     {
         if (zip_queue_index >= 10) {
-			ui_print("Maximum zip queue reached!\n");
+			gui_print("Maximum zip queue reached!\n");
 			return 0;
 		}
 		DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
@@ -645,7 +642,7 @@
 	if (function == "cancelzip")
     {
         if (zip_queue_index <= 0) {
-			ui_print("Minimum zip queue reached!\n");
+			gui_print("Minimum zip queue reached!\n");
 			return 0;
 		} else {
 			zip_queue_index--;
@@ -694,7 +691,7 @@
 
 				ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
 				if (ret_val != 0) {
-					ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
+					gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
 					i = 10; // Error flashing zip - exit queue
 					ret_val = 1;
 				}
@@ -707,7 +704,7 @@
 			string result;
 			if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
 				operation_start("ReinjectTWRP");
-				ui_print("Injecting TWRP into boot image...\n");
+				gui_print("Injecting TWRP into boot image...\n");
 				if (simulate) {
 					simulate_progress_bar();
 				} else {
@@ -718,7 +715,7 @@
 						string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
 						TWFunc::Exec_Cmd(injectcmd, result);
 					}
-					ui_print("TWRP injection complete.\n");
+					gui_print("TWRP injection complete.\n");
 				}
 			}
 			PartitionManager.Update_System_Details();
@@ -768,15 +765,15 @@
 					TWPartition* wipe_part = NULL;
 
 					DataManager::GetValue("tw_wipe_list", Wipe_List);
-					LOGI("wipe list '%s'\n", Wipe_List.c_str());
+					LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
 					if (!Wipe_List.empty()) {
 						size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
 						while (end_pos != string::npos && start_pos < Wipe_List.size()) {
 							wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
-							LOGI("wipe_path '%s'\n", wipe_path.c_str());
+							LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
 							if (wipe_path == "/and-sec") {
 								if (!PartitionManager.Wipe_Android_Secure()) {
-									LOGE("Unable to wipe android secure\n");
+									LOGERR("Unable to wipe android secure\n");
 									ret_val = false;
 									break;
 								} else {
@@ -784,7 +781,7 @@
 								}
 							} else if (wipe_path == "DALVIK") {
 								if (!PartitionManager.Wipe_Dalvik_Cache()) {
-									LOGE("Failed to wipe dalvik\n");
+									LOGERR("Failed to wipe dalvik\n");
 									ret_val = false;
 									break;
 								} else {
@@ -793,7 +790,7 @@
 							}
 							if (!skip) {
 								if (!PartitionManager.Wipe_By_Path(wipe_path)) {
-									LOGE("Unable to wipe '%s'\n", wipe_path.c_str());
+									LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
 									ret_val = false;
 									break;
 								} else if (wipe_path == DataManager::GetSettingsStoragePath()) {
@@ -814,12 +811,12 @@
 					string Storage_Path = DataManager::GetSettingsStoragePath();
 
 					if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
-						LOGI("Making TWRP folder and saving settings.\n");
+						LOGINFO("Making TWRP folder and saving settings.\n");
 						Storage_Path += "/TWRP";
 						mkdir(Storage_Path.c_str(), 0777);
 						DataManager::Flush();
 					} else {
-						LOGE("Unable to recreate TWRP folder and save settings.\n");
+						LOGERR("Unable to recreate TWRP folder and save settings.\n");
 					}
 				}
 			}
@@ -879,7 +876,7 @@
 		if (function == "fixpermissions")
 		{
 			operation_start("Fix Permissions");
-            LOGI("fix permissions started!\n");
+            LOGINFO("fix permissions started!\n");
 			if (simulate) {
 				simulate_progress_bar();
 			} else {
@@ -915,7 +912,7 @@
 				int allow_partition;
 				DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
 				if (allow_partition == 0) {
-					ui_print("This device does not have a real SD Card!\nAborting!\n");
+					gui_print("This device does not have a real SD Card!\nAborting!\n");
 				} else {
 					if (!PartitionManager.Partition_SDCard())
 						ret_val = 1; // failed
@@ -963,7 +960,7 @@
 			string result;
 
 			operation_start("Command");
-			LOGI("Running command: '%s'\n", arg.c_str());
+			LOGINFO("Running command: '%s'\n", arg.c_str());
 			if (simulate) {
 				simulate_progress_bar();
 			} else {
@@ -982,19 +979,19 @@
 
 			DataManager::GetValue("tw_terminal_location", cmdpath);
 			operation_start("CommandOutput");
-			ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
+			gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
 			if (simulate) {
 				simulate_progress_bar();
 				operation_end(op_status, simulate);
 			} else {
 				command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
-				LOGI("Actual command is: '%s'\n", command.c_str());
+				LOGINFO("Actual command is: '%s'\n", command.c_str());
 				DataManager::SetValue("tw_terminal_command_thread", command);
 				DataManager::SetValue("tw_terminal_state", 1);
 				DataManager::SetValue("tw_background_thread_running", 1);
 				op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
 				if (op_status != 0) {
-					LOGE("Error starting terminal command thread, %i.\n", op_status);
+					LOGERR("Error starting terminal command thread, %i.\n", op_status);
 					DataManager::SetValue("tw_terminal_state", 0);
 					DataManager::SetValue("tw_background_thread_running", 0);
 					operation_end(1, simulate);
@@ -1006,7 +1003,7 @@
 		{
 			int op_status = 0;
 
-			LOGI("Sending kill command...\n");
+			LOGINFO("Sending kill command...\n");
 			operation_start("KillCommand");
 			DataManager::SetValue("tw_operation_status", 0);
 			DataManager::SetValue("tw_operation_state", 1);
@@ -1020,12 +1017,12 @@
 			int op_status = 0;
 			string result;
 			operation_start("ReinjectTWRP");
-			ui_print("Injecting TWRP into boot image...\n");
+			gui_print("Injecting TWRP into boot image...\n");
 			if (simulate) {
 				simulate_progress_bar();
 			} else {
 				TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
-				ui_print("TWRP injection complete.\n");
+				gui_print("TWRP injection complete.\n");
 			}
 
 			operation_end(op_status, simulate);
@@ -1077,7 +1074,7 @@
 
 							theme_path = DataManager::GetSettingsStoragePath();
 							if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
-								LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
+								LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
 								check = 1;
 							}
 
@@ -1086,10 +1083,10 @@
 								if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
 								{
 									// Loading the custom theme failed - try loading the stock theme
-									LOGI("Attempting to reload stock theme...\n");
+									LOGINFO("Attempting to reload stock theme...\n");
 									if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
 									{
-										LOGE("Failed to load base packages.\n");
+										LOGERR("Failed to load base packages.\n");
 									}
 								}
 							}
@@ -1121,20 +1118,23 @@
 				if (TWFunc::Path_Exists(Sideload_File)) {
 					unlink(Sideload_File.c_str());
 				}
-				ui_print("Starting ADB sideload feature...\n");
+				gui_print("Starting ADB sideload feature...\n");
 				DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
-				ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
+				ret = apply_from_adb(Sideload_File.c_str());
+				DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
 				if (ret != 0) {
 					ret = 1; // failure
-				} else {
+				} else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
 					if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
 						PartitionManager.Wipe_By_Path("/cache");
 					if (wipe_dalvik)
 						PartitionManager.Wipe_Dalvik_Cache();
+				} else {
+					ret = 1; // failure
 				}
 				if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
 					operation_start("ReinjectTWRP");
-					ui_print("Injecting TWRP into boot image...\n");
+					gui_print("Injecting TWRP into boot image...\n");
 					if (simulate) {
 						simulate_progress_bar();
 					} else {
@@ -1145,7 +1145,7 @@
 							string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
 							TWFunc::Exec_Cmd(injectcmd, result);
 						}
-						ui_print("TWRP injection complete.\n");
+						gui_print("TWRP injection complete.\n");
 					}
 				}
 			}
@@ -1155,11 +1155,17 @@
 		if (function == "adbsideloadcancel")
 		{
 			int child_pid;
+			char child_prop[PROPERTY_VALUE_MAX];
 			string Sideload_File;
 			Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
 			unlink(Sideload_File.c_str());
-			DataManager::GetValue("tw_child_pid", child_pid);
-			ui_print("Cancelling ADB sideload...\n");
+			property_get("tw_child_pid", child_prop, "error");
+			if (strcmp(child_prop, "error") == 0) {
+				LOGERR("Unable to get child ID from prop\n");
+				return 0;
+			}
+			child_pid = atoi(child_prop);
+			gui_print("Cancelling ADB sideload...\n");
 			kill(child_pid, SIGTERM);
 			DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
 			return 0;
@@ -1174,14 +1180,14 @@
 				// Run those first.
 				int reboot = 0;
 				if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
-					ui_print("Processing AOSP recovery commands...\n");
+					gui_print("Processing AOSP recovery commands...\n");
 					if (OpenRecoveryScript::run_script_file() == 0) {
 						reboot = 1;
 					}
 				}
 				// Check for the ORS file in /cache and attempt to run those commands.
 				if (OpenRecoveryScript::check_for_script_file()) {
-					ui_print("Processing OpenRecoveryScript file...\n");
+					gui_print("Processing OpenRecoveryScript file...\n");
 					if (OpenRecoveryScript::run_script_file() == 0) {
 						reboot = 1;
 					}
@@ -1263,7 +1269,7 @@
 	DataManager::GetValue("tw_terminal_command_thread", command);
 	fp = popen(command.c_str(), "r");
 	if (fp == NULL) {
-		LOGE("Error opening command to run.\n");
+		LOGERR("Error opening command to run.\n");
 	} else {
 		int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
 		struct timeval timeout;
@@ -1290,7 +1296,7 @@
 				memset(line, 0, sizeof(line));
 				bytes_read = read(fd, line, sizeof(line));
 				if (bytes_read > 0)
-					ui_print("%s", line); // Display output
+					gui_print("%s", line); // Display output
 				else
 					keep_going = 0; // Done executing
 			}