twrpapp: restore system mount to previous state

The current function to check for the existence of the TWRP app
in system unconditionally unmounts system once the check is
complete. Since the TWRP app check now occurs more frequently since
change 70e0a397e00c605cf7b01a5f5ce027dea84f1a14, system is now
getting unmounted every time the Advanced menu is entered, which
is undesirable if it was mounted in order to perform tasks from that
menu.

This patch first checks the state of the system mount point, and
once the check is complete, either unmounts system or leaves it
mounted, based on the initial state.

Change-Id: I7ad1f3e4e6cf3579134f1a9c5468ad90a142feae
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 8b00951..2db797d 100755
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -1299,24 +1299,28 @@
 	LOGINFO("checking for twrp app\n");
 	TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
 	if (!sys->Get_Super_Status()) {
-		if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) {
-			string base_path = PartitionManager.Get_Android_Root_Path();
-			if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
-				base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
-			string install_path = base_path + "/priv-app";
-			if (!TWFunc::Path_Exists(install_path))
-				install_path = base_path + "/app";
-			install_path += "/twrpapp";
-			if (TWFunc::Path_Exists(install_path)) {
-				LOGINFO("App found at '%s'\n", install_path.c_str());
-				DataManager::SetValue("tw_app_installed_in_system", 1);
-				return true;
-			}
+		bool is_system_mounted = true;
+		if(!PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path())) {
+			is_system_mounted = false;
+			PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 		}
+		string base_path = PartitionManager.Get_Android_Root_Path();
+		if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
+			base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
+		string install_path = base_path + "/priv-app";
+		if (!TWFunc::Path_Exists(install_path))
+			install_path = base_path + "/app";
+		install_path += "/twrpapp";
+		if (TWFunc::Path_Exists(install_path)) {
+			LOGINFO("App found at '%s'\n", install_path.c_str());
+			DataManager::SetValue("tw_app_installed_in_system", 1);
+			return true;
+		}
+		if (!is_system_mounted)
+			PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 		DataManager::SetValue("tw_app_installed_in_system", 0);
 	}
 	DataManager::SetValue("tw_app_installed_in_system", 0);
-	PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false);
 	return false;
 }