Improve ORS sideload to display GUI with cancel button
diff --git a/gui/action.cpp b/gui/action.cpp
index 1f3647d..14ef716 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -253,9 +253,8 @@
 		LOGE("Error setting pthread_attr_setstacksize\n");
 		return -1;
 	}
-	LOGI("creating thread\n");
+	LOGI("Creating thread\n");
 	int ret = pthread_create(&t, &tattr, thread_start, this);
-	LOGI("after thread creation\n");
     if (ret) {
 		LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
 		thread_start(this);
@@ -1071,6 +1070,7 @@
 				string result, Sideload_File;
 
 				if (!PartitionManager.Mount_Current_Storage(true)) {
+					DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
 					operation_end(1, simulate);
 					return 0;
 				}
@@ -1101,6 +1101,7 @@
 					}
 				}
 			}
+			DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
 			operation_end(ret, simulate);
 			return 0;
 		}
@@ -1113,6 +1114,7 @@
 			DataManager::GetValue("tw_child_pid", child_pid);
 			ui_print("Cancelling ADB sideload...\n");
 			kill(child_pid, SIGTERM);
+			DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
 			return 0;
 		}
     }
diff --git a/gui/gui.cpp b/gui/gui.cpp
index e128337..c431d8f 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -60,6 +60,7 @@
 static int gGuiConsoleTerminate = 0;
 static int gForceRender = 0;
 static int gNoAnimation = 1;
+static int gGuiInputRunning = 0;
 
 // Needed by pages.cpp too
 int gGuiRunning = 0;
@@ -391,6 +392,56 @@
     return 0;
 }
 
+static int runPage(const char* page_name)
+{
+    gui_changePage(page_name);
+
+	// Raise the curtain
+    if (gCurtain != NULL)
+    {
+        gr_surface surface;
+
+        PageManager::Render();
+        gr_get_surface(&surface);
+        curtainRaise(surface);
+        gr_free_surface(surface);
+    }
+
+    gGuiRunning = 1;
+
+    DataManager::SetValue("tw_loaded", 1);
+
+    for (;;)
+    {
+        loopTimer();
+
+        if (!gForceRender)
+        {
+            int ret;
+
+            ret = PageManager::Update();
+            if (ret > 1)
+                PageManager::Render();
+
+            if (ret > 0)
+                flip();
+        }
+        else
+        {
+            gForceRender = 0;
+            PageManager::Render();
+            flip();
+        }
+		if (DataManager::GetIntValue("tw_page_done") != 0) {
+			gui_changePage("main");
+			break;
+		}
+    }
+
+    gGuiRunning = 0;
+    return 0;
+}
+
 int gui_forceRender(void)
 {
     gForceRender = 1;
@@ -537,13 +588,36 @@
     // Set the default package
     PageManager::SelectPackage("TWRP");
 
-    // Start by spinning off an input handler.
-    pthread_t t;
-    pthread_create(&t, NULL, input_thread, NULL);
+    if (!gGuiInputRunning) {
+		// Start by spinning off an input handler.
+		pthread_t t;
+		pthread_create(&t, NULL, input_thread, NULL);
+		gGuiInputRunning = 1;
+	}
 
     return runPages();
 }
 
+extern "C" int gui_startPage(const char* page_name)
+{
+    if (!gGuiInitialized)   return -1;
+
+    gGuiConsoleTerminate = 1;
+    while (gGuiConsoleRunning)  loopTimer();
+
+    // Set the default package
+    PageManager::SelectPackage("TWRP");
+
+    if (!gGuiInputRunning) {
+		// Start by spinning off an input handler.
+		pthread_t t;
+		pthread_create(&t, NULL, input_thread, NULL);
+		gGuiInputRunning = 1;
+	}
+
+    return runPage(page_name);
+}
+
 static void *console_thread(void *cookie)
 {
     PageManager::SwitchToConsole();
diff --git a/gui/gui.h b/gui/gui.h
index 948e11b..ee3cc27 100644
--- a/gui/gui.h
+++ b/gui/gui.h
@@ -5,6 +5,7 @@
 int gui_init();
 int gui_loadResources();
 int gui_start();
+int gui_startPage(const char* page_name);
 void gui_print(const char *fmt, ...);
 void gui_print_overwrite(const char *fmt, ...);
 
diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp
index e05f059..8e95555 100644
--- a/openrecoveryscript.cpp
+++ b/openrecoveryscript.cpp
@@ -40,6 +40,7 @@
 extern "C" {
 #include "data.h"
 #include "twinstall.h"
+#include "gui/gui.h"
 int TWinstall_zip(const char* path, int* wipe_cache);
 }
 
@@ -66,7 +67,7 @@
 
 int OpenRecoveryScript::run_script_file(void) {
 	FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
-	int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0;
+	int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
 	char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
 		 value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
 		 value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
@@ -328,34 +329,27 @@
 			} else if (strcmp(command, "print") == 0) {
 				ui_print("%s\n", value);
 			} else if (strcmp(command, "sideload") == 0) {
-				int wipe_cache = 0;
-				string result, Sideload_File;
-
-				if (!PartitionManager.Mount_Current_Storage(true)) {
-					continue;
+				// ADB Sideload
+				DataManager_SetStrValue("tw_page_done", "0");
+				DataManager_SetStrValue("tw_back", "main");
+				DataManager_SetStrValue("tw_action", "adbsideload");
+				DataManager_SetStrValue("tw_has_action2", "0");
+				DataManager_SetStrValue("tw_action2", "");
+				DataManager_SetStrValue("tw_action2_param", "");
+				DataManager_SetStrValue("tw_action_text1", "ADB Sideload");
+				DataManager_SetStrValue("tw_action_text2", "Usage: adb sideload filename.zip");
+				DataManager_SetStrValue("tw_complete_text1", "ADB Sideload Complete");
+				DataManager_SetIntValue("tw_has_cancel", 1);
+				DataManager_SetIntValue("tw_show_reboot", 1);
+				DataManager_SetStrValue("tw_cancel_action", "adbsideloadcancel");
+				DataManager_SetStrValue("tw_cancel_param", "");
+				if (gui_startPage("action_page") != 0) {
+					LOGE("Failed to load sideload GUI page.\n");
 				}
-				Sideload_File = DataManager_GetCurrentStoragePath();
-				Sideload_File += "/sideload.zip";
-				if (TWFunc::Path_Exists(Sideload_File)) {
-					unlink(Sideload_File.c_str());
-				}
-				ui_print("Starting ADB sideload feature...\n");
-				ret_val = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
-				if (!ret_val && wipe_cache)
-					PartitionManager.Wipe_By_Path("/cache");
-				if (DataManager_GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager_GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
-					ui_print("Injecting TWRP into boot image...\n");
-					TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
-					if (Boot == NULL || Boot->Current_File_System != "emmc")
-						TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
-					else {
-						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");
-				}
-				ret_val = 1; // Causes device to go to the home screen afterwards
-				ui_print("Sideload finished.\nGoing to main screen.\n");
+				DataManager_SetIntValue("tw_page_done", 1);
+				gui_console_only();
+				sideload = 1; // Causes device to go to the home screen afterwards
+				ui_print("Sideload finished.\n");
 			} else {
 				LOGE("Unrecognized script command: '%s'\n", command);
 				ret_val = 1;
@@ -379,6 +373,8 @@
 		}
 		ui_print("TWRP injection complete.\n");
 	}
+	if (sideload)
+		ret_val = 1; // Forces booting to the home page after sideload
 	return ret_val;
 }